标签:
http://acm.hdu.edu.cn/showproblem.php?pid=4027
Description
Input
Output
Sample Input
Sample Output
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #define N 100010 #define Lson root<<1, L, tree[root].Mid() #define Rson root<<1|1, tree[root].Mid() + 1, R using namespace std; struct Tree { int L, R; long long sum; int Mid() { return (L + R) / 2; } int Len() { return (R - L + 1); } } tree[N * 4]; long long al[N]; void Build(int root, int L, int R) { tree[root].L = L, tree[root].R = R; if(L == R) { tree[root].sum = al[L]; return ; } Build(Lson); Build(Rson); tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum; } void Update(int root, int L, int R) { if(tree[root].Len() == tree[root].sum) return ;//注意这儿如果省了会TLE if(tree[root].L == tree[root].R) { tree[root].sum = (long long)sqrt(tree[root].sum); return ; } if(R <= tree[root].Mid()) Update(root<<1, L, R); else if(L > tree[root].Mid()) Update(root<<1|1, L, R); else { Update(Lson); Update(Rson); } tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum; } long long Query(int root, int L, int R) { if(tree[root].L == L && tree[root].R == R) return tree[root].sum; if(R <= tree[root].Mid()) return Query(root<<1, L, R); else if(L > tree[root].Mid()) return Query(root<<1|1, L, R); else return Query(Lson) + Query(Rson); } int main() { int n, m, i, t, a, b, x = 0; while(~scanf("%d", &n)) { x++; for(i = 1 ; i <= n ; i++) scanf("%I64d", &al[i]); Build(1, 1, n); scanf("%d", &m); printf("Case #%d:\n", x); while(m--) { scanf("%d%d%d", &t, &a, &b); if(a > b) swap(a, b);//注意,如果所给的a比b大,两者就需要调换位置 if(t == 0) Update(1, a, b); else printf("%I64d\n", Query(1, a, b)); } printf("\n"); } return 0; }
hdu 4027 Can you answer these queries?
标签:
原文地址:http://www.cnblogs.com/qq2424260747/p/4699407.html