标签:
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4217 Accepted Submission(s): 1164
3 2 5 1 2 3 2 1 2 3 I 1 3 5 Q 2 D 1 2 2 Q 1 Q 3
7 4 8Hint1.The number of enemies may be negative. 2.Huge input, be careful.
题意:给一棵树,并给定各个点权的值,然后有3种操作:
I C1 C2 K: 把C1与C2的路径上的所有点权值加上K
D C1 C2 K:把C1与C2的路径上的所有点权值减去K
Q C:查询节点编号为C的权值
大胆地用C++提交吧!
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int head[50010],tail;
struct Edge
{
int to,next;
}edge[50000*2+10];
void add(int from,int to)
{
edge[tail].to=to;
edge[tail].next=head[from];
head[from]=tail++;
}
int siz[50010],dep[50010],fa[50010],son[50010];
void dfs1(int from)
{
siz[from]=1;
for(int i=head[from];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(to!=fa[from])
{
fa[to]=from;
dep[to]=dep[from]+1;
dfs1(to);
siz[from]+=siz[to];
if(siz[to]>siz[son[from]])
son[from]=to;
}
}
}
int w[50010],top[50010],cnt;
void dfs2(int from,int tp)
{
w[from]=++cnt;
top[from]=tp;
if(son[from]==0)
return;
dfs2(son[from],tp);
for(int i=head[from];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(to!=son[from]&&to!=fa[from])
dfs2(to,to);
}
}
struct Tree
{
int l,r,m,v;
}tree[50000*4+10];
void build(int l,int r,int k)
{
tree[k].l=l;
tree[k].r=r;
tree[k].m=l+r>>1;
tree[k].v=0;
if(l==r)
return;
build(l,tree[k].m,k<<1);
build(tree[k].m+1,r,k<<1|1);
}
void update(int l,int r,int val,int k)
{
if(l==tree[k].l&&r==tree[k].r)
{
tree[k].v+=val;
return;
}
if(tree[k].v!=0)
{
tree[k<<1].v+=tree[k].v;
tree[k<<1|1].v+=tree[k].v;
tree[k].v=0;
}
if(r<=tree[k].m)
update(l,r,val,k<<1);
else if(l>tree[k].m)
update(l,r,val,k<<1|1);
else
{
update(l,tree[k].m,val,k<<1);
update(tree[k].m+1,r,val,k<<1|1);
}
}
int seek(int x,int k)
{
if(tree[k].l==tree[k].r&&tree[k].l==x)
return tree[k].v;
if(tree[k].v!=0)
{
tree[k<<1].v+=tree[k].v;
tree[k<<1|1].v+=tree[k].v;
tree[k].v=0;
}
if(x<=tree[k].m)
return seek(x,k<<1);
return seek(x,k<<1|1);
}
void update(int x,int y,int val)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
swap(x,y);
update(w[top[x]],w[x],val,1);
x=fa[top[x]];
}
int l=w[x],r=w[y];
if(l>r)
swap(l,r);
update(l,r,val,1);
}
int num[50010];
char od[2];
int main()
{
int n,m,p;
while(scanf("%d%d%d",&n,&m,&p)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",num+i);
tail=0;
memset(head,-1,sizeof(head));
while(m--)
{
int from,to;
scanf("%d%d",&from,&to);
add(from,to);
add(to,from);
}
memset(son,0,sizeof(son));
dfs1(1);
cnt=0;
dfs2(1,1);
build(1,cnt,1);
for(int i=1;i<=n;i++)
update(i,i,num[i]);
while(p--)
{
scanf("%s",od);
if(od[0]=='Q')
{
int x;
scanf("%d",&x);
printf("%d\n",seek(w[x],1));
}
else
{
int l,r,v;
scanf("%d%d%d",&l,&r,&v);
if(od[0]=='D')
v=-v;
update(l,r,v);
}
}
}
}标签:
原文地址:http://blog.csdn.net/stl112514/article/details/44758921