(传送门)
Description
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
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.
Input
The 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 263.
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.
Output
For 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
核心思想:
线段树。
坑点:给定的区间是X和Y之间,X可能大于Y!!!
每一次操作都会将相应的叶子结点的值开方,一个数n被开方log(n)次就会成为1,再被开方值就不变了(另外0被开方也是0)。
引入失活标记。如果一个区间内所有叶子结点的值都是0或1,那么此区间不需要再被更新,则此区间的失活标记置为1,否则置为0。更新线段树时先判断区间是否失活,如果失活直接return。
线段树回溯时的pushup函数维护区间和以及失活标记。此区间失活当且仅当左子区间和右子区间都失活。
代码如下:
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e5+20;
struct node{
int l,r,f;//f为失活标记
ll sum;
}tr[N<<2];
void pushup(int m)
{
//维护区间和
tr[m].sum=tr[m<<1].sum+tr[m<<1|1].sum;
//维护区间活性
if(tr[m<<1].f&&tr[m<<1|1].f)
tr[m].f=1;
else
tr[m].f=0;
return;
}
void build(int m,int l,int r)
{
tr[m].l=l;
tr[m].r=r;
if(l==r)
{
scanf("%lld",&tr[m].sum);
if(tr[m].sum<=1)
tr[m].f=1;
else
tr[m].f=0;
return;
}
int mid=(l+r)>>1;
build(m<<1,l,mid);
build(m<<1|1,mid+1,r);
pushup(m);
return;
}
void update(int m,int l,int r)
{
if(tr[m].f)//区间失活直接return
return;
if(tr[m].l==tr[m].r)
{
tr[m].sum=sqrt(tr[m].sum);
if(tr[m].sum<=1)
tr[m].f=1;
return;
}
int mid=(tr[m].l+tr[m].r)>>1;
if(r<=mid)
update(m<<1,l,r);
else if(l>mid)
update(m<<1|1,l,r);
else
{
update(m<<1,l,mid);
update(m<<1|1,mid+1,r);
}
pushup(m);
return;
}
ll query(int m,int l,int r)
{
if(tr[m].l==l&&tr[m].r==r)
return tr[m].sum;
int mid=(tr[m].l+tr[m].r)>>1;
if(r<=mid)
return query(m<<1,l,r);
if(l>mid)
return query(m<<1|1,l,r);
return query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
}
int main()
{
int n,ca=0;
while(scanf("%d",&n)!=EOF)
{
printf("Case #%d:\n",++ca);
build(1,1,n);
int m,x,y,z;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&z,&x,&y);
if(z)
{
if(x>y)
printf("%lld\n",query(1,y,x));
else
printf("%lld\n",query(1,x,y));
}
else
{
if(x>y)
update(1,y,x);
else
update(1,x,y);
}
}
printf("\n");
}
return 0;
}