• ZOJ 3279


    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3279

    数状数组

    #include <stdio.h>
    #include <string.h>
    #define lowbit(x) ((x)&(-x))
    int n;
    int a[100005],b[100005];
    void update(int x,int v)
    {
        while(x <= n)
        {
            a[x] += v;
            x += lowbit(x);
        }
    }
    int sum(int x)
    {
        int ret = 0;
        while(x)
        {
            ret += a[x];
            x -= lowbit(x);
        }
        return ret;
    }
    
    int main()
    {
        int i,m,x,y;
        char ch[2];
        while(scanf("%d",&n) ==1)
        {
            memset(a,0,sizeof(a));
            for(i = 1;i <= n;i ++)
            {
                scanf("%d",&b[i]);
                update(i,b[i]);
            }
            scanf("%d",&m);
            while(m --)
            {
                scanf("%s",ch);
                if(ch[0] == 'p')
                {
                    scanf("%d %d",&x,&y);
                    update(x,y-b[x]);
                    b[x] = y;
                }
                else
                {
                    scanf("%d",&x);
                    int l=1;
                    int r=n;
                    int h;
                    while(l<=r)
                    {
                        h=(l+r)/2;
                        if(x>sum(h-1)&&x<=sum(h)) break;
                        if(x<=sum(h-1)) {r=h-1;continue;}
                        if(x>sum(h)) l=h+1;
                    }
                    printf("%d\n",h);
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    2020-03-03
    2020-03-02
    2020-03-01
    2020-02-29
    简单自我介绍
    福大软工1816 · 第六次作业
    福大软工1816 · 第五次作业
    python爬虫解决编码问题
    第四次作业-团队介绍
    福大软工1816 · 第三次作业
  • 原文地址:https://www.cnblogs.com/zhangteng512/p/2118743.html
Copyright © 2020-2023  润新知