[CF555C] Case of Chocolate - map
Description
给定一个 (n imes n(nleq 10^9)) 的方格阵列。 接下来,我们将从该方阵的副对角线开始进行一些操作。 操作 "(y x U)" 或 "(y x L)" 分别表示一个人从第 (x) 行第 (y) 列开始走,(U) 表示向上,(L) 表示向左。保证初始位置在副对角线上面。已经有人走过的不能再走,每次操作走到不能走为止。对于每次操作,输出能走几格。
Solution
对于每个向上操作,答案取决于它右边的第一个已经发生的操作,如果是向上那么它们到达的位置相同,如果是向左那么只能走到这个操作对应的位置
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define U 'U'
#define L 'L'
const int N = 505;
map<int, pair<int, int>> mp;
signed main()
{
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
mp[0] = {0, U};
mp[n + 1] = {0, L};
while (q--)
{
int x, y;
char ch;
cin >> x >> y >> ch;
auto it = mp.lower_bound(x);
if (it->first == x)
{
cout << 0 << endl;
}
else
{
if (ch == L)
it--;
int ans = abs(x - it->first);
if (it->second.second == ch)
{
ans += it->second.first;
}
mp[x] = {ans, ch};
cout << ans << endl;
}
}
}