码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ4066 简单题

时间:2015-05-17 23:17:32      阅读:607      评论:0      收藏:0      [点我收藏+]

标签:

妈蛋真简单。。。

离线版的可以乱搞。。。貌似是BZOJ的2683

在线的话。。一开始想到了BIT套平衡树。。。空间复杂度$O(n * logm)$非常开心的MLE了。。。QAQ

于是写KD树,空间复杂度$O(n)$,时间复杂度$O(msqrt(m))$,于是就过了QAQQQ

看错题了非常开心。。。一开始读入的终止条件写成了:

    for (i = 1; i <= n; ++i)

于是再见了QAQQQ

 

技术分享
  1 /**************************************************************
  2     Problem: 4066
  3     User: rausen
  4     Language: C++
  5     Result: Accepted
  6     Time:42812 ms
  7     Memory:9408 kb
  8 ****************************************************************/
  9  
 10 #include <cstdio>
 11 #include <algorithm>
 12  
 13 using namespace std;
 14 typedef long long ll;
 15 const int M = 2e5 + 5;
 16  
 17 int n;
 18 ll ans;
 19  
 20 inline int read(int f = 1) {
 21     static int x;
 22     static char ch;
 23     x = 0, ch = getchar();
 24     while (ch < 0 || 9 < ch)
 25         ch = getchar();
 26     while (0 <= ch && ch <= 9)
 27         x = x * 10 + ch - 0, ch = getchar();
 28     if (f) x ^= ans;
 29     return x;
 30 }
 31  
 32 struct point {
 33     int x[2], v;
 34      
 35     int& operator [] (int i) {
 36         return x[i];
 37     }
 38     inline bool operator == (const point &p) const {
 39         return x[0] == p.x[0] && x[1] == p.x[1];
 40     }
 41     inline void get() {
 42         x[0] = read(), x[1] = read(), v = read();
 43     }
 44 };
 45  
 46 struct KD_tree {
 47     KD_tree *son[2];
 48     point p;
 49     int mn[2], mx[2];
 50     ll sum;
 51      
 52     KD_tree(point _p) {
 53         son[0] = son[1] = NULL, p.v = sum = _p.v;
 54         p[0] = mn[0] = mx[0] = _p[0], p[1] = mn[1] = mx[1] = _p[1];
 55     }
 56     KD_tree() {}
 57      
 58     inline void* operator new(size_t, point _p) {
 59         static KD_tree mempool[M], *c = mempool;
 60         *c = KD_tree(_p);
 61         return c++; 
 62     }
 63      
 64     inline void update() {
 65         static int i;
 66         for (i = 0; i < 2; ++i) {
 67             mn[i] = mx[i] = p[i];
 68             if (son[0]) {
 69                 mn[i] = min(mn[i], son[0] -> mn[i]);
 70                 mx[i] = max(mx[i], son[0] -> mx[i]);
 71             }
 72             if (son[1]) {
 73                 mn[i] = min(mn[i], son[1] -> mn[i]);
 74                 mx[i] = max(mx[i], son[1] -> mx[i]);
 75             }
 76         }
 77         sum = p.v;
 78         if (son[0]) sum += son[0] -> sum;
 79         if (son[1]) sum += son[1] -> sum;
 80     }
 81      
 82     void insert(point _p, int dep) {
 83         if (p == _p) {
 84             p.v += _p.v, sum += _p.v;
 85             return;
 86         }
 87         bool d = _p[dep] < p[dep];
 88         if (!son[d]) son[d] = new(_p)KD_tree;
 89         else son[d] -> insert(_p, !dep);
 90         update();
 91     }
 92  
 93     inline bool in(int x1, int y1, int x2, int y2, int X1, int Y1, int X2, int Y2) {
 94         return x1 <= X1 && X2 <= x2 && y1 <= Y1 && Y2 <= y2;
 95     }
 96      
 97     inline bool out(int x1, int y1, int x2, int y2, int X1, int Y1, int X2, int Y2) {
 98         return X2 < x1 || x2 < X1 || Y2 < y1 || y2 < Y1;
 99     }
100      
101     ll query(int x1, int y1, int x2, int y2) {
102         ll res = 0;
103         if (in(x1, y1, x2, y2, mn[0], mn[1], mx[0], mx[1])) return sum;
104         if (out(x1, y1, x2, y2, mn[0], mn[1], mx[0], mx[1])) return 0;
105         if (in(x1, y1, x2, y2, p[0], p[1], p[0], p[1])) res += p.v;
106         if (son[0]) res += son[0] -> query(x1, y1, x2, y2);
107         if (son[1]) res += son[1] -> query(x1, y1, x2, y2);
108         return res;
109     }
110 } *T;
111  
112 void work_add() {
113     static point p;
114     p.get();
115     if (!T) T = new(p)KD_tree;
116     else T -> insert(p, 0);
117 }
118  
119 inline void work_query() {
120     static int x1, y1, x2, y2;
121     x1 = read(), y1 = read(), x2 = read(), y2 = read();
122     if (!T) printf("%lld\n", ans = 0);
123     else printf("%lld\n", ans = T -> query(x1, y1, x2, y2));
124 }
125  
126 int main() {
127     int i, oper;
128     n = read(0), ans = 0;
129     while (1) {
130         oper = read(0);
131         if (oper == 1) work_add();
132         else if (oper == 2) work_query();
133         else break;
134     }
135     return 0;
136 }
View Code

 

