HDUJ 1166 敵兵布陣

來源:互聯網
上載者:User

標籤:hduj   c++   

敵兵布陣Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42987    Accepted Submission(s): 18193


Problem DescriptionC國的死對頭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
1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End 
 
Sample Output
Case 1:63359


樹狀數組:

#include<stdio.h>#include<string.h>#include<math.h>int a[50005];int n;int lowbit(int t){    return t&(-t);}void insert(int t,int d){    while(t<=n)    {        a[t]+=d;        t+=lowbit(t);    }}int getsum(int t){    int sum=0;    while(t>0)    {        sum+=a[t];        t-=lowbit(t);    }    return sum;}int main(){    int T,i,j,k,t;    scanf("%d",&T);    t=0;    while(T--)    {        memset(a,0,sizeof(a));        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&k);            insert(i,k);        }        char str[10];        scanf("%s",str);        printf("Case %d:\n",++t);        while(str[0]!='E')        {            int x,y;            scanf("%d%d",&x,&y);            if(str[0]=='Q')                printf("%d\n",getsum(y)-getsum(x-1));            else    if(str[0]=='A')                insert(x,y);            else    if(str[0]=='S')                insert(x,(-1)*y);            scanf("%s",str);        }    }    return 0;}



線段樹:

#include<iostream>#include<cstring>]#include<cstdio>using namespace std;struct Node{    int left,right;    int sum;}node[4*50005];int a[50005];void build(int l,int r,int i){    node[i].left=l;    node[i].right=r;    if(l==r)    {        node[i].sum=a[l];        return ;    }    int m=(l+r)/2;    build(l,m,i*2);    build(m+1,r,i*2+1);    node[i].sum=node[i*2].sum+node[i*2+1].sum;}void update(int i,int x,int y){    int l=node[i].left;    int r=node[i].right;    int m=(l+r)/2;    if(x==l && x==r)    {        node[i].sum += y;        return ;    }    if(x<=m)        update(i*2,x,y);    else        update(i*2+1,x,y);    node[i].sum += y;}int query(int l,int r,int i){    if(node[i].left==l && node[i].right==r)        return node[i].sum;    //if(node[i].left==node[i].right)        //return 0;    int m=(node[i].left+node[i].right)/2;    if(r<=m)        return query(l,r,i*2);    else   if(l>m)        return query(l,r,i*2+1);    else        return query(l,m,i*2)+query(m+1,r,i*2+1);}int main(){    int T;    scanf("%d",&T);    for(int k=1;k<=T;k++)    {        memset(a,0,sizeof(a));        int i,n;        scanf("%d",&n);        for(i=1;i<=n;i++)            scanf("%d",&a[i]);        build(1,n,1);        char str[10];        int x,y;        printf("Case %d:\n",k);        scanf("%s",str);        while(str[0]!='E')        {            scanf("%d%d",&x,&y);            if(str[0]=='Q')                printf("%d\n",query(x,y,1));            else  if(str[0]=='A')                update(1,x,y);            else                update(1,x,-y);            scanf("%s",str);        }    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.