BZOJ3932] [CQOI2015 task Inquiry System & Luo Gu P3168-- former Chairman of the tree section K + small difference

Topic links: https://www.lydsy.com/JudgeOnline/problem.php?id=3932

BZOJ:Time Limit: 20 Sec  Memory Limit: 512 MB

Luo Valley:
Time limit 2.00s
Memory limit 500.00MB
 

Description

Recent laboratory is preparing a task management system for its supercomputers management, and you are scheduled to complete the query part of it. Super computer
Task the triples (Si, Ei, Pi) description, (Si, Ei, Pi) indicates that the task started from the second Si, Ei seconds after the end of the (first and second Si Ei second task also runs
), Whose priority Pi. At the same time may have to perform multiple tasks simultaneously, their priority may be the same or may be different. Often to the scheduling system
Query asked, running tasks on Xi seconds, the smallest Ki priority tasks (tasks in the upcoming priorities in ascending order after taking a pre-Ki
) Priority sum is. In particular, if Ki is greater than the total number of tasks running on Xi seconds, the first task of a direct answer Xi seconds running priority
And the level of. All of the above parameters are integers, a range of time between 1 to n (containing 1 and n).
 

Input

The first line of the input file contains two space-separated positive integers m and n, respectively, represent the total number of tasks and time. Subsequently m rows, each row comprising three spaces
Separate positive integers Si, Ei and Pi (Si≤Ei), describe a task. Subsequently n rows, each row comprising four space-separated integers Xi, Ai, Bi and Ci,
Describe a query. Ki query parameters required by the equation Ki = 1 + (Ai * Pre + Bi) mod Ci calculated. Pre which represents the results of the previous query,
For the first query, Pre = 1.
 
 

Output

Total n output lines, each line an integer that represents the query results.
 

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

 

Sample interpretation

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

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

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

To 100% of the data, 1≤m, n, Si, Ei, Ci≤100000,0≤Ai, Bi≤100000,1≤Pi≤10000000, Xi is a 1 to n are arranged

 


 

Chinese face problems, very friendly, another Los Valley K-value formula wrong. . .

The question is seeking face for the first time a single small values ​​of k and then the front and a small value of k, then it is easy to think of the Chairman of the tree. But how is a maintenance problem. If the direct modification of violence into a single point, then not only can not afford the time and space but also can not afford. So I thought of the giant giant difference. . . Each time interval modification only need to modify the head and tail ends, namely update (st), update (ed + 1); when you learn to believe that the Chairman of the tree must have learned a tree array. . . So certainly I learned the difference, not to say.

First us, then when we go directly to the query on the line in the x-th tree for every moment achievements. In every moment, there is a section start or end, then we are directly update at the beginning and end of this time is represented by the number 1 and -1, 1 | -1 * b [pos] as its priority, then this is a differential, and wherein the b [pos] is the size of the discretization of the priority:

for (int i=1; i<=m; i++) {
    for (int j=0; j<st[i].size(); j++) {
        int p=lower_bound(fall+1,fall+1+nb,lis[st[i][j]])-fall;
        ver[++np]=update(1,nb,p,1,ver[np-1]);
    }
    for (int j=0; j<ed[i].size(); j++) {
        int p=lower_bound(fall+1,fall+1+nb,lis[ed[i][j]])-fall;
        ver[++np]=update(1,nb,p,-1,ver[np-1]);
    }
    last[i]=ver[np];
}

 

But before and differentiated version here each will have 0-2, but not the only one , so the final version of the i-th time we produced another record should look, here I use the last recorded

update function and the query function, then I do not speak, the former Chairman of the interval on a large tree k values ​​are related to, and finally a reminder, each point may have a lot number, so the query time of l == r We should sum / num * k, where k is the number of remaining, corresponding to the number of monovalent *. . .

The following is the code :( AC Los Valley A No, for details see my blog description)

#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
 
const int mac=1e5+10;
 
struct Tree
{
    ll sum;
    int size,l,r;
}tree[mac<<6];
int lis[mac],fall[mac],ver[mac*2];
vector<int>st[mac],ed[mac];
int last[mac];
int tot=0;
 
void in(int &x)
{
    int f=0;
    char ch=getchar();
    while (ch>'9' || ch<'0') ch=getchar();
    while (ch>='0' && ch<='9') f=(f<<1)+(f<<3)+ch-'0',ch=getchar();
    x=f;
}
 
int build(int l,int r)
{
    int mid=(l+r)>>1;
    int rt=++tot;
    if (l==r) return rt;
    tree[rt].l=build(l,mid);
    tree[rt].r=build(mid+1,r);
    return rt;
}
 
int update(int l,int r,int pos,int val,int per)
{
    int rt = ++ all;
    tree[rt]=tree[per];
    tree[rt].size+=val;tree[rt].sum+=1ll*val*fall[pos];
    if (l==r) return rt;
    int mid=(l+r)>>1;
    if (mid>=pos) tree[rt].l=update(l,mid,pos,val,tree[per].l);
    else tree[rt].r=update(mid+1,r,pos,val,tree[per].r);
    return rt;
}
 
ll query(int l,int r,int rt,int k)
{
    int x=tree[tree[rt].l].size;
    if (l==r) return tree[rt].sum/(1ll*tree[rt].size)*1ll*k;
    int mid=(l+r)>>1;
    if (x>=k) return query(l,mid,tree[rt].l,k);
    else return query(mid+1,r,tree[rt].r,k-x)+tree[tree[rt].l].sum;
}
 
int main ()
{
    int n,m;
    in(n);in(m);
    for (int i=1; i<=n; i++){
        int x,y,z;
        in(x);in(y);in(z);
        November [i] = z; fall [i] = z;
        st[x].push_back(i);
        ed[y+1].push_back(i);
    }
    sort(fall+1,fall+1+n);
    int nb=unique(fall+1,fall+1+n)-fall-1;
    ver[0]=build(1,nb);
    int np=0;
    for (int i=1; i<=m; i++){
        for (int j=0; j<st[i].size(); j++){
            int p=lower_bound(fall+1,fall+1+nb,lis[st[i][j]])-fall;
            ver[++np]=update(1,nb,p,1,ver[np-1]);
        }
        for (int j=0; j<ed[i].size(); j++){
            int p=lower_bound(fall+1,fall+1+nb,lis[ed[i][j]])-fall;
            ver[++np]=update(1,nb,p,-1,ver[np-1]);
        }
        last[i]=ver[np];
    }
    ll per=1;
    for (int i=1; i<=m; i++){
        int x,a,b,c;
        in(x);in(a);in(b);in(c);
        ll k=(1ll*a*per+b)%c+1;
        if (tree[last[x]].size<=k) per=tree[last[x]].sum;
        else per=query(1,nb,last[x],k);
        printf ("%lld\n",per);
    }
    return 0;
}

 

 

 

Guess you like

Origin www.cnblogs.com/lonely-wind-/p/11891451.html