zoukankan      html  css  js  c++  java
  • HDU 1166 敌兵布阵(树状数组解法)

    垃圾HDU,不给数据,浪费我大好青春!


    【题目信息】

    敌兵布阵

    【分析】

    树状数组的裸题,单点修改+区间查询。

    【心路历程】

    脑残的我固输“Case 1”,memset了个sizeof(0)......

    ……

    【代码】

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int n,a;
    int c[110005];
    int lowbit(int x)
    { 
        return x&(-x);
    }
    
    void update(int x,int y,int n)
    {
        while(x<=n)
        {
            c[x]+=y;
            x+=lowbit(x);
        }
    }
    
    int sum(int x)
    {
        int ans=0;
        while(x>0)
        {
            ans+=c[x];
            x-=lowbit(x);
        }
        return ans;
    }
    
    int main()
    {
        int t,x,y,k=1;
        char s[1005];
        scanf("%d",&t);
        while(t--)
        {
            memset(c,0,sizeof(c));//de了一下午,才发现自己写的是sizeof(0) 
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a);
                update(i,a,n);
            }
                printf("Case %d:
    ",k++);
             while(~scanf("%s",s))
            {
                if(s[0]=='E') break;
                scanf("%d%d",&x,&y); 
                if(s[0]=='Q')
                {
                    printf("%d
    ",sum(y)-sum(x-1));
                }
                else if(s[0]=='A')
                {
                    update(x,y,n);
                }
                else if(s[0]=='S')
                {
                    update(x,-y,n);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    安装jdk
    chrome
    Jenkins启动
    Red Hat Linux分辨率调整
    Jemeter第一个实例
    grep与正则表达式
    使用ngx_lua构建高并发应用
    UML建模之时序图(Sequence Diagram)
    secureCRT mac 下破解
    跨域通信的解决方案JSONP
  • 原文地址:https://www.cnblogs.com/TheZealous/p/15185526.html
Copyright © 2011-2022 走看看