标签:
我说怎么做过。。。以前交了个暴力,竟然8s过了。。。Σ( ° △ °|||)︴
好吧好吧。。重新写。。。
但是怎么感觉。。。hzwer的trie的insert有点小小的问题呢?(再研究研究)
1 /************************************************************** 2 Problem: 3166 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:1112 ms 7 Memory:22964 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <algorithm> 12 #include <set> 13 14 using namespace std; 15 const int N = 50005; 16 const int inf = 1e9; 17 struct data { 18 int v, w; 19 data() {} 20 data(int _v, int _w) : v(_v), w(_w) {} 21 22 inline bool operator < (const data &x) const { 23 return v > x.v; 24 } 25 } a[N]; 26 27 struct trie_node { 28 int son[2], cnt; 29 } t[N * 35]; 30 int cnt_trie; 31 32 int n, root[N]; 33 set <int> q; 34 35 inline int read() { 36 int x = 0; 37 char ch = getchar(); 38 while (ch < ‘0‘ || ‘9‘ < ch) 39 ch = getchar(); 40 while (‘0‘ <= ch && ch <= ‘9‘) { 41 x = x * 10 + ch - ‘0‘; 42 ch = getchar(); 43 } 44 return x; 45 } 46 47 int A[35]; 48 void insert(int &root, int v) { 49 int R = root, now = ++cnt_trie, i; 50 root = now; 51 for (i = 0; i <= 30; ++i) 52 A[i] = v & 1, v >>= 1; 53 reverse(A, A + 31); 54 for (i = 0; i <= 30; ++i) { 55 t[now].son[0] = t[R].son[0], t[now].son[1] = t[R].son[1]; 56 R = t[R].son[A[i]], now = t[now].son[A[i]] = ++cnt_trie; 57 t[now].cnt = t[R].cnt + 1; 58 } 59 } 60 61 int query(int x, int y, int v) { 62 int res = 0, i; 63 for (i = 0; i <= 30; ++i) 64 A[i] = v & 1, v >>= 1; 65 reverse(A, A + 31); 66 for (i = 0; i <= 30; ++i) { 67 if (t[y].son[!A[i]] - t[x].son[!A[i]]) 68 x = t[x].son[!A[i]], y = t[y].son[!A[i]], res += (1 << 30 - i); 69 else x = t[x].son[A[i]], y = t[y].son[A[i]]; 70 } 71 return res; 72 } 73 74 int main() { 75 int i, l, r, ans = 0; 76 n = read(), root[0] = cnt_trie = 1; 77 for (i = 1; i <= n; ++i) { 78 a[i] = data(read(), i), root[i] = root[i - 1]; 79 insert(root[i], a[i].v); 80 } 81 sort(a + 1, a + n + 1); 82 q.insert(-1), q.insert(-2), q.insert(inf), q.insert(inf + 1); 83 q.insert(a[i].w); 84 for (i = 2; i <= n; ++i) { 85 l = max(1, *--(--q.lower_bound(a[i].w)) + 1); 86 r = min(n, *++q.upper_bound(a[i].w) - 1); 87 if (l > r) continue; 88 ans = max(ans, query(root[l - 1], root[r], a[i].v)); 89 q.insert(a[i].w); 90 } 91 printf("%d\n", ans); 92 return 0; 93 }
(p.s. 论大神们都喜欢封装struct和class...)
标签:
原文地址:http://www.cnblogs.com/rausen/p/4176201.html