bzoj 3211 分类: bzoj 2015...
2015-08-07 18:00
204 查看
原文链接:https://www.geek-share.com/detail/2649941601.html
分析一下,每个数最多只会做5次开根操作就会小于或等于1,在这之后它的值就不会改变。。。
用线段树记录区间的最大值,然后只对max > 1 的区间做开根操作即可。
时间复杂度:O(M∗logN+N)
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> #include <utility> #include <stack> #include <queue> #include <iostream> #include <algorithm> template<class Num>void read(Num &x) { char c; int flag = 1; while((c = getchar()) < '0' || c > '9') if(c == '-') flag *= -1; x = c - '0'; while((c = getchar()) >= '0' && c <= '9') x = (x<<3) + (x<<1) + (c-'0'); x *= flag; return; } template<class Num>void write(Num x) { if(x < 0) putchar('-'), x = -x; static char s[20];int sl = 0; while(x) s[sl++] = x%10 + '0',x /= 10; if(!sl) {putchar('0');return;} while(sl) putchar(s[--sl]); } const int maxn = 1e5 + 5; int n , m, a[maxn]; int max[maxn<<2]; long long sum[maxn<<2]; #define L(x) (x<<1) #define R(x) ((x<<1)|1) long long query(int l,int r,int ll,int rr,int si) { if(l == ll && r == rr) return sum[si]; else { int mid = (ll+rr)>>1; if(r <= mid) return query(l,r,ll,mid,L(si)); else if(l > mid) return query(l,r,mid+1,rr,R(si)); else return query(l, mid,ll,mid,L(si)) + query(mid+1,r,mid+1,rr,R(si)); } } void update(int x) { sum[x] = sum[L(x)] + sum[R(x)]; max[x] = std::max(max[L(x)], max[R(x)]); } void build(int ll,int rr,int si) { if(ll == rr) sum[si] = max[si] = a[ll]; else { int mid = (ll+rr)>>1; build(ll,mid,L(si)); build(mid+1,rr,R(si)); update(si); } } void change(int l,int r,int ll,int rr,int si) { if(max[si] <= 1)return; if(ll == rr) { max[si] = sqrt(max[si]); sum[si] = max[si]; } else { int mid = (ll+rr)>>1; if(r <= mid) change(l,r,ll,mid,L(si)); else if(l > mid) change(l,r,mid+1,rr,R(si)); else { change(l,mid,ll,mid,L(si)); change(mid+1,r,mid+1,rr,R(si)); } update(si); } } #undef L #undef R int main() { #ifndef ONLINE_JUDGE freopen("3211.in","r",stdin); freopen("3211.out","w",stdout); #endif read(n); for(int i = 1; i <= n; i++) read(a[i]); build(1, n, 1); read(m); while(m--) { int op, l, r; read(op),read(l),read(r); switch(op) { case 1: write(query(l, r, 1, n, 1)), puts(""); break; case 2: change(l, r, 1, n, 1); break; } } #ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout); #endif return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
转载于:https://www.cnblogs.com/dashgua/p/4722960.html
相关文章推荐
- bzoj 3211 分类: bzoj 2015-08-07 18:00 8人阅读 评论(0) 收藏
- 【BZOJ 1084】【SCOI 2005】最大子矩阵【DP & 分类讨论】
- 【bzoj3211】花神游历各国
- 树线段BZOJ 3211(花神游历各国-线段树区间开方)
- 【BZOJ3211】花神游历各国 树状数组 并查集 均摊分析
- BZOJ 3211: 花神游历各国( 线段树 )
- bzoj3211 花神游历各国
- 【bzoj3211】花神游历各国
- bzoj2118 分类: bzoj 2015-08-07 17:15 78人阅读 评论(0) 收藏
- BZOJ 1099([POI2007]树Drz-9次线段树&分类讨论+线段树与插入顺序维护2个参数)
- BZOJ3038 && BZOJ3211 上帝造题的七分钟2 && 花神游历各国 (线段树 + 开方标记)
- bzoj3211 花神游历各国
- bzoj 4011 分类: bzoj 2015-05-28 14:11 33人阅读 评论(0) 收藏
- BZOJ-4976 (分类+dp)
- [BZOJ3211]花神游历各国
- 【BZOJ3211】【并查集+树状数组】花神游历各国
- BZOJ 3211 弗洛拉前往国家 树阵+并检查集合
- [bzoj2118] 墨墨的等式 模数分类 最短路
- bzoj题目大体分类
- 【BZOJ3211】花神游历各国(线段树)