zoukankan      html  css  js  c++  java
  • 基础线段树模板

    1.单点更新+区段查找

    敌兵布阵

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
    中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

    Input
    第一行一个整数T,表示有T组数据。
    每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
    接下来每行有一条命令,命令有4种形式:
    (1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
    (2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
    (3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
    (4)End 表示结束,这条命令在每组数据最后出现;
    每组数据最多有40000条命令
    Output
    对第i组数据,首先输出“Case i:”和回车,
    对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
    Sample Input
    1
    10
    1 2 3 4 5 6 7 8 9 10
    Query 1 3
    Add 3 6
    Query 2 7
    Sub 10 2
    Add 6 3
    Query 3 10
    End 
    Sample Output
    Case 1:
    6
    33
    59

    单点增加或是减少,区段查找
    代码:
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #define lson l,m,i<<1
      5 #define rson m+1,r,i<<1|1
      6 int sum;
      7 const int MAXN=2e5+10;
      8 using namespace std;
      9 struct node
     10 {
     11     int l;
     12     int r;
     13     int mid()
     14     {
     15         return (l+r)/2.0;
     16     }
     17     int value;
     18 }tree[MAXN<<2];
     19 void push_up(int i)
     20 {
     21     tree[i].value=tree[i<<1].value+tree[i<<1|1].value;
     22 }
     23 void Build(int l,int r,int i)
     24 {
     25     tree[i].l=l;
     26     tree[i].r=r;
     27     tree[i].value=0;
     28     if(l==r)///叶子节点
     29     {
     30         scanf("%d",&tree[i].value);///存储需要维护的信息
     31         return ;
     32     }
     33     int m=tree[i].mid();
     34     Build(lson);///左孩子
     35     Build(rson);///右孩子
     36     push_up(i);///向上回溯
     37 }
     38 
     39 void query(int l,int r,int i)
     40 {
     41     if(tree[i].l==l&&tree[i].r==r)///叶子节点,同时也是目目标节点
     42     {
     43         sum+=tree[i].value;
     44         return ;
     45     }
     46     int m=tree[i].mid();
     47     if(r<=m)
     48     {
     49         query(l,r,i<<1);
     50     }
     51     else if(l>m)
     52     {
     53         query(l,r,i<<1|1);
     54     }
     55     else///占两段
     56     {
     57         query(lson);
     58         query(rson);
     59     }
     60 }
     61 
     62 void update(int l,int r,int i,int v,int num)
     63 {
     64     if(l==r&&l==num)///找到目标位置
     65     {
     66         tree[i].value+=v;
     67         return ;
     68     }
     69     int m=tree[i].mid();
     70     if(m>=num)
     71     {
     72         update(l,m,i<<1,v,num);
     73     }
     74     else
     75     {
     76         update(m+1,r,i<<1|1,v,num);
     77     }
     78     push_up(i);
     79 }
     80 int main()
     81 {
     82     int n,m,a,b,t,i,counts,flag;
     83     char s[10];
     84     scanf("%d",&t);
     85     counts=1;
     86     flag=0;
     87     while(t--)
     88     {
     89         scanf("%d",&n);
     90         Build(1,n,1);///前两个参数是节点的左右端点,最后一个参数是节点在结构体中的位置
     91         printf("Case %d:
    ",counts++);
     92         while(1)
     93         {
     94             scanf("%s",&s);
     95             if(!strcmp(s,"Query"))
     96             {
     97                 scanf("%d%d",&a,&b);
     98                 sum=0;
     99                 query(a,b,1);///同Build
    100                 printf("%d
    ",sum);
    101             }
    102             else if(!strcmp(s,"Add"))
    103             {
    104                  scanf("%d%d",&a,&b);
    105                 update(1,n,1,b,a);
    106             }
    107             else if(!strcmp(s,"Sub"))
    108             {
    109                  scanf("%d%d",&a,&b);
    110                 update(1,n,1,-b,a);
    111             }
    112             else if(!strcmp(s,"End"))
    113             {
    114                 flag=1;
    115                 break;
    116             }
    117         }
    118     }
    119     return 0;
    120 }
    View Code

    I Hate It

    很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
    这让很多学生很反感。

    不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

    Input本题目包含多组测试,请处理到文件结束。
    在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
    学生ID编号分别从1编到N。
    第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
    接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
    当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
    当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
    Output对于每一次询问操作,在一行里面输出最高成绩。Sample Input
    5 6
    1 2 3 4 5
    Q 1 5
    U 3 6
    Q 3 4
    Q 4 5
    U 2 9
    Q 1 5
    Sample Output
    5
    6
    5
    9
    
    
    Hint
    Huge input,the C function scanf() will work better than cin
            
    单点更新,区段查找 
    代码:
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #define lson l,m,i<<1
      5 #define rson m+1,r,i<<1|1
      6 int M;
      7 const int MAXN=2e5+10;
      8 using namespace std;
      9 struct node
     10 {
     11     int l;
     12     int r;
     13     int mid()
     14     {
     15         return (l+r)/2.0;
     16     }
     17     int value;
     18 }tree[MAXN<<2];
     19 void push_up(int i)
     20 {
     21     tree[i].value=max(tree[i<<1].value,tree[i<<1|1].value);
     22 }
     23 void Build(int l,int r,int i)
     24 {
     25     tree[i].l=l;
     26     tree[i].r=r;
     27     tree[i].value=0;
     28     if(l==r)///叶子节点
     29     {
     30         scanf("%d",&tree[i].value);///存储需要维护的信息
     31         return ;
     32     }
     33     int m=tree[i].mid();
     34     Build(lson);///左孩子
     35     Build(rson);///右孩子
     36     push_up(i);///向上回溯
     37 }
     38 
     39 void query(int l,int r,int i)
     40 {
     41     if(tree[i].l==l&&tree[i].r==r)///叶子节点,同时也是目目标节点
     42     {
     43         M=max(tree[i].value,M);
     44         return ;
     45     }
     46     int m=tree[i].mid();
     47     if(r<=m)
     48     {
     49         query(l,r,i<<1);
     50     }
     51     else if(l>m)
     52     {
     53         query(l,r,i<<1|1);
     54     }
     55     else///占两段
     56     {
     57         query(lson);
     58         query(rson);
     59     }
     60 }
     61 
     62 void update(int l,int r,int i,int v,int num)
     63 {
     64     if(l==r&&l==num)///找到目标位置
     65     {
     66         tree[i].value=v;
     67         return ;
     68     }
     69     int m=tree[i].mid();
     70     if(m>=num)
     71     {
     72         update(l,m,i<<1,v,num);
     73     }
     74     else
     75     {
     76         update(m+1,r,i<<1|1,v,num);
     77     }
     78     push_up(i);
     79 }
     80 int main()
     81 {
     82     int n,m,a,b;
     83     char c;
     84     while(scanf("%d%d",&n,&m)!=EOF)
     85     {
     86         Build(1,n,1);///前两个参数是节点的左右端点,最后一个参数是节点在结构体中的位置
     87         while(m--)
     88         {
     89             scanf(" %c",&c);
     90             scanf("%d%d",&a,&b);
     91             if(c=='Q')
     92             {
     93                 M=0;
     94                 query(a,b,1);///同Build
     95                 printf("%d
    ",M);
     96             }
     97             else if(c=='U')
     98             {
     99                 update(1,n,1,b,a);
    100             }
    101         }
    102     }
    103     return 0;
    104 }
    View Code

     

    2.区段更新,单点查找

    Color the ball

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?

    Input
    每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
    当N = 0,输入结束。
    Output
    每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。Sample Input
    3
    1 1
    2 2
    3 3
    3
    1 1
    1 2
    1 3
    0
    Sample Output
    1 1 1
    3 2 1


    代码:
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #define ll long long int
      5 const int MAXN=1e5+10;
      6 using namespace std;
      7 struct node
      8 {
      9     int l;
     10     int r;
     11     int mid()
     12     {
     13         return (l+r)/2.0;
     14     }
     15     ll sum;///每一个节点的sum
     16     int add;///延迟标记数组
     17 } tree[MAXN<<2];
     18 void push_up(int i)
     19 {
     20     tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
     21 }
     22 void push_down(int i,int L)///L为区间长度
     23 {
     24     if(tree[i].add)
     25     {
     26         tree[i<<1].add+=tree[i].add;
     27         tree[i<<1|1].add+=tree[i].add;
     28         tree[i<<1].sum+=tree[i].add*(L-(L>>1));
     29         tree[i<<1|1].sum+=tree[i].add*(L>>1);
     30         tree[i].add=0;
     31     }
     32 }
     33 void Build(int l,int r,int i)
     34 {
     35     tree[i].l=l;
     36     tree[i].r=r;
     37     tree[i].add=0;
     38     tree[i].sum=0;
     39     if(l==r)///叶子节点
     40     {
     41         //scanf("%lld",&tree[i].sum);///存储需要维护的信息
     42         return ;
     43     }
     44     int m=tree[i].mid();
     45     Build(l,m,i<<1);///左孩子
     46     Build(m+1,r,i<<1|1);///右孩子
     47     push_up(i);///向上回溯
     48 }
     49 int query(int l,int i)///单点查询
     50 {
     51     if(tree[i].l==tree[i].r)///叶子节点,同时也是目目标节点
     52     {
     53         return tree[i].sum;
     54     }
     55     push_down(i,tree[i].r-tree[i].l+1);
     56     int m=tree[i].mid();
     57     if(l<=m)
     58     {
     59          return query(l,i<<1);///左孩子
     60     }
     61     else
     62     {
     63          return query(l,i<<1|1);///右孩子
     64     }
     65 }
     66 
     67 void update(int l,int r,int i)
     68 {
     69     if(tree[i].l==l&&tree[i].r==r)///找到目标位置
     70     {
     71         tree[i].sum+=(r-l+1);
     72         tree[i].add+=1;
     73         return ;
     74     }
     75     push_down(i,tree[i].r-tree[i].l+1);///懒标记下传
     76     int m = tree[i].mid();
     77     if(r<=m)
     78     {
     79         update(l,r,i<<1);
     80     }
     81     else if(l>m)
     82     {
     83         update(l,r,i<<1|1);
     84     }
     85     else
     86     {
     87         update(l,m,i<<1);
     88         update(m+1,r,i<<1|1);///右孩子
     89     }
     90     push_up(i);
     91 }
     92 int main()
     93 {
     94     int n,l,r,i;
     95     while(scanf("%d",&n)!=EOF)
     96     {
     97         if(n==0)
     98         {
     99             break;
    100         }
    101         Build(1,n,1);
    102         for(i=1;i<=n;i++)
    103         {
    104             scanf("%d%d",&l,&r);
    105             update(l,r,1);
    106         }
    107         for(i=1;i<=n;i++)
    108         {
    109             if(i==n)
    110              {
    111                  printf("%d
    ",query(i,1));
    112              }
    113              else
    114              {
    115                  printf("%d ",query(i,1));
    116              }
    117         }
    118     }
    119     return 0;
    120 }
    View Code
    
    
    

    3.区段更新,区段查找

     

    Simple Problem with Integers

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

    Input

    The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
    The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

    Output

    You need to answer all Q commands in order. One answer in a line.

    Sample Input
    10 5
    1 2 3 4 5 6 7 8 9 10
    Q 4 4
    Q 1 10
    Q 2 4
    C 3 6 3
    Q 2 4
    
    Sample Output
    4
    55
    9
    15
    Hint
    The sums may exceed the range of 32-bit integers.
     
    代码:
     
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #define ll long long int
      5 #define lson l,m,i<<1
      6 #define rson m+1,r,i<<1|1
      7 ll ans;
      8 const int MAXN=2e5+10;
      9 using namespace std;
     10 struct node
     11 {
     12     int l;
     13     int r;
     14     int mid()
     15     {
     16         return (l+r)/2.0;
     17     }
     18 } tree[MAXN<<2];
     19 ll add[MAXN<<2];///延迟标记数组
     20 ll sum[MAXN<<2];///每个节点的sum
     21 void push_up(int i)
     22 {
     23     sum[i]=sum[i<<1]+sum[i<<1|1];
     24 }
     25 void Build(int l,int r,int i)
     26 {
     27     tree[i].l=l;
     28     tree[i].r=r;
     29     sum[i]=0;
     30     add[i]=0;
     31     if(l==r)///叶子节点
     32     {
     33         scanf("%lld",&sum[i]);///存储需要维护的信息
     34         return ;
     35     }
     36     int m=tree[i].mid();
     37     Build(lson);///左孩子
     38     Build(rson);///右孩子
     39     push_up(i);///向上回溯
     40 }
     41 void push_down(int i,int L)///L为区间长度
     42 {
     43     if(add[i])
     44     {
     45         add[i<<1]+=add[i];
     46         add[i<<1|1]+=add[i];
     47         sum[i<<1]+=add[i]*(L-(L>>1));
     48         sum[i<<1|1]+=add[i]*(L>>1);
     49         add[i]=0;
     50     }
     51 }
     52 void query(int l,int r,int i)
     53 {
     54     if(tree[i].l==l&&tree[i].r==r)///叶子节点,同时也是目目标节点
     55     {
     56         ans+=sum[i];
     57         return ;
     58     }
     59     push_down(i,tree[i].r-tree[i].l+1);
     60     int m=tree[i].mid();
     61     if(r<=m)
     62     {
     63         query(l,r,i<<1);
     64     }
     65     else if(l>m)
     66     {
     67         query(l,r,i<<1|1);
     68     }
     69     else
     70     {
     71         query(l,m,i<<1);
     72         query(m+1,r,i<<1|1);
     73     }
     74 }
     75 
     76 void update(int l,int r,int i,int v)
     77 {
     78     if(tree[i].l==l&&tree[i].r==r)
     79     {
     80         sum[i]+=(ll)v*(r-l+1);
     81         add[i]+=(ll)v;///延迟标记+v
     82         return ;
     83     }
     84     push_down(i,tree[i].r-tree[i].l+1);
     85     int m = tree[i].mid();
     86     if(r<=m)
     87     {
     88         update(l,r,i<<1,v);
     89     }
     90     else if(l>m)
     91     {
     92         update(l,r,i<<1|1,v);
     93     }
     94     else
     95     {
     96         update(l,m,i<<1,v);
     97         update(m+1,r,i<<1|1,v);
     98     }
     99     push_up(i);
    100 }
    101 int main()
    102 {
    103     int n,m,a,b,d;
    104     char c;
    105     scanf("%d%d",&n,&m);
    106     Build(1,n,1);///前两个参数是节点的左右端点,最后一个参数是节点在结构体中的位置
    107     while(m--)
    108     {
    109         scanf(" %c",&c);
    110         if(c=='Q')
    111         {
    112             ans=0;
    113             scanf("%d%d",&a,&b);
    114             query(a,b,1);///同Build
    115             printf("%lld
    ",ans);
    116         }
    117         else if(c=='C')
    118         {
    119             scanf("%d%d%d",&a,&b,&d);
    120             update(a,b,1,d);
    121         }
    122     }
    123     return 0;
    124 }
    View Code

    Just a Hook

    In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



    Now Pudge wants to do some operations on the hook.

    Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
    The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

    For each cupreous stick, the value is 1.
    For each silver stick, the value is 2.
    For each golden stick, the value is 3.

    Pudge wants to know the total value of the hook after performing the operations.
    You may consider the original hook is made up of cupreous sticks.

    InputThe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
    For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
    Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
    OutputFor each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
    Sample Input
    1
    10
    2
    1 5 2
    5 9 3
    Sample Output
    Case 1: The total value of the hook is 24.

    区段替换,此题最后所求的结果实际上就是根节点的

    代码:
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define ll long long int
     5 #define lson l,m,i<<1
     6 #define rson m+1,r,i<<1|1
     7 int ans;
     8 const int MAXN=1e6+10;
     9 using namespace std;
    10 struct node
    11 {
    12     int l;
    13     int r;
    14     int mid()
    15     {
    16         return (l+r)/2.0;
    17     }
    18     ll sum;
    19     int add;
    20 } tree[MAXN<<2];
    21 void push_up(int i)
    22 {
    23     tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
    24 }
    25 void push_down(int i,int L)
    26 {
    27     if(tree[i].add)
    28     {
    29         tree[i<<1].add=tree[i].add;
    30         tree[i<<1|1].add=tree[i].add;
    31         tree[i<<1].sum=tree[i].add*(L-(L>>1));///注意这一行代码并不是累加,而是赋值
    32         tree[i<<1|1].sum=tree[i].add*(L>>1);
    33         tree[i].add=0;
    34     }
    35 }
    36 void Build(int l,int r,int i)
    37 {
    38     tree[i].l=l;
    39     tree[i].r=r;
    40     tree[i].add=0;
    41     if(l==r)
    42     {
    43         tree[i].sum=1;
    44         return ;
    45     }
    46     int m=tree[i].mid();
    47     Build(l,m,i<<1);
    48     Build(m+1,r,i<<1|1);
    49     push_up(i);
    50 }
    51 void update(int l,int r,int i,int v)
    52 {
    53     if(tree[i].l==l&&tree[i].r==r)///找到目标位置
    54     {
    55         tree[i].sum=(r-l+1)*v;
    56         tree[i].add=v;
    57         return ;
    58     }
    59     push_down(i,tree[i].r-tree[i].l+1);///懒标记下传
    60     int m = tree[i].mid();
    61     if(r<=m)
    62     {
    63         update(l,r,i<<1,v);
    64     }
    65     else if(l>m)
    66     {
    67         update(l,r,i<<1|1,v);
    68     }
    69     else
    70     {
    71         update(l,m,i<<1,v);
    72         update(m+1,r,i<<1|1,v);///右孩子
    73     }
    74     push_up(i);
    75 }
    76 int main()
    77 {
    78     int m,a,b,d,n,i;
    79     int t,counts=1;
    80     scanf("%d",&t);
    81     for(i=1;i<=t;i++)
    82     {
    83         scanf("%d%d",&n,&m);
    84         Build(1,n,1);
    85         while(m--)
    86         {
    87             scanf("%d%d%d",&a,&b,&d);
    88             update(a,b,1,d);
    89         }
    90         printf("Case %d: The total value of the hook is %d.
    ",i,tree[1].sum);
    91     }
    92     return 0;
    93 }
    View Code
    
    
    
     



  • 相关阅读:
    form表单介绍
    if条件语句
    表格.html
    列表.html
    CSS Js链接HTML文件
    DQL
    mysql介绍
    第一次接触mysql
    逻辑运算、作用域问题、DOM
    Js数据类型具体分析
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9414911.html
Copyright © 2011-2022 走看看