Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 14605 Accepted Submission(s): 3415
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
Recommend
lcy
题意:给你N个数,有M个操作,操作有两类,(1)"0 l r",表示将区间[l,r]里的每个数都开根号。(2)"1 l r",表示查询区间[l,r]里所有数的和。
解题思路:虽然数据范围达到了2^63次方,但最多开根号7次就会变成1。所以在每次更新的时候,如果这个区间里的所有数全都为1,就不用再往下更新了,所以在更新的时可以一直更新到叶子结点。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const int MAXN=100005;
LL a[MAXN],sum[MAXN<<2];
int flag[MAXN<<2];
void build(int k,int l,int r)
{
if(l==r) {sum[k]=a[l];return ;}
int mid=(l+r)/2;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
sum[k]=sum[k<<1]+sum[k<<1|1];
}
void update(int k,int l,int r,int ll,int rr)
{
if(l==r)
{
sum[k]=sqrt(sum[k]);
if(sum[k]==1) flag[k]=1;
return ;
}
int mid=(l+r)>>1;
if(ll<=mid&&!flag[k<<1]) update(k<<1,l,mid,ll,rr);
if(rr>mid&&!flag[k<<1|1]) update(k<<1|1,mid+1,r,ll,rr);
sum[k]=sum[k<<1]+sum[k<<1|1];
if(flag[k<<1]&&flag[k<<1|1]) flag[k]=1;
}
LL query(int k,int l,int r,int ll,int rr)
{
if(ll<=l&&r<=rr) return sum[k];
if(r<ll||l>rr) return 0;
int mid=(l+r)>>1;
LL ans=0;
if(mid>=ll) ans+=query(k<<1,l,mid,ll,rr);
if(rr>mid) ans+=query(k<<1|1,mid+1,r,ll,rr);
return ans;
}
int main()
{
int n,m,cas=0;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
memset(flag,0,sizeof flag);
build(1,1,n);
int k,l,r;
printf("Case #%d:\n",++cas);
scanf("%d",&m);
while(m--)
{
scanf("%d %d %d",&k,&l,&r);
if(r<l) swap(l,r);
if(k==0) update(1,1,n,l,r);
else printf("%lld\n",query(1,1,n,l,r));
}
printf("\n");
}
return 0;
}