hdu 3966 Aragorn's Story
Posted 日拱一卒 功不唐捐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 3966 Aragorn's Story相关的知识,希望对你有一定的参考价值。
Aragorn‘s Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
http://acm.hdu.edu.cn/showproblem.php?pid=3966
Problem Description
Our protagonist is the handsome human prince Aragorn
comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who
want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his
kingdom and M edges connect them. It is guaranteed that for any two camps, there
is one and only one path connect them. At first Aragorn know the number of
enemies in every camp. But the enemy is cunning , they will increase or decrease
the number of soldiers in camps. Every time the enemy change the number of
soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on
the path from C1 to C2, they will increase or decrease K soldiers to these
camps. Now Aragorn wants to know the number of soldiers in some particular camps
real-time.
Input
Multiple test cases, process to the end of
input.
For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.
The next line contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.
The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.
The next P lines will start with a capital letter ‘I‘, ‘D‘ or ‘Q‘ for each line.
‘I‘, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.
‘D‘, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.
‘Q‘, followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.
For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.
The next line contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.
The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.
The next P lines will start with a capital letter ‘I‘, ‘D‘ or ‘Q‘ for each line.
‘I‘, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.
‘D‘, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.
‘Q‘, followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.
Output
For each query, you need to output the actually number
of enemies in the specified camp.
Sample Input
3 2 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
Sample Output
7
4
8
Hint
1.The number of enemies may be negative.
2.Huge input, be careful.
Source
题目大意:
给出一棵n个点m条边的树,有p种操作
操作I:点x到y的路径上经过的所有点+k
操作D:点x到y的路径上经过的所有点-k
操作Q:询问点x的点权
树链剖分+线段树
线段树:根据dfs序建树
#include<cstdio> #include<cstring> #include<iostream> #define N 50001 using namespace std; int n,m,p; class work { private: int a[N],head[N],edge_tot,tree_cnt; int son[N],dep[N],sz,bl[N],id[N],f[N]; struct edge{int to,next;}e[N*2]; struct node{int l,r,f,sum;}tr[N*2]; public: inline int read() { int x=0,f=1;char c=getchar(); while(c<‘0‘||c>‘9‘) {if(c==‘-‘) f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘) {x=x*10+c-‘0‘;c=getchar();} return x*f; } inline void add(int u,int v)//加边 { e[++edge_tot].to=v;e[edge_tot].next=head[u];head[u]=edge_tot; e[++edge_tot].to=u;e[edge_tot].next=head[v];head[v]=edge_tot; } inline void point_change(int k,int x,int y)//单点修改 { if(tr[k].l==tr[k].r) {tr[k].sum=y;return;} int mid=tr[k].l+tr[k].r>>1,l=k+1,r=k+(tr[k+1].r-tr[k+1].l+1<<1); if(x<=mid) point_change(l,x,y); else point_change(r,x,y); tr[k].sum=tr[l].sum+tr[r].sum; } inline void build(int l,int r) { tree_cnt++; tr[tree_cnt].l=l;tr[tree_cnt].r=r; if(l==r) return; int mid=l+r>>1; build(l,mid);build(mid+1,r); } void init() { build(1,n); for(int i=1;i<=n;i++) a[i]=read(); int u,v; for(int i=1;i<=m;i++) { u=read();v=read(); add(u,v); } } inline void dfs1(int x,int fa)//树链剖分2次dfs { son[x]++; for(int i=head[x];i;i=e[i].next) { if(e[i].to==fa) continue; dep[e[i].to]=dep[x]+1; f[e[i].to]=x; dfs1(e[i].to,x); son[x]+=son[e[i].to]; } } inline void dfs2(int x,int chain) { sz++; id[x]=sz; bl[x]=chain; int m=0; for(int i=head[x];i;i=e[i].next) { if(dep[e[i].to]<dep[x]) continue; if(son[e[i].to]>son[m]) m=e[i].to; } if(!m) return; dfs2(m,chain); for(int i=head[x];i;i=e[i].next) { if(e[i].to==m||dep[e[i].to]<dep[x]) continue; dfs2(e[i].to,e[i].to); } } inline void down(int k)//标记下传 { int l=k+1,r=k+(tr[k+1].r-tr[k+1].l+1<<1); tr[l].sum+=(tr[l].r-tr[l].l+1)*tr[k].f; tr[l].f+=tr[k].f; tr[r].sum+=(tr[r].r-tr[r].l+1)*tr[k].f; tr[r].f+=tr[k].f; tr[k].f=0; } inline void query_change(int k,int opl,int opr,int w)//区间修改 { if(tr[k].l>=opl&&tr[k].r<=opr) { tr[k].sum+=(tr[k].r-tr[k].l+1)*w; tr[k].f+=w; return; } if(tr[k].f&&tr[k].l!=tr[k].r) down(k); int mid=tr[k].l+tr[k].r>>1,l=k+1,r=k+(tr[k+1].r-tr[k+1].l+1<<1); if(opl<=mid) query_change(l,opl,opr,w); if(opr>mid) query_change(r,opl,opr,w); tr[k].sum=tr[l].sum+tr[r].sum; } inline void solve_q_change(int u,int v,int w) { while(bl[u]!=bl[v]) { if(dep[bl[u]]<dep[bl[v]]) swap(u,v); query_change(1,id[bl[u]],id[u],w); u=f[bl[u]]; } if(id[u]>id[v]) swap(u,v); query_change(1,id[u],id[v],w); } inline void solve_p_query(int k,int x)//区间查询 { if(tr[k].l==tr[k].r) {printf("%d\n",tr[k].sum);return;} if(tr[k].f&&tr[k].l!=tr[k].r) down(k); int mid=tr[k].l+tr[k].r>>1,l=k+1,r=k+(tr[k+1].r-tr[k+1].l+1<<1); if(x<=mid) solve_p_query(l,x); else solve_p_query(r,x); } void solve() { for(int i=1;i<=n;i++) point_change(1,id[i],a[i]); int u,v,k;char c; for(int i=1;i<=p;i++) { cin>>c; switch(c) { case ‘I‘:u=read();v=read();k=read();solve_q_change(u,v,k);break; case ‘D‘:u=read();v=read();k=read();solve_q_change(u,v,-k);break; case ‘Q‘:u=read();solve_p_query(1,id[u]);break; } } } inline void pre() { memset(head,0,sizeof(head)); memset(son,0,sizeof(son)); memset(dep,0,sizeof(dep)); memset(tr,0,sizeof(tr)); edge_tot=tree_cnt=sz=0; } void start() { pre(); init(); dfs1(1,0); dfs2(1,1); solve(); } }o; int main() { while(scanf("%d%d%d",&n,&m,&p)!=EOF) o.start(); return 0; }
3个错误
1、Multiple test cases, process to the end of input.多组数据输入
不看清楚输入输出,一周内第三次犯此错误
2、sz变量未清零
3、在输入点权时,同时执行修改操作,错。
因为线段树是根据时间戳建立的,所以要在dfs完,确定完id[]后在修改
以上是关于hdu 3966 Aragorn's Story的主要内容,如果未能解决你的问题,请参考以下文章
AC日记——Aragorn's Story HDU 3966
HDU - 3966 Aragorn's Story(树链剖分入门+线段树)