分类 树状数组 下的文章

HDU1166敌兵布阵

原题链接

HDU1166


题目抽象

给出一串数,要求
1)单点加减修改
2)区间查询


做法

树状数组,线段树,zkw。
尤其:如果常数不能像我的一样小,请用特技(scanf)。


代码

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

inline int lowbit(int x)
{
    return x&-x;
}

const int maxn = 5e4 + 10;
int a[maxn];
int n;

void add(int x, int v)
{
    while(x <= n)
    {
        a[x] += v;
        x += lowbit(x);
    }
}

int query(int x)
{
    int ans = 0;
    while(x > 0)
    {
        ans += a[x];
        x -= lowbit(x);
    }
    return ans;
}

int main()
{
    int t;
    cin>>t;
    for(int i = 1; i <= t; i++)
    {
        cout<<"Case "<<i<<":"<<endl;
        memset(a, 0, sizeof a);
        cin>>n;
        for(int j = 1; j <= n; j++)
        {
            int tmp;
            cin>>tmp;
            add(j, tmp);
        }
        string type;
        while(cin>>type && type != "End")
        {
            if(type == "Add")
            {
                int x, v;
                cin>>x>>v;
                add(x, v);
            }
            else if(type == "Sub")
            {
                int x, v;
                cin>>x>>v;
                add(x, -v);
            }
            else if(type == "Query")
            {
                int x, y;
                cin>>x>>y;
                cout<<query(y)-query(x-1)<<endl;
            }
        }
    }
    return 0;
}
bst g22 jinniu lilai opebet orange88 vinbet xbet yuebo zunlong shijiebei bet007 hg0088 ju111 letiantang m88 mayaba qg777 qianyiguoji sbf777 tengbohui tlc ule weilianxier waiweitouzhu xingfayule xinhaotiandi yinheyule youfayule zhongying 2018shijiebei w88 18luck 188bet beplay manbet 12bet 95zz shenbo weide1946 ca88 88bifa aomenxinpujing betway bodog bt365 bwin tongbao vwin weinisiren 88jt fenghuangyule hongyunguoji 918botiantang huanyayule jianada28 jixiangfang libo long8 hongzuyishi zuqiutouzhu