BZOJ3038: 上帝造题的七分钟2

【传送门:BZOJ3038


简要题意:

  给出一个序列,对这些序列进行两种操作:

  1 x y求出x到y的和

  2 x y将x到y的数全部开方(向下取整)


题解:

  同BZOJ3211


参考代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
struct node
{
    int l,r,lc,rc;LL c;
    bool lazy;
}tr[210000];int trlen;
LL a[110000];
void bt(int l,int r)
{
    trlen++;int now=trlen;
    tr[now].l=l;tr[now].r=r;tr[now].c=0;
    tr[now].lc=tr[now].rc=-1;tr[now].lazy=false;
    if(l==r)
    {
        tr[now].c=a[l];
        if(tr[now].c==1||tr[now].c==0) tr[now].lazy=true;
    }
    else
    {
        int mid=(l+r)/2;
        tr[now].lc=trlen+1;bt(l,mid);
        tr[now].rc=trlen+1;bt(mid+1,r);
        tr[now].c=tr[tr[now].lc].c+tr[tr[now].rc].c;
        if(tr[tr[now].lc].lazy==true&&tr[tr[now].rc].lazy==true) tr[now].lazy=true;
    }
}
LL getsum(int now,int l,int r)
{
    if(tr[now].l==l&&tr[now].r==r) return tr[now].c;
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid) return getsum(lc,l,r);
    else if(l>mid) return getsum(rc,l,r);
    else return getsum(lc,l,mid)+getsum(rc,mid+1,r);
}
void change(int now,int l,int r)
{
    if(tr[now].lazy==true) return ;
    if(tr[now].l==tr[now].r)
    {
        tr[now].c=LL(sqrt(tr[now].c));
        if(tr[now].c==1||tr[now].c==0) tr[now].lazy=true;
        return ;
    }
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid) change(lc,l,r);
    else if(l>mid) change(rc,l,r);
    else
    {
        change(lc,l,mid);
        change(rc,mid+1,r);
    }
    tr[now].c=tr[lc].c+tr[rc].c;
    if(tr[lc].lazy==true&&tr[rc].lazy==true) tr[now].lazy=true;
}
int main()
{
    int n,m;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    trlen=0;bt(1,n);
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int t,x,y;
        scanf("%d%d%d",&t,&x,&y);
        if(x>y) swap(x,y);
        if(t==1) printf("%lld\n",getsum(1,x,y));
        else change(1,x,y);
    }
    return 0;
}
posted @ 2018-03-07 19:42  Star_Feel  阅读(133)  评论(0编辑  收藏  举报