标签:ring bsp tac queue sub math pac bre mic
线段树(单点更新)
/* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<cstring> #include<queue> #include<set> #include<string> #include<map> #include <time.h> #define PI acos(-1) using namespace std; typedef long long ll; typedef double db; const int maxn = 500002; const ll maxm = 1e7; const ll base = 2333; const int INF = 1<<30; const db eps = 1e-8; const ll mod = 1e9+13; struct nodee{ int left, right, mid, num; }node[maxn]; int ca=1; void build(int left, int right, int n) { node[n].left=left; node[n].right=right; node[n].mid=(left+right)/2; node[n].num=0; if (left+1==right) return; build(left, (left+right)/2, 2*n); build((left+right)/2, right, 2*n+1); } void updata(int pos, int value, int n) { node[n].num+=value; if (node[n].left+1==node[n].right) return; if (pos<node[n].mid) updata(pos, value, 2*n); else updata(pos, value, 2*n+1); } int query(int left, int right, int n) { if (node[n].left==left && node[n].right==right) { return node[n].num; } if (left<node[n].mid) { if(right <= node[n].mid ){ return query(left,right,2*n); } else{ return query(left,node[n].mid,2*n) + query(node[n].mid,right,2*n + 1); } } else return query(left,right,2*n+1); } void solve() { int n; scanf("%d", &n); build(1, n+1, 1); for (int i=1; i<=n; i++) { int x; scanf("%d", &x); updata(i, x, 1); } char c[10]; printf("Case %d:\n", ca++); while(1) { scanf("%s", c); if (strcmp(c, "End")==0) break; int x, y; scanf("%d%d", &x, &y); if (strcmp(c, "Query")==0) { int ans=query(x, y+1, 1); printf("%d\n", ans); } else if(strcmp(c,"Add") == 0){ updata(x, y, 1); } else if(strcmp(c,"Sub") == 0){ updata(x, -y, 1); } } } int main() { int t = 1; // freopen("in.txt","r",stdin); // freopen("gcd.out","w",stdout); scanf("%d", &t); while(t--) solve(); return 0; }
标签:ring bsp tac queue sub math pac bre mic
原文地址:http://www.cnblogs.com/gggyt/p/7249559.html