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.
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<iostream>
#include<cstdio>
#include<map>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define getmid int mid=(l+r)>>1
typedef long long sint;
#define maxn 4000010
sint sum[maxn];
void pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
getmid;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
build(lson);
build(rson);
pushup(rt);
}
void update(int l,int r,int rt,int x,int y)
{
if(r-l+1==sum[rt]) return;
if(l==r)
{
sum[rt]=sint(sqrt(sum[rt]));
return;
}
getmid;
if(x<=mid) update(lson,x,y);
if(y>mid) update(rson,x,y);
pushup(rt);
}
sint query(int l,int r,int rt,int x,int y)
{
if(x<=l&&y>=r)
{
return sum[rt];
}
getmid;
sint tmp=0;
if(x<=mid) tmp+=query(lson,x,y);
if(y>mid) tmp+=query(rson,x,y);
return tmp;
}
int main()
{
int cas=0,n,m,q;
while(scanf("%d",&n)!=EOF)
{
build(1,n,1);
scanf("%d",&q);
printf("Case #%d:\n",++cas);
while(q--)
{
int k,l,r;
scanf("%d%d%d",&k,&l,&r);
if(l>r) swap(l,r);
if(k==0) update(1,n,1,l,r);
else printf("%lld\n",query(1,n,1,l,r));
}
printf("\n");
}
return 0;
}

本文介绍了一种处理大规模整数序列的操作与查询问题的方法,包括取平方根修改及区间求和查询,通过构建特殊的数据结构实现高效更新与查询。
1207

被折叠的 条评论
为什么被折叠?



