BZOJ 3932 CQOI2015 主席树

题目链接


思路:
观察询问的格式:在 x i x_i xi的时间点的前 k k k个任务的优先度总和。

易想到我们可以对于每一个时间点都维护一个权值线段树,其权值为当前存在任务的优先度。可以利用差分的思想,对于任务 [ S , E , P ] [S,E,P] [S,E,P],可以将 S S S + P +P +P,而 E + 1 E+1 E+1 − P -P P,则可以维护出每一个时间点的权值线段树。

但这样的话内存是爆炸的。

故可以考虑只保存有修改的时间点的权值线段树。
比如两个有两个任务,第一个任务的时间是 [ 1 , 10 ] [1,10] [110],而第二个为 [ 1000 , 2000 ] [1000,2000] [1000,2000]
则时间点: 11 11 11~ 999 999 999都可以使用时间点为 10 10 10的权值线段树代替。

故套上主席树,此题可解。


代码:

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
 
const int A = 2e5 + 10;
class Seg_Tree{
public:
    int l_rt,r_rt,siz;
    ll sum;
}T[A*25];
class P{
public:
    int pos,val;
    bool operator<(const P& rhs) const{return pos < rhs.pos;}
}a[A];
int m,n,x,y,z,t,Mx,tot,cnt,root[A],to[A];
 
void update(int l,int r,int &x,int y,int pos){
    x = ++cnt;T[x] = T[y];
    T[x].sum += pos;T[x].siz += pos>0?1:-1;
    if(l == r) return;
    int mid = (l+r)>>1;
    if(abs(pos) <= mid) update(l,mid,T[x].l_rt,T[y].l_rt,pos);
    else                update(mid+1,r,T[x].r_rt,T[y].r_rt,pos);
}
 
ll query(int l,int r,int x,int k){
    if(T[x].siz <= k) return T[x].sum;
    if(l==r) return 1LL*l*k;
    int mid = (l+r)>>1;
    if(T[T[x].l_rt].siz >= k) return query(l,mid,T[x].l_rt,k);
    return T[T[x].l_rt].sum + query(mid+1,r,T[x].r_rt,k-T[T[x].l_rt].siz);
}
 
int main(){
    n = read();m = read();
    for(int i=1 ;i<=n ;i++){
        x = read();y = read(); z = read();
        a[++tot].pos = x;a[tot].val = z;
        a[++tot].pos = y+1;a[tot].val = -z;
        Mx = max(Mx,z);
    }
    sort(a+1,a+tot+1);
    for(int i=1 ;i<=tot ;i++) update(1,Mx,root[i],root[i-1],a[i].val);
    for(int i=tot ;i>=1 ;i--) if(a[i].pos != a[i+1].pos) to[a[i].pos] = root[i];
    for(int i=1 ;i<=n ;i++) if(!to[i]) to[i] = to[i-1];
    ll pre = 1;
    for(int i=1 ;i<=m ;i++){
        t = read();x = read();y = read();z = read();
        int k = (1LL*x*pre + y)%z + 1;
        ll ans = query(1,Mx,to[t],k);
        pre = ans;
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值