标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 12290 Accepted Submission(s): 2912
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <set> #include <stack> #include <cmath> using namespace std; #define lson l, m, rt<<1 #define rson m + 1, r, rt<<1|1 #define ll long long const int maxn = 1e5 + 10; ll sum[maxn<<2]; void PushUp(int rt) { sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void build(int l, int r, int rt) { sum[rt] = 0; if(l == r) { scanf("%lld", &sum[rt]); return; } int m = (l + r) >> 1; build(lson); build(rson); PushUp(rt); } void update(int L, int R, int l, int r, int rt) { if(sum[rt] == r - l + 1) return; if(l == r) { sum[rt] = sqrt((double) sum[rt]); return; } int m = (l + r) >> 1; if(L <= m) update(L, R, lson); if(R > m) update(L, R, rson); PushUp(rt); } ll query(int L, int R, int l, int r, int rt) { if(L <= l && r <= R) return sum[rt]; ll res = 0; int m = (l + r) >> 1; if(L <= m) res += query(L, R, lson); if(R > m) res += query(L, R, rson); return res; } int main() { int n; int val = 0; while(~scanf("%d", &n)) { printf("Case #%d:\n", ++val); build(1, n, 1); int m; scanf("%d", &m); while(m--) { int a, b, c; scanf("%d %d %d", &a, &b, &c); if(c < b) swap(b, c); if(a == 0) update(b, c, 1, n, 1); else printf("%lld\n", query(b, c, 1, n, 1)); }puts(""); } }
HDU 4027 Can you answer these queries? (线段树区间求和)
标签:
原文地址:http://www.cnblogs.com/lonewanderer/p/5648130.html