Time limit 500 ms
Memory limit 1572864 kB
You are given a sequence A of N(N <= 100,000) positive integers. There sum will be less than 1018. On this sequence you have to apply M (M <= 100,000) operations:
(A) For given x,y, for each elements between the x-th and the y-th ones (inclusively, counting from 1), modify it to its positive square root (rounded down to the nearest integer).
(B) For given x,y, query the sum of all the elements between the x-th and the y-th ones (inclusively, counting from 1) in the sequence.
Input
Multiple test cases, please proceed them one by one. Input terminates by EOF.
For each test case:
The first line contains an integer N. The following line contains N integers, representing the sequence A1…AN.
The third line contains an integer M. The next M lines contain the operations in the form “i x y”.i=0 denotes the modify operation, i=1 denotes the query operation.
Output
For each test case:
Output the case number (counting from 1) in the first line of output. Then for each query, print an integer as the problem required.
Print an blank line after each test case.
See the sample output for more details.
题目分析
区间和不支持直接开方,但是我们注意到
1
0
18
10^{18}
1018内的数据至多开方6次即可得1
所以我们在维护区间和的同时再维护一个区间最大值
对于区间最大值为1的区间可以不用更新,剩下的单点修改
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;
lt read()
{
lt f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
const int maxn=100010;
int n,m,cs;
lt a[maxn],mx[maxn<<2],sum[maxn<<2];
void build(int s,int t,int p)
{
if(s==t){ mx[p]=sum[p]=a[s]; return;}
int mid=s+t>>1;
build(s,mid,p<<1); build(mid+1,t,p<<1|1);
mx[p]=max(mx[p<<1],mx[p<<1|1]);
sum[p]=sum[p<<1]+sum[p<<1|1];
}
lt qsum(int ll,int rr,int s,int t,int p)
{
if(ll<=s&&t<=rr) return sum[p];
lt mid=s+t>>1,ans=0;
if(ll<=mid) ans+=qsum(ll,rr,s,mid,p<<1);
if(rr>mid) ans+=qsum(ll,rr,mid+1,t,p<<1|1);
return ans;
}
void update(int ll,int rr,int s,int t,int p)
{
if(s==t){ a[s]=sqrt(a[s]); sum[p]=mx[p]=a[s]; return;}
int mid=s+t>>1;
if(ll<=mid&&mx[p<<1]>1) update(ll,rr,s,mid,p<<1);
if(rr>mid&&mx[p<<1|1]>1) update(ll,rr,mid+1,t,p<<1|1);
mx[p]=max(mx[p<<1],mx[p<<1|1]);
sum[p]=sum[p<<1]+sum[p<<1|1];
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;++i) a[i]=read();
build(1,n,1);
printf("Case #%d:\n",++cs);
m=read();
while(m--)
{
int k=read(),ll=read(),rr=read();
if(ll>rr) swap(ll,rr);
if(k==0) update(ll,rr,1,n,1);
else if(k==1) printf("%lld\n",qsum(ll,rr,1,n,1));
}
printf("\n");
}
return 0;
}