标签:
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4027
代码:
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<iostream> 7 #include<cmath> 8 const int N = 100005; 9 using namespace std; 10 11 #define Lson r<<1 12 #define Rson r<<1|1 13 #define mid a[r].Mid() 14 15 struct node 16 { 17 int L, R; 18 long long sum; 19 int Mid() 20 { 21 return (L+R)>>1; 22 } 23 int len() 24 { 25 return (R-L+1); 26 }; 27 } a[N<<2]; 28 29 void BuildTree(int r, int L, int R) 30 { 31 a[r].L=L, a[r].R=R; 32 33 if(L==R) 34 { 35 scanf("%lld", &a[r].sum); 36 return ; 37 } 38 39 BuildTree(Lson, L, mid); 40 BuildTree(Rson, mid+1, R); 41 42 a[r].sum = a[Lson].sum + a[Rson].sum; 43 } 44 45 void Oper(int r, int L, int R) 46 { 47 if(a[r].len() == a[r].sum) 48 return ; 49 50 if(a[r].L==a[r].R) 51 { 52 a[r].sum = (long long)sqrt(a[r].sum*1.0); 53 return ; 54 } 55 56 if(R<=mid) 57 Oper(Lson, L, R); 58 else if(L>mid) 59 Oper(Rson, L, R); 60 else 61 { 62 Oper(Lson, L, mid); 63 Oper(Rson, mid+1, R); 64 } 65 66 a[r].sum = a[Lson].sum + a[Rson].sum; 67 } 68 69 long long Query(int r, int L, int R) 70 { 71 if(a[r].L==L && a[r].R==R) 72 return a[r].sum; 73 74 if(R<=mid) 75 return Query(Lson, L, R); 76 else if(L>mid) 77 return Query(Rson, L, R); 78 else 79 { 80 return Query(Lson, L, mid) + Query(Rson, mid+1, R); 81 } 82 } 83 84 85 int main() 86 { 87 int n, m, t=1; 88 while(scanf("%d", &n)!=EOF) 89 { 90 91 BuildTree(1, 1, n); 92 93 int L, R, e; 94 95 scanf("%d", &m); 96 97 printf("Case #%d:\n", t++); 98 while(m--) 99 { 100 scanf("%d%d%d", &e, &L, &R); 101 102 if(L>R) 103 swap(L, R); 104 if(e==0) 105 Oper(1, L, R); 106 else 107 { 108 printf("%lld\n", Query(1, L, R)); 109 } 110 } 111 112 printf("\n"); 113 } 114 return 0; 115 }
(线段树 区间查询更新) Can you answer these queries? -- hdu--4027
标签:
原文地址:http://www.cnblogs.com/YY56/p/4693336.html