Description
XLk觉得《上帝造题的七分钟》不太过瘾,于是有了第二部。
"第一分钟,X说,要有数列,于是便给定了一个正整数数列。
第二分钟,L说,要能修改,于是便有了对一段数中每个数都开平方(下取整)的操作。
第三分钟,k说,要能查询,于是便有了求一段数的和的操作。
第四分钟,彩虹喵说,要是noip难度,于是便有了数据范围。
第五分钟,诗人说,要有韵律,于是便有了时间限制和内存限制。
第六分钟,和雪说,要省点事,于是便有了保证运算过程中及最终结果均不超过64位有符号整数类型的表示范围的限制。
第七分钟,这道题终于造完了,然而,造题的神牛们再也不想写这道题的程序了。"
——《上帝造题的七分钟·第二部》
所以这个神圣的任务就交给你了。
Input
第一行一个整数n,代表数列中数的个数。
第二行n个正整数,表示初始状态下数列中的数。
第三行一个整数m,表示有m次操作。
接下来m行每行三个整数k,l,r,k=0表示给[l,r]中的每个数开平方(下取整),k=1表示询问[l,r]中各个数的和。
Output
对于询问操作,每行输出一个回答。
Sample Input
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
Sample Output
19
7
6
7
6
HINT
1:对于100%的数据,1<=n<=100000,1<=l<=r<=n,数列中的数大于0,且不超过1e12。
2:数据不保证L<=R 若L>R,请自行交换L,R,谢谢!
Source
同bzoj3211
题解见:http://blog.csdn.net/lzr010506/article/details/51001651
其实把那道题的代码的case:2改成case:0即可
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#define N 100010
#define ll long long
#define lson l, mid, lx
#define rson mid + 1, r, rx
using namespace std;
struct Tree
{
int l, r;
ll sum;
bool flag;
}t[N * 4];
int n, m;
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void build(int l, int r, int rt)
{
t[rt].l = l;
t[rt].r = r;
int lx = rt << 1, rx = rt << 1 | 1;
if(l == r)
{
t[rt].sum = read();
t[rt].l = t[rt].r = t[rt].sum;
if(t[rt].sum == 0 || t[rt].sum == 1)
t[rt].flag = 1;
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
t[rt].sum = t[lx].sum + t[rx].sum;
t[rt].flag = t[lx].flag & t[rx].flag;
}
ll query(int l, int r, int rt, int x, int y)
{
if(l == x && r == y) return t[rt].sum;
int mid = (l + r) >> 1;
int lx = rt << 1, rx = rt << 1 | 1;
if(mid < x) return query(rson, x, y);
else if(mid >= y) return query(lson, x, y);
else return query(lson, x, mid) + query(rson, mid + 1, y);
}
void update(int l, int r, int rt, int x, int y)
{
if(t[rt].flag == 1) return;
if(l == r)
{
t[rt].sum = sqrt(t[rt].sum);
if(t[rt].sum == 0 || t[rt].sum == 1)
t[rt].flag = 1;
return;
}
int lx = rt << 1, rx = rt << 1 | 1;
int mid = (l + r) >> 1;
if(mid >= y) update(lson, x, y);
else if(mid < x) update(rson, x, y);
else update(lson, x, mid), update(rson, mid + 1, y);
t[rt].sum = t[lx].sum + t[rx].sum;
t[rt].flag = t[lx].flag & t[rx].flag;
}
int main()
{
n = read();
build(1, n, 1);
m = read();
for(int i = 1; i <= m; i ++)
{
int f, x, y;
f = read();
x = read();
y = read();
if(x > y) swap(x, y);
switch(f)
{
case 1:printf("%lld\n", query(1, n, 1, x, y));break;
case 0:update(1, n, 1, x, y);break;
}
}
return 0;
}