题目链接:https://codeforces.com/gym/102028/problem/C

Lewis likes playing chess. Now he has n rooks on the chessboard with $n$ rows and $n$ columns. All rows of the chessboard are labelled with $1$ through $n$ from top to bottom. All columns of the chessboard are labelled with $1$ through $n$ from left to right. All rooks are labelled with $1$ through $n$ as well. At the very beginning, each row or column contains exactly one rook. However, Lewis allows a square with two or more rooks during the game.

Now he starts to play a game named Supreme Command. He will provide several supreme commands to all rooks. All possible commands are in the following four different formats.

L $k$: Every rook moves k squares to the left;
R $k$: Every rook moves k squares to the right;
U $k$: Every rook moves k squares upward;
D $k$: Every rook moves k squares downward.
For a Supreme Command with given number $k$, if a rook, after moving less than $k$ squares, had arrived at a boundary (which locates in the left-most columns, right-most column, top row or bottom row) such that the rook cannot move further, it would stay there and not move outside the chessboard.

He will also have several queries about rooks. The only two possible formats about queries are listed as follows.

? $k$: Ask the current position of the $k$-th rook;
!: Ask how many pairs of rooks there are currently located in the same square.
Your task in this problem is to answer these queries correctly.

Input

The input contains several test cases, and the first line contains a positive integer $T$ indicating the number of test cases which is up to $1000$.

For each test case, the first line contains two integers $n$ which is described as above, and m indicating the total number of supreme commands and queries, where $1 \le n,m \le 3 \times 10^5$.

Each of the following $n$ lines contains two integers $x$ and $y$, describing a rook located at the intersection of the $x$-th row and the $y$-th column, where $1 \le x,y \le n$.

Then the following $m$ lines describe all Supreme Commands and queries in chronological order, where all given parameters $k$ are integers ranged from $1$ to $n$.

We guarantee that the sum of $n$ and the sum of $m$ in all test cases are up to $10^6$ respectively.

Output

For each test case, output several lines to answer all queries.

For each query of the first type ("? $k$"), output a line containing two integers $x$ and $y$, which indicate the current position of the $k$-th rook is the intersection of the $x$-th row and the $y$-th column. You should output exactly one whitespace between these two numbers.

For each query of the second type ("!"), output a line containing an integer which indicates the number of pairs of rocks that are currently located in the same square.

Example

Input
1
4 9
3 4
2 1
4 2
1 3
L 2
? 1
? 2
R 1
? 1
? 3
!
U 3
!
Output
3 2
2 1
3 3
4 2
0
3

Note

The following figures illustrate the chessboard at the beginning and after each Supreme Commands in the sample case.

题意:

给定一个 $n \times n$ 的棋盘,记左上角的坐标为 $(1,1)$,右下角的坐标为 $(n,n)$。棋盘上有 $n$ 个编号 $1 \sim n$ 的棋子,每一行或者每一列都有且仅有一个棋子。

给出操作序列,操作有以下三种:

  1. $L(R,U,D) \; k$:所有棋子向左(右、上、下)移动 $k$ 格,棋子可以重叠,若操作使棋子向边界外移动则实际上停止不动。
  2. $? \; k$:查询编号 $k$ 的棋子现在的位置。
  3. $!$:查询现有多少对棋子在同一个格子里。

题解:

这个题,由于可以棋子可以重叠的缘故,不难想到行和列是相互独立的。不妨分开来看,那么问题就从二维的变成了两个一维的合成。

不妨仅仅考虑列坐标,也就是说,在一条横轴上 $1 \sim n$ 这 $n$ 个坐标点上,都放置了一枚棋子。

先考虑怎么查询第 $k$ 个棋子的位置,不妨先记录下其初始位置 $pos_k$,又记 $[L,R]$ 是全体棋子位置的“紧实的”左右边界。

那么,由于移动棋子的操作是所有棋子一起移动。因此,我们不妨把左移所有棋子操作看做把棋盘的左边界 $L$ 往右推动,而把右移所有棋子操作看做把棋盘的右边界 $R$ 往左推动。当然,这样一来 $[L,R]$ 和棋子实际所处区间显然是存在偏差的,可以用一个变量 $\Delta$ 来记录这个偏差。

这样一来,由于 $L$ 只能右移,$R$ 只能左移。也就是说,一颗棋子一旦碰到过边界,就不会再离开边界。因此,我们根据初始位置 $pos_k$,以及目前全体棋子的左右边界 $[L,R]$ 就能判断第 $k$ 枚棋子是否处在边界。当然,我们知道 $[L,R]$ 一旦被往里推动一段距离之后(即 $1 < L \le R < n$),一定范围内的左右移动棋子是不会使得 $[L,R]$ 变小的,这个时候只需要调整一下 $\Delta$ 即可。

