A simple simulation problem. HDU - 4973

http://acm.hdu.edu.cn/showproblem.php?pid=4973

线段树始终维护一个从1到n的序列其上的权值 对于题目给的l和r 通过区间二分查询 找到在序列上对应的位置 然后左右端点pl和pr特殊处理 [pl+1,pr-1]区间处理即可

#include <bits/stdc++.h>
using namespace std;
#define ll long long

struct node
{
    int l;
    int r;
    ll val;
    ll maxx;
    ll laz1;
    ll laz2;
};

node tree[200010];
int n,q;

void pushup(int cur)
{
    tree[cur].val=tree[2*cur].val+tree[2*cur+1].val;
    tree[cur].maxx=max(tree[2*cur].maxx,tree[2*cur+1].maxx);
}

void pushdown(int cur)
{
    if(tree[cur].laz2!=1)
    {
        tree[2*cur].val*=tree[cur].laz2;
        tree[2*cur].maxx*=tree[cur].laz2;
        tree[2*cur].laz2*=tree[cur].laz2;
        tree[2*cur+1].val*=tree[cur].laz2;
        tree[2*cur+1].maxx*=tree[cur].laz2;
        tree[2*cur+1].laz2*=tree[cur].laz2;
        tree[cur].laz2=1;
    }
    if(tree[cur].laz1!=0)
    {
        tree[2*cur].val+=tree[cur].laz1;
        tree[2*cur].maxx+=tree[cur].laz1;
        tree[2*cur].laz1+=tree[cur].laz1;
        tree[2*cur+1].val+=tree[cur].laz1;
        tree[2*cur+1].maxx+=tree[cur].laz1;
        tree[2*cur+1].laz1+=tree[cur].laz1;
        tree[cur].laz1=0;
    }
}

void build(int l,int r,int cur)
{
    int m;
    tree[cur].l=l;
    tree[cur].r=r;
    tree[cur].val=0;
    tree[cur].maxx=0;
    tree[cur].laz1=0;
    tree[cur].laz2=1;
    if(l==r)
    {
        tree[cur].val=1;
        tree[cur].maxx=1;
        return;
    }
    m=(l+r)/2;
    build(l,m,2*cur);
    build(m+1,r,2*cur+1);
    pushup(cur);
}

int getpos(ll val,int cur)
{
    if(tree[cur].l==tree[cur].r) return tree[cur].l;
    pushdown(cur);
    if(val<=tree[2*cur].val) return getpos(val,2*cur);
    else return getpos(val-tree[2*cur].val,2*cur+1);
}

ll queryI(int pl,int pr,int cur)
{
    ll res;
    if(pl<=tree[cur].l&&tree[cur].r<=pr) return tree[cur].val;
    res=0;
    pushdown(cur);
    if(pl<=tree[2*cur].r) res+=queryI(pl,pr,2*cur);
    if(pr>=tree[2*cur+1].l) res+=queryI(pl,pr,2*cur+1);
    return res;
}

ll queryII(int pl,int pr,int cur)
{
    ll res;
    if(pl<=tree[cur].l&&tree[cur].r<=pr) return tree[cur].maxx;
    res=0;
    pushdown(cur);
    if(pl<=tree[2*cur].r) res=max(res,queryII(pl,pr,2*cur));
    if(pr>=tree[2*cur+1].l) res=max(res,queryII(pl,pr,2*cur+1));
    return res;
}

void updateI(int tar,ll val,int cur)
{
    if(tree[cur].l==tree[cur].r)
    {
        tree[cur].val+=val;
        tree[cur].maxx+=val;
        tree[cur].laz1+=val;
        return;
    }
    pushdown(cur);
    if(tar<=tree[2*cur].r) updateI(tar,val,2*cur);
    else updateI(tar,val,2*cur+1);
    pushup(cur);
}

void updateII(int pl,int pr,int cur)
{
    if(pl<=tree[cur].l&&tree[cur].r<=pr)
    {
        tree[cur].val*=2;
        tree[cur].maxx*=2;
        tree[cur].laz1*=2;
        tree[cur].laz2*=2;
        return;
    }
    pushdown(cur);
    if(pl<=tree[2*cur].r) updateII(pl,pr,2*cur);
    if(pr>=tree[2*cur+1].l) updateII(pl,pr,2*cur+1);
    pushup(cur);
}

int main()
{
    ll val1,val2,val3;
    int t,cas,l,r,pl,pr;
    char op[10];
    scanf("%d",&t);
    for(cas=1;cas<=t;cas++)
    {
        scanf("%d%d",&n,&q);
        build(1,n,1);
        printf("Case #%d:\n",cas);
        while(q--)
        {
            scanf("%s%d%d",op,&l,&r);
            if(op[0]=='Q')
            {
                pl=getpos(l,1);
                pr=getpos(r,1);
                if(pl==pr) printf("%d\n",r-l+1);
                else if(pl+1==pr)
                {
                    val1=queryI(1,pl,1)-l+1;
                    val2=r-queryI(1,pr-1,1);
                    printf("%lld\n",max(val1,val2));
                }
                else
                {
                    val1=queryI(1,pl,1)-l+1;
                    val2=r-queryI(1,pr-1,1);
                    val3=queryII(pl+1,pr-1,1);
                    printf("%lld\n",max(val1,max(val2,val3)));
                }
            }
            else
            {
                pl=getpos(l,1);
                pr=getpos(r,1);
                if(pl==pr) updateI(pl,r-l+1,1);
                else if(pl+1==pr)
                {
                    val1=queryI(1,pl,1)-l+1;
                    val2=r-queryI(1,pr-1,1);
                    updateI(pl,val1,1);
                    updateI(pr,val2,1);
                }
                else
                {
                    val1=queryI(1,pl,1)-l+1;
                    val2=r-queryI(1,pr-1,1);
                    updateI(pl,val1,1);
                    updateI(pr,val2,1);
                    updateII(pl+1,pr-1,1);
                }
            }
        }
    }
    return 0;
}

/*
100
6 100
Q 1 6
D 2 5
Q 1 10
D 3 7
Q 1 7
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值