【CQOI2015】 Task Query System (Chairman Tree)

luogu  &  bzoj

Topic description

Recently the lab is working on a task management system for the supercomputers it manages, and you're assigned the query part of it. Tasks in supercomputers are described by triples (Si, Ei, Pi), where (Si, Ei, Pi) means that the task starts at the Si second and ends after the Ei second (Si and Ei seconds tasks are also running ), whose priority is Pi. There may be multiple tasks executing at the same time, and their priorities may or may not be the same. The scheduling system will often ask the query system what is the sum of the priorities of the Ki tasks with the lowest priority (that is, the tasks are sorted according to their priorities from small to large, and then the first Ki tasks are taken) among the tasks that are running in the xi second. In particular, if Ki is greater than the total number of running tasks in the Xith second, directly answer the sum of the priorities of the tasks running in the Xith second. All of the above parameters are integers and the time ranges from 1 to n inclusive.

Input and output format

Input format:

 

The first line of the input file contains two space-separated positive integers m and n, representing the total number of tasks and the time range, respectively. The next m lines, each containing three space-separated positive integers Si, Ei, and Pi (Si<=Ei), describe a task. The next n lines, each containing four space-separated integers Xi, Ai, Bi, and Ci, describe a query. The query parameter Ki needs to be calculated by the formula Ki=1+(Ai*Pre+Bi) mod Ci. Among them, Pre represents the result of the previous query. For the first query, Pre=1.

 

Output format:

 

A total of n lines are output, and each line contains an integer representing the query result.

 

Problem solving ideas

 

code

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<math.h>
 6 #define LL long long
 7 using namespace std;
 8 const int maxn=(1e5+10)*85;
 9 struct nod{
10     int pos,val;
11 };
12 nod p[maxn];
13 LL root[maxn];
14 LL sum[maxn],num[maxn],lson[maxn],rson[maxn];
15 int end[maxn],num_start[maxn];
16 int n,m,cntt=0,cnt=1;
17 inline bool cmp(nod a,nod b){
18     return a.pos<b.pos;
19 }
20 inline void read(long long &x){
21     x=0; register char ch=getchar();
22     while(ch<'0'||ch>'9')ch=getchar();
23     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
24 }
25 inline int ins(LL &ID,LL IDl,int nl,int nr,int val){
26     if(ID==0)ID=++cnt;
27     if(val>0)num[ID]=num[IDl]+1;
28     else num[ID]=num[IDl]-1;
29     sum[ID]=sum[IDl]+val;
30     if(nl==nr)return 0;
31     int m=(nl+nr)>>1;
32     if(m>=abs(val)){
33         rson[ID]=rson[IDl];
34         ins(lson[ID],lson[IDl],nl,m,val);
35     }
36     else {
37         lson[ID]=lson[IDl];
38         ins(rson[ID],rson[IDl],m+1,nr,val);
39     }
40     num[ID]=num[lson[ID]]+num[rson[ID]];
41     sum[ID]=sum[lson[ID]]+sum[rson[ID]];
42 }
43 inline LL query(int nl,int nr,int now,int k){
44     if(nl==nr)return (LL)nl*k;
45     int m=(nl+nr)>>1;
46     if(num[lson[now]]>=k)return query(nl,m,lson[now],k);
47     else return query(m+1,nr,rson[now],k-num[lson[now]])+sum[lson[now]];
48 }
49 int main(){
50     int max_val=-99999999;
51     scanf("%d%d",&m,&n);
52     register int S,E,P;
53     for(register int i=1;i<=m;i++){
54         scanf("%d%d%d",&S,&E,&P);
55         p[++cntt].pos=S; p[cntt].val=P;
56         p[++cntt].pos=E+1; p[cntt].val=-P;
57         max_val=max(max_val,P);
58     }
59     sort(p+1,p+cntt+1,cmp);
60     root[0]=1;
61     for(register int i=1;i<=cntt;i++)
62         ins(root[i],root[i-1],1,max_val,p[i].val);
63     for(register int i=cntt;i>=0;i--)
64         if(p[i].pos!=p[i+1].pos)end[p[i].pos]=i;
65     for(register int i=1;i<=1e6;i++)
66         if(!end[i])end[i]=end[i-1];
67     register long long X,A,B,C,K,pre=1;
68     for(register int i=1;i<=n;i++){
69         read(X),read(A),read(B),read(C);
70         K=1+(A*pre+B)%C;
71         if(num[root[end[X]]]<=K)pre=sum[root[end[X]]];
72         else pre=query(1,max_val,root[end[X]],K);
73         printf("%lld\n",pre);
74     }
75 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324955154&siteId=291194637
Recommended