[bzoj3211][并查集][树状数组]花神游历各国
2018-01-20 14:19
316 查看
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了,那么这个数是不需要再次开根的。
区间求和树状数组,单点修改树状数组
用一个并查集记录一下第i个点下一个不为1的点,乱搞一下就可以了。。
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了,那么这个数是不需要再次开根的。
区间求和树状数组,单点修改树状数组
用一个并查集记录一下第i个点下一个不为1的点,乱搞一下就可以了。。
hljs cpp">#include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; inline LL read() { LL f=1,x=0;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; } LL s[210000],n,m,a[210000]; int lowbit(int x){return x&-x;} void change(int x,LL k) { while(x<=n) { s[x]+=k; x+=lowbit(x); } } LL sol(int x) { LL ret=0; while(x) { ret+=s[x]; x-=lowbit(x); } return ret; } int fa[110000]; int findfa(int x) { if(fa[x]!=x)fa[x]=findfa(fa[x]); return fa[x]; } int main() { n=read(); for(int i=1;i<=n;i++) { a[i]=read();change(i,a[i]);fa[i]=i; } fa[n+1]=n+1; m=read(); while(m--) { int op=read(),u=read(),v=read(); if(op==1)printf("%lld\n",sol(v)-sol(u-1)); else { for(int i=findfa(u);i<=v;i=findfa(i+1)) { LL tmp=sqrt(a[i]); change(i,tmp-a[i]); a[i]=tmp; if(a[i]<=1)fa[i]=findfa(fa[i+1]); } } } return 0; }
相关文章推荐
- BZOJ 3211: 花神游历各国 |树状数组|并查集
- [BZOJ3211]花神游历各国-树状数组-并查集
- BZOJ3211 花神游历各国 【树状数组 + 并查集】
- BZOJ3211 花神游历各国 【树状数组 + 并查集】
- 【BZOJ3211】花神游历各国 树状数组 并查集 均摊分析
- |BZOJ 3211|树状数组|并查集|花神游历各国
- 【BZOJ3211】【并查集+树状数组】花神游历各国
- 【bzoj3211】【花神游历各国】【线段树+并查集】
- BZOJ-3211花神游历各国 并查集+树状数组
- 【线段树/区间开平方】BZOJ3211-花神游历各国
- BZOJ 3211 花神游历各国
- BZOJ 3211 花神游历各国 树状数组+并查集
- bzoj3211 花神游历各国(线段树)
- BZOJ3038 && BZOJ3211 上帝造题的七分钟2 && 花神游历各国 (线段树 + 开方标记)
- BZOJ-3211花神游历各国 并查集+树状数组
- codevs2492 上帝造题的七分钟2 BZOJ3211 花神游历各国
- bzoj3211 花神游历各国
- BZOJ 3211 花神游历各国 线段树
- bzoj3211 花神游历各国
- 【bzoj3211】花神游历各国 并查集+树状数组