码迷,mamicode.com
首页 > Web开发 > 详细

bzoj1014: [JSOI2008]火星人prefix

时间:2016-01-07 13:27:48      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

欸 这不是做过的原题吗qwq

顺手码一码

欸怎么各种不对啊

 

哦 以前基本没用过get_segment 基本都是split的

嗯 就是这个东西 要开个tmp保存一下结果 不然会出奇奇怪股的事情

 

技术分享
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<algorithm>
  5 #include<iostream>
  6 
  7 using namespace std;
  8 
  9 void setIO(const string& s) {
 10     freopen((s + ".in").c_str(), "r", stdin);
 11     freopen((s + ".out").c_str(), "w", stdout);
 12 }
 13 template<typename Q> Q read(Q& x) {
 14     static char c, f;
 15     for(f = 0; c = getchar(), !isdigit(c); ) if(c == -) f = 1;
 16     for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - 0;
 17     if(f) x = -x;
 18     return x;
 19 }
 20 template<typename Q> Q read() {
 21     static Q x; read(x); return x;
 22 }
 23 
 24 typedef unsigned uLL;
 25 const int N = 150000 + 10;
 26 
 27 uLL powers[N];
 28 
 29 struct Node *null, *pis;
 30 struct Node {
 31     int v, sz;
 32     uLL h;
 33     Node *ch[2];
 34     
 35     Node(int v = 0) : v(v) {
 36         h = v;
 37         sz = 1;
 38         ch[0] = ch[1] = null;
 39     }
 40     
 41     int size() const {
 42         return this ? sz : 0;
 43     }
 44     
 45     void maintain() {
 46         sz = ch[0]->sz + ch[1]->sz + 1;
 47         h = ch[0]->h * powers[ch[1]->sz + 1] + (uLL) v * powers[ch[1]->sz] + ch[1]->h;
 48     }
 49     
 50     int cmp(int k) const {
 51         int s = ch[0]->sz + 1;
 52         if(s == k) return -1;
 53         return k < s ? 0 : 1;
 54     }
 55     
 56     void *operator new(size_t) {
 57         return pis++;
 58     }
 59 }pool[N], *root;
 60 
 61 void rotate(Node *&o, int d) {
 62     Node *t = o->ch[d];
 63     o->ch[d] = t->ch[d ^ 1];
 64     t->ch[d ^ 1] = o;
 65     o->maintain();
 66     (o = t)->maintain();
 67 }
 68 
 69 void splay(Node *&o, int k) {
 70     int d = o->cmp(k);
 71     if(d == -1) return;
 72     if(d == 1) k -= o->ch[0]->sz + 1;
 73     Node *&c = o->ch[d];
 74     int d2 = c->cmp(k);
 75     if(d2 != -1) {
 76         if(d2 == 1) k -= c->ch[0]->sz + 1;
 77         splay(c->ch[d2], k);
 78         if(d == d2) rotate(o, d);
 79         else rotate(c, d2);
 80     }
 81     rotate(o, d);
 82 }
 83 
 84 void split(Node *o, int k, Node *&l, Node *&r) {
 85     splay(o, k);
 86     l = o;
 87     r = o->ch[1];
 88     o->ch[1] = null;
 89     o->maintain();
 90 }
 91 
 92 Node *merge(Node *l, Node *r) {
 93     splay(l, l->sz);
 94     l->ch[1] = r;
 95     return l->maintain(), l;
 96 }
 97 
 98 char s[N];
 99 
100 void build(Node*& o, int l, int r) {
101     if(l > r) return o = null, void();
102     int mid = (l + r) >> 1;
103     o = new Node(s[mid]);
104     build(o->ch[0], l, mid - 1);
105     build(o->ch[1], mid + 1, r);
106     o->maintain();
107 }
108 
109 void splay_init() {
110     pis = pool;
111     null = new Node(0);
112     null->ch[0] = null->ch[1] = null;
113     null->sz = 0;
114 }
115 
116 Node *segment(int l, int r) {
117     splay(root, l);
118     splay(root->ch[1], r + 1);
119     return root->ch[1]->ch[0];
120 }
121 
122 void update() {
123     root->ch[1]->ch[0]->maintain();
124     root->ch[1]->maintain();
125     root->maintain();
126 }
127 
128 void print(Node *o) {
129     if(o == null) return;
130     print(o->ch[0]);
131     fprintf(stderr, "%c", (char)o->v);
132     print(o->ch[1]);
133 }
134 
135 const uLL xx[] = {53, 1313, 1003};
136 
137 int main() {
138 #ifdef DEBUG
139     freopen("in.txt", "r", stdin);
140     freopen("out.txt", "w", stdout);
141 #endif
142     
143     powers[0] = 1;
144     for(int j = 1; j < N; j++) powers[j] = powers[j-1] * 1313;
145     
146     scanf("%s", s + 1);
147     int n = strlen(s + 1);
148     
149     splay_init();
150     build(root, 0, n + 1);
151     
152 //    print(root);
153     
154     char opt[10];
155     int m, x, y;
156     scanf("%d", &m);
157     while(m--) {
158         scanf("%s%d", opt, &x);
159         if(opt[0] == Q) {
160             scanf("%d", &y);
161             int L = 1, R = n - max(x, y) + 1, res = 0;
162             while(L <= R) {
163                 int mid = (L + R) >> 1;
164                 static uLL tmp;
165                 tmp = segment(x, mid)->h;
166                 if(tmp == segment(y, mid)->h)
167                     res = mid, L = mid + 1; else R = mid - 1;
168             }
169             printf("%d\n", res);
170         }else if(opt[0] == R) {
171             scanf("%s", opt);
172             segment(x, 1)->v = opt[0];
173             update();
174 //            print(root);
175         }else {
176             scanf("%s", opt);
177             static Node *lft, *rgt;
178             split(root, x + 1, lft, rgt);
179             root = merge(merge(lft, new Node(opt[0])), rgt);
180             n++;
181         }
182     }
183     
184     return 0;
185 }
View Code

 

bzoj1014: [JSOI2008]火星人prefix

标签:

原文地址:http://www.cnblogs.com/showson/p/5109238.html

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