标签:sam stream hdu show while 就是 -- include ott
裸的线段树。
#include <stdio.h> #include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define mem(arr, num) memset(arr, 0, sizeof(arr)) #define _for(i, a, b) for (int i = a; i <= b; i++) #define __for(i, a, b) for (int i = a; i >= b; i--) #define IO \ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); using namespace std; typedef long long ll; const ll inf = 0x3f3f3f3f; const double EPS = 1e-10; const ll mod = 1000000007LL; const int N = 1 << 17; int dat[N]; int n; void init(int n_) { n = 1; while(n < n_) n *= 2; _for(i, 1, n * 2 -1) dat[i] = 0; } void update(int k, int a) { k += n-1; dat[k] += a; while(k > 1) { k >>= 1; dat[k] += a; } } int query(int a, int b, int k, int l, int r) { if(a > r || b < l) return 0; if(a <=l && b >= r) return dat[k]; else { int vl = query(a, b, k * 2, l, (l + r) / 2); int vr = query(a, b, k * 2 + 1,(l+r)/2 + 1,r); return vl + vr; } } int main(){ int T,num,x,y; IO string str; cin >> T; _for(k, 1, T) { cin >> num; init(num); _for(i, 1, num){ cin >> x; update(i, x); } cout << "Case " << k <<":" << endl; while(cin >> str,str!="End") { if(str == "Query") { cin >> x >> y; cout << query(x, y, 1, 1, n) << endl; } else if(str == "Add") { cin >> x >> y; update(x, y); } else if(str == "Sub") { cin >> x >> y; update(x,-y); } } } return 0; }
标签:sam stream hdu show while 就是 -- include ott
原文地址:https://www.cnblogs.com/GHzz/p/8877286.html