3932: [CQOI2015]任务查询系统
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 3715 Solved: 1194
[ Submit][ Status][ Discuss]
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
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
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的一个排列
Source
差分一下主席树
代码:
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn = 100100;
const int maxseg = 2 * maxn * 20;
struct data{
int s,t;
LL p;
}opt[maxn];
struct node{
int x,cnt;
};
vector<node> cur[maxn];
int rt[maxn],n,m,now;
LL ha[maxn],tot_ha,data[maxn];
LL sum[maxseg];
int cnt[maxseg],lc[maxseg],rc[maxseg],tot;
LL lastans = 1;
inline LL getint()
{
LL ret = 0,f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
ret = ret * 10 + c - '0',c = getchar();
return ret * f;
}
inline void maintain(int o)
{
cnt[o] = cnt[lc[o]] + cnt[rc[o]];
sum[o] = sum[lc[o]] + sum[rc[o]];
}
inline int modify(int ac,int l,int r,int pos,int all)
{
int o = ++tot;
cnt[o] = cnt[ac]; sum[o] = sum[ac]; lc[o] = lc[ac]; rc[o] = rc[ac];
if (l == r)
{
cnt[o] += all;
sum[o] += ha[pos] * all;
return o;
}
int mid = l + r >> 1;
if (pos <= mid) lc[o] = modify(lc[ac],l,mid,pos,all);
if (mid < pos) rc[o] = modify(rc[ac],mid + 1,r,pos,all);
maintain(o);
return o;
}
inline LL query(int o,int l,int r,int k)
{
if (l == r) return k * ha[l];
int mid = l + r >> 1;
if (cnt[lc[o]] <= k) return query(rc[o],mid + 1,r,k - cnt[lc[o]]) + sum[lc[o]];
else return query(lc[o],l,mid,k);
}
int main()
{
m = getint(); n = getint();
for (int i = 1; i <= m; i++)
{
opt[i].s = getint();
opt[i].t = getint();
opt[i].p = getint();
data[i] = opt[i].p;
}
sort(data + 1,data + m + 1);
for (int i = 1; i <= m; i++)
if (data[i] != data[i + 1])
ha[++tot_ha] = data[i];
for (int i = 1; i <= m; i++)
opt[i].p = lower_bound(ha + 1,ha + tot_ha + 1,opt[i].p) - ha;
for (int i = 1; i <= m; i++)
{
cur[opt[i].s].push_back((node){opt[i].p,1});
cur[opt[i].t + 1].push_back((node){opt[i].p,-1});
}
now = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < cur[i].size(); j++)
now = modify(now,1,tot_ha,cur[i][j].x,cur[i][j].cnt);
rt[i] = now;
}
for (int i = 1; i <= n; i++)
{
int x = getint();
LL a = getint(),b = getint(),c = getint();
LL k = 1ll + (a * lastans + b) % c;
k = min(k,1ll * cnt[rt[x]]);
printf("%lld\n",lastans = query(rt[x],1,tot_ha,k));
}
return 0;
}