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;
}

codechef The Ball And Cups题解的更多相关文章

  1. CodeChef November Challenge 2013 部分题解

    http://www.codechef.com/NOV13 还在比...我先放一部分题解吧... Uncle Johny 排序一遍 struct node{ int val; int pos; }a[ ...

  2. codechef February Challenge 2018 简要题解

    比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...

  3. codechef Row and Column Operations 题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  4. codechef January Lunchtime 2017简要题解

    题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...

  5. codechef January Challenge 2017 简要题解

    https://www.codechef.com/JAN17 Cats and Dogs 签到题 #include<cstdio> int min(int a,int b){return ...

  6. HDU 1556 Color the Ball 线段树 题解

    本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...

  7. codechef Sums in a Triangle题解

    Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...

  8. codechef Little Elephant and Permutations题解

    The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...

  9. codechef Little Elephant and Bombs题解

    The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...

随机推荐

  1. 【BZOJ 4568】 4568: [Scoi2016]幸运数字 (线性基+树链剖分+线段树)

    4568: [Scoi2016]幸运数字 Description A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个 幸运数字,以纪念碑的形 ...

  2. [BJOI2017]魔法咒语 --- AC自动机 + 矩阵优化

    bzoj 4860   LOJ2180   洛谷P3175 [BJOI2017]魔法咒语 题目描述: Chandra 是一个魔法天才. 从一岁时接受火之教会洗礼之后,Chandra 就显示出对火元素无 ...

  3. Linux的十个最危险的命令

    Linux命令行佷有用.很高效,也很有趣,但有时候也很危险,尤其是在你不确定你自己在正在做什么时候. 这篇文章将会向你介绍十条命令,但你最好不要尝试着去使用. 当然,以下命令通常都是在root权限下才 ...

  4. 三周学会小程序第四讲:Heroku 绑定 Github 自动部署

    这一讲是根据读者的反馈补充的一个讲解,好多读者反应安装 Heroku-cli 遇到问题,或者是操作繁琐,其实上一讲中提到的 Heroku 只是为了免费部署,而安装 Heroku-CLI只是为了部署,所 ...

  5. poj 2001 trie

    第一道trie 还需要写题来建立自己的代码习惯. #include <cstdio> #include <vector> #include <algorithm> ...

  6. Unity Pivot/Center与Local/Global总结

    Untiy左上角有两个按钮  Pivot/Center 和 Local/Global  它们叫做 变换Gizmo工具 Pivot/Center:现实游戏对象的轴心参考点.Center为以所有选中物体所 ...

  7. Android 按钮长按下去重复执行某个动作,放开后停止执行动作

    Android开发中,常遇到一种需求,即按钮长按下去重复执行某个动作,放开后停止执行动作.网上找了许多代码,都没有适合的,于是自己动手写了一个. 基本思路是:首先设置一个标识变量,用于标识是否处于按下 ...

  8. Shell基础学习(一) Shell简介

    Shell是什么? Shell是C语言编写的一种程序,用于用户与linux操作系统交互:Shell既是命令语言,又是程序设计语言. Shell脚本是什么? Shell脚本是用Shell编写的脚本程序. ...

  9. Object-C编程基础总结:

    1,nil,NULL,NSNull:nil用来给对象附值,object—c里允许对象为空,空对象也可以接受消息.但是不允许指针为空,NULL是给任何指针附值的.所以NULL只在C或C++里才用.NSN ...

  10. WPF中的导航框架(一)——概述

    有的时候,我们需要一个支持页面跳转的UI,例如文件浏览器,开始向导等.对于这样的界面,简单的可以使用ContentControl + ContentTemplateSelector的方式来实现,但是有 ...