HDU-1166 敌兵布阵 (线段树&&树状数组入门)

                                     敌兵布阵

 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。 

中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的. 

Input第一行一个整数T,表示有T组数据。 
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。 
接下来每行有一条命令,命令有4种形式: 
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30) 
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30); 
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数; 
(4)End 表示结束,这条命令在每组数据最后出现; 
每组数据最多有40000条命令 
Output对第i组数据,首先输出“Case i:”和回车, 
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。 
Sample Input

1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End 

Sample Output

Case 1:
6
33
59

中文题emmmm

树状数组
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a))
#define P pair< int , int >
const int maxn=50000+10;
const int INF = 0x3f3f3f3f3f;
int tree[maxn],n;
void add(int k,int num)
{
    while(k<=n)
    {
        tree[k]+=num;
        k+=k&(-k);
    }
}
int sum(int k)
{
    int ans=0;
    while(k)
    {
        ans+=tree[k];
        k-=k&(-k);
    }
    return ans;
}
int main()
{
    int t,x,y,a,k=1;
    char p[6];
    scanf("%d",&t);
    while(t--)
    {
        printf("Case %d:\n",k++);
        memset(tree,0,sizeof(tree));
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a);
            add(i,a);
        }
        while(~scanf("%s",p))
        {
            if(p[0]=='E')
                break;
            scanf("%d%d",&x,&y);
            if(p[0]=='Q')
                printf("%d\n",sum(y)-sum(x-1));
            else if(p[0]=='A')
            {
                add(x,y);
            }
            else if(p[0]=='S')
            {
                add(x,-y);
            }

        }
    }
    return 0;
}

线段树

#include<iostream>
#include<cstdio>
#include<conio.h>
#include<cstdlib>
#include<cstring>
#include<ctime>
using namespace std;
const int maxn = 50000+10;
int sum[maxn<<2],a[maxn<<2];
void pushup(int root)
{
    sum[root]=sum[root<<1]+sum[root<<1|1];
}
void build(int l,int r,int root)
{
    if(l==r)
    {
        sum[root]=a[l];
        return ;
    }
    int m=(l+r)>>1;
    build(l,m,root<<1);
    build(m+1,r,root<<1|1);
    pushup(root);
}
void updata(int L,int c,int l,int r,int root)
{
    if(l==r)
    {
        sum[root]+=c;
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m)
        updata(L,c,l,m,root<<1);
    else updata(L,c,m+1,r,root<<1|1);
    pushup(root);
}
int query(int L,int R,int l,int r,int root)
{
    if(L<=l&&R>=r)
        return sum[root];
    int m=(l+r)>>1;
    int ans=0;
    if(L<=m) ans+=query(L,R,l,m,root<<1);
    if(R>m)  ans+=query(L,R,m+1,r,root<<1|1);
    return ans;
}
int main()
{
    int t, c;
    char d[10];
    scanf("%d", &t);
    for (c = 1; c <= t; c++)
    {
        printf("Case %d:\n", c);
        int n;
        scanf("%d", &n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        build(1, n, 1);
        while (scanf("%s", d) != EOF)
        {
            if (d[0] == 'E') break;
            int x, y;
            scanf("%d%d", &x, &y);
            if (d[0] == 'Q')
            {
                int ans = query(x, y, 1, n, 1);
                printf("%d\n", ans);
            }
            if (d[0] == 'S') updata(x, -y, 1, n, 1);
            if (d[0] == 'A') updata(x, y, 1, n, 1);
        }
    }
    return 0;
}

PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

猜你喜欢

转载自www.cnblogs.com/MengX/p/9063587.html
今日推荐