InputThe input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
OutputFor each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.Sample Input
10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
Sample Output
Case #1: 19 7 6
题意:有一个长度不超过100000个数组,数组的数大小不超过2^63,且一直是整型,执行两个操作,0操作表示将指定区间内的数都开平方,1操作
表示查询指定区间的总数和。注意操作的区间x,y,x可能大于y,输出格式按样例,输入以EOF结束。
思路:首先一个数的被开方次数不会超过7次,因为超过7次后这个数会变成1,因此单曲奖和变成r-l+1时可以直接return ,方便节省时间,
比较注意的是区间开方问题,由于是开方,依此标记就没用了,改用定区间,单点修改值的方法,具体实现见代码,最后再加一个区间求和就行。
注意数要开longlong型。
代码:
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 998244353 16 #define eps 1e-6 17 #define ll long long 18 #define INF 0x3f3f3f3f 19 using namespace std; 20 21 struct node 22 { 23 //l表示左边,r表示右边 24 int l,r; 25 ll sum; 26 27 }; 28 node no[500000]; 29 ll num[100000]; 30 int q,a,b,c; 31 //初始化 32 //k表示当前节点的编号,l表示当前区间的左边界,r表示当前区间的右边界 33 void build(int k,int l,int r) 34 { 35 no[k].l=l; 36 no[k].r=r; 37 //如果递归到最低点 38 if(l==r) 39 { 40 no[k].sum=num[l]; 41 return ; 42 } 43 //对半分 44 int mid=(l+r)/2; 45 //递归到左线段 46 build(k*2,l,mid); 47 //递归到右线段 48 build(k*2+1,mid+1,r); 49 //更新当前节点的值 50 no[k].sum=no[k*2].sum+no[k*2+1].sum; 51 } 52 //区间单点修改 53 //从区间1到n中搜索在定区间b,c中的元素, 54 void change(int k,int l,int r) 55 { 56 if(no[k].sum==(no[k].r-no[k].l+1)) 57 { 58 return ; 59 } 60 if(no[k].l==no[k].r) 61 { 62 no[k].sum=(ll)sqrt(no[k].sum); 63 return ; 64 } 65 int mid = (no[k].l+no[k].r)/2; 66 if(b<=mid) 67 { 68 change(k*2,l,mid); 69 } 70 if(c>mid) 71 { 72 change(k*2+1,mid+1,r); 73 } 74 //更新当前节点的值 75 no[k].sum=no[k*2].sum+no[k*2+1].sum; 76 } 77 //求区间 78 ll query(int k,int l,int r) 79 { 80 //到对应层时返回值 81 if(no[k].l==l&&no[k].r==r) 82 { 83 return no[k].sum; 84 } 85 //取中值 86 int mid=(no[k].l+no[k].r)/2; 87 if(r<=mid) 88 { 89 return query(k*2,l,r); 90 } 91 else if(l>mid) 92 { 93 return query(k*2+1,l,r); 94 } 95 else 96 { 97 return query(k*2,l,mid)+query(k*2+1,mid+1,r); 98 } 99 } 100 101 int main() 102 { 103 int n; 104 int ans=1; 105 while(scanf("%d",&n)!=EOF) 106 { 107 memset(num,0,sizeof(num)); 108 memset(no,0,sizeof(no)); 109 printf("Case #%d:\n",ans++); 110 for(int i=1;i<=n;i++) 111 { 112 scanf("%lld",&num[i]); 113 } 114 build(1,1,n); 115 scanf("%d",&q); 116 while(q--) 117 { 118 scanf("%d %d %d",&a,&b,&c); 119 if(b>c) 120 { 121 swap(b,c); 122 } 123 if(a==0) 124 { 125 change(1,1,n); 126 } 127 else if(a==1) 128 { 129 printf("%lld\n",query(1,b,c)); 130 } 131 } 132 printf("\n"); 133 } 134 }
Can you answer these queries? HDU - 4027 线段树之区间单点开方,区间求和
原文:https://www.cnblogs.com/mzchuan/p/11808828.html