zoukankan      html  css  js  c++  java
  • 树状数组解决敌兵布阵~~

    View Code 
    #include <stdio.h>
    #include <string.h>
    int n;
    long long a[50005],f,b[50005];
    char s[10];
    int lowbit (int t)
    {
        return t & ( t ^ ( t - 1 ) );
    }
    int sum(int end)
    {
        int s=0;
        while (end>0)
        {
            s+=b[end];
            end-=lowbit(end);
        }
        return s;
    }
    void plu(int pos ,int num)
    {
        while (pos<=n)
        {
            b[pos]+=num;
            pos+=lowbit(pos);
        }
    }
    int main()
    {
        int T,i,j,t,c;
        scanf("%d",&T);
        t=0;
        while (T--)
        {
            ++t;
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            scanf("%d",&n);
            for (i=1;i<=n;i++)
                scanf("%lld",&a[i]);
            for (i=1;i<=n;i++)
            {
                int x;
                x=lowbit(i);
                while (x--)
                {
                    b[i]+=a[i-x];
                }
            }
            getchar();
            printf("Case %d:\n",t);
            while (scanf("%s",&s) && s[0]!='E')
            {
                scanf("%d%d",&i,&j);
                if (s[0]=='A') plu(i,j);
                else if (s[0]=='S') plu(i,-j);
                else 
                {
                    f=sum(j)-sum(i-1);
                    printf("%lld\n",f);
                }
            }
        }
        return 0;
    当你试图了解你的祖国时,你已踏上了犯罪的路程。
  • 相关阅读:
    旋转卡壳(1)求凸包(点集)直径 poj 2187
    求解平面最近点对的问题
    java中的内部类初识
    java中的多重继承
    ObjectiveC中的Protocols
    Mac中配置java的src.jar
    Mac OS X Lion无线网络问题
    Xcode中修改默认文件头部注释
    全文索引查询和like查询对比
    获取Repeater选中行索引
  • 原文地址:https://www.cnblogs.com/modiz/p/2972154.html
Copyright © 2011-2022 走看看