Caves and Tunnels
Time limit: 3.0 second
Memory limit: 64 MB
After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that there exists exactly one route between every pair of caves. But then scientists faced a particular problem. Sometimes in the caves faint explosions happen. They cause emission of radioactive isotopes and increase radiation level in the cave. Unfortunately robots don‘t stand radiation well. But for the research purposes they must travel from one cave to another. So scientists placed sensors in every cave to monitor radiation level in the caves. And now every time they move robots they want to know the maximal radiation level the robot will have to face during its relocation. So they asked you to write a program that will solve their problem.
Input
The first line of the input contains one integer N (1 ≤ N ≤ 100000) — the number of caves. NextN − 1 lines describe tunnels. Each of these lines contains a pair of integers ai, bi(1 ≤ ai, bi ≤ N) specifying the numbers of the caves connected by corresponding tunnel. The next line has an integer Q (Q ≤ 100000) representing the number of queries. The Q queries follow on a single line each. Every query has a form of "C U V", where C is a single character and can be either ‘I‘ or ‘G‘ representing the type of the query (quotes for clarity only). In the case of an ‘I‘ query radiation level in U-th cave (1 ≤ U ≤ N) is incremented by V (0 ≤ V ≤ 10000). In the case of a ‘G‘ query your program must output the maximal level of radiation on the way between caves with numbers U and V (1 ≤ U, V ≤ N) after all increases of radiation (‘I‘ queries) specified before current query. It is assumed that initially radiation level is 0 in all caves, and it never decreases with time (because isotopes‘ half-life time is much larger than the time of observations).
Output
For every ‘G‘ query output one line containing the maximal radiation level by itself.
Sample
input | output |
---|---|
4 1 2 2 3 2 4 6 I 1 1 G 1 1 G 3 4 I 2 3 G 1 1 G 3 4 |
1 0 1 3 |
分析:树上点修改+区间极值查询,树链剖分;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, rt<<1 #define Rson mid+1, R, rt<<1|1 const int maxn=1e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;} int n,m,k,t,q,tot,fa[maxn],dep[maxn],son[maxn],h[maxn],bl[maxn],pos[maxn]; struct node { int to,nxt; }a[maxn<<1]; struct node1 { char a[2]; int x,y; }op[maxn]; void add(int x,int y) { tot++; a[tot].to=y; a[tot].nxt=h[x]; h[x]=tot; } void dfs(int now,int pre) { son[now]=1; for(int i=h[now];i;i=a[i].nxt) { if(a[i].to!=pre) { fa[a[i].to]=now; dep[a[i].to]=dep[now]+1; dfs(a[i].to,now); son[now]+=son[a[i].to]; } } } void dfs1(int now,int chain) { int k=0; pos[now]=++t; bl[now]=chain; for(int i=h[now];i;i=a[i].nxt) { int x=a[i].to; if(dep[x]>dep[now]&&son[x]>son[k]) k=x; } if(k==0)return; dfs1(k,chain); for(int i=h[now];i;i=a[i].nxt) { int x=a[i].to; if(dep[x]>dep[now]&&k!=x) dfs1(x,x); } } struct Node { ll Max, lazy; } T[maxn<<2]; void PushUp(int rt) { T[rt].Max = max(T[rt<<1].Max, T[rt<<1|1].Max); } void PushDown(int L, int R, int rt) { int mid = (L + R) >> 1; ll t = T[rt].lazy; T[rt<<1].Max += t; T[rt<<1|1].Max += t; T[rt<<1].lazy += t; T[rt<<1|1].lazy += t; T[rt].lazy = 0; } void Update(int l, int r, int v, int L, int R, int rt) { if(l==L && r==R) { T[rt].lazy += v; T[rt].Max += v; return ; } int mid = (L + R) >> 1; if(T[rt].lazy) PushDown(L, R, rt); if(r <= mid) Update(l, r, v, Lson); else if(l > mid) Update(l, r, v, Rson); else { Update(l, mid, v, Lson); Update(mid+1, r, v, Rson); } PushUp(rt); } ll Query(int l, int r, int L, int R, int rt) { if(l==L && r== R) { return T[rt].Max; } int mid = (L + R) >> 1; if(T[rt].lazy) PushDown(L, R, rt); if(r <= mid) return Query(l, r, Lson); else if(l > mid) return Query(l, r, Rson); return max(Query(l, mid, Lson) , Query(mid + 1, r, Rson)); } ll gao(int x,int y) { ll ma=-1e19; while(bl[x]!=bl[y]) { if(dep[bl[x]]<dep[bl[y]])swap(x,y); ma=max(ma,Query(pos[bl[x]],pos[x],1,n,1)); x=fa[bl[x]]; } if(pos[x]>pos[y])swap(x,y); ma=max(ma,Query(pos[x],pos[y],1,n,1)); return ma; } int main() { int i,j; scanf("%d",&n); rep(i,1,n-1) { int x,y; scanf("%d%d",&x,&y); add(x,y); add(y,x); } dfs(1,-1); dfs1(1,1); scanf("%d",&q); rep(i,1,q) { scanf("%s%d%d",op[i].a,&op[i].x,&op[i].y); if(op[i].a[0]==‘I‘)Update(pos[op[i].x],pos[op[i].x],op[i].y,1,n,1); else printf("%lld\n",gao(op[i].x,op[i].y)); } //system("Pause"); return 0; }