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.

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

数据结构-树链剖分

由于重链和子树的编号是连续的,处理连续区间可以使用线段树处理,注意lazy的更新何时为=,何时为+=
本题是区间增减修改,单点查询,可以用树状数组解决

点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=6e4;
struct Segtree{
    LL val;
    int lazy;
}tr[N<<2];
int h[N],e[N<<1],ne[N<<1],idx;
int s[N],d[N],son[N],f[N],top[N],wei[N];
int nid[N],oid[N],cnt;
int n,m,p;
void add(int u,int v)
{
    e[++idx]=v;
    ne[idx]=h[u];
    h[u]=idx;
}
void dfs1(int x,int fa)
{
    s[x]=1;d[x]=d[fa]+1;
    son[x]=0;f[x]=fa;
    for(int i=h[x];~i;i=ne[i])
    {
        int j=e[i];
        if(j==fa) continue;
        dfs1(j,x);
        s[x]+=s[j];
        if(s[j]>s[son[x]]) son[x]=j;
    }
}
void dfs2(int x,int t)
{
    top[x]=t;
    nid[x]=++cnt;
    oid[cnt]=x;
    if(son[x]) dfs2(son[x],t);
    for(int i=h[x];~i;i=ne[i])
    {
        int j=e[i];
        if(j!=f[x]&&j!=son[x]) dfs2(j,j);
    }
}
void pushup(int rt)
{
    tr[rt].val=tr[rt<<1].val+tr[rt<<1|1].val;
}
void pushdown(int rt,int ll,int rl)
{
    if(tr[rt].lazy)
    {
        int t=tr[rt].lazy;
        tr[rt<<1].lazy+=t;
        tr[rt<<1|1].lazy+=t;
        tr[rt<<1].val+=(LL)t*ll;
        tr[rt<<1|1].val+=(LL)t*rl;
        tr[rt].lazy=0;
    }
}
void build(int l,int r,int rt)
{
    if(l==r) 
    {
        tr[rt].val=wei[oid[l]];return ;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        tr[rt].val+=(LL)c*(r-l+1);
        tr[rt].lazy+=c;
        return ;
    }
    int m=(l+r)>>1;
    pushdown(rt,m-l+1,r-m);
    if(L<=m) update(L,R,c,l,m,rt<<1);
    if(R>m) update(L,R,c,m+1,r,rt<<1|1);
    pushup(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
    if(L>r||R<l) return 0;
    if(L<=l&&R>=r) return tr[rt].val;
    int m=(l+r)>>1;
    pushdown(rt,m-l+1,r-m);
    return query(L,R,l,m,rt<<1)+query(L,R,m+1,r,rt<<1|1);
}
void chain(int u,int v,int k)
{
    while(top[u]!=top[v])
    {
        if(d[top[u]]<d[top[v]]) swap(u,v);
        update(nid[top[u]],nid[u],k,1,n,1);
        u=f[top[u]];
    }
    if(d[u]<d[v]) swap(u,v);
    update(nid[v],nid[u],k,1,n,1);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    while(cin>>n>>m>>p)
    {
        memset(h,-1,sizeof h);
        memset(tr,0,sizeof tr);
        idx=0;cnt=0;
        for(int i=1;i<=n;++i) 
        cin>>wei[i];
        for(int i=0;i<m;++i)
        {
            int u,v;
            cin>>u>>v;
            add(u,v);
            add(v,u);
        }
        dfs1(1,0);
        dfs2(1,1);
        build(1,n,1);
        for(int i=0;i<p;++i)
        {
            char op;
            cin>>op;
            if(op=='I') 
            {
                int c1,c2,k;
                cin>>c1>>c2>>k;
                chain(c1,c2,k);
            }
            else if(op=='D')
            {
                int c1,c2,k;
                cin>>c1>>c2>>k;
                chain(c1,c2,-k);
            }
            else 
            {
                int c;
                cin>>c;
                c=nid[c];
                cout<<query(c,c,1,n,1)<<'\n';
            }
        }
    }
    
}
 posted on 2023-05-15 19:55  ruoye123456  阅读(24)  评论(0)    收藏  举报