zoukankan      html  css  js  c++  java
  • HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)

    Aragorn's Story

    Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 10483    Accepted Submission(s): 2757


    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
    树链剖分入门
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 2e9
    #define met(a,b) memset(a,b,sizeof a)
    typedef long long ll;
    using namespace std;
    const int N =2e5+5;
    const int M = 4e6+5;
    int n,sum[N],m,tot,num,q;
    int tre[N*2],laz[N*2];
    int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N],c[N];
    int head[N];
    struct EDG{
        int to,next;
    }edg[N];
    void add(int u,int v){
        edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
    }
    void init(){
        met(head,-1);met(tre,0);
        met(son,0);met(laz,0);
        tot=0;num=0;
    }
    void dfs1(int u, int f, int d) {
        dep[u] = d;
        siz[u] = 1;
        son[u] = 0;
        fa[u] = f;
        for (int i =head[u]; i !=-1; i=edg[i].next) {
            int ff = edg[i].to;
            if (ff == f) continue;
            dfs1(ff, u, d + 1);
            siz[u] += siz[ff];
            if (siz[son[u]] < siz[ff])
                son[u] = ff;
        }
    }
    void dfs2(int u, int tp) {
        top[u] = tp;
        id[u] = ++num;
        if (son[u]) dfs2(son[u], tp);
        for (int i =head[u]; i !=-1; i=edg[i].next) {
            int ff = edg[i].to;
            if (ff == fa[u] || ff == son[u]) continue;
            dfs2(ff, ff);
        }
    }
    void build(int l,int r,int pos){
        if(l==r){
            tre[pos]=val[l];
            return;
        }
        int mid=(l+r)>>1;
        build(l,mid,pos<<1);
        build(mid+1,r,pos<<1|1);
        return;
    }
    void pushdown(int num) {
        if(laz[num]!=0) {
            tre[num*2]+=laz[num];
            tre[num*2+1]+=laz[num];
            laz[num*2]+=laz[num];
            laz[num*2+1]+=laz[num];
            laz[num]=0;
        }
    }
    void update(int num,int le,int ri,int x,int y,int p) {
        if(x<=le&&y>=ri) {
            tre[num]+=p;
            laz[num]+=p;
            return ;
        }
        pushdown(num);
        int mid=(le+ri)/2;
        if(x<=mid)
            update(num*2,le,mid,x,y,p);
        if(y>mid)
            update(num*2+1,mid+1,ri,x,y,p);
    }
    int query(int num,int le,int ri,int x) {
        if(le==ri) {
            return tre[num];
        }
        pushdown(num);
        int mid=(le+ri)/2;
        if(x<=mid)
            return query(num*2,le,mid,x);
        else
            return query(num*2+1,mid+1,ri,x);
    }
    void  Youngth(int u,int v,int p){
        int tp1=top[u],tp2=top[v];
        while(tp1!=tp2){
            if(dep[tp1]<dep[tp2]){
                swap(tp1,tp2);swap(u,v);
            }
            update(1,1,num,id[tp1],id[u],p);
            u=fa[tp1];
            tp1=top[u];
        }
        if(dep[u]>dep[v])swap(u,v);
        update(1,1,num,id[u],id[v],p);
    }
    int main() {
        int u,v,p;
        while(~scanf("%d%d%d",&n,&m,&q)) {
            init();
            for(int i=1;i<=n;i++)scanf("%d",&c[i]);
            while(m--){
                scanf("%d%d",&u,&v);
                add(u,v);add(v,u);
            }
            dfs1(1,0,1);
            dfs2(1,1);
            for(int i=1;i<=n;i++){
                val[id[i]]=c[i];
            }
            build(1,num,1);
            char str[100];
            while(q--){
                scanf("%s",str);
                if(str[0]=='I'){
                    scanf("%d%d%d",&u,&v,&p);
                    Youngth(u,v,p);
                }
                else if(str[0]=='D'){
                    scanf("%d%d%d",&u,&v,&p);
                    Youngth(u,v,-p);
                }
                else {
                    scanf("%d",&u);
                    printf("%d
    ",query(1,1,num,id[u]));
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    jQuery radio的取值与赋值
    jquery 单击选中 再次选中取消选中
    jqweui 正在加载样式的用法
    html5 横向滑动导航栏
    关于css清除元素浮动的方法总结(overflow clear floatfix)
    JavaScript中常用的事件
    baiduMap试手《办理进京证和市区警察查询进京证的地址浏览》
    原生JavaScript常用本地浏览器存储方法五(LocalStorage+userData的一个浏览器兼容类)
    原生JavaScript常用本地浏览器存储方法四(HTML5 LocalStorage sessionStorage)
    原生JavaScript常用本地浏览器存储方法三(UserData IE Only)
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6357485.html
Copyright © 2011-2022 走看看