任务查询系统(bzoj 3932)

Description

最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分。超级计算机中的
任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第Ei秒后结束(第Si秒和Ei秒任务也在运行
),其优先级为Pi。同一时间可能有多个任务同时执行,它们的优先级可能相同,也可能不同。调度系统会经常向
查询系统询问,第Xi秒正在运行的任务中,优先级最小的Ki个任务(即将任务按照优先级从小到大排序后取前Ki个
)的优先级之和是多少。特别的,如果Ki大于第Xi秒正在运行的任务总数,则直接回答第Xi秒正在运行的任务优先
级之和。上述所有参数均为整数,时间的范围在1到n之间(包含1和n)。
 

Input

输入文件第一行包含两个空格分开的正整数m和n,分别表示任务总数和时间范围。接下来m行,每行包含三个空格
分开的正整数Si、Ei和Pi(Si≤Ei),描述一个任务。接下来n行,每行包含四个空格分开的整数Xi、Ai、Bi和Ci,
描述一次查询。查询的参数Ki需要由公式 Ki=1+(Ai*Pre+Bi) mod Ci计算得到。其中Pre表示上一次查询的结果,
对于第一次查询,Pre=1。
 
 

Output

输出共n行,每行一个整数,表示查询结果。
 

Sample Input

4 3
1 2 6
2 3 3
1 3 2
3 3 4
3 1 3 2
1 1 3 4
2 2 4 3

Sample Output

2
8
11

HINT

样例解释

K1 = (1*1+3)%2+1 = 1

K2 = (1*2+3)%4+1 = 2

K3 = (2*8+4)%3+1 = 3

对于100%的数据,1≤m,n,Si,Ei,Ci≤100000,0≤Ai,Bi≤100000,1≤Pi≤10000000,Xi为1到n的一个排列
/*
  做完这道题,感觉自己对主席树的理解还是蒙蔽的
  很显然,我们需要对于每个时间点维护一个线段树,修改时运用差分的原理,对于在x~y上修改就在x上+1,在y+1上-1,  这样就完美契合了主席树。 
  对于每棵线段树,从小到大维护优先级即可。 
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define N 300010
#define lon long long
using namespace std;
lon n,m,lim,cnt,cn,to[N*2],root[N*2],son[N*40][2],w[N*40],sum[N*40];
struct node{
    lon pos,v;
};node e[N*2];
lon read(){
    lon num=0,flag=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')flag=-1;c=getchar();}
    while(c>='0'&&c<='9'){num=num*10+c-'0';c=getchar();}
    return num*flag;
}
bool cmp(const node&s1,const node&s2){
    return s1.pos<s2.pos;
}
void push_up(lon x){
    w[x]=w[son[x][0]]+w[son[x][1]];
    sum[x]=sum[son[x][0]]+sum[son[x][1]];
}
void insert(lon x,lon &y,lon l,lon r,lon v){
    y=++cn;
    if(l==r){
        if(v>0) w[y]=w[x]+1;
        else w[y]=w[x]-1;
        sum[y]=sum[x]+v;
        return;
    }
    son[y][0]=son[x][0];
    son[y][1]=son[x][1];
    lon mid=l+r>>1;
    if(abs(v)<=mid) insert(son[x][0],son[y][0],l,mid,v);
    else insert(son[x][1],son[y][1],mid+1,r,v);
    push_up(y);
}
lon query(lon R,lon k){
    lon x=root[R];
    if(w[x]<=k)return sum[x];
    lon l=1,r=lim,ans=0;
    while(l<r){
        lon mid=l+r>>1;    
        if(w[son[x][0]]>=k){
            r=mid;
            x=son[x][0];
        }
        else {
            ans+=sum[son[x][0]];
            k-=w[son[x][0]];
            l=mid+1;
            x=son[x][1];
        }
        if(l>=r) return ans+sum[x]/w[x]*k;
    }
    return ans;
}
int main(){
    n=read();m=read();
    for(lon i=1;i<=n;i++){
        lon x=read(),y=read(),p=read();
        e[++cnt].pos=x;e[cnt].v=p;
        e[++cnt].pos=y+1;e[cnt].v=-p;
        lim=max(lim,p);
    }
    sort(e+1,e+cnt+1,cmp);
    for(lon i=1;i<=cnt;i++) insert(root[i-1],root[i],1,lim,e[i].v);
    for(lon i=cnt;i>=1;i--)
        if(e[i].pos!=e[i+1].pos)
            to[e[i].pos]=i;
    for(lon i=1;i<=n;i++)
        if(!to[i])to[i]=to[i-1];
    lon pre=1;
    for(lon i=1;i<=m;i++){
        lon x=read(),a=read(),b=read(),c=read();
        lon k=(a*pre+b)%c+1;
        pre=query(to[x],k);
        printf("%lld
",pre);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/harden/p/6399389.html