holiday/8.10/C - Aragorn's Story.cpp

158 lines
2.6 KiB
C++
Raw Normal View History

2022-08-11 21:46:01 +08:00
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=3e4+1,N2=N<<1,inf=0x3f3f3f3f;//N2 ˫<><CBAB><EFBFBD>߼ǵ<DFBC>*2
int to[N2],h[N2],ne[N2];
int nid[N],oid[N],top[N],w[N],dep[N],fa[N];
int siz[N],son[N];//<2F><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر߶<D8B1><DFB6>ӣ<EFBFBD>siz<69><7A><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6>ӣ<EFBFBD>
int a[N<<2];
int t,u,v,w1,qu,n,m,cnt=0;
char opt;
#define ls rt<<1
#define rs rt<<1|1
#define mid ((l+r)>>1) //>>1д<31><D0B4>>>2
#define lson ls,l,mid //lд<6C><D0B4>1
#define rson rs,mid+1,r
#define len (r-l+1)
void add(int u,int v)
{
to[++cnt]=v;
ne[cnt]=h[u];
h[u]=cnt;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><DFB6><EFBFBD>
int tot=0;
void build(int rt,int l,int r)
{
if(l==r)
{
a[rt]=w[oid[l]];
// printf("l:%d,qm[%d]=w[%d]=%d\n",l,rt,oid[l],qm[rt]);
return;
}
// if(rt<=mid)
build(ls,l,mid);
// else
build(rs,mid+1,r);
// printf("a[%d]=a[%d]%d/a[%d]%d\n",rt,ls,qm[ls],rs,qm[rs]);
}
inline void pushdown(int rt)
{
a[ls]+=a[rt];
a[rs]+=a[rt];
a[rt]=0;
}
inline int q(int rt,int l,int r,int u)
{
if(l==r)
{
// printf("[%d]",rt);
return a[rt];
}
pushdown(rt);
int res=0;
if(u<=mid)res=q(lson,u);
if(u>mid)res=q(rson,u);
return res;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><DFB6><EFBFBD>
inline void dfs1(int u,int f,int deep)
{
fa[u]=f;
dep[u]=deep;
siz[u]=1;
for(int i=h[u]; i; i=ne[i])
{
int v=to[i];//bug int
if(v==f) continue;//bug <20>ź<EFBFBD><C5BA><EFBFBD>
// printf("(wo[%d]=val[%d]=%d)\n",v,i,val[i]);
dfs1(v,u,deep+1);
siz[u]+=siz[v];
if(son[u]==0||siz[v]>siz[son[u]])
{
son[u]=v;
}
}
// printf("out:%d:fa:%d,dep:%d,siz:%d\n",u,f,deep,siz[u]);
}
inline void dfs2(int u,int tp)
{
nid[u]=++cnt;
oid[cnt]=u;
top[u]=tp;
// printf("%d:nid:%d,nw:%d,top:%d\n",u,cnt,wo[u],topf);
if(!son[u]) return;
dfs2(son[u],tp);
for(int i=h[u]; i; i=ne[i])
{
v=to[i];
if(v!=fa[u]&&v!=son[u])
{
dfs2(v,v);
}
}
}
inline void change(int L,int R,int w1,int rt,int l,int r)
{
if(L<=l&&r<=R)
{
a[rt]+=w1;
// printf("u:%d,a[%d]=%d(l=r=%d)\n",u,rt,w1,l);
}
else
{
if(u<=mid)
{
change(L,R,w1,lson);
}
else change(L,R,w1,rson);
// printf("u:%d,a[%d]=a[%d]%d/a[%d]%d\n",u,rt,ls,a[ls],rs,a[rs]);
}
}
int main()
{
scanf("%d%d%d",&n,&m,&qu);
cnt=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&w[i]);
}
for(int i=1; i<=m; i++)
{
scanf("%d%d",&u,&v);// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>%d c0000005 APPCRASH
// printf("(%d)",i);
add(u,v);
add(v,u);
}
dfs1(1,0,1);
cnt=0;
dfs2(1,1);
build(1,1,cnt);
printf("(%d)",qu);
while(qu--)
{
scanf("%c",&opt);//"&"
if(opt=='I')
{
scanf("%d%d%d",&u,&v,&w1);//%d,<2C><><EFBFBD>˱<EFBFBD><CBB1><EFBFBD>û<EFBFBD><C3BB>%d
change(nid[u],nid[v],w1,1,1,cnt);
}
else if(opt=='D')
{
scanf("%d%d%d",&u,&v,&w1);
change(nid[u],nid[v],w1*-1,1,1,cnt);
}
else
{
scanf("%d",&u);
printf("%d\n",q(1,1,n,u));
}
}
return 0;
}