spoj GSS4 - Can you answer these queries IV
2017-07-16 18:09
1261 查看
GSS4 - Can you answer these queries IV
http://www.spoj.com/problems/GSS4/#tree
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: 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
Submit solution!
题意:
区间开根
区间求和
线段树
x<=10^18,最多开7、8次就变成1,所以直接单点该
当一个区间全是1的时候直接return
总复杂度:8nlogn
#include<cmath> #include<cstdio> #include<algorithm> #define N 100001 using namespace std; int n,m,opl,opr,type; long long sum[N*4]; long long ans; bool v ; 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
- SPOJ 2713. Can you answer these queries IV(GSS4 线段树)
- [SPOJ GSS4] Can you answer these queries IV [树状数组+并查集][线段树+双向链表]
- SPOJ 2713. Can you answer these queries IV(GSS4 线段树)
- SPOJ GSS3 Can you answer these queries III ——线段树
- bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树
- 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III
- SPOJ 4487. Can you answer these queries VI(GSS6) splay
- SPOJ GSS1 Can you answer these queries I
- BZOJ 2482 || SPOJ GSS2 Can you answer these queries II(线段树 离线 后缀和)
- SPOJ 2916 Can you answer these queries V(GSS5 线段树)
- SPOJ GSS 1. Can you answer these queries I
- SPOJ GSS6 Can you answer these queries VI
- SPOJ GSS1 Can you answer these queries I (线段树求区间最大连续和)
- spoj 2713. Can you answer these queries IV(线段树)
- Spoj 2713 Can you answer these queries IV 水线段树
- SPOJ GSS 5. Can you answer these queries V
- spoj 1557 Can you answer these queries II (gss2)线段树
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
- SPOJ GSS1 Can you answer these queries I