Codeforces Round #310 (Div. 1) D. Case of a Top Secret binary stl Application

Source: Internet
Author: User

Codeforces Round #310 (Div. 1) D. Case of a Top Secret binary stl Application

D. Case of a Top Secret
Time limit per test2 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
Andrewid the Android is a galaxy-famous detective. Now he is busy with a top secret case, the details of which are not subject to disclosure.

However, he needs help conducting one of the investors experiment. there are n pegs put on a plane, they are numbered from 1 to n, the coordinates of the I-th of them are (xi, limit 0 ). then, we tie to the bottom of one of the pegs a weight on a tight rope of length l (thus, its coordinates will be equal to (xi, Hangzhou-Hangzhou l ), where I is the number of the used peg ). then the weight is pushed to the right, so that it starts to rotate counterclockwise. at the same time, if the weight during rotation touches some of the other pegs, it then begins to rotate around that peg. suppose that each peg itself is very thin and does not affect the rope length while weight is rotating around it.

More formally, if at some moment the segment of the rope contains one or more pegs in addition to the peg around which the weight is rotating, the weight will then rotate around the farthermost one of them on a shorter segment of a rope. in particle, if the segment of the rope touches some peg by its endpoint, it is considered that the weight starts to rotate around that peg on a segment of the rope of length 0.

At some moment the weight will begin to rotate around some peg, without affecting the rest of the pegs. Andrewid interested in determining the number of this peg.

Andrewid prepared m queries containing initial conditions for pushing the weight, help him to determine for each of them, around what peg the weight will eventually rotate.

Input
The first line contains integers n and m (1 rows ≤ limit n, interval m ≤ limit 2 · 105)-the number of pegs and queries.

The next line contains n integers x1, limit x2, limit ,..., When xn (cost-limit 109 ≤ cost xi limit ≤ cost 109)-the coordinates of the pegs. It is guaranteed that the coordinates of all the pegs are distinct integers.

Next m lines contain the descriptions of the queries of pushing the weight, each consists of two integers ai (1 segment ≤ artificial ai segment ≤ artificial n) and li (1 hour ≤ hour li hour ≤ hour 109)-the number of the starting peg and the length of the rope.

Output
Print m lines, the I-th line shoshould contain the number of the peg around which the weight will eventually rotate after the I-th push.

Sample test (s)
Input
3 2
0 3 5
2 3
1 8
Output
3
2
Input
4
1 5 7 15
1 4
2 15
3 16
1 28
Output
2
4
3
1
Note
Picture to the first sample test:

Picture to the second sample test:

Note that in the last query weight starts to rotate around the peg 1 attached to a rope segment of length 0.
The question is, there are different n points on the number axis. Select a starting point and a starting length of rope to turn around these nails, and output the final turning around which nail!
When you go down, you need to find the point smaller than or equal to the longest distance and go up to the point greater than or equal to the longest distance. Therefore, you can simply use set to figure the chart, of course, you can also write binary data. This is not the key. This question mainly involves a major optimization.

The formula in is the formula in the code, which is easy to skip. See the figure below.

Obviously, it is always in the circle around xl xr, so using l % (l-l2) directly can greatly accelerate the processing speed. The overall complexity is k * n * logn!

# Define INF 9000000000 # define EPS (double) 1e-9 # define mod 1000000007 # define PI 3.14159265358979 //***************************** **************************************** * ********/# endif # define N 200050 # define M 100005 # define maxn 205 # define MOD limit 000007int n, x [N], m, l, s, xl, xr, lxl, lxr, l1, l2, yl, yr, role; set
  
   
Myset; set
   
    
: Iterator it; // applicable to positive and negative integer template
    
     
Inline bool scan_d (T & ret) {char c; int sgn; if (c = getchar (), c = EOF) return 0; // EOF while (c! = '-' & (C <'0' | c> '9') c = getchar (); sgn = (c = '-')? -1: 1; ret = (c = '-')? 0: (c-'0'); while (c = getchar (), c> = '0' & c <= '9 ') ret = ret * 10 + (c-'0'); ret * = sgn; return 1;} inline void out (int x) {if (x> 9) out (x/10); putchar (x % 10 + '0');} int main () {while (S2 (n, m )! = EOF) {myset. clear (); for (int I = 1; I <= n; I ++) {scan_d (x [I]); myset. insert (make_pair (x [I], I);} FI (m) {scan_d (rows); scan_d (l); lxr = lxl =-1; s = x [distinct]; while (true) {yr = s + l; it = myset. upper_bound (make_pair (yr, N + 1); it --; xr = x [it-> second]; l1 = yr-xr; yl = xr-(yr-xr); it = myset. lower_bound (make_pair (yl,-1); xl = x [it-> second]; l2 = xl-yl; if (l2 = l) {out (it-> second); putchar (''); break;} else {if (lxl = xl & lxr = xr) {l = l % (l-l2);} else {l = l2;} s = xl; lxl = xl; lxr = xr ;}}} return 0 ;}
    
   
  

 

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.