luoguP3960 [noip2017] Queue (tree-like array)

Source: Internet
Author: User

www.cnblogs.com/shaokele/

luoguP3960 [noip2017] Queue

Time Limit:2 Sec
Memory limit:512 MB

Description

Sylvia is a woman who loves to learn ♂ children.
  
A while ago, Sylvia participated in the school military training. As we all know, military training needs to stand square.
  
In the phalanx where the Sylvia\ (n \times m\)Students, the number of rows in the Phalanx is\ (n\), the number of columns is\ (m\)。
  
In order to facilitate the management, the instructor at the beginning of the training, according to the former, from left to right in order to the students in the Phalanx from 1 to\ (n \times m\)The number is numbered (see the following example). That is: At the beginning, the first\ (i\)Line No.\ (j\)The number of the students in the column is\ ((i-1) \times m + j\)。
  
However, in the practice of the square, there are often students because of a variety of things need to leave. In a day, there was altogether a\ (q\)Such a departure event. You can use a few pairs for each time you leave the event.\ ((x, y) (1 \le x \le N, 1 \le y \le m) \)Description, indicating the first\ (x\)Line No.\ (y\)List of students.
  
There was a vacancy in the team after the students had dropped out. For the team to be neat, the instructors will order such two instructions in turn:
  
Align to the left. At this point the first column remained motionless and all the students left to fill the vacancy. It is not difficult to find out after this instruction that the vacancy is in the first\ (x\)Line No.\ (m\)Column.
  
Forward. At this point the first line remained motionless, and all the students filled the vacancies forward. It is not difficult to find out after this instruction that the vacancy is in the first\ (n\)Line No.\ (m\)Column.
  
The instructor stipulated that two or more students could not leave at the same time. That is, the next student will be able to leave after the former student has returned. Therefore, in each of the students leaving the team to rejoin, there is only the first\ (n\)Line No.\ (m\)Make a vacancy, and the student union naturally fills this position.
  
Because the station square is really boring, so Sylvia want to calculate every time the departure event, the number of the students who leave the class.
  
Note: Each student's number will not be changed with the occurrence of the departure event, the number of students in the matrix after the departure event may be disorderly sequence.
 

Input

Enter a common \ (q+1\) line.
  
The 1th line contains 3 positive integers separated by spaces \ (n, M, q\) , which indicates that the square size is a \ (n\) row \ (m\) column, and a total of \ (q\) occurrences occurred.
  
The next \ (q\) line describes the \ (q\) event in the order in which the event occurred. Each line is two integers \ (x, y\) , separated by a space, indicating that the student who dropped out of the departure event was in column x\ ( y\) .
 

Output

In the order in which events are entered, each event outputs an integer that represents the number of the dropped students in the outgoing event.
 

Sample Input

2 2 3
  
1 1
  
2 2
  
0 S
 

Sample Output

1
  
1
  
4
  

HINT

"Input and Output Example 1 description"
  
The process of queuing as shown, each line describes an event. In the first event, the student with number 11 is removed, and the vacancy is in the first column of the first row. Then all the students to the left, when the number of students numbered 2 2 left one step, the vacancy moved to the first row of the second column. Then all the students are marked up, then the classmate numbered 4 4 up one step, then the vacancy moves to the second row of the second column. The last student numbered 11 returned to fill the vacancy.
  
"Data size and conventions"
  
Data guarantees each event satisfies \ (1 \le x \le n,1 \le y \le m\)
  

title Address: luoguP3960 [noip2017] Queue Main topic:

A \ (n*m\) queue with the number of rows ( i*j\) numbered \ ((i-1) *m+j\)
Remove the number of \ (x\) rows \ (y\) column at a time and delete it from the queue.
Move the number on the right to the left.
Move the number of the last column up
Insert result in section \ (n\) line \ (m\) column
Repeat the process above
  

The following:

  Put code first, later pits
  

AC Code

#include <cstdio> #include <vector> #include <algorithm> #define LL long longusing namespace Std;const int n=300005;int n,m,q,len;int x[n],y[n],sol[n];int t[n+n];ll num[n+n];vector<ll> a[N];inline int read () {int x=0    , F=1;char Ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} struct node{int x,y,id;}    A[n];bool CMP (node A,node b) {if (a.x!=b.x) return a.x<b.x; return a.id<b.id;} void Modify (int k,int tot) {for (int i=k;i<=len;i+=i& (-i)) T[i]+=tot;}    int query (int k) {int res=0;    for (int i=k;i;i-=i& (-i)) res+=t[i]; return res;}    int search (int tot) {int l=1,r=len,res=-1;        while (l<=r) {int mid= (L+R) >>1;            if (query (mid) >=tot) {res=mid;        R=mid-1;    }else l=mid+1; } return res; int main () {n=read (); M=read ();    Q=read ();    Len=max (n,m) +q; for (int i=1;i< =q;i++) {a[i].x=x[i]=read ();        A[i].y=y[i]=read ();    A[i].id=i;    } sort (a+1,a+q+1,cmp);    for (int i=1;i<=len;i++) modify (i,1);    int pre=1; for (int i=2;i<=q+1;i++) if (a[i].x!=a[i-1].x) {for (int j=pre;j<i;j++) if (a[j].y<m                    ) {Sol[a[j].id]=search (A[J].Y);                Modify (sol[a[j].id],-1);            } for (int j=pre;j<i;j++) if (a[j].y<m) modify (sol[a[j].id],1);        Pre=i;        } for (int i=1;i<=q;i++) {ll ans;        int K=search (x[i]);        if (k<=n) ans= (LL) k*m;        else Ans=num[k-n];        Modify (k,-1);            if (y[i]<m) {a[x[i]].push_back (ans);            if (sol[i]<m) ans= (LL) (x[i]-1) *m+sol[i];        else ans=a[x[i]][sol[i]-m];        } Num[i]=ans;    printf ("%lld\n", ans); } return 0;}

luoguP3960 [noip2017] Queue (tree-like array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.