Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 14079 Accepted Submission(s): 3269
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 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
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
题意大概就是有很多战舰,给你他们的初始生命值,然后有两个操作,首先是攻击,每次攻击给相应区间里的所有数逐个开根号。第二个操作就是查询,输出一个区间里所有战舰当前的生命值的和。
首先2^63最多开八次方就会变成1。所以如果有一段已经是8次了就没必要更新他了。然后就是区间更新。好像看有人写单点更新据说也能过?我不是很懂。。总之节点里记录被攻击的次数,同时如果一个点的左右子节点的次数不一样还要做个标记。cnt一下输出答案即可。
#include "cstring"
#include "cstdio"
#include "iostream"
#include "string.h"
#include "cmath"
using namespace std;
#define MAX 100005
long long num[MAX];
struct node
{
int left,right,cnt;
}tree[MAX*4];
long long sum=0;
void build(int root,int lf,int rt)
{
tree[root].cnt=0;
tree[root].left=lf;
tree[root].right=rt;
if(lf==rt)
return;
int mid=(lf+rt)/2;
build(root*2,lf,mid);
build(root*2+1,mid+1,rt);
}
void push_down(int rt)
{
tree[rt*2+1].cnt=tree[rt*2].cnt=tree[rt].cnt;
}
void kill(int root,int lf,int rt)
{
if(tree[root].cnt!=-1&&(tree[root].left!=tree[root].right))
{
push_down(root);
}
if(tree[root].cnt>=8)
return;
if(tree[root].left>=lf&&tree[root].right<=rt&&tree[root].cnt!=-1)
{
tree[root].cnt++;
return;
}
if(tree[root].left==tree[root].right)
{
tree[root].cnt++;
return;
}
int mid=(tree[root].left+tree[root].right)/2;
if(rt<=mid)
{
kill(root*2,lf,rt);
}
else if(lf>=(mid+1))
{
kill(root*2+1,lf,rt);
}
else
{
kill(root*2,lf,rt);
kill(root*2+1,lf,rt);
}
if((tree[root*2].cnt!=tree[root*2+1].cnt)||(tree[root*2].cnt==-1)||tree[root*2+1].cnt==-1)
tree[root].cnt=-1;
else if(tree[root*2].cnt>=8&&tree[root*2+1].cnt>=8)
tree[root].cnt=8;
else
tree[root].cnt=tree[root*2].cnt;
}
long long get_sum(int left,int right,int cnt)
{
long long temp=0;
for(int i=left;i<=right;i++)
{
long long now=num[i];
for(int j=1;j<=cnt;j++)
{
now=sqrt(now);
}
temp+=now;
}
return temp;
}
void cnt(int root,int lf,int rt)
{
if(tree[root].left!=tree[root].right&&tree[root].cnt!=-1)
push_down(root);
if(tree[root].left>=lf&&tree[root].right<=rt)
{
if(tree[root].cnt>=8)
{
sum+=(tree[root].right-tree[root].left+1);
return;
}
else
{
if(tree[root].cnt!=-1)
{
sum+=get_sum(tree[root].left,tree[root].right,tree[root].cnt);
return;
}
}
}
if(tree[root].left==tree[root].right)
return;
int mid=(tree[root].left+tree[root].right)/2;
if(rt<=mid)
{
cnt(root*2,lf,rt);
}
else if(lf>=(mid+1))
{
cnt(root*2+1,lf,rt);
}
else
{
cnt(root*2,lf,rt);
cnt(root*2+1,lf,rt);
}
}
int main()
{
int n;
int t=0;
while(~scanf("%d",&n))
{
printf("Case #%d:\n",++t);
for(int i=1;i<=n;i++)
{
scanf("%lld",&num[i]);
}
build(1,1,n);
int m;
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if(op==0)
{
if(r>=l)
kill(1,l,r);
else
kill(1,r,l);
}
else
{
sum=0;
if(r>=l)
cnt(1,l,r);
else
cnt(1,r,l);
printf("%lld\n",sum);
}
}
printf("\n");
}
}
/*
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
0 5 8
1 4 8
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 5
*/