Light OJ 1348 Aladdin and the Return Journey(树剖-点)
来源:互联网 发布:朝闻道 刘慈欣 知乎 编辑:程序博客网 时间:2024/05/17 01:58
Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't want to take any help from the Genie because he thought that it might be another adventure for him. All he remembered was the paths he had taken to reach there. But since he took the lamp, all the genies in the cave became angry and they were planning to attack. As Aladdin was not afraid, he wondered how many genies were there. He summoned the Genie from the lamp and asked this.
Now you are given a similar problem. For simplicity assume that, you are given a tree (a connected graph with no cycles) with n nodes, nodes represent places, edges represent roads. In each node, initially there are an arbitrary number of genies. But the numbers of genies change in time. So, you are given a tree, the number of genies in each node and several queries of two types. They are:
1) 0 i j, it means that you have to find the total number of genies in the nodes that occur in path from node i to j (0 ≤ i, j < n).
2) 1 i v, it means that number of genies in node i is changed to v (0 ≤ i < n, 0 ≤ v ≤ 1000).
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a blank line. Next line contains an integer n (2 ≤ n ≤ 30000). The next line contains n space separated integers between 0 and 1000, denoting the number of genies in the nodes respectively. Then there are n-1 lines each containing two integers: u v (0 ≤ u, v < n, u ≠ v) meaning that there is an edge from node u and v. Assume that the edges form a valid tree. Next line contains an integer q (1 ≤ q ≤ 105) followed by q lines each containing a query as described above.
Output
For each case, print the case number in a single line. Then for each query 0 i j, print the total number of genies in the nodes that occur in path i to j.
Sample Input
Output for Sample Input
1
4
10 20 30 40
0 1
1 2
1 3
3
0 2 3
1 1 100
0 2 3
Case 1:
90
170
Note
Dataset is huge, use faster I/O methods.
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int maxn=5*(1e4+100);struct node{ int to,next;}e[maxn*2];int head[maxn],tot;int top[maxn],fa[maxn],deep[maxn],num[maxn],p[maxn],fp[maxn],son[maxn];int pos;void init(){ tot=0;pos=1; CLEAR(head,-1); CLEAR(son,-1);}void addedge(int u,int v){ e[tot].to=v;e[tot].next=head[u]; head[u]=tot++;}void dfs1(int u,int pre,int d){ deep[u]=d;fa[u]=pre; num[u]=1; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; if(v==pre) continue; dfs1(v,u,d+1); num[u]+=num[v]; if(son[u]==-1||num[v]>num[son[u]]) son[u]=v; }}void dfs2(int u,int sp){ top[u]=sp;p[u]=pos++; fp[p[u]]=u; if(son[u]==-1) return ; dfs2(son[u],sp); for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; if(v!=son[u]&&v!=fa[u]) dfs2(v,v); }}int n,m,q;int A[maxn],C[maxn];int lowbit(int x){ return x&(-x);}void update(int x,int val){ while(x<maxn) { C[x]+=val; x+=lowbit(x); }}int query(int x){ int sum=0; while(x>0) { sum+=C[x]; x-=lowbit(x); } return sum;}int Query(int u,int v){ int f1=top[u],f2=top[v]; int temp=0; while(f1!=f2) { if(deep[f1]<deep[f2]) { swap(f1,f2); swap(u,v); } temp+=query(p[u])-query(p[f1]-1); u=fa[f1];f1=top[u]; } if(deep[u]>deep[v]) swap(u,v); return temp+query(p[v])-query(p[u]-1);}int main(){ int t,cas=1; int u,v,op; scanf("%d",&t); while(t--) { scanf("%d",&n); init(); for(int i=1;i<=n;i++) scanf("%d",&A[i]); for(int i=0;i<n-1;i++) { scanf("%d%d",&u,&v); u++;v++; addedge(u,v); addedge(v,u); } dfs1(1,0,0); dfs2(1,1); CLEAR(C,0); for(int i=1;i<=n;i++) update(p[i],A[i]); scanf("%d",&q); printf("Case %d:\n",cas++); while(q--) { scanf("%d%d%d",&op,&u,&v); if(op==0) printf("%d\n",Query(u+1,v+1)); else update(p[u+1],v-(query(p[u+1])-query(p[u+1]-1))); } }}
- Light OJ 1348 Aladdin and the Return Journey(树剖-点)
- Light OJ 1348 - Aladdin and the Return Journey(树链剖分)
- Light 1348 - Aladdin and the Return Journey(树链剖分)
- Light OJ 1348 Aladdin and the Return Journey 树链剖分水题
- LightOJ 1348 - Aladdin and the Return Journey 树链剖分 点权
- LightOJ - 1348 Aladdin and the Return Journey(树剖)
- LightOJ 1348 Aladdin and the Return Journey (树链剖分)
- LightOJ 1348 Aladdin and the Return Journey(树链剖分)
- LightOJ 1348 Aladdin and the Return Journey (树链剖分)
- LightOJ 1348 Aladdin and the Return Journey(树链剖分)
- LightOJ 1348 Aladdin and the Return Journey 树链剖分
- lightoj 1348 - Aladdin and the Return Journey 【树链剖分】
- LightOJ 1348Aladdin and the Return Journey 树链剖分
- LightOJ 1348 Aladdin and the Return Journey【树链剖分入门题】
- Aladdin and the Return Journey LightOJ
- LightOJ 1348 Aladdin and the Return Journey(树链剖分+线段树)
- Light OJ Aladdin and the Flying Carpet(约数个数)
- Light OJ 1341 Aladdin and the Flying Carpet
- Linux授权普通用户读写 ttyUSB (USB2Serial)
- makefile 隐含规则
- hdu1856 More is better 并查集
- tableView动态获取行高
- linux防火墙
- Light OJ 1348 Aladdin and the Return Journey(树剖-点)
- spring 工厂方法配置bean
- linux下安装vsftp
- 百度地图定位demo
- 希尔排序
- pycharm调整字母长度分割线为80
- C---练习题部分2
- opencv里关于开启摄像头的一点心得
- 【栈项目2 - 建立链栈算法库--第6周】