3211: 花神游历各国
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 5051 Solved: 1870
Description
Input
Output
每次x=1时,每行一个整数,表示这次旅行的开心度
Sample Input
4
1 100 5 5
5
1 1 2
2 1 2
1 1 2
2 2 3
1 1 4
Sample Output
101
11
11
HINT
对于100%的数据, n ≤ 100000,m≤200000 ,data[i]非负且小于10^9
解析:
线段树区间开方区间求和。
重点讲一下区间开方,我们记录每个区间的最大值,如果最大值为 1 或 0 说明改区间不需要再进行修改直接跳过,因为1 和 0 开方后还是本身,否则暴力进行修改。一个数开方到 1 的次数为 log log n 次,是非常小的,所以不用担心超时。
代码:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int Max=100005;
int n,m,ans;
int num[Max],maxx[Max<<2];
LL sum[Max<<2];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') {f=-1;c=getchar();}
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline int mx(int x,int y){return x > y ? x : y;}
inline void update(int root)
{
sum[root] = sum[root<<1] + sum[root<<1|1];
maxx[root] = mx(maxx[root<<1] , maxx[root<<1|1]);
}
inline void build(int root,int l,int r)
{
if(l==r) {sum[root] = maxx[root] = num[l];return;}
int mid = l + r >> 1;
build(root<<1,l,mid),build(root<<1|1,mid+1,r);
update(root);
}
inline void change(int root,int l,int r,int L,int R)
{
if(l==r){maxx[root] = sum[root] = (int)floor(sqrt(maxx[root]));return;}
int mid = l + r >> 1;
if(L <= mid && maxx[root<<1] > 1) change(root<<1,l,mid,L,R);
if(R > mid && maxx[root<<1|1] > 1) change(root<<1|1,mid+1,r,L,R);
update(root);
}
inline LL Q(int root,int l,int r,int L,int R)
{
if(L <= l && R >= r) return sum[root];
int mid = l + r >> 1;
LL ans=0;
if(L <= mid) ans += Q(root<<1,l,mid,L,R);
if(R > mid) ans += Q(root<<1|1,mid+1,r,L,R);
return ans;
}
inline void print(LL x)
{
if(x > 9) print(x/10);
putchar('0' + x % 10);
}
int main()
{
n=get_int();
for(int i=1;i<=n;i++) num[i] = get_int();
build(1,1,n);
m=get_int();
for(int i=1;i<=m;i++)
{
int flag = get_int(),x=get_int(),y=get_int();
if(flag == 2) change(1,1,n,x,y);
else print(Q(1,1,n,x,y)),putchar('\n');
}
return 0;
}