zoukankan      html  css  js  c++  java
  • 敌兵布阵( 线段树模板,单点更新,区间查询)

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<string>
     7 #include<cmath>
     8 #include<set>
     9 #include<vector>
    10 #include<stack>
    11 #include<queue>
    12 #include<map>
    13 using namespace std;
    14 #define ll long long
    15 #define se second
    16 #define fi first
    17 const int INF= 0x3f3f3f3f;
    18 const int N=1e5+5;
    19 
    20 
    21 int sum[210000];
    22 
    23 void Pushup(int rt)
    24 {
    25     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    26 }
    27 void build(int l,int r,int rt)
    28 {
    29     if(l==r){
    30         cin>>sum[rt];
    31         return;
    32     }
    33     int mid=(l+r)>>1;
    34     build( l, mid, rt<<1);
    35     build(mid+1, r, rt<<1|1);
    36     Pushup(rt);
    37 }
    38 void update(int a,int b,int l,int r,int rt)
    39 {
    40     if(l==r){
    41         sum[rt]+=b;
    42         return ;
    43     }
    44     int mid=(r+l)>>1;
    45     if(a<=mid) update(a,b,l,mid,rt<<1);
    46     if(a>=mid+1) update(a,b,mid+1,r,rt<<1|1);
    47     Pushup(rt);
    48 }
    49 int query(int L, int R, int l,int r,int rt)
    50 {
    51     if(L<=l && R>=r) return sum[rt];
    52     int mid=(l+r)>>1;
    53     int res=0;
    54     if(L<=mid) res+=query(L,R,l,mid,rt<<1);
    55     if(R>=mid+1) res+=query(L,R,mid+1,r,rt<<1|1);
    56     return res;
    57 }
    58 int main()
    59 {
    60     ios::sync_with_stdio(false);
    61     cin.tie(0);
    62     int T,n;
    63     cin>>T;
    64     for(int i=1;i<=T;i++)
    65     {
    66         cout<<"Case "<<i<<':'<<endl;
    67         cin>>n;
    68         build(1,n,1);
    69 
    70         char s[10];    int a,b;
    71         while(cin>>s)
    72         {
    73             if(s[0]=='E') break;
    74             cin>>a>>b;
    75             if(s[0]=='Q') cout<<query(a,b,1,n,1)<<endl;
    76             else if(s[0]=='A') update(a,b,1,n,1);
    77             else update(a,-b,1,n,1);
    78         }
    79 
    80     }
    81 }
  • 相关阅读:
    关于ajax无刷新分页 vue
    ajax无刷新分页
    querylist采集 爬虫
    201703-1 分蛋糕 Java
    201612-2 工资计算 Java
    201612-1 中间数 Java
    201609-2 火车购票 Java
    201609-1 最大波动 Java
    201604-2 俄罗斯方块 Java
    201604-1 折点计数 Java
  • 原文地址:https://www.cnblogs.com/thunder-110/p/9322119.html
Copyright © 2011-2022 走看看