记录编号 505194 评测结果 AAAAAAEEEE
题目名称 [BOI 2007] 摩基亚Mokia 最终得分 60
用户昵称 GravatarGKxx 是否通过 未通过
代码语言 C++ 运行时间 2.523 s
提交时间 2018-08-11 21:46:51 内存使用 20.14 MiB
显示代码纯文本
  1. #include <cctype>
  2. #include <cstdio>
  3. #include <climits>
  4. #include <algorithm>
  5.  
  6. namespace io
  7. {
  8. #define BUF_SIZE 5000010
  9. char buf[BUF_SIZE]; int cur = BUF_SIZE; FILE *in = stdin;
  10. inline char gnc()
  11. {
  12. if (cur == BUF_SIZE) { fread(buf, BUF_SIZE, 1, in); cur = 0; }
  13. return buf[cur++];
  14. }
  15. template <typename T>
  16. inline void read(T &dx)
  17. {
  18. dx = 0;
  19. int yc = gnc();
  20. bool nega = false;
  21. while (yc < '0' || yc > '9') { nega = (yc == '-' ? true : nega); yc = gnc(); }
  22. while (yc >= '0' && yc <= '9') { dx = (T)(dx * 10 + yc - '0'); yc = gnc(); }
  23. if (nega) { dx = -dx; }
  24. }
  25. void gc(char &a)
  26. {
  27. do a = gnc(); while (!isgraph(a));
  28. }
  29. void gss(char *c)
  30. {
  31. *c = gnc();
  32. while (!isgraph(*c)) *c = gnc();
  33. while (isgraph(*c)) *++c = gnc();
  34. *c = 0;
  35. }
  36. }
  37. using io::read;
  38.  
  39. #define npt NULL
  40. #define rep(I, A, B) for (int I = (A); I <= (B); ++I)
  41. #define rrep(I, A, B) for (int I = (A); I >= (B); --I)
  42.  
  43. #ifdef WIN32
  44. #define OUTLL "%I64d"
  45. #else
  46. #define OUTLL "%lld"
  47. #endif
  48.  
  49. inline int lowbit(int x) { return x & -x; }
  50.  
  51. struct Command {
  52. int q, x, y, x1, y1, x2, y2;
  53. int a;
  54. };
  55.  
  56. const int maxq = 2e5 + 100;
  57. Command cmd[maxq];
  58. int exes[maxq << 2];
  59. int n, m, tot;
  60.  
  61. struct Node {
  62. int loc;
  63. int value, sum;
  64. Node *ch[2], *fa;
  65. explicit Node(int l = 0, int v = 0)
  66. : loc(l), value(v), sum(v), fa(npt) { ch[0] = ch[1] = npt; }
  67. int isc(int c) const { return fa && fa->ch[c] == this; }
  68. int isc() const { return fa ? isc(1) : -1; }
  69. };
  70.  
  71. inline void update(Node*& x) {
  72. x->sum = x->value;
  73. rep(i, 0, 1) if (x->ch[i])
  74. x->sum += x->ch[i]->sum;
  75. }
  76. inline void rotate(Node*& x) {
  77. if (!x->fa) return;
  78. int d = x->isc();
  79. Node* f = x->fa;
  80. if (f->isc() != -1)
  81. f->fa->ch[f->isc()] = x;
  82. x->fa = f->fa;
  83. f->ch[d] = x->ch[d ^ 1];
  84. if (x->ch[d ^ 1]) x->ch[d ^ 1]->fa = f;
  85. x->ch[d ^ 1] = f;
  86. f->fa = x;
  87. update(f);
  88. update(x);
  89. }
  90. inline void splay(Node*& x, Node*& root) {
  91. if (x == root) return;
  92. Node* p = root->fa;
  93. while (x->fa != p) {
  94. Node* f = x->fa;
  95. if (f->fa == p) rotate(x);
  96. else {
  97. if (f->isc() == x->isc())
  98. rotate(f);
  99. else rotate(x);
  100. rotate(x);
  101. }
  102. }
  103. root = x;
  104. }
  105. inline void insertSplay(Node*& root, int pos, int val) {
  106. if (!root) {
  107. root = new Node(pos, val);
  108. return;
  109. }
  110. Node* curr = root;
  111. while (0207) {
  112. curr->sum += val;
  113. if (pos == curr->loc) {
  114. curr->value += val;
  115. return;
  116. }
  117. int d = (pos > curr->loc);
  118. if (curr->ch[d]) curr = curr->ch[d];
  119. else {
  120. curr->ch[d] = new Node(pos, val);
  121. curr->ch[d]->fa = curr;
  122. curr = curr->ch[d];
  123. splay(curr, root);
  124. return;
  125. }
  126. }
  127. }
  128. inline int querySplay(Node* curr, int x) {
  129. if (!curr) return 0;
  130. int ret = 0;
  131. while (curr) {
  132. if (x < curr->loc) curr = curr->ch[0];
  133. else {
  134. if (curr->ch[0])
  135. ret += curr->ch[0]->sum + curr->value;
  136. else
  137. ret += curr->value;
  138. curr = curr->ch[1];
  139. }
  140. }
  141. return ret;
  142. }
  143.  
  144. Node *root[maxq << 2];
  145.  
  146. inline void addBit(int x, int y, int v) {
  147. for (; x <= tot; x += lowbit(x)) insertSplay(root[x], y, v);
  148. }
  149. inline int queryBit(int x, int y) {
  150. int ans = 0;
  151. for (; x; x -= lowbit(x)) ans += querySplay(root[x], y);
  152. return ans;
  153. }
  154.  
  155. int main() {
  156. freopen("mokia.in", "r", stdin);
  157. freopen("mokia.out", "w", stdout);
  158. { int zero; read(zero); }
  159. read(n);
  160. int q; read(q);
  161. while (q != 3) {
  162. cmd[++m].q = q;
  163. if (q == 1) {
  164. read(cmd[m].x); read(cmd[m].y); read(cmd[m].a);
  165. exes[++tot] = cmd[m].x;
  166. } else {
  167. read(cmd[m].x1); read(cmd[m].y1); read(cmd[m].x2); read(cmd[m].y2);
  168. exes[++tot] = cmd[m].x1;
  169. exes[++tot] = cmd[m].x2;
  170. }
  171. read(q);
  172. }
  173. std::sort(exes + 1, exes + tot + 1);
  174. tot = std::unique(exes + 1, exes + tot + 1) - (exes + 1);
  175. rep(i, 1, m)
  176. if (cmd[i].q == 1) {
  177. cmd[i].x = std::lower_bound(exes + 1, exes + tot + 1, cmd[i].x) - exes;
  178. addBit(cmd[i].x, cmd[i].y, cmd[i].a);
  179. } else {
  180. cmd[i].x1 = std::lower_bound(exes + 1, exes + tot + 1, cmd[i].x1) - exes;
  181. cmd[i].x2 = std::lower_bound(exes + 1, exes + tot + 1, cmd[i].x2) - exes;
  182. int w1 = queryBit(cmd[i].x2, cmd[i].y2);
  183. int w2 = queryBit(cmd[i].x2, cmd[i].y1 - 1);
  184. int w3 = queryBit(cmd[i].x1 - 1, cmd[i].y2);
  185. int w4 = queryBit(cmd[i].x1 - 1, cmd[i].y1 - 1);
  186. printf("%d\n", w1 - w2 - w3 + w4);
  187. }
  188. return 0;
  189. }