18.9.10 週一 線段樹經典(單點更新 區間求和)
敵兵佈陣 https://vjudge.net/contest/229162#problem/A
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條命令
Outpu
t對第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
模板題
#include <set> #include <cmath> #include <queue> #include <stack> #include <vector> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #include <functional> #include<string.h> #include<stdio.h> using namespace std; const int maxn = 2e5 + 10; int qwq[maxn]; struct node { int value; int left, right; }tree[maxn]; void build(int root, int l, int r) { tree[root].left = l; tree[root].right = r; //當找到l和r相等的節點 如[2,2],把第2個輸入的值賦給當前節點的value if (l == r) { tree[root].value = qwq[l]; return; } int mid = (l + r) / 2; //二分查詢兒子 build(root << 1, l, mid); build(root << 1 | 1, mid + 1, r); //將兒子的值加到父親節點上 tree[root].value = tree[root * 2].value + tree[root * 2 + 1].value; } //n是要更新的節點編號,v是要加上的值 void update(int root, int n, int v) { //當某一個節點的left和righr相等時,更新該點的值 if (tree[root].left == tree[root].right) { tree[root].value += v; return; } int mid = (tree[root].left + tree[root].right) / 2; //二分查詢兒子 if (n <= mid) update(root << 1, n, v); else update(root << 1 | 1, n, v); tree[root].value = tree[root * 2].value + tree[root * 2 + 1].value; } //查詢某個區間的和 int query(int root, int l, int r) { //當被查詢的區間完全包含當前節點的區間時 //返回該節點的value值 if (l <= tree[root].left && r >= tree[root].right) return tree[root].value; int mid = (tree[root].left + tree[root].right) / 2; //累加變數ans int ans = 0; //二分向下查詢兒子 並加到ans if (l <= mid) ans = ans + query(root << 1, l, r); if (r>mid) ans = ans + query(root << 1 | 1, l, r); return ans; } int main() { int t, n; int cnt = 1; scanf("%d", &t); while (t--) { printf("Case %d:\n", cnt++); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &qwq[i]); build(1, 1, n); int a, b; char op[10]; while (scanf("%s", op)) { if (op[0] == 'E') break; scanf("%d%d", &a, &b); if (op[0] == 'Q') printf("%d\n", query(1, a, b)); if (op[0] == 'A') update(1, a, b); if (op[0] == 'S') update(1, a, -b); } } return 0; }