HDU1166 敵兵佈陣 (線段樹或樹狀陣列)
阿新 • • 發佈:2018-11-09
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的責罵的.
中央情報局要研究敵人究竟演習什麼戰術,所以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
簡單的模板題,但是重要的是理解!!!
樹狀陣列:首先理解lowbit(pos),心中有'樹‘,然後理解含義即可。
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <stringView Code.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <algorithm> 12 #include <sstream> 13 #include <stack> 14 using namespace std; 15 #define FO freopen("in.txt","r",stdin); 16#define rep(i,a,n) for (int i=a;i<n;i++) 17 #define per(i,a,n) for (int i=n-1;i>=a;i--) 18 #define pb push_back 19 #define mp make_pair 20 #define all(x) (x).begin(),(x).end() 21 #define fi first 22 #define se second 23 #define SZ(x) ((int)(x).size()) 24 #define debug(x) cout << "&&" << x << "&&" << endl; 25 #define lowbit(x) (x&-x) 26 #define mem(a,b) memset(a, b, sizeof(a)); 27 typedef vector<int> VI; 28 typedef long long ll; 29 typedef pair<int,int> PII; 30 const ll mod=1000000007; 31 const int inf = 0x3f3f3f3f; 32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} 33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} 34 //head 35 const int maxn=50010; 36 int c[maxn],_,n,pos,val; 37 38 void Updata(int pos,int val) {//覆蓋該點的段,都+=val 39 while(pos<=n) { 40 c[pos]+=val; 41 pos+=lowbit(pos); 42 } 43 } 44 45 int Query(int pos) {//將覆蓋1~pos的各個段相加 46 int ans=0; 47 while(pos>0) { 48 ans+=c[pos]; 49 pos-=lowbit(pos); 50 } 51 return ans; 52 } 53 54 int main() { 55 int cur=1; 56 for(scanf("%d",&_);_;_--) { 57 mem(c,0); 58 scanf("%d",&n); 59 rep(i,1,n+1) { 60 scanf("%d",&val); 61 Updata(i,val);//注意這裡的更新!!! 62 } 63 char s[10]; 64 int pos,val; 65 printf("Case %d:\n",cur++); 66 while(1) { 67 scanf("%s",s); 68 if(s[0]=='E') break; 69 scanf("%d%d",&pos,&val); 70 if(s[0]=='A') Updata(pos,val); 71 else if(s[0]=='S') Updata(pos,-val); 72 else if(s[0]=='Q') printf("%d\n",Query(val)-Query(pos-1)); 73 } 74 } 75 }
線段樹:單點修改+區間查詢。還是要明白過程!
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <algorithm> 12 #include <sstream> 13 #include <stack> 14 using namespace std; 15 #define FO freopen("in.txt","r",stdin); 16 #define rep(i,a,n) for (int i=a;i<n;i++) 17 #define per(i,a,n) for (int i=n-1;i>=a;i--) 18 #define pb push_back 19 #define mp make_pair 20 #define all(x) (x).begin(),(x).end() 21 #define fi first 22 #define se second 23 #define SZ(x) ((int)(x).size()) 24 #define debug(x) cout << "&&" << x << "&&" << endl; 25 #define lowbit(x) (x&-x) 26 #define mem(a,b) memset(a, b, sizeof(a)); 27 typedef vector<int> VI; 28 typedef long long ll; 29 typedef pair<int,int> PII; 30 const ll mod=1000000007; 31 const int inf = 0x3f3f3f3f; 32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} 33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} 34 //head 35 const int N=50010; 36 int a[N<<2],sum[N<<2]; 37 int n,_,cur=1,pos,val; 38 char s[10]; 39 40 void Pushup(int rt) {//更新父節點 41 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 42 } 43 44 void build(int rt,int L,int R) {//建樹 45 if(L==R) { 46 scanf("%d",&sum[rt]);//注意這裡是sum[rt] 47 return; 48 } 49 int mid=(L+R)>>1; 50 build(rt<<1,L,mid); 51 build(rt<<1|1,mid+1,R); 52 Pushup(rt);//不要忘了更新 53 } 54 55 56 void Updata(int rt,int L,int R,int pos,int val) {//找pos的位置 57 if(L==R) { 58 sum[rt]+=val; 59 return; 60 } 61 int mid=(L+R)>>1;//左右子樹找pos 62 if(pos<=mid) Updata(rt<<1,L,mid,pos,val); 63 else Updata(rt<<1|1,mid+1,R,pos,val); 64 Pushup(rt);//資訊變更,及時更新 65 } 66 67 int Query(int rt,int L,int R,int l,int r) { 68 if(L>=l&&R<=r) 69 return sum[rt]; 70 int mid=(L+R)>>1; 71 int ans=0; 72 if(l<=mid) ans+=Query(rt<<1,L,mid,l,r); 73 if(r>mid) ans+=Query(rt<<1|1,mid+1,R,l,r); 74 return ans; 75 } 76 77 int main() { 78 for(scanf("%d",&_);_;_--) { 79 scanf("%d",&n); 80 build(1,1,n); 81 printf("Case %d:\n",cur++); 82 while(1) { 83 scanf("%s",s); 84 if(s[0]=='E') break; 85 scanf("%d%d",&pos,&val); 86 if(s[0]=='A') Updata(1,1,n,pos,val); 87 else if(s[0]=='S') Updata(1,1,n,pos,-val); 88 else printf("%d\n",Query(1,1,n,pos,val)); 89 } 90 } 91 }View Code