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 of the N cups after their positions are changed in a special way. At the beginning of the game, The Chef places N cups in a row and put a ball under the C-th
cup from the left (the cups are numbered from 1 to N). All players can see the initial position of the ball. Then Chef performs Q flip operations. Each flip operation is defined by two integers L and R such
that 1 ≤ L ≤ R ≤ N and consists in reversing the segment [L, R] of cups. Namely, Chef swaps L-th and R-th cups, (L+1)-th and (R−1)-th cups, and so on. After
performing 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 all the N cups at the end of the game. He notes down the value of C and 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 integer T, denoting the number of test cases. The description of Ttest cases follows. The first line of each test case contains three space-separated integers NC and Q,
denoting the total number of cups, the initial position of the ball and the number of flip operations Chef will perform. Each of the following Q lines contains two space-separated integers L and R, 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

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 100000 (105)
  • 1 ≤ C ≤ N
  • 1 ≤ Q ≤ 10000 (104)
  • 1 ≤ L ≤ R ≤ N

Example

Input:
1
5 2 3
1 4
3 5
1 5 Output:
1

也是个构造数学公式的样例。

这里是过万个输入。故此最优点理一下输入,使得程序能够0ms过。

注意:

1 陷阱 - C会不在[L, R]范围内

2 fread处理输入,记得推断最后输入结束的条件 - fread返回长度为零,否则。尽管能够AC。可是程序是有bug的。

我都使用类当做函数使用了,能够非常好降低变量名的冲突。

#pragma once
#include <stdio.h> 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 != len推断输入结束
{
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;
}

最新文章

  1. NK3C框架(MyBatis、Durid)连接SQL Server
  2. OpenCV基于傅里叶变换进行文本的旋转校正
  3. java 里面的string 和byte[] 怎么互转
  4. [转载]求平方根sqrt()函数的底层算法效率问题
  5. 利用iOS8新特性计算cell的实际高度
  6. IsPostBack and DropdownList.
  7. JavaScript里面三个等号和两个等号的区别
  8. js判断移动端与pc端
  9. 诡异错误一: ValueError: embedded null character
  10. hihoCoder 1051 补提交卡(贪心,枚举)
  11. 移动端添加横向滚动条&amp;隐藏
  12. Linux查看当前目录下所有子目录是否包含某个文件
  13. tensorflow object detection api graph rewriter
  14. Maven中groupId和artifactId的含义
  15. 如何删除PeopleSoft Process Definition
  16. POJ-2081 Recaman's Sequence
  17. Codeforces1106F 【BSGS】【矩阵快速幂】【exgcd】
  18. qt cmake
  19. Hadoop NameNode 高可用 (High Availability) 实现解析[转]
  20. Extjs4 中date时间格式的问题

热门文章

  1. dp洋洋散散的知识+code
  2. luoguP3232 [HNOI2013]游走 贪心 + 概率期望 + 高斯消元
  3. FIS构建工具学习(一)
  4. leetcode 587. Erect the Fence 凸包的计算
  5. mybatis源码分析(2)-----SqlSession创建
  6. java类中元素初始化顺序详解
  7. xcode的ios工程目录结构复习
  8. X86调用约定 calling convention
  9. Spring+JDBC实例
  10. Eclipse配置Struts2问题:ClassNotFoundException: org...dispatcher.ng.filter.StrutsPrepareAndExecuteFilter