• hdu 1166 线段树单点更新


    等线段树复习完再做个总结

    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

    Case 1:
    6
    33
    59

    2015-05-15:

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<queue>
      7 #include<map>
      8 using namespace std;
      9 #define MOD 1000000007
     10 const int INF=0x3f3f3f3f;
     11 const double eps=1e-5;
     12 #define cl(a) memset(a,0,sizeof(a))
     13 #define ts printf("*****
    ");
     14 #define lson l,mid,rt<<1
     15 #define rson mid+1,r,rt<<1|1
     16 #define root 1,n,1
     17 #define mid ((l+r)>>1)
     18 const int MAXN=60000;
     19 int n,m,t;
     20 int num[MAXN];
     21 struct Node
     22 {
     23     int l,r;
     24     int sum;
     25 }node[MAXN<<2];
     26 void build(int l,int r,int rt)
     27 {
     28     node[rt].l=l;
     29     node[rt].r=r;
     30     if(l==r)
     31     {
     32         node[rt].sum=num[l];
     33         return;
     34     }
     35     build(lson);
     36     build(rson);
     37     node[rt].sum=node[rt<<1|1].sum+node[rt<<1].sum;
     38 }
     39 void update(int p,int val,int l,int r,int rt)
     40 {
     41     if(l==r)
     42     {
     43         node[rt].sum+=val;
     44         return;
     45     }
     46     if(p<=mid)    update(p,val,lson);
     47     else  update(p,val,rson);
     48     node[rt].sum=node[rt<<1|1].sum+node[rt<<1].sum;
     49 }
     50 int query(int L,int R,int l,int r,int rt)
     51 {
     52     if(l>=L&&r<=R)
     53     {
     54         return node[rt].sum;
     55     }
     56     int ret=0;
     57     if(L<=mid)  ret+=query(L,R,lson);
     58     if(R>mid)   ret+=query(L,R,rson);
     59     return ret;
     60 }
     61 int main()
     62 {
     63     int i,j,k,tt;
     64     #ifndef ONLINE_JUDGE
     65     freopen("1.in","r",stdin);
     66     #endif
     67     scanf("%d",&tt);
     68     int ca=1;
     69     while(tt--)
     70     {
     71         scanf("%d",&n);
     72         for(i=1;i<=n;i++)
     73         {
     74             scanf("%d",num+i);
     75         }
     76         build(root);
     77         char s[100];
     78         int l,r,po,v;
     79         printf("Case %d:
    ",ca++);
     80         while(scanf("%s",s))
     81         {
     82             if(s[0]=='E')   break;
     83             if(s[0]=='Q')
     84             {
     85                 scanf("%d%d",&l,&r);
     86                 printf("%d
    ",query(l,r,root));
     87             }
     88             if(s[0]=='S')
     89             {
     90                 scanf("%d%d",&po,&v);
     91                 update(po,-v,root);
     92             }
     93             if(s[0]=='A')
     94             {
     95                 scanf("%d%d",&po,&v);
     96                 update(po,v,root);
     97             }
     98         }
     99     }
    100 
    101 }
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<map>
     8 using namespace std;
     9 #define MOD 1000000007
    10 const int INF=0x3f3f3f3f;
    11 const double eps=1e-5;
    12 #define cl(a) memset(a,0,sizeof(a))
    13 #define ts printf("*****
    ");
    14 #define lson l,mid,rt<<1
    15 #define rson mid+1,r,rt<<1|1
    16 #define root 1,n,1
    17 #define mid ((l+r)>>1)
    18 const int MAXN=60000;
    19 int n,m,t;
    20 int sum[MAXN<<4];
    21 void pushup(int rt)
    22 {
    23     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    24 }
    25 void build(int l,int r,int rt)
    26 {
    27     if(l==r)
    28     {
    29         scanf("%d",&sum[rt]);
    30         return;
    31     }
    32     build(lson);
    33     build(rson);
    34     pushup(rt);
    35 }
    36 void update(int pos,int val,int l,int r,int rt)
    37 {
    38     if(l==r)
    39     {
    40         sum[rt]+=val;
    41         return;
    42     }
    43     if(pos<=mid)    update(pos,val,lson);
    44     else update(pos,val,rson);
    45     pushup(rt);
    46 }
    47 int query(int L,int R,int l,int r,int rt)
    48 {
    49     if(l>=L&&r<=R)
    50     {
    51         return sum[rt];
    52     }
    53     int ans=0;
    54     if(L<=mid)  ans+=query(L,R,lson);
    55     if(R>mid)   ans+=query(L,R,rson);
    56     return ans;
    57 }
    58 int main()
    59 {
    60     int i,j,k,tt;
    61     #ifndef ONLINE_JUDGE
    62     freopen("1.in","r",stdin);
    63     #endif
    64     scanf("%d",&tt);
    65     int ca=1;
    66     while(tt--)
    67     {
    68         scanf("%d",&n);
    69         printf("Case %d:
    ",ca++);
    70         build(root);
    71         char s[10];
    72         int p,v;
    73         while(1)
    74         {
    75             scanf("%s",s);
    76             if(s[0]=='E')    break;
    77             scanf("%d%d",&p,&v);
    78             if(s[0]=='A')   update(p,v,1,n,1);
    79             else if(s[0]=='S')   update(p,-v,1,n,1);
    80             else if(s[0]=='Q')  printf("%d
    ",query(p,v,1,n,1));
    81         }
    82     }
    83 }
    2015/7/21

     

  • 相关阅读:
    [HDU5184] Brackets
    L2-036 网红点打卡攻略 (25 分)
    L2-017 人以群分 (25 分)
    L2-029 特立独行的幸福 (25 分)
    L2-035 完全二叉树的层序遍历 (25 分)
    L2-031 深入虎穴 (25 分)
    L2-020 功夫传人 (25 分)
    第 50 场双周赛
    L2-027 名人堂与代金券 (25 分)
    L2-024 部落 (25 分)
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4280938.html
Copyright © 2020-2023  润新知