niyaznigmatullin

Untitled

Feb 2nd, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <cstdio>
  2. #include <ctime>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <iostream>
  7. #include <cmath>
  8. #include <set>
  9. #include <map>
  10. #include <cassert>
  11. #include <memory.h>
  12.  
  13. using namespace std;
  14.  
  15. const int K = 1000;
  16. const int M = 5122;
  17.  
  18. int f[M][M];
  19.  
  20. void add(int x, int y, int z) {
  21.   for (int i = x; i < M; i |= i + 1) {
  22.     for (int j = y; j < M; j |= j + 1) {
  23.       f[i][j] += z;
  24.     }
  25.   }
  26. }
  27.  
  28. int getsum(int x, int y) {
  29.   int ret = 0;
  30.   for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
  31.     for (int j = y; j >= 0; j = (j & (j + 1)) - 1) {
  32.       ret += f[i][j];
  33.     }
  34.   }
  35.   return ret;
  36. }
  37.  
  38. int main() {
  39.   int X;
  40.   scanf("%d", &X);
  41.   int T;
  42.   scanf("%d", &T);
  43.   set<pair<int, int>> all;
  44.   for (int i = 0; i < T; i++) {
  45.     int c = getchar();
  46.     while (c <= 32) c = getchar();
  47.     int x, y;
  48.     scanf("%d%d", &x, &y);
  49.     --x;
  50.     --y;
  51.     if (c == 'N') {
  52.       if (all.find(make_pair(x, y)) != all.end()) assert(false);
  53.       all.insert(make_pair(x, y));
  54.       if (x < X)
  55.         add(x - y + K - 1, x + y, 1);
  56.     } else if (c == 'L') {
  57.       all.erase(make_pair(x, y));
  58.       if (x < X)
  59.         add(x - y + K - 1, x + y, -1);    
  60.     } else if (c == 'S') {
  61.       if (x >= X || y + 1 >= K || all.find(make_pair(x, y)) != all.end() || all.find(make_pair(x, y + 1)) != all.end()) {
  62.         puts("No");
  63.       } else {
  64.         printf("%d\n", getsum(x - y + K - 1, x + y) + getsum(x - (y + 1) + K - 1, x + (y + 1)));
  65.       }
  66.     }
  67.   }
  68.   int m1 = 1 << 30;
  69.   int m2 = 1 << 30;
  70.   for (int i = 0; i < K; i++) {
  71.     int x = X;
  72.     int y = i;
  73.     pair<int, int> p(x, y);
  74.     if (all.find(p) == all.end()) {
  75.       int cur = getsum(x - y + K - 1, x + y);
  76.       if (cur < m1) {
  77.         m2 = m1;
  78.         m1 = cur;
  79.       } else if (cur < m2) {
  80.         m2 = cur;
  81.       }
  82.     } else {
  83.       add(x - y + K - 1, x + y, 1);
  84.     }
  85.     if (i == K - 1 && (m1 == (1 << 30) || m2 == (1 << 30))) {
  86.       ++X;
  87.       i = -1;
  88.     }
  89.   }
  90.   printf("%d\n", m1 + m2);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment