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 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.
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
题意:
意思就是给你一个数组 然后有两个操作 操作0表示对区间内的每个数进行根号的操作 操作1是求区间内经过若干次根号操作后的总和
思路:
标准的区间求和以及单点更新问题 我们只需要对我们需要操作的区间的每个叶子结点进行修改 然后就是线段树的区间求和操作了 这道题并不难 但一遍却不容易 以下是这道题的三个坑点
- 在区间更新操作的时候必须考虑一种特殊情况 就是所在区间内的总和等于区间长度 意味着每个叶子结点都是1 也就是说根号操作是不需要的 这个时候跳出此层递归 注意 不这样写是超时的 这也是我常说的 在线段树操作中 请写上所有能想到的优化
- 在获得区间的操作中 给你的两个值不是严格的第一个大第二个小 这是很坑的 这里推荐一个简单的函数swap(a,b) 直接交换a与b的值 在头文件algorithm中
- 在每个样例后记得有一行空格 比赛里忘掉可又是20分钟的罚时
- 为什么写第四个呢 因为这道题可能没有这个坑 有些题就会出现 所以一并加上 希望大家能够注意 那就是cin与scanf并不一样 cin固然优秀 但scanf有一个显著的优点:速度远快于cin 在线段树中很多时候我们是把n2的复杂度转换为nlogn 在这个讨论时间性能的题目里 请用scanf防止不必要的意外
AC代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define lson (q<<1)
#define rson (q<<1|1)
#define mid ((l+r)>>1)
#define ll long long
const int maxn=100000+5;
ll book[maxn];
ll segt[maxn<<2];
int m,n,tmp;
int tmpa,tmpb;
int ans;
void push_up(int q)
{
segt[q]=segt[lson]+segt[rson];
}
void build_tree(int q,int l,int r)
{
if(l==r)
{
segt[q]=book[l];
return;
}
int m=mid;
build_tree(lson,l,m);
build_tree(rson,m+1,r);
push_up(q);
return;
}
void update(int q,int l,int r,int a,int b)
{
if(segt[q]==r-l+1)
return; /*这道题坑点之一
不对这种看起来的特殊情况进行考虑就会超时*/
if(l==r)
{
segt[q]=sqrt((double)segt[q]);
return;
}
int m=mid;
if(a<=m) update(lson,l,m,a,b);
if(b>m) update(rson,m+1,r,a,b);
push_up(q);
return;
}
ll query(int q,int l,int r,int a,int b)
{
ll ans=0;
if(l>=a && r<=b)
{
return segt[q];
}
int m=mid;
if(a<=m) ans+=query(lson,l,m,a,b);
if(b>m) ans+=query(rson,m+1,r,a,b);
return ans;
}
int main()
{
while(~scanf("%d",&m))
{
memset(segt,0,sizeof(segt));
for(int i=1;i<=m;i++)
scanf("%lld",&book[i]);
scanf("%d",&n);
printf("Case #%d:\n",++ans);
build_tree(1,1,m);
while(n--)
{
scanf("%d %d %d",&tmp,&tmpa,&tmpb);
if(tmpa>tmpb)
{
swap(tmpa,tmpb);//也是一个坑点
}
if(tmp)
{
printf("%lld\n",query(1,1,m,tmpa,tmpb));
}
else//千万不能忘了加上空行 比赛二十分钟就没了呀
{
update(1,1,m,tmpa,tmpb);
}
}
printf("\n");
}
return 0;
}