恩。。线段树维护凸壳。。。什么鬼

介绍什么的略。为甚我的程序略长。。。

 

  1 /**************************************************************
  2     Problem: 3165
  3     User: rausen
  4     Language: C++
  5     Result: Accepted
  6     Time:1820 ms
  7     Memory:5180 kb
  8 ****************************************************************/
  9  
 10 #include <cstdio>
 11 #include <algorithm>
 12  
 13 using namespace std;
 14 typedef double lf;
 15  
 16 const int n = 40005;
 17 const int mod1 = 39989;
 18 const int mod2 = 1e9;
 19 const int Cnt_seg = n << 2;
 20 const lf inf = 1e60;
 21  
 22 struct line {
 23     lf k, b;
 24     int id;
 25 };
 26  
 27 struct data {
 28     lf v;
 29     int id;
 30     data() {}
 31     data(lf _v, int _i) : v(_v), id(_i) {}
 32      
 33     friend inline bool operator < (const data &x, const data &y) {
 34         if (x.id == 0) return 1;
 35         if (y.id == 0) return 0;
 36         return x.v == y.v ? x.id > y.id : x.v < y.v;
 37     }
 38 };
 39  
 40 struct seg_node {
 41     seg_node *ls, *rs;
 42     line li;
 43 } *seg_root, mempool[Cnt_seg], *cnt_seg = mempool;
 44  
 45 int ans, cnt_id;
 46  
 47 inline void calc(int &x, int y) {
 48     ((x += ans - 1) %= y) += 1;
 49 }
 50  
 51 #define K l.k
 52 #define B l.b
 53 #define ID l.id
 54 inline line make_line(int x0, int y0, int x1, int y1) {
 55     line l;
 56     ID = ++cnt_id;
 57     if (x0 == x1) K = 0, B = max(y0, y1);
 58     else K = (lf) (y0 - y1) / (x0 - x1), B = y0 - K * x0;
 59     return l;
 60 }
 61  
 62 inline lf get_point(line l, int x) {
 63     return ID == 0 ? -inf : K * (lf) x + B;
 64 }
 65 #undef K
 66 #undef B
 67 #undef ID
 68  
 69 #define mid (l + r >> 1)
 70 #define Li p -> li
 71 #define Ls p -> ls
 72 #define Rs p -> rs
 73 void seg_build(seg_node *&p, int l, int r) {
 74     p = ++cnt_seg;
 75     Li.id = 0;
 76   if (l == r) return;
 77     seg_build(Ls, l, mid), seg_build(Rs, mid + 1, r);
 78 }
 79  
 80 void seg_update(seg_node *p, int l, int r, line li) {
 81     if (Li.id == 0) Li = li;
 82     if (get_point(Li, l) < get_point(li, l)) swap(Li, li);
 83     if (l == r || Li.k == li.k) return;
 84     lf w = (Li.b - li.b) / (li.k - Li.k);
 85     if (w < l || w > r) return;
 86     if (w <= mid) seg_update(Ls, l, mid, Li), Li = li;
 87     else seg_update(Rs, mid + 1, r, li);
 88 }
 89  
 90 void seg_insert(seg_node *p, int l, int r, int L, int R, line li) {
 91     if (L <= l && r <= R) {
 92         seg_update(p, l, r, li);
 93         return;
 94     }
 95     if (L <= mid) seg_insert(Ls, l, mid, L, R, li);
 96     if (mid < R) seg_insert(Rs, mid + 1, r, L, R, li);
 97 }
 98  
 99 #define Data data(get_point(Li, w), Li.id)
100 data seg_query(seg_node *p, int l, int r, int w) {
101     if (l == r) return Data;
102     if (w <= mid) return max(Data, seg_query(Ls, l, mid, w));
103     else return max(Data, seg_query(Rs, mid + 1, r, w));
104 }
105 #undef mid
106 #undef Ls
107 #undef Rs
108 #undef L
109  
110 int main() {
111     seg_build(seg_root, 1, n);
112     int Q, oper, x, y, x0, y0, x1, y1;
113     scanf("%d", &Q);
114     while (Q--) {
115         scanf("%d", &oper);
116         if (oper == 0) {
117             scanf("%d", &x);
118             calc(x, mod1);
119             printf("%d\n", ans = seg_query(seg_root, 1, n, x).id);
120         } else {
121             scanf("%d%d%d%d", &x0, &y0, &x1, &y1);
122             calc(x0, mod1), calc(x1, mod1), calc(y0, mod2), calc(y1, mod2);
123             if (x0 > x1)
124                 swap(x0, x1), swap(y0, y1);
125             seg_insert(seg_root, 1, n, x0, x1, make_line(x0, y0, x1, y1));
126         }
127     }
128     return 0;
129 }
View Code

 

posted on 2015-02-19 15:21  Xs酱~  阅读(266)  评论(0编辑  收藏  举报