2713. Can you answer these queries IVProblem code: 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.
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.
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.
Input: 5 1 2 3 4 5 5 1 2 4 0 2 4 1 2 4 0 4 5 1 1 5 4 10 10 10 10 3 1 1 4 0 2 3 1 1 4 Output: Case #1: 9 4 6 Case #2: 40 26
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #include<cmath> #include<cctype> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define ForD(i,n) for(int i=n;i;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define RepD(i,n) for(int i=n;i>=0;i--) #define MAXN (300000+10) #define MAXM (600000+10) #define Lson (x<<1) #define Rson ((x<<1)+1) long long n,m,a[MAXN*2]={0},b[MAXN*2]={0}; long long sum[MAXN*2]; void update(int x) { if (b[Lson]==2||b[Rson]==2) b[x]=2; else b[x]=1; sum[x]=sum[Lson]+sum[Rson]; } void build(int x,int l,int r) { if (l==r) { scanf("%lld",&a[x]);sum[x]=a[x]; if (a[x]<=1) b[x]=1; else b[x]=2; } if (l>=r) return; int m=(l+r)>>1; build(Lson,l,m); build(Rson,m+1,r); update(x); } void pushdown(int x,int l,int r) { if (b[x]<=1) return; if (l==r) { a[x]=sum[x]=sqrt(a[x]);//floor(sqrt((double)a[x])+0.5); if (a[x]<=1) b[x]=1; return; } int m=(l+r)>>1; pushdown(Lson,l,m); if (m<r) pushdown(Rson,m+1,r); update(x); } void change(int x,int l,int r,int L,int R) { if (b[x]<=1) return; int m=(l+r)>>1; if (L<=l&&r<=R) { pushdown(x,l,r); return; } if (L<=m) change(Lson,l,m,L,R); if (m<R) change(Rson,m+1,r,L,R); update(x); } long long qur(int x,int l,int r,int L,int R) { int m=(l+r)>>1; if (L<=l&&r<=R) { return sum[x]; } long long ans=0; if (L<=m) ans+=qur(Lson,l,m,L,R); if (m<R) ans+=qur(Rson,m+1,r,L,R); return ans; } int main() { // freopen("spoj2713.in","r",stdin); // freopen(".out","w",stdout); int tt=1; while (scanf("%d",&n)!=-1) { // memset(a,0,sizeof(a)); // memset(b,0,sizeof(b)); // memset(sum,0,sizeof(sum)); printf("Case #%d:\n",tt++); build(1,1,n); scanf("%d",&m); For(i,m) { int x,l,r; scanf("%d%d%d",&x,&l,&r); if (l>r) swap(l,r); if (x==1) printf("%lld\n",qur(1,1,n,l,r)); else change(1,1,n,l,r); } puts(""); } return 0; }