• Hdu 3966-Aragorn's Story LCT,动态树


    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3966

    Aragorn's Story

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


    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
    Hint
    1.The number of enemies may be negative. 2.Huge input, be careful.
     
    Source
     
    Recommend
     
    题意:
    给定一棵树和树上的点权,每次要区间加上一个数或减去一个数,查询某一个结点的权值。 
     题解:
    直接用LCT区间修改即可。。。
    查询前要先access一下。。。
    多组数据每次要清空一下树。。。QAQ Tle了一发。。。
      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 #define MAXN 50010
      4 struct node
      5 {
      6     int left,right,val;
      7 }tree[MAXN];
      8 int father[MAXN],rev[MAXN],tag[MAXN],Stack[MAXN];
      9 int read()
     10 {
     11     int s=0,fh=1;char ch=getchar();
     12     while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
     13     while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
     14     return s*fh;
     15 }
     16 int isroot(int x)
     17 {
     18     return tree[father[x]].left!=x&&tree[father[x]].right!=x;
     19 }
     20 void pushdown(int x)
     21 {
     22     int l=tree[x].left,r=tree[x].right;
     23     if(rev[x]!=0)
     24     {
     25         rev[x]^=1;rev[l]^=1;rev[r]^=1;
     26         swap(tree[x].left,tree[x].right);
     27     }
     28     if(tag[x]!=0)
     29     {
     30         tag[l]+=tag[x];tag[r]+=tag[x];
     31         tree[l].val+=tag[x];tree[r].val+=tag[x];
     32         tag[x]=0;
     33     }
     34 }
     35 void rotate(int x)
     36 {
     37     int y=father[x],z=father[y];
     38     if(!isroot(y))
     39     {
     40         if(tree[z].left==y)tree[z].left=x;
     41         else tree[z].right=x;
     42     }
     43     if(tree[y].left==x)
     44     {
     45         father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
     46     }
     47     else
     48     {
     49         father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
     50     }
     51 }
     52 void splay(int x)
     53 {
     54     int top=0,i,y,z;Stack[++top]=x;
     55     for(i=x;!isroot(i);i=father[i])Stack[++top]=father[i];
     56     for(i=top;i>=1;i--)pushdown(Stack[i]);
     57     while(!isroot(x))
     58     {
     59         y=father[x];z=father[y];
     60         if(!isroot(y))
     61         {
     62             if((tree[y].left==x)^(tree[z].left==y))rotate(x);
     63             else rotate(y);
     64         }
     65         rotate(x);
     66     }
     67 }
     68 void access(int x)
     69 {
     70     int last=0;
     71     while(x!=0)
     72     {
     73         splay(x);
     74         tree[x].right=last;
     75         last=x;x=father[x];
     76     }
     77 }
     78 void makeroot(int x)
     79 {
     80     access(x);splay(x);rev[x]^=1;
     81 }
     82 void link(int u,int v)
     83 {
     84     makeroot(u);father[u]=v;splay(u);
     85 }
     86 void cut(int u,int v)
     87 {
     88     makeroot(u);access(v);splay(v);father[u]=tree[v].left=0;
     89 }
     90 int findroot(int x)
     91 {
     92     access(x);splay(x);
     93     while(tree[x].left!=0)x=tree[x].left;
     94     return x;
     95 }
     96 int main()
     97 {
     98     int n,m,p,bb,ee,k,camp,i;
     99     char fh[2];
    100     while(scanf("%d %d %d",&n,&m,&p)!=EOF)
    101     {
    102         for(i=0;i<=n;i++)tree[i].val=tree[i].left=tree[i].right=0,tag[i]=0,father[i]=0,rev[i]=0;
    103         for(i=1;i<=n;i++)tree[i].val=read();
    104         for(i=1;i<=m;i++)
    105         {
    106             bb=read();ee=read();link(bb,ee);
    107         }
    108         for(i=1;i<=p;i++)
    109         {
    110             scanf("%s",fh);
    111             if(fh[0]=='I')
    112             {
    113                 bb=read();ee=read();k=read();
    114                 makeroot(bb);access(ee);splay(ee);
    115                 tag[ee]+=k;tree[ee].val+=k;
    116             }
    117             else if(fh[0]=='D')
    118             {
    119                 bb=read();ee=read();k=read();
    120                 makeroot(bb);access(ee);splay(ee);
    121                 tag[ee]-=k;tree[ee].val-=k;
    122             }
    123             else
    124             {
    125                 camp=read();
    126                 access(camp);
    127                 printf("%d
    ",tree[camp].val);
    128             }
    129         }
    130     }
    131     fclose(stdin);
    132     fclose(stdout);
    133     return 0;
    134 }
    View Code
  • 相关阅读:
    1010考试T1
    P5631 最小mex生成树 分治 并查集
    P4366 [Code+#4]最短路 建图 最短路
    P1654 OSU! 期望概率DP
    7.26集训
    7.25集训
    7.23集训
    7.22集训
    7.21test
    7.12test
  • 原文地址:https://www.cnblogs.com/Var123/p/5294925.html
Copyright © 2020-2023  润新知