zoukankan      html  css  js  c++  java
  • hdu1166 树状数组

    不知道为什么用C++输入输出死活不过,换成C的就过了。。。
    #include <stdio.h>
    #include <string.h>
    
    //==============================
    #define maxn 50020
    
    int c[maxn];
    int a[maxn];
    int n;
    int t;
    
    int lowbit(int x)
    {
        return x&(-x);
    }
    
    int Sum(int n)
    {
        int sum = 0;
        while(n>0)
        {
            sum += c[n];
            n = n - lowbit(n);
        }
        return sum;
    }
    
    void update(int i,int x)
    {
        while(i <= n)
        {
            c[i] = c[i] + x;
            i = i + lowbit(i);
        }
    }
    
    int GetSum(int x1,int x2)
    {
        return Sum(x2) - Sum(x1-1);
    }
    
    //===================================
    int main()
    {
        scanf("%d",&t);
        int count = 0;
        while(t--)
        {
            count++;
            memset(a,0,sizeof(a));
            memset(c,0,sizeof(c));
            scanf("%d",&n);
            for(int i = 1; i <= n; i++)
            {
                scanf("%d",&a[i]);
                update(i,a[i]);
            }
            //cout<<"case "<<count<<":"<<endl;
            printf("Case %d:
    ",count);
            char oper[11];
            int i,j;
            while(scanf("%s",oper)==1)
            {
                if(strcmp(oper,"End")==0)
                    break;
                scanf("%d%d",&i,&j);
                if(strcmp(oper,"Query")==0)
                {
                    printf("%d
    ",GetSum(i,j));
                }
                if(strcmp(oper,"Add")==0)
                {
                    a[i] += j;
                    update(i,j);
                }
                if(strcmp(oper,"Sub")==0)
                {
                    a[i] -= j;
                    update(i,-j);
                }
            }
        }
        return 0;
    }


  • 相关阅读:
    AdvStringGrid使用小结
    svn提示out of date的解决方法
    delphi之socket通讯
    Delphi的Socket编程步骤
    C++ Socket编程步骤
    centos7安装docker
    centos7安装指南
    UltraISO制作U盘启动盘
    浅谈linux 文件的三个时间
    自动配置zabbix-agent
  • 原文地址:https://www.cnblogs.com/riskyer/p/3244036.html
Copyright © 2011-2022 走看看