题目链接:点击查看
题意:有一个n*n的矩形,给你对角线上的坐标,给你一个方向,问能走几个坐标,遇到之前已经经过的坐标就停止
题解:当给你一个坐标(x,y),方向向上时,他能所经过的点数是受y比他大的那些点影响的,并且比y大离y最近的那个点向上能到达哪个x值,他就能到达哪个x值,如果之前有点(x1,y1)向左,那也就说明这个点没法向上了,这个点的向上临界值就为x。向左的同理是受大于x的那些点影响的。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
set< pair<int,int> > s1, s2;
map< pair<int,int>, int> mp;
int n, q;
int main()
{
pair<int,int > pos;
int x, y;
char op[2];
scanf("%d %d", &n, &q);
s1.insert(make_pair(n+1, 0) ); // 设置基准
s2.insert(make_pair(n+1, 0) );
while(q--) {
scanf("%d %d %s", &y, &x, op);
if(mp[ make_pair(x,y) ] )
{
printf("0\n");
continue;
}
mp[ make_pair(x,y) ] = 1;
if(op[0] == 'U') {
pos = *s1.lower_bound( make_pair(y, 0) );
printf("%d\n", x - pos.second);
s1.insert( make_pair(y, pos.second) );
s2.insert( make_pair(x, y) );
} else {
pos = *s2.lower_bound( make_pair(x, 0) );
// cout<<pos.first<<" "<<pos.second<<endl;
printf("%d\n", y - pos.second);
s2.insert( make_pair(x, pos.second) );
s1.insert( make_pair(y, x));
}
}
return 0;
}