zoukankan      html  css  js  c++  java
  • HDU 1166 敌兵步阵

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1166

    源代码:

    树状数组:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int MAXN=50005;
    int a[MAXN],bit[MAXN];
    int n;
    
    int lowbit(int i){
        return i&(-i);
    }
    
    void build(){
        bit[0]=0;
        for(int i=1;i<=n;i++){
            bit[i]=a[i];
            for(int j=i-1;j>=i-lowbit(i)+1;j--)
                bit[i]+=a[j];
        }
    }
    
    int getresult(int k){
        int ans=0;
        while(k>0){
            ans+=bit[k];
            k-=lowbit(k);
        }
        return ans;
    }
    
    void add(int k,int value){
        while(k<=n){
            bit[k]+=value;
            k+=lowbit(k);
        }
    }
    
    
    int main(){
        int t;
        cin>>t;
        for(int k=1;k<=t;k++){
            printf("Case %d:
    ",k);
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%d",&a[i]);
            build();
    
            char str[10];
            while(scanf("%s",str)&&str[0]!='E'){
                int a,b;
                scanf("%d%d",&a,&b);
                if(str[0]=='Q'){
                    printf("%d
    ",getresult(b)-getresult(a-1));
                }else if(str[0]=='A'){
                    add(a,b);
                }else{
                    add(a,-b);
                }
            }
    
    
        }
    
    }
    
    线段树:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int MAXN=50005;
    int a[MAXN],sum[MAXN<<2];
    int N;
    
    
    
    inline void PushUp(int rt){
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    }
    
    
    void build(int l,int r,int rt){
        if(l==r){
            sum[rt]=a[l];
            return ;
        }
    
        int m=(l+r)>>1;
        build(l,m,rt<<1);
        build(m+1,r,rt<<1|1);
        PushUp(rt);
    }
    
    void Build(int l,int r,int rt)
    {
        if(l == r)
        {
           sum[rt]=a[l];
            return;
        }
        int m = (l + r) >> 1;
        //递归构造左右子树
        Build(l,m,rt<<1);
        Build(m+1,r,rt<<1|1);
        PushUp(rt);
    }
    
    void update(int p,int value,int l,int r,int rt){
        if(l==r){
            sum[rt]+=value;
            return;
        }
        int m=(l+r)>>1;
    
        if(p<=m)
            update(p,value,l,m,rt<<1);
        else
            update(p,value,m+1,r,rt<<1|1);
        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,l,m,rt<<1);
        if(R > m)
            ans += query(L,R,m+1,r,rt<<1|1);
        return ans;
    }
    
    void solve(){
         char chioce[10];
         while(scanf("%s",chioce)){
            if(chioce[0]=='E')
                break;
            int p,value;
            scanf("%d%d",&p,&value);
            if(chioce[0]=='S')
                update(p,-value,1,N,1);
            else if(chioce[0]=='A')
                update(p,value,1,N,1);
            else
                printf("%d
    ",query(p,value,1,N,1));
         }
    
    }
    int main(){
        int t;
    
        scanf("%d",&t);
        for(int j=1;j<=t;j++){
            printf("Case %d:
    ",j);
            scanf("%d",&N);
            for(int i=1;i<=N;i++)
                scanf("%d",&a[i]);
            build(1,N,1);
            solve();
        }
        return 0;
    }
    自己选的路,跪着也要把它走完------ACM坑
  • 相关阅读:
    FAL_CLIENT和FAL_SERVER参数详解
    Goldengate OGG常见问题与错误列表
    Goldengate:ERROR 180 encountered commit SCN that is not greater than the highest SCN already processed
    OGG-01028 Incompatible Record解决办法
    goldengate–使用filter+@GETENV在线重新初始化指定的table
    RAC环境中threads变更后如何确保goldengate继续正常复制
    default listener is not configured in grid infrastructure home
    11gr2 RAC安装INS-35354问题一例
    为11.2.0.2 Grid Infrastructure添加节点
    修改/dev/shm的大小
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6405544.html
Copyright © 2011-2022 走看看