思路:
观察询问的格式:在第
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]
[1,10],而第二个为
[
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;
}