Codechef The Ball And Cups question

Source: Internet
Author: User

 

The Ball And Cups

 

 

At the end of a busy day, The Chef and his assistants play a game together. the game is not just for fun but also used to decide who will have to clean the kitchen. the Chef is a Game Master, so his concern is how to manage the game but not how to win the game like his assistants do.

The game requires players to find the only ball under one ofNCups after their positions are changed in a special way. At the beginning of the game, The Chef placesNCups in a row and put a ball underC-Th cup from the left (the cups are numbered from1ToN). All players can see the initial position of the ball. Then Chef performsQFlip operations. Each flip operation is defined by two integersLAndRSuch that1 ≤ L ≤ R ≤ NAnd consists in reversing the segment[L, R]Of cups. Namely, Chef swapsL-Th andR-Th cups,(L + 1)-Th and(R? 1)-Th cups, and so on. after refreshing all the operations Chef asks his assistants to choose a cup that they think the ball is under it. who can guess the position of the ball will win the game, and of course, the others will have to clean the kitchen.

The Chef doesn' t want to check allNCups at the end of the game. He notes down the valueCAnd the pairs(L, R)And asked you, the mastered programmer, to determine the cup that contains the ball.

Input

The first line of the input contains a single integerT, Denoting the number of test cases. The descriptionTTest cases follows. The first line of each test case contains three space-separated integersN,CAndQ, Denoting the total number of cups, the initial position of the ball and the number of flip operations Chef will perform. Each of the followingQLines contains two space-separated integersLAndR, Denoting the ends of the segment of the current flip operation.

Output

For each test case output on a separate line the final position of the ball.

Constraints
  • 1T10
  • 1N100000(105)
  • 1CN
  • 1Q10000(104)
  • 1LRNExample
    Input:15 2 31 43 51 5Output:1

    It is also an example of constructing a mathematical formula.

    There are tens of thousands of inputs, so it is best to process the input so that the program can be over 0 ms.

    Note:

    1 trap-C will not be in the range of [L, R]

    2 fread processes the input. Remember to judge the final input condition-fread returns a zero length. Otherwise, although the input can be AC, There is a bug in the program.

    I use classes as functions, which can effectively reduce variable name conflicts.

     

    # Pragma once # include
       
        
    Class TheBallAndCups {int st, len; static const int BU_MAX = 5120; char buffer [BU_MAX]; char getFromBuffer () {if (st> = len) {len = fread (buffer, 1, BU_MAX, stdin); st = 0;} return buffer [st ++];} int scanInt () {char c = getFromBuffer (); while (c <'0' | '9' <c) {c = getFromBuffer ();} int num = 0; while ('0' <= c & c <= '9' & 0! = Len) // 0 must be added! = Len judge input end {num = (num <3) + (num <1) + (c-'0'); c = getFromBuffer ();} return num;} public: TheBallAndCups (): st (0), len (0) {int T = 0, N = 0, C = 0, L = 0, R = 0, Q = 0; T = scanInt (); while (T --) {N = scanInt (); C = scanInt (); Q = scanInt (); while (Q --) {L = scanInt (); R = scanInt (); if (C <L | R <C) continue; int M = L + (R-L)> 1); if (C <= M) {int diff = C-L; C = R-diff ;} else {int diff = R-C; C = L + diff ;}} printf ("% d \ n", C) ;}}; int theBallAndCups () {TheBallAndCups (); return 0 ;}
       


     

     

Related Article

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.