Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 8808 Accepted Submission(s): 2009
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.
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.
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
Source
一般的线段树区间更新为区间要么加上某个值,要么重新赋值为某个数,本题区间更新特点是 更新的值更不统一,并非统一加上或变成某个值,而是跟子叶结点有关。
因次如果按一般的思路想下去,更新到某区间时发现没法再标记下去,矛盾出现了。 本题解题哦思路巧妙处在于最大数2^63最多开6次根号就为1了,而且再开根号结果仍为1
当某个区间值都为1时,根据线段树特点,区间和sum正好等于区间长度 r - l + 1 ,因此每次更新时先判断该区间是否需要更新,是的话向下更新,直到子叶结点改变,剩下转化为区间去和问题。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
typedef long long LL;
#define mem0(a) memset(a,0,sizeof(a))
const int maxn = 100000+10;
struct node{
LL sum;
int l,r;
}a[maxn<<2];
void pushup(int cur){
a[cur].sum = a[cur<<1].sum+a[cur<<1|1].sum;
}
void build(int l,int r,int cur){
a[cur].l = l;
a[cur].r = r ;
if(l == r){
scanf("%I64d",&a[cur].sum);
return ;
}
int mid = (l + r)>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
pushup(cur);
}
void update(int l,int r,int cur){
if(a[cur].r - a[cur].l +1 == a[cur].sum)
return ;
if(a[cur].l == a[cur].r){//遍历到子叶节点时,更新值
a[cur].sum =(LL)sqrt((double)a[cur].sum);
return ;
}
int mid = (a[cur].l + a[cur].r )>>1;
if(r <= mid)
update(l,r,cur<<1);
else if(l > mid)
update(l,r,cur<<1|1);
else {
update(l,mid,cur<<1);
update(mid+1,r,cur<<1|1);
}
pushup(cur);
}
LL query(int l,int r,int cur){
if(l <= a[cur].l && r >= a[cur].r ){
return a[cur].sum;
}
int mid = (a[cur].l + a[cur].r )>>1;
if( r <= mid )
return query(l,r,cur<<1);
else if( l > mid)
return query(l,r,cur<<1|1);
else {
return query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1);
}
}
int main()
{
int n,t= 1;
while(scanf("%d",&n)!=EOF){
build(1,n,1);
int m;
scanf("%d",&m);
printf("Case #%d:\n",t++);
while(m--){
int T,x,y;
scanf("%d%d%d",&T,&x,&y);
if(x>y)
swap(x,y);
if(T==0){
update(x,y,1);
}
else if(T==1){
printf("%I64d\n",query(x,y,1));
}
}
printf("\n");
}
return 0;
}