[Luo Gu P3960] NOIP2017 parade

Problem Description

Sylvia is a girl's love of learning.

Some time ago, Sylvia attended the school of military training. As we all know, when the military need to stand square.

Sylvia square where there are $ n \ times m $ pupils, the number of rows square is n, the number of columns is m.

For ease of administration, at the beginning of the training instructors, according to front to back, left to right in order of square to students from 1 to $ n \ times m $ compiled on the number (see examples below). Namely: the initial i-th row j-th column number of students is \ ((. 1-i) \ j Times m + \) .

However, in practice, when the square, because students often have a variety of things to leave. In a day, a total of q pieces happened to leave such an event. Each event may leave a number for \ ((x, y) ( 1 \ le x \ le n, 1 \ le y \ le m) \) described, it represents the column x, line y students leave.

After the students leave, the team there is a vacancy. To tidy team, instructors will be issued this order two instructions:

  1. Left par. Then the first column to remain intact, all the students to fill the gap left. After difficult to find this instruction, gap x m-th column in the first row.
  2. Look ahead together. Then the first line to remain intact, all the students to fill the vacancy forward. After difficult to find this instruction, the first gap in the n-th row and m columns.

Instructors provisions can not have two or more students at the same time leaving the team. That left the band after a student rejoin former student next to leave. Therefore, when each of the students to leave a rejoin, and only the ranks of the m-th column n-th row a gap, then naturally the student to fill this position.

Because the station square is really boring, so Sylvia wants to leave is calculated every event, the number of students leaving the team is.

Note: Each student numbers will not leave with the occurrence of an event of change, leaving the team after the incident in the square number of students may be out of order.

Input Format

Q + 1 input common line.

The first row contains three spaces separated by a positive integer n, m, q, represents the size of the square matrix of n rows and m columns is, a total of q times the event occurred.

The next sequence of events in accordance with the line q q incident is described. Each row is two integers x, y, are separated by a space, leaving the team event indicates that the student was leaving the team ranked first in x row y column.

Output Format

In accordance with the input sequence of events, each event output line an integer representing the students left the band leaving the team event number.

Explanation

Sample Description 1 [O]

The queue process shown above, each row describes an event. In the first incident, a number of students leaving the team, then the vacancy in the first column of the first row. Subsequently all the students standard flush left, then the number of students 2 moves one step to the left, to the first line vacancy second column. All students then marked up together, then students numbered 4 up step, then move to the second column of the second gap line. Finally, the students numbered 1 to fill the vacancy in return.

[Agreed] with the scale data

To ensure that each event data satisfies a \ (1 \ le x \ le n, 1 \ le y \ le m \)

Resolve

Sub-section

We can start before the m-1 for each column of a row vector, while the last one is also open to a vector, and a simulated team enqueued processes. Obviously in time and space they can not pass. Therefore, we consider the optimization.

Space Optimization

If you want to output a team's number, it means that the vector need to save all the points. However, for the former classmates m-1 each row, their number can be directly calculated, no need to save. Then each team to determine what the students are not the former m-1 Ge, is a direct calculation, not a vector and then to go inside.

Time Optimization

The most time-consuming part is that every long even when you remove the left, look ahead together, means that every time traversing the last vector. So, consider the use of weights segment tree maintenance and vector with each row and the last one. Save weights tree line in which someone is the current position, vector saved into the history of each team. For each operation leaving the team, with the right to query the value segment tree this is the first of several students, and then to the query vector number.

According to the above wording to achieve, get good grades TLE & MLE's.

Reverse operation

Even with weights tree line, modify the complexity of each node is still too high. Might, in turn, the initial default for each position there are people, you can directly use the k-index query large. When someone from the team, put that person's position is set to 1, minus 1 point when all queries k-th largest. This can be used to modify the operation once achieved.

Dynamic prescription

Dynamic tree line with a prescription can optimize the space complexity. Coupled with the previously mentioned space optimization, you can pass this question.

detail

Note that the preceding discussion the column and row m-1 m. To modify the time information before the column-1 m m-th column element in the corresponding row segment tree of weights added in the row, while an updated value of the m-th column.

Code

#include <iostream>
#include <cstdio>
#include <vector>
#define int long long
#define N 300002
using namespace std;
struct SegmentTree{
    int l,r,val;
}t[N*40];
int n,m,q,i,root[N],num;
vector<int> v[N];
int read()
{
    char c=getchar();
    int w=0;
    while(c<'0'||c>'9') c=getchar();
    while(c<='9'&&c>='0'){
        w=w*10+c-'0';
        c=getchar();
    }
    return w;
}
int kth(int &p,int l,int r,int x)
{
    if(p==0) p=++num,t[p].val=0;
    t[p].val++;
    if(l==r) return l;
    int mid=(l+r)/2;
    int tmp=mid-l+1-t[t[p].l].val;
    if(x<=tmp) return kth(t[p].l,l,mid,x);
    return kth(t[p].r,mid+1,r,x-tmp);
}
signed main()
{
    n=read();m=read();q=read();
    int l1=m-1+q,l2=n+q;
    for(i=1;i<=q;i++){
        int x=read(),y=read();
        if(y<m){
            int pos1=kth(root[x],1,l1,y);
            int ans=pos1<=m-1?m*(x-1)+pos1:v[x][pos1-m];
            v[n+1].push_back(ans);
            int pos2=kth(root[n+1],1,l2,x);
            int id=pos2<=n?pos2*m:v[n+1][pos2-n-1];
            v[x].push_back(id);
            printf("%lld\n",ans);
        }
        else{
            int pos=kth(root[n+1],1,l2,x);
            int ans=pos<=n?pos*m:v[n+1][pos-n-1];
            v[n+1].push_back(ans);
            printf("%lld\n",ans);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/LSlzf/p/11806338.html