BZOJ 3211: 花神游历各国(势能分析线段树)
2018-03-02 22:07
471 查看
题目在这里
http://www.lydsy.com/JudgeOnline/problem.php?id=3211题解
这就是道傻逼题,但为了能完成我日更两篇的梦想,我就水一篇博客。。对一段区间每个数开根号用线段树是打不了标记的,但是我们发现,一个数开根号很快就会变成1(或0),10^9开个5次基本上就稳定了。所以我们直接暴力下放标记到叶子,对于那些区间最大值<=1的,直接跳过即可。
对于线段树不能维护的标记,这种套路很常用的。它为什么叫势能分析线段树呢?我也不知道。(可能跟这个算法看上去好骗访问量有关吧)
代码
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <cmath> #define maxn 100010 #define mid ((L + R) >> 1) #define Lson (root << 1) #define Rson (root << 1 | 1) using namespace std; typedef long long LL; int n, m; struct Tnode{ LL Max, sum; } Tree[maxn<<2]; LL data[maxn]; void Build(int root, int L, int R){ if(L == R){ Tree[root].sum = Tree[root].Max = data[L]; return; } Build(Lson, L, mid); Build(Rson, mid+1, R); Tree[root].sum = Tree[Lson].sum + Tree[Rson].sum; Tree[root].Max = max(Tree[Lson].Max, Tree[Rson].Max); } void Update(int root, int L, int R, int x, int y){ if(x > R || y < L) return; if(x <= L && y >= R){ if(Tree[root].Max <= 1) return; if(L == R){ Tree[root].sum = (LL)sqrt(Tree[root].sum); Tree[root].Max = Tree[root].sum; return; } } Update(Lson, L, mid, x, y); Update(Rson, mid+1, R, x, y); Tree[root].Max = max(Tree[Lson].Max, Tree[Rson].Max); Tree[root].sum = Tree[Lson].sum + Tree[Rson].sum; } LL Query(int root, int L, int R, int x, int y){ if(x > R || y < L) return 0LL; if(x <= L && y >= R) return Tree[root].sum; LL temp1 = Query(Lson, L, mid, x, y); LL temp2 = Query(Rson, mid+1, R, x, y); return temp1 + temp2; } int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%lld", &data[i]); Build(1, 1, n); scanf("%d", &m); int op, l, r; for(int i = 1; i <= m; i++){ scanf("%d%d%d", &op, &l, &r); if(op == 1) printf("%lld\n", Query(1, 1, n, l, r)); else Update(1, 1, n, l, r); } return 0; }
相关文章推荐
- 【线段树】【bzoj 3211】花神游历各国
- BZOJ 3211 花神游历各国 - 线段树
- bzoj3211花神游历各国 线段树
- [BZOJ3211]花神游历各国(线段树)
- bzoj3211 花神游历各国(线段树)
- 树线段BZOJ 3211(花神游历各国-线段树区间开方)
- bzoj3211 花神游历各国 线段树
- BZOJ 3211: 花神游历各国( 线段树 )
- 花神游历各国 bzoj3211 线段树
- BZOJ 3211 花神游历各国 树状数组(线段树)+优化
- BZOJ 3211-花神游历各国(线段树)
- BZOJ 题目3211: 花神游历各国(线段树区间减为平方根,区间求和)
- BZOJ 3211: 花神游历各国 线段树区间开根
- 【BZOJ】3211 花神游历各国 【CODE[VS]】2492 上帝造题的七分钟2 线段树
- 【线段树/区间开平方】BZOJ3211-花神游历各国
- bzoj3211花神游历各国 线段树
- [BZOJ3211][SPOJ2713][线段树]GSS 4(花神游历各国)[一般题]
- [BZOJ3211]花神游历各国(线段树+区间开根)
- BZOJ 3211(花神游历各国-线段树区间开方)
- 【BZOJ 3211】花神游历各国 &【 线段树区间开根】