Can you answer these queries?
Problem 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 Input10
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 OutputCase #1:
19
7
6
题意:
首先给出一个数字n代表,然后又n个数据。
再给出一个数字k,代表n次查询,查询分为两种:
0 x y:将 [x,y ]区间的每个数字开根号
1 x y:=求[x,y ]区间和,并输出
思路:
比较明显的线段树模板题。
有几个坑点:
WR:数据x ,y可能会出现,x大于y的情况
TLE:更新的时候要剪枝下,即:当下面要更新的子节点都是1的时候是不用更新的,应该根号1还是1。为什么这个剪枝会很快?因为在整范围内,开几次根号就变成1了,而查询次数比较多,完全不需要一直往下遍历。
#include
using namespace std;
const int M = 100005;
long long n,a[M<<2];
void init(int l,int r,int alt){
if(l==r)
{
long long k;
scanf("%lld",&k);
a[alt]=k;
return ;
}
int mid=(l+r)/2;
init(l,mid,alt<<1);
init(mid+1,r,(alt<<1)+1);
a[alt]=a[alt<<1]+a[(alt<<1)+1];
}
int update(int x,int y,int l,int r,int alt){
if(x<=l&&r<=y)//剪枝
if(a[alt]==r-l+1)
return 0;
if(l==r)
{
a[alt]=(long long)sqrt(a[alt]);
return 0;
}
int mid=(l+r)/2;
if(mid>=x)
update(x,y,l,mid,alt<<1);
if(mid
update(x,y,mid+1,r,(alt<<1)+1);
a[alt]=a[alt<<1]+a[(alt<<1)+1];
return 0;
}
long long query(int x,int y,int l,int r,int alt){
if(x<=l&&r<=y)
return a[alt];
int mid=(l+r)/2;
long long m1=0,m2=0;
if(mid>=x)
m1=query(x,y,l,mid,alt<<1);
if(mid
m2=query(x,y,mid+1,r,(alt<<1)+1);
return m1+m2;
}
int main(){
int k,num=0;
while(cin>>n)
{
init(1,n,1);//输出数据
scanf("%d",&k);
printf("Case #%d:\n",++num);
for(int i=0;i
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(x==0)
{
update(min(y,z),max(y,z),1,n,1);//更新
}
else
printf("%lld\n",query(min(y,z),max(y,z),1,n,1));//查询
}
printf("\n");
}
return 0;
}