接下来,要考虑怎么查询有多少对棋子处在同一个格子内。这个时候不妨重新考虑二维原问题,也就是说现在有上下左右四条边界往里压棋子。那么很显然的,棋子的重叠只可能发生在左上、右上、左下、右下四个角上,且棋子一旦重叠就再也不可能分开。

所以,我们只需要用 $cUL,cUR,cDL,cDR$ 分别维护撞到过上左角、上右角、下左角、下右角的棋子数目即可,并且控制所有棋子只会被计数一次。

这样一来,只需要特判一下所有棋子被压成一行或一列甚至一格的情况,剩下来直接求 $C_{cUL}^2+C_{cUR}^2+C_{cDL}^2+C_{cDR}^2$ 即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=3e5+; ll C[maxn]; //C[n,2]
void prework() {
for(int i=;i<maxn;i++) C[i]=(ll)i*(i-)/;
} int n,m;
void Cnt(int id);
struct Sol
{
int pos[maxn],id[maxn];
int l,r,d;
void InitLR() {
l=, r=n, d=;
}
void InitCnt() {
Cnt(id[]), Cnt(id[n]);
}
void SetPos(int p,int i) {
pos[i]=p, id[p]=i;
}
void Left(int k)
{
while(l<r && l+d-k<) Cnt(id[++l]);
if(l+d-k<) d=-l;
else d-=k;
}
void Right(int k)
{
while(l<r && r+d+k>n) Cnt(id[--r]);
if(r+d+k>n) d=n-r;
else d+=k;
}
int Ask(int id)
{
if(pos[id]<=l) return l+d;
if(pos[id]>=r) return r+d;
return pos[id]+d;
}
}A,B; int cLL,cLR,cRL,cRR;
bool vis[maxn];
void Cnt(int id)
{
if(vis[id]) return;
else if(A.pos[id]<=A.l && B.pos[id]<=B.l) cLL++, vis[id]=;
else if(A.pos[id]<=A.l && B.pos[id]>=B.r) cLR++, vis[id]=;
else if(A.pos[id]>=A.r && B.pos[id]<=B.l) cRL++, vis[id]=;
else if(A.pos[id]>=A.r && B.pos[id]>=B.r) cRR++, vis[id]=;
} ll Query()
{
//printf("上左=%d 上右=%d 下左=%d 下右=%d\n",cLL,cLR,cRL,cRR);
if(A.l==A.r && B.l==B.r) return C[cLL+cLR+cRL+cRR];
else if(A.l==A.r) return C[cLL+cRL]+C[cLR+cRR];
else if(B.l==B.r) return C[cLL+cLR]+C[cRL+cRR];
else return C[cLL]+C[cLR]+C[cRL]+C[cRR];
} int main()
{
prework();
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
A.InitLR(); B.InitLR();
for(int i=,x,y;i<=n;i++)
{
scanf("%d%d",&x,&y);
A.SetPos(x,i);
B.SetPos(y,i);
} cLL=cLR=cRL=cRR=;
memset(vis,,(n+)*sizeof(bool));
A.InitCnt(); B.InitCnt(); char op[]; int k;
while(m--)
{
scanf("%s",op);
if(op[]=='!') printf("%I64d\n",Query());
else
{
scanf("%d",&k);
if(op[]=='U') A.Left(k);
if(op[]=='D') A.Right(k);
if(op[]=='L') B.Left(k);
if(op[]=='R') B.Right(k);
if(op[]=='?') printf("%d %d\n",A.Ask(k),B.Ask(k));
}
}
}
}

注:有很多是参考标程的QAQ,要不然感觉自己估计是写不出来的QAQ。

