# BZOJ 3211 弗洛拉前往国家 树阵+并检查集合

1.将[l.r]每个号码间隔a[i]变sqrt(a[i])

2.查询[l,r]间隔和

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 100100
using namespace std;
typedef long long ll;
int n,m,a[M],fa[M];
ll c[M];
inline int getc() {
static const int L = 1 << 15;
static char buf[L], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, L, stdin);
if (S == T)
return EOF;
}
return *S++;
}
inline int getint() {
int c;
while(!isdigit(c = getc()) && c != '-');
bool sign = c == '-';
int tmp = sign ? 0 : c - '0';
while(isdigit(c = getc()))
tmp = (tmp << 1) + (tmp << 3) + c - '0';
return sign ? -tmp : tmp;
}
int Find(int x)
{
if(!fa[x]||fa[x]==x)
return fa[x]=x;
return fa[x]=Find(fa[x]);
}
void Update(int x,int y)
{
for(;x<=n;x+=x&-x)
c[x]+=y;
}
ll Get_Ans(int x)
{
ll re=0;
for(;x;x-=x&-x)
re+=c[x];
return re;
}
void Modify(int x,int y)
{
int i=x;
for( i=x ; i<=y ; i=Find(i+1) )
{
int temp=(int)sqrt(a[i]);
Update(i,temp-a[i]);
a[i]=temp;
if(a[i]<=1)
fa[i]=Find(i+1);
}
}
int main()
{
int i,p,x,y;
cin>>n;
for(i=1;i<=n;i++)
{
a[i]=getint();
if(a[i]==1||!a[i])
fa[i]=i+1;
Update(i,a[i]);
}
m=getint();
for(i=1;i<=m;i++)
{
p=getint();
x=getint();
y=getint();
if(p==1)
printf("%lld\n", Get_Ans(y)-Get_Ans(x-1) );
else
Modify(x,y);
}
}


posted @ 2015-06-13 09:41  lcchuguo  阅读(150)  评论(0编辑  收藏  举报