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);
            }
        }
    }
    
    
  • 相关阅读:
    PyCharm小技巧
    How to install torcs package in Debian
    QT4.8.6静态编译
    Debian初识(选择最佳镜像发布站点加入source.list文件)
    Dev-C++ 小问题锦集
    ubuntu 12.04lts 安装mysql ,并通过QT连接
    win7下安装ubuntu14.04lts 双系统
    cmake打印变量值
    驾车常识:小轿车灯光
    汽车点火开关的功能介绍
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9375545.html
Copyright © 2011-2022 走看看