zoukankan      html  css  js  c++  java
  • HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问

    Aragorn's Story

    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
    Hint

    1.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的权值

    题解:

    先划分树链再套 树状数组就可以了

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N = 1e5+10, M = 30005, mod = 1e9 + 7, inf = 0x3f3f3f3f;
    typedef long long ll;
    
    int head[N],t=1,sz,v[N],pos[N],belong[N],siz[N],vis[N],n,m,p,C[N],deep[N],fa[N][18];
    struct data{int to,next;}e[N*3];
    //树链
    void add(int u,int v) {e[t].to = v;e[t].next = head[u];head[u]=t++;}
    void dfs1(int x) {
        siz[x] = vis[x] =1;
        for(int i=1;i<=17;i++) {
            if(deep[x] < (1<<i)) break;
            fa[x][i] = fa[fa[x][i-1]][i-1];
        }
        for(int i=head[x];i;i=e[i].next) {
            if(vis[e[i].to]) continue;
            deep[e[i].to] = deep[x]+1;
            fa[e[i].to][0] = x;
            dfs1(e[i].to);
            siz[x]+=siz[e[i].to];
        }
    }
    void dfs2(int x,int chain) {
        int k=0;
        sz++;
        pos[x] =sz;
        belong[x] = chain;
        for(int i=head[x];i;i=e[i].next)
            if(deep[x]<deep[e[i].to]&&siz[k]<siz[e[i].to]) k = e[i].to;
        if(k==0)return ;
        dfs2(k,chain);
        for(int i=head[x];i;i=e[i].next)
            if(deep[x]<deep[e[i].to]&&k!=e[i].to) dfs2(e[i].to,e[i].to);
    }
    int lca(int x,int y) {
        if(deep[x] < deep[y]) swap(x,y);
        int t = deep[x] - deep[y];
        for(int i=0;i<=17;i++)
            if(t&(1<<i)) x = fa[x][i];
        for(int i=17;i>=0;i--)
        if(fa[x][i]!=fa[y][i]) {
            x = fa[x][i];
            y = fa[y][i];
        }
        if(x==y) return x;
        return fa[x][0];
    }
    //数据结构
    void update(int x,int c) {
         while(x<=n) {
            C[x]+=c;
            x+=x&-x;
         }
    }
    int ask(int x) {
        int sum=0;
        while(x>0) {
            sum+=C[x];
            x-=x&-x;
        }
        return sum;
    }
    void solvechange(int x,int f,int c) {
        while(belong[x]!=belong[f]) {
            update(pos[belong[x]],c);
            update(pos[x]+1,-c);
            x = fa[belong[x]][0];
        }
        update(pos[f],c);
        update(pos[x]+1,-c);
    }
    void scan() {
        for(int i=1;i<=n;i++) scanf("%d",&v[i]);
        for(int i=1;i<n;i++) {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);add(y,x);
        }
        dfs1(1);
        dfs2(1,1);
    
        for(int i=1;i<=n;i++) {
            update(pos[i],v[i]);
            update(pos[i]+1,-v[i]);
        }
        char ch[3];
        int x,y,c;
        for(int i=1;i<=p;i++) {
            scanf("%s",ch);
            if(ch[0] == 'I') {
                scanf("%d%d%d",&x,&y,&c);
                int t = lca(x,y);
                solvechange(x,t,c);
                solvechange(y,t,c);
                solvechange(t,t,-c);
            }
            else if(ch[0] == 'D') {
                scanf("%d%d%d",&x,&y,&c);
                int t = lca(x,y);
                solvechange(x,t,-c);
                solvechange(y,t,-c);
                solvechange(t,t,c);
            }
            else {
                scanf("%d",&x);
                printf("%d
    ",ask(pos[x]));
            }
        }
    }
    void init() {
        memset(head,0,sizeof(head));
        t=1;
        sz=0;
        memset(fa,0,sizeof(fa));
        memset(deep,0,sizeof(deep));
        memset(v,0,sizeof(v));
        memset(vis,0,sizeof(vis));
        memset(C,0,sizeof(C));
        memset(pos,0,sizeof(pos));
    }
    int main() {
        while(scanf("%d%d%d",&n,&m,&p)!=EOF) {
            init();
            scan();
        }
    }
  • 相关阅读:
    非科班毕业生四个月的面试准备,网易三轮面面试,成功拿下offer(面经奉上)全靠这份阿里面试手册
    太赞了!华为鸿蒙工程师总结的Linux笔记,提供下载
    阿里java岗面试有多难?分享阿里面试真题(附面试专题答案)及P7所要掌握的技能体系!
    阿里腾讯百度等大厂2020秋招面试总结,内附答案
    小小码农的大梦想,2020最新BAT大厂面试题干货总结
    历经三个月的紧张复习,秋招终于进了百度,附上面经分享希望可以帮助大家
    这份《高性能MySQL》适合数据库管理员(DBA)阅读,也适合开发人员参考学习
    这份程序员必备书籍《Spring实战4》,你确定不看看?
    自学JAVA的我提交给2020的问卷答案,JAVA核心知识点分享这一年来的努力并没有白费!
    Delphi连接数据库的两种方式
  • 原文地址:https://www.cnblogs.com/zxhl/p/5244533.html
Copyright © 2011-2022 走看看