您的位置:首页 > 其它

[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的点,乱搞一下就可以了。。

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: