zoukankan      html  css  js  c++  java
  • HDU 1611 敌兵布阵【线段树模板】

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const ll LNF = 1e18;
    const int maxn = 1e5;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    int sum[maxn<<2];
    
    void PushUp(int rt)
    {
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    }
    void build(int l,int r,int rt)
    {
        if(l==r)
        {
            scanf("%d",&sum[rt]);
            return ;
        }
        int m = (l+r)>>1;
        build(lson);
        build(rson);
        PushUp(rt);
    }
    void update(int p,int add,int l,int r,int rt)
    {
        if(l==r)
        {
            sum[rt] += add;
            return ;
        }
        int m = (l+r)>>1;
        if(p<=m) update(p,add,lson);
        else update(p,add,rson);
        PushUp(rt);
    }
    int query(int L,int R,int l,int r,int rt)
    {
        if(L<=l && r<=R)
        {
            return sum[rt];
        }
        int m = (l+r)>>1;
        int ans = 0;
        if(L<=m) ans += query(L,R,lson);
        if(R>m) ans += query(L,R,rson);
        return ans;
    }
    
    int main()
    {
        int T,n;
        scanf("%d",&T);
        for(int cas=1;cas<=T;cas++)
        {
            printf("Case %d:
    ",cas);
            scanf("%d",&n);
            build(1,n,1);
            char op[15];
            while(scanf("%s",op))
            {
                if(op[0]=='E') break;
                int a,b;
                scanf("%d%d",&a,&b);
                if(op[0]=='Q')
                {
                    printf("%d
    ",query(a,b,1,n,1));
                }
                else if(op[0]=='S') update(a,-b,1,n,1);
                else update(a,b,1,n,1);
            }
        }
    }
    
    
  • 相关阅读:
    ngnix+uwsgi+django 部署mezzanine
    shell三剑客之find
    Flask常见面试问题
    redis宕机如何解决?如果是项目上线的宕机呢?
    UiPath,容智Ibot在线接单,有需求的欢迎过来
    CORS和CSRF
    JWT黑名单和白名单
    Django项目常见面试问题
    降低Redis内存占用
    Redis-缓存有效期与淘汰策略
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9375545.html
Copyright © 2011-2022 走看看