회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
pst lca 쿼리 문제 어디가 틀린지 모르겠습니다.
gs25
2년 전
1
pst 처음 짜보는데 어디가 틀린지 도저히 모르겠습니다... ㅜㅜ
#include <bits/stdc++.h> #define pb push_back #define all(v) (v).begin(), (v).end() #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, n) for (int i = 1; i <= n; ++i) #define ff first #define ss second using namespace std; typedef long long ll; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char* x) { cerr << '\"' << x << '\"'; } void __print(const string& x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V>& x) { cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}'; } template <typename T> void __print(const T& x) { int f = 0; cerr << '{'; for (auto& i : x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifdef LOCAL #define dbg(...) \ cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; \ _print(__VA_ARGS__); \ cerr << "\e[39m" << endl; #else #define dbg(...) #endif const int MAXN = 100500; struct node { int v; node* l, * r; node(int c, node* l, node* r) : v(c), l(l), r(r) {} node() { l = r = NULL; v = 0; } }; int pv; node* root[MAXN]; void build(node* v, int s, int e) { if (s == e) return; v->l = new node(); v->r = new node(); int m = s + e >> 1; build(v->l, s, m); build(v->r, m + 1, e); } void upd(node* prv, node* cur, int s, int e, int pos, int val) { //dbg(s, e, pos, val); if (s == e) { cur->v = prv->v + val; return; } int m = s + e >> 1; if (pos <= m) { cur->r = prv->r; cur->l = new node(); upd(prv->l, cur->l, s, m, pos, val); } else { cur->l = prv->l; cur->r = new node(); upd(prv->r, cur->r, m + 1, e, pos, val); } cur->v = cur->l->v + cur->r->v; dbg(pv, s, e, cur->v); } // pos 이하에 있는 개수 int query(node* cur, int s, int e, int pos) { //dbg(s,e,pos,cur->v); if (e <= pos) return cur->v; if (pos < s) return 0; int m = s + e >> 1; return query(cur->l, s, m, pos) + query(cur->r, m + 1, e, pos); } vector<int> tmp; vector<int> adj[MAXN]; int par[MAXN][22]; int cost[MAXN]; int dfsn[MAXN]; int n; int d[MAXN]; void dfs(int v, int p) { dbg(v, p); par[v][0] = p; dfsn[v] = ++pv; root[pv] = new node(); if(v==1) upd(root[0], root[pv], 0, n, cost[v], 1); else upd(root[dfsn[p]], root[pv], 0, n, cost[v], 1); for (int i = 1; i < 22; i++) par[v][i] = par[par[v][i - 1]][i - 1]; for (auto ne : adj[v]) { if (ne == p) continue; d[ne] = d[v] + 1; dfs(ne, v); } } int lca(int a, int b) { if (d[a] < d[b]) swap(a, b); int diff = d[a] - d[b]; for (int i = 21; i >= 0; i--) { if ((diff >> i) & 1) a = par[a][i]; } if (a == b) return a; for (int i = 21; i >= 0; i--) { if (par[a][i] != par[b][i]) { int t = par[a][i]; a = t, b = t; } } a = par[a][0]; return a; } void solve() { root[0] = new node(); cin >> n; build(root[0], 0, n); rrep(i, n) { cin >> cost[i]; tmp.pb(cost[i]); } sort(all(tmp)); tmp.erase(unique(all(tmp)), tmp.end()); rrep(i, n) cost[i] = lower_bound(all(tmp), cost[i]) - tmp.begin(); rrep(i,n){ dbg(cost[i],i); } rep(i, n - 1) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } dfs(1, 1); int m; cin >> m; while (m--) { int u, v, k; cin >> u >> v >> k; // x 에서 경로상 x 이하의 개수가 k 미만인 것 : l, r int w = lca(u, v); dbg(u, v, w); int l = 0, r = tmp.size()-1; int fuck = query(root[dfsn[u]], 0, n, 0) + query(root[dfsn[v]], 0, n, 0) - query(root[dfsn[w]], 0, n, 0); if (w != 1) fuck -= query(root[dfsn[par[w][0]]], 0, n, 0); dbg(fuck,u,v,k); if(fuck>=k) { cout << tmp[0] << "\n"; continue; } while (l + 1 < r) { int m = l + r >> 1; int fuck = query(root[dfsn[u]], 0, n, m) + query(root[dfsn[v]], 0, n, m) - query(root[dfsn[w]], 0, n, m); if (w != 1) fuck -= query(root[dfsn[par[w][0]]], 0, n, m); if (fuck < k) l = m; else r = m; } assert(r>=0 && r<tmp.size()); cout << tmp[r] << "\n"; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; // cin >> t; while (t--) solve(); return 0; }
댓글을 작성하려면
로그인
해야 합니다.
gs25 2년 전 1
pst 처음 짜보는데 어디가 틀린지 도저히 모르겠습니다... ㅜㅜ