SPOJ 2713 Can you answer these queries IV(线段树)
来源:互联网 发布:qq象棋作弊软件 编辑:程序博客网 时间:2024/06/05 16:34
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<bits/stdc++.h>#define LL long long#define N 201000using namespace std;LL a[N];int n;struct ST{struct node{LL sum;int l,r;} tree[N*4];inline void push_up(int i){ tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;}inline void build(int i,int l,int r){tree[i].r=r;tree[i].l=l;if (l==r){tree[i].sum=a[l];return;}LL mid=(l+r)>>1;build(i<<1,l,mid);build(i<<1|1,mid+1,r);push_up(i);} inline LL getsum(int i,int l,int r){if ((tree[i].l==l)&&(tree[i].r==r)) return tree[i].sum;LL mid=(tree[i].l+tree[i].r)>>1;if (mid>=r) return getsum(i<<1,l,r);else if (mid<l) return getsum(i<<1|1,l,r);else return getsum(i<<1,l,mid)+getsum(i<<1|1,mid+1,r);}inline void update(int i,int l,int r){ if (tree[i].r-tree[i].l+1==tree[i].sum) return;//区间长等于区间和,说明全部为1,没必要再开根号 if (tree[i].r==tree[i].l)//否则直接暴力到叶子节点再修改 { tree[i].sum=(int)sqrt(tree[i].sum); return; } LL mid=(tree[i].l+tree[i].r)>>1;if (mid>=r) update(i<<1,l,r);else if (mid<l) update(i<<1|1,l,r);else{ update(i<<1,l,mid); update(i<<1|1,mid+1,r);} push_up(i);}} seg;int main(){ int T_T=0; while(~scanf("%d",&n)) { printf("Case #%d:\n",++T_T); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); seg.build(1,1,n); int q; scanf("%d",&q); while(q--) { int op,l,r; scanf("%d%d%d",&op,&l,&r); if (l>r) swap(l,r); if (op==1) printf("%lld\n",seg.getsum(1,l,r)); else seg.update(1,l,r); } puts(""); } return 0;}
- 【线段树】 SPOJ 2713 Can you answer these queries IV
- spoj 2713. Can you answer these queries IV(线段树)
- SPOJ GSS4 Can you answer these queries IV (线段树)
- 【SPOJ】Can you answer these queries IV【线段树】
- spoj GSS4 - Can you answer these queries IV 线段树
- SPOJ 2713 Can you answer these queries IV(线段树)
- SPOJ 2713. Can you answer these queries IV(GSS4 线段树)
- SPOJ 2713. Can you answer these queries IV(GSS4 线段树)
- spoj 2713 Can you answer these queries IV
- Can you answer these queries IV(线段树区间和)
- SPOJ/GSS3:Can you answer these queries III(线段树)
- spoj 1043. Can you answer these queries I (线段树)
- spoj 1557. Can you answer these queries II(线段树)
- spoj 1716. Can you answer these queries III(线段树)
- spoj 2916. Can you answer these queries V(线段树)
- SPOJ GSS3 Can you answer these queries III (线段树)
- 【SPOJ】Can you answer these queries I【线段树】
- 【SPOJ】Can you answer these queries III【线段树】
- Visual Studio Code配置PHP开发环境
- 解决Error:null value in entry: incrementalFolder=null
- Source Insight 4.0 注册版
- JAVA之对象初始化过程详解及面试题
- Hibernate学习第二天
- SPOJ 2713 Can you answer these queries IV(线段树)
- Maven Dependencies 不见了
- LightOJ 1038 Race to 1 Again [概率dp]
- 调用高德的接口,地理位置和经纬度相互转换
- OMRON E6B2-CWZ6C
- RGB
- 网站打开速度慢的原因,排查方法及优化方法(大全)
- java eclipse 的安装流程
- 解決thinkPHP出現no input file specified.問題