【bzoj 3932】任务查询系统

传送门~

解题思路

每个任务(Si,Ei,Pi),在Si的位置加入Pi,在Ei+1的位置删掉Pi,这样取前缀和就能表示每个点包含的所有数。
将每个任务拆成两个操作:在Si加Pi和在Ei减Pi。将所有操作排序后按操作建主席树。
然后还需要求个to数组,存询问中的每个时间点要在哪棵树中查询。
可能会有重复的数,所以在查询到最底层的时候需要判一下。因为这个东西wa了好久。。。mdzz
代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#define ll long long
using namespace std;
struct ldx{
    ll wi,xi;
    ll opt;
    bool operator < (const ldx p) const{
        return wi<p.wi;
    }
}s[200005];
struct node{
    ll x,y,num;
    ll sum;
    node* ch[2];
    node(ll,ll,ll);
}*null=new node(0,0,0),*root[200005];
node:: node(ll _,ll __,ll ___){
    x=_;y=__;num=___;sum=0;
    ch[0]=ch[1]=null;
}
ll to[100005];
ll n,m,tot,xx,u;
ll ans=1;
void build(node* p,node* &t,ll lx,ll ly){
    if(t==null) t=new node(lx,ly,0);
    t->num=p->num+u;t->sum=p->sum+1ll*xx*u;
    if(lx==ly) return ;
    ll mid=(lx+ly)>>1;
    if(mid>=xx) {t->ch[1]=p->ch[1];build(p->ch[0],t->ch[0],lx,mid);}
    else {t->ch[0]=p->ch[0];build(p->ch[1],t->ch[1],mid+1,ly);}
}
ll srch(node* p,ll k){
    if(p->num<=k) return p->sum;
    else if(p->x==p->y && p->num>k) return p->x*k;
    else if(p->ch[0]->num>=k) return srch(p->ch[0],k);
    else return p->ch[0]->sum+srch(p->ch[1],k-p->ch[0]->num);
}
void sett(){
    null->ch[0]=null->ch[1]=null;
    for(ll i=0;i<=tot;i++) root[i]=null;
}
int main(){
    ll ai,bi,ci;
    scanf("%lld%lld",&m,&n);
    for(ll i=1;i<=m;i++){
        scanf("%lld%lld%lld",&ai,&bi,&ci);
        tot++;s[tot].wi=ai;s[tot].xi=ci;s[tot].opt=1;
        tot++;s[tot].wi=bi+1;s[tot].xi=ci;s[tot].opt=-1;
    }
    sort(s+1,s+tot+1);sett();
    for(ll i=1;i<=tot;i++){
        xx=s[i].xi;u=s[i].opt;
        build(root[i-1],root[i],1,1e9);
    }
    ll tp=0;
    for(ll i=1;i<=n;i++){
        while(s[tp+1].wi<=i && tp<tot) tp++;
        to[i]=tp;
    }
    for(ll i=1;i<=n;i++){
        scanf("%lld%lld%lld%lld",&xx,&ai,&bi,&ci);
        ll ki=(1ll*ai*ans+bi)%ci+1;
        ans=srch(root[to[xx]],ki);
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值