[bzoj 3211]花神游历各国

124 篇文章 2 订阅
14 篇文章 0 订阅

这里写图片描述
这道题一开始用分块写,结果不知是什么,可能写挂了,TLE。(呵呵)
之后便仔细想一想,可以用线段树做,思路跟分块差不多,因为这道题的开根号后是向下取整,所以经过几次开根后这个数就变成了1或0,这样就给它打上一个标记,以后便不用动它,可以省很多时间。这题便就解决了。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
struct node
{
    int l,r,lc,rc;long long c;
    bool lazy;
    node(){lazy=false;}
}tr[210000];int trlen;
int n,m;long long a[110000];
void bt(int l,int r)
{
    trlen++;
    int now=trlen;
    tr[now].l=l;tr[now].r=r;
    tr[now].lc=tr[now].rc=-1;
    if(l==r)
    {
        tr[now].c=a[l];
        if(a[l]==1 || a[l]==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;
    }
}
long long 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(mid+1<=l)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=(long long)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(mid+1<=l)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()
{
    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 q,x,y;
        scanf("%d%d%d",&q,&x,&y);
        if(x>y)swap(x,y);
        if(q==1)printf("%lld\n",getsum(1,x,y));
        else change(1,x,y);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值