You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
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 题意:就是它的每次操作是将一段区间的所有数都开一次平方,这个好像分块的时候也做过一次。
题解:因为一个数经过几次平方根就会GG变为1,然后就只需要记录这段区间是否为r-l+1即可。
是的话就不需要继续开根好了,否则就暴力下去,各个数开平方,就没了,每个数平均5次暴力,
然后就是普通线段树了,复杂度近似为O(n log n)
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
const int MAXN=;
typedef long long LL;
long long a[MAXN],tree[MAXN*];
int x,y,z,q,n,t;
using namespace std; void build(int l,int r,int p)
{
if (l==r) tree[p]=a[l];
else
{
int mid=(l+r)>>;
build(l,mid,p*);
build(mid+,r,p*+);
tree[p]=tree[p*]+tree[p*+];
}
}
LL query(int l,int r,int p,int x,int y)
{
if (l==x&&r==y) return tree[p];
else
{
int mid=(l+r)>>;
LL res;
if (y<=mid) res=query(l,mid,p*,x,y);
else if(x>=mid+) res=query(mid+,r,p*+,x,y);
else
{
res=query(l,mid,p*,x,mid);
res=res+query(mid+,r,p*+,mid+,y);
}
return res;
}
}
void change(int l,int r,int p,int x,int y)
{
if (l==r) tree[p]=LL(sqrt(tree[p]));
else
{
if (tree[p]!=(r-l+))
{
int mid=(l+r)>>;
if (y<=mid) change(l,mid,p*,x,y);
else if (x>=mid+) change(mid+,r,p*+,x,y);
else
{
change(l,mid,p*,x,mid);
change(mid+,r,p*+,mid+,y);
}
tree[p]=tree[p*]+tree[p*+];
}
}
}
int main()
{
t=;
while (~scanf("%d",&n))
{
memset(tree,,sizeof(tree));
memset(a,,sizeof(a));
printf("Case #%d:\n",++t);
for (int i=;i<=n;i++)
scanf("%lld",&a[i]);
build(,n,);
scanf("%d",&q);
for (int i=;i<=q;i++)
{
scanf("%d%d%d",&x,&y,&z);
if (y>z) swap(y,z);
if (x) printf("%lld\n",query(,n,,y,z));
else change(,n,,y,z);
}
printf("\n");
}
}