标签:
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 368 Accepted Submission(s): 202
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <ctime> 6 #include <iostream> 7 #include <map> 8 #include <set> 9 #include <algorithm> 10 #include <vector> 11 #include <deque> 12 #include <queue> 13 #include <stack> 14 using namespace std; 15 typedef long long LL; 16 typedef double DB; 17 #define MIT (2147483647) 18 #define MLL (1000000000000000001LL) 19 #define INF (1000000001) 20 #define For(i, s, t) for(int i = (s); i <= (t); i ++) 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i --) 22 #define Rep(i, n) for(int i = (0); i < (n); i ++) 23 #define Repn(i, n) for(int i = (n)-1; i >= (0); i --) 24 #define mk make_pair 25 #define ft first 26 #define sd second 27 #define puf push_front 28 #define pub push_back 29 #define pof pop_front 30 #define pob pop_back 31 #define sz(x) ((int) (x).size()) 32 #define clr(x, y) (memset(x, y, sizeof(x))) 33 inline void SetIO(string Name) 34 { 35 string Input = Name + ".in"; 36 string Output = Name + ".out"; 37 freopen(Input.c_str(), "r", stdin); 38 freopen(Output.c_str(), "w", stdout); 39 } 40 41 inline int Getint() 42 { 43 char ch = ‘ ‘; 44 int Ret = 0; 45 bool Flag = 0; 46 while(!(ch >= ‘0‘ && ch <= ‘9‘)) 47 { 48 if(ch == ‘-‘) Flag ^= 1; 49 ch = getchar(); 50 } 51 while(ch >= ‘0‘ && ch <= ‘9‘) 52 { 53 Ret = Ret * 10 + ch - ‘0‘; 54 ch = getchar(); 55 } 56 return Ret; 57 } 58 59 const int N = 1010, M = 1000010, Dep = 32; 60 struct SegmentType 61 { 62 int Child[2], Sum; 63 #define Lc(x) (Seg[x].Child[0]) 64 #define Rc(x) (Seg[x].Child[1]) 65 #define Child(x, y) (Seg[x].Child[y]) 66 #define Sum(x) (Seg[x].Sum) 67 } Seg[(M+N)*Dep]; 68 int Tot; 69 int n, Arr[N]; 70 int Cnt[M], Length; 71 int Ans; 72 73 inline void Solve(); 74 75 inline void Input() 76 { 77 int TestNumber = Getint(); 78 while(TestNumber--) 79 { 80 n = Getint(); 81 For(i, 1, n) Arr[i] = Getint(); 82 Solve(); 83 } 84 } 85 86 inline void Init(int x) { 87 clr(Seg[x].Child, 0), Sum(x) = 0; 88 } 89 90 inline void Insert(int Val, int x, int Depth) 91 { 92 Sum(x)++; 93 if(Depth < 0) return; 94 int Type = (Val & (1 << Depth)) > 0; 95 if(!Child(x, Type)) { 96 Child(x, Type) = ++Tot; 97 Init(Child(x, Type)); 98 } 99 Insert(Val, Child(x, Type), Depth-1); 100 } 101 102 inline int Work(int Val, int x, int Depth) 103 { 104 if(!x) return 0; 105 if(Depth < 0) return Sum(x); 106 int Type = (Val & (1 << Depth)) > 0; 107 if(Type) return Sum(Lc(x)) + Work(Val, Rc(x), Depth - 1); 108 return Work(Val, Lc(x), Depth - 1); 109 } 110 111 inline void Query(int Val, int x, int Depth, int &Ret) 112 { 113 if(Depth < 0) return; 114 int Type = (Val & (1 << Depth)) > 0; 115 if(Child(x, Type ^ 1)) 116 { 117 int p1 = Ret; 118 p1 |= (1 << Depth) * (Type ^ 1); 119 p1 = p1 - Val - 1; 120 int A = Work(p1, 2, 30); 121 int p2 = Ret; 122 p2 |= (1 << Depth) * (Type ^ 1); 123 p2 |= (1 << Depth) - 1; 124 p2 = p2 - Val; 125 int B = Work(p2, 2, 30); 126 int p = B - A; 127 if(Val > p1 && Val <= p2) p--; 128 if(Sum(Child(x, Type ^ 1)) - p > 0) 129 { 130 Ret |= (1 << Depth) * (Type ^ 1); 131 Query(Val, Child(x, Type ^ 1), Depth - 1, Ret); 132 return; 133 } 134 } 135 Ret |= (1 << Depth) * Type; 136 Query(Val, Child(x, Type), Depth - 1, Ret); 137 } 138 139 inline void Solve() 140 { 141 Tot = 2, Length = 0; 142 Init(1), Init(2), Init(0); 143 For(i, 1, n) 144 { 145 For(j, i + 1, n) 146 { 147 Cnt[++Length] = Arr[i] + Arr[j]; 148 Insert(Cnt[Length], 1, 30); 149 } 150 Insert(Arr[i], 2, 30); 151 } 152 153 Ans = 0; 154 for(int i = 1; i <= n; i++) 155 { 156 int x = 0; 157 Query(Arr[i], 1, 30, x); 158 Ans = max(Ans, x ^ Arr[i]); 159 } 160 printf("%d\n", Ans); 161 } 162 163 int main() 164 { 165 Input(); 166 //Solve(); 167 return 0; 168 }
2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory
标签:
原文地址:http://www.cnblogs.com/StupidBoy/p/4937427.html