Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <ctime>
- #include <algorithm>
- #include <cstdlib>
- #include <vector>
- #include <iostream>
- #include <cmath>
- #include <set>
- #include <map>
- #include <cassert>
- #include <memory.h>
- using namespace std;
- const int K = 1000;
- const int M = 5122;
- int f[M][M];
- void add(int x, int y, int z) {
- for (int i = x; i < M; i |= i + 1) {
- for (int j = y; j < M; j |= j + 1) {
- f[i][j] += z;
- }
- }
- }
- int getsum(int x, int y) {
- int ret = 0;
- for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
- for (int j = y; j >= 0; j = (j & (j + 1)) - 1) {
- ret += f[i][j];
- }
- }
- return ret;
- }
- int main() {
- int X;
- scanf("%d", &X);
- int T;
- scanf("%d", &T);
- set<pair<int, int>> all;
- for (int i = 0; i < T; i++) {
- int c = getchar();
- while (c <= 32) c = getchar();
- int x, y;
- scanf("%d%d", &x, &y);
- --x;
- --y;
- if (c == 'N') {
- if (all.find(make_pair(x, y)) != all.end()) assert(false);
- all.insert(make_pair(x, y));
- if (x < X)
- add(x - y + K - 1, x + y, 1);
- } else if (c == 'L') {
- all.erase(make_pair(x, y));
- if (x < X)
- add(x - y + K - 1, x + y, -1);
- } else if (c == 'S') {
- if (x >= X || y + 1 >= K || all.find(make_pair(x, y)) != all.end() || all.find(make_pair(x, y + 1)) != all.end()) {
- puts("No");
- } else {
- printf("%d\n", getsum(x - y + K - 1, x + y) + getsum(x - (y + 1) + K - 1, x + (y + 1)));
- }
- }
- }
- int m1 = 1 << 30;
- int m2 = 1 << 30;
- for (int i = 0; i < K; i++) {
- int x = X;
- int y = i;
- pair<int, int> p(x, y);
- if (all.find(p) == all.end()) {
- int cur = getsum(x - y + K - 1, x + y);
- if (cur < m1) {
- m2 = m1;
- m1 = cur;
- } else if (cur < m2) {
- m2 = cur;
- }
- } else {
- add(x - y + K - 1, x + y, 1);
- }
- if (i == K - 1 && (m1 == (1 << 30) || m2 == (1 << 30))) {
- ++X;
- i = -1;
- }
- }
- printf("%d\n", m1 + m2);
- }
Advertisement
Add Comment
Please, Sign In to add comment