首页 > 代码库 > spoj GSS4 - Can you answer these queries IV
spoj GSS4 - Can you answer these queries IV
GSS4 - Can you answer these queries IV
http://www.spoj.com/problems/GSS4/
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.
Example
Input:51 2 3 4 551 2 40 2 41 2 40 4 51 1 5410 10 10 1031 1 40 2 31 1 4Output:Case #1:946Case #2:4026
#include<cmath>#include<cstdio>#include<algorithm>#define N 100001using namespace std;int n,m,opl,opr,type;long long sum[N*4];long long ans;bool v[N];void read(long long &x){ x=0; int f=1; char c=getchar(); while(c<‘0‘|| c>‘9‘) { if(c==‘-‘) f=-1; c=getchar(); } while(c>=‘0‘ && c<=‘9‘) { x=x*10+c-‘0‘; c=getchar(); } x*=f;}void build(int k,int l,int r){ sum[k]=0; if(l==r) {read(sum[k]); return; } int mid=l+r>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); sum[k]=sum[k<<1]+sum[k<<1|1];}void change(int k,int l,int r){ if(sum[k]==r-l+1) return; if(l==r) { sum[k]=sqrt(sum[k]); return; } int mid=l+r>>1; if(opl<=mid) change(k<<1,l,mid); if(opr>mid) change(k<<1|1,mid+1,r); sum[k]=sum[k<<1]+sum[k<<1|1];}void query_sum(int k,int l,int r){ if(l>=opl && r<=opr) { ans+=sum[k]; return; } int mid=l+r>>1; if(opl<=mid) query_sum(k<<1,l,mid); if(opr>mid) query_sum(k<<1|1,mid+1,r);}void out(long long x){ if(x/10) out(x/10); putchar(x%10+‘0‘);}int main(){ int cas=0; while(scanf("%d",&n)!=EOF) { printf("Case #%d:\n",++cas); build(1,1,n); scanf("%d",&m); while(m--) { scanf("%d%d%d",&type,&opl,&opr); if(opl>opr) swap(opl,opr); if(!type) change(1,1,n); else { ans=0; query_sum(1,1,n); out(ans); puts(""); } } }}
spoj GSS4 - Can you answer these queries IV