标签:
思路:对于每个点而言、只与它相邻的两个点有关系、所以可以用stl或者线段树来找到它的相邻点、
代码:187ms(开挂之后貌似是最快的- -)
#include <cstdio> #include <map> #include <algorithm> using namespace std; const int N = 200000 + 1; int x[N], y[N], t[N]; //适用于正负整数 template <class T> inline bool scan_d(T &ret) { char c; int sgn; if(c = getchar(),c == EOF) return 0; //EOF while(c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while(c = getchar(),c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } inline void out(int x) { if(x > 9) out(x / 10); putchar(x % 10 + '0'); } int main() { int n, q; scanf("%d%d", &n, &q); x[q] = y[q] = 0; map <int, int> mp; mp[0] = q; mp[n + 1] = q; for (int i = 0; i < q; ++ i) { char buffer[2]; scan_d(x[i]); scan_d(y[i]); scanf("%s",buffer); t[i] = *buffer == 'U'; map <int, int>::iterator iterator; iterator = mp.lower_bound(x[i]); if (x[i] == iterator->first) { puts("0"); continue; } if (!t[i]) iterator --; int j = iterator->second; mp[x[i]] = i; if (t[i]) { out(y[i] - y[j]); putchar('\n'); y[i] = y[j]; } else { out(x[i] - x[j]); putchar('\n'); x[i] = x[j]; } } return 0; }
Codeforces Round #310 (Div. 1) C Case of Chocolate
标签:
原文地址:http://blog.csdn.net/a1dark/article/details/46672073