附送二逼对拍程序:

技术分享
  1 #include <cstdio>
  2 #include <algorithm>
  3 #include <ext/pb_ds/assoc_container.hpp>
  4 #include <ext/pb_ds/tree_policy.hpp>
  5 
  6 using namespace std;
  7 using namespace __gnu_pbds;
  8 const int N = 5e5 + 5;
  9 
 10 int n, ans;
 11 
 12 inline int read(int f = 1) {
 13     static int x;
 14     static char ch;
 15     x = 0, ch = getchar();
 16     while (ch < 0 || 9 < ch)
 17         ch = getchar();
 18     while (0 <= ch && ch <= 9)
 19         x = x * 10 + ch - 0, ch = getchar();
 20     if (f) x ^= ans;
 21     return x;
 22 }
 23 
 24 #define const_node Node_CItr
 25 #define node Node_Itr
 26 template <class const_node, class node, class Cmp_Fn, class _Alloc> struct my_node_update {
 27     virtual const_node node_begin() const = 0;
 28     virtual const_node node_end() const = 0;
 29     typedef int metadata_type;
 30     
 31     #define lson get_l_child
 32     #define rson get_r_child
 33     #define begin node_begin
 34     #define end node_end
 35     inline void operator () (node p, const_node null) {
 36         static node ls, rs;
 37         static metadata_type left, right;
 38         ls = p.lson(), rs = p.rson(), left = right = 0;
 39         if (ls != null) left = (*ls) -> second;
 40         if (rs != null) right = (*rs) -> second;
 41         const_cast <metadata_type &> (p.get_metadata()) = left + right + (*p) -> second;
 42     }
 43     
 44     inline int prefix_sum(int x) {
 45         static int res;
 46         static const_node p, ls, rs;
 47         res = 0, p = begin();
 48         while (p != end()) {
 49             ls = p.lson(), rs = p.rson();
 50             if (Cmp_Fn() (x, (*p) -> first)) p = ls;
 51             else {
 52                 res += (*p) -> second;
 53                 if (ls != end()) res += ls.get_metadata();
 54                 p = rs;
 55             }
 56         }
 57         return res;
 58     }
 59     
 60     inline int interval_sum(int l, int r) {
 61         return prefix_sum(r) - prefix_sum(l - 1);
 62     }
 63     #undef lson
 64     #undef rson
 65     #undef begin
 66     #undef end
 67 };
 68 #undef const_node
 69 #undef node
 70 typedef __gnu_pbds :: tree <int, int, less<int>, splay_tree_tag, my_node_update> balanced_tree;
 71 
 72 balanced_tree bit[N];
 73 
 74 #define lowbit(x) (x & (-x))
 75 inline void modify(int x, int y, int d) {
 76     while (x <= n)
 77         bit[x][y] += d, x += lowbit(x);
 78 }
 79 
 80 inline int query(int x, int y1, int y2) {
 81     static int res;
 82     res = 0;
 83     while (x)
 84         res += bit[x].interval_sum(y1, y2), x -= lowbit(x);
 85     return res;
 86 }
 87 #undef lowbit
 88 
 89 inline int work_add() {
 90     static int x, y, d;
 91     x = read(), y = read(), d = read();
 92     modify(x, y, d);
 93 }
 94 
 95 inline void work_query() {
 96     static int x1, y1, x2, y2;
 97     x1 = read(), y1 = read(), x2 = read(), y2 = read();
 98     printf("%d\n", ans = query(x2, y1, y2) - query(x1 - 1, y1, y2));
 99 }
100 
101 int main() {
102     int i, oper;
103     n = read(0), ans = 0;
104     for (i = 1; i <= n; ++i) {
105         oper = read(0);
106         if (oper == 1) work_add();
107         else if (oper == 2) work_query();
108         else break;
109     }
110     return 0;
111 }
BIT套平衡树大法各种MLE

 

BZOJ4066 简单题

标签:

原文地址:http://www.cnblogs.com/rausen/p/4510775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!