회원가입
로그인
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
문제집
대회
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
17033번 - Cow Land
jhnah917
C++14
#include <bits/stdc++.h> using namespace std; struct Info{ int val; Info() : Info(0) {} Info(int val) : val(val) {} void Set(int v){ val = v; } Info operator + (const Info &t) const { return Info(val ^ t.val); } }; struct Node{ Node *l, *r, *p; /// Child, Parent Pointer int sz; /// (splay) subtree size Info v, sum; Node() : Node(Info()) {} Node(Info v) : v(v) { l = r = p = nullptr; sz = 1; } bool IsRoot() const { return !p || (this != p->l && this != p->r); } /// splay root? bool IsLeft() const { return this == p->l; } /// left child? void Rotate(){ /// move this to p if(IsLeft()){ if(r) r->p = p; p->l = r; r = p; } else{ if(l) l->p = p; p->r = l; l = p; } if(!p->IsRoot()) (p->IsLeft() ? p->p->l : p->p->r) = this; auto t = p; p = t->p; t->p = this; t->Update(); Update(); } void Update(){ /// Update info sz = 1; sum = v; if(l) sz += l->sz, sum = sum + l->sum; if(r) sz += r->sz, sum = sum + r->sum; } void Update(const Info &val){ v = val; Update(); } }; /// move x to (splay) root void Splay(Node *x){ for(; !x->IsRoot(); x->Rotate()){ if(x->p->IsRoot()) continue; // zig-step if(x->IsLeft() == x->p->IsLeft()) x->p->Rotate(); // zig-zag step else x->Rotate(); // zig-zig step } } /// make chain x to (lct) root void Access(Node *x){ Splay(x); x->r = nullptr; // un-tie lower node for(; x->p; Splay(x)) Splay(x->p), x->p->r = x; // tie upper node } /// link (lct) node p and c void Link(Node *p, Node *c){ Access(c); Access(p); // p and c is root of own splay tree c->l = p; p->p = c; // p -> c } /// cut (lct) node x and par(x) void Cut(Node *x){ Access(x); // x->l == par(x), x->l->p == x x->l->p = nullptr; x->l = nullptr; } /// get (lct) LCA of u and v Node* GetLCA(Node *x, Node *y){ Access(x); Access(y); Splay(x); return x->p ? x->p : x; // is preferred edge exist? } /// get (lct) root node on tree Node* GetRoot(Node *x){ Access(x); // make chain to root while(x->l) x = x->l; // get top node Splay(x); // amortized return x; } /// get (lct) par(x) Node* GetPar(Node *x){ Access(x); // make chain to root if(!x->l) return nullptr; // x is root x = x->l; while(x->r) x = x->r; // get predecessor Splay(x); // amortized return x; } int GetDepth(Node *x){ Access(x); // make chain to root if(x->l) return x->l->sz; // left? return 0; } Info VertexQuery(Node *x, Node *y){ Node *l = GetLCA(x, y); Info ret = l->v; Access(x); Splay(l); // (x to before l) == l->r if(l->r) ret = ret + l->r->sum; Access(y); Splay(l); // (y to before l) == l->r if(l->r) ret = ret + l->r->sum; return ret; } int N, Q; Node Tree[101010]; vector<int> G[101010]; void DFS(int v, int b = -1){ for(auto i : G[v]) if(i != b) DFS(i, v), Link(Tree+v, Tree+i); } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> Q; for(int i=1; i<=N; i++){ int t; cin >> t; Tree[i] = Node(t); } for(int i=1; i<N; i++){ int s, e; cin >> s >> e; G[s].push_back(e); G[e].push_back(s); } DFS(1, -1); for(int i=1; i<=Q; i++){ int op, a, b; cin >> op >> a >> b; if(op == 1){ Splay(Tree+a); Tree[a].Update(b); } else cout << VertexQuery(Tree+a, Tree+b).val << "\n"; } }
결과
메모리
시간
코드 길이
맞았습니다!!
14512
200
3720