Gym 102028C - Supreme Command - [思维题][2018-2019 ACM-ICPC Asia Jiaozuo Regional Contest Problem C]的更多相关文章

  1. Gym - 101981K The 2018 ICPC Asia Nanjing Regional Contest K.Kangaroo Puzzle 暴力或随机

    题面 题意:给你1个20*20的格子图,有的是障碍有的是怪,你可以每次指定上下左右的方向,然后所有怪都会向那个方向走, 如果2个怪撞上了,就融合在一起,让你给不超过5w步,让所有怪都融合 题解:我们可 ...

  2. Gym - 101981M The 2018 ICPC Asia Nanjing Regional Contest M.Mediocre String Problem Manacher+扩增KMP

    题面 题意:给你2个串(长度1e6),在第一个串里找“s1s2s3”,第二个串里找“s4”,拼接后,是一个回文串,求方案数 题解:知道s1和s4回文,s2和s3回文,所以我们枚举s1的右端点,s1的长 ...

  3. Gym - 101981G The 2018 ICPC Asia Nanjing Regional Contest G.Pyramid 找规律

    题面 题意:数一个n阶三角形中,有多少个全等三角形,n<=1e9 题解:拿到题想找规律,手画开始一直数漏....,最后还是打了个表 (打表就是随便定个点为(0,0),然后(2,0),(4,0), ...

  4. Gym - 101981I The 2018 ICPC Asia Nanjing Regional Contest I.Magic Potion 最大流

    题面 题意:n个英雄,m个怪兽,第i个英雄可以打第i个集合里的一个怪兽,一个怪兽可以在多个集合里,有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 题解:显 ...

  5. Gym - 101981D The 2018 ICPC Asia Nanjing Regional Contest D.Country Meow 最小球覆盖

    题面 题意:给你100个三维空间里的点,让你求一个点,使得他到所有点距离最大的值最小,也就是让你找一个最小的球覆盖掉这n个点 题解:红书模板题,这题也因为数据小,精度也不高,所以也可以用随机算法,模拟 ...

  6. Gym - 101981J The 2018 ICPC Asia Nanjing Regional Contest J.Prime Game 计数

    题面 题意:1e6的数组(1<a[i]<1e6),     mul (l,r) =l × (l+1) ×...× r,  fac(l,r) 代表 mul(l,r) 中不同素因子的个数,求s ...

  7. Gym - 101981A The 2018 ICPC Asia Nanjing Regional Contest A.Adrien and Austin 简单博弈

    题面 题意:一堆有n个石子,编号从1⋯N排成一列,两个人Adrien 和Austin玩游戏,每次可以取1⋯K个连续编号的石子,Adrien先手,谁不能取了则输 题解:k==1时,显然和n奇偶相关,当k ...

  8. Gym 101981G - Pyramid - [打表找规律][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem G]

    题目链接:http://codeforces.com/gym/101981/attachments The use of the triangle in the New Age practices s ...

  9. Gym 101981I - Magic Potion - [最大流][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem I]

    题目链接:http://codeforces.com/gym/101981/attachments There are n heroes and m monsters living in an isl ...

随机推荐

  1. datagrip离线安装驱动jar

    问题描述: datagrip离线安装驱动,在线的安装驱动一般默认安装在当前用户下.DataGrip xxxx 问题解决: 在线在线下载驱动jar,复制jar到内网离线环境 01.外网已经存在的jar提 ...

  2. 解决Visual Studio调试突然变慢卡死的问题

    最开始摸不到头脑,之前还能好好调试的啊.后来在VS的调试菜单的符号选项里面发现了系统环境变量_NT_SYMBOL_PATH 的值为:srv*c:\symbols*http://msdl.microso ...

  3. AngularJS 使用ng-repeat报错

    [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq ...

  4. mac 10.12 sierra 机械键盘+ratm可编程鼠标记录

      系统:mac 10.12 sierra 键盘:机械键盘 鼠标:mad catz ratm 在mac 10.11/10.12 之前: 机械键盘:一般的机械键盘在mac上使用, alt 和 win 键 ...

  5. Jmeter执行python脚本函数使用说明

    一.下载地址插件百度网盘下载地址:https://pan.baidu.com/s/1SvJjyThsXYryXuEEg9rm3g提取码:q9hd 二.使用说明1.将下载的jmeter-function ...

  6. Generate google sitemap xml

    方式一. XNamespace xNamespace = "http://www.sitemaps.org/schemas/sitemap/0.9"; XNamespace xht ...

  7. Canvas入门到高级详解(下)

    四. Canvas 开发库封装 4.1 封装常用的绘制函数 4.1.1 封装一个矩形 //思考:我们用到的矩形需要哪些绘制的东西呢? 矩形的 x.y坐标 矩形的宽高 矩形的边框的线条样式.线条宽度 矩 ...

  8. CTF线下防御战 — 让你的靶机变成“铜墙铁壁”

    本文首发安全客,未经允许禁止转载.原文链接 一. 前言 随着CTF的普及,比赛的形式也有了越来越多的花样,对于线下赛来说,开始出现了安全加固或者防御战之类的环节,亦或者因为拿下靶机后不希望其他攻击者进 ...

  9. 删除maven仓库中的LastUpdated文件

    转自:http://www.oschina.net/code/snippet_151849_49131 @echo off rem create by sunhao(sunhao.java@gmail ...

  10. 网络I/O模型---同步异步阻塞非阻塞之惑

    网络I/O模型 人多了,就会有问题.web刚出现的时候,光顾的人很少.近年来网络应用规模逐渐扩大,应用的架构也需要随之改变.C10k的问题,让工程师们需要思考服务的性能与应用的并发能力. 网络应用需要 ...