模板1.0
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <map> 6 #include <queue> 7 #include <cmath> 8 #include <vector> 9 #include <ctime> 10 #include <cctype> 11 12 using namespace std; 13 14 #define mem0(a) memset(a, 0, sizeof(a)) 15 #define lson l, m, rt << 1 16 #define rson m + 1, r, rt << 1 | 1 17 #define define_m int m = (l + r) >> 1 18 #define Rep(a, b) for(int a = 0; a < b; a++) 19 #define lowbit(x) ((x) & (-(x))) 20 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 21 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 22 23 typedef double db; 24 typedef long long LL; 25 26 const int dx[4] = {1, 0, -1, 0}; 27 const int dy[4] = {0, -1, 0, 1}; 28 const int maxn = 1e4 + 7; 29 const int maxm = 1e5 + 7; 30 const int MD = 1e9 +7; 31 32 struct Point { 33 int x, y; 34 bool operator < (const Point &opt) const { 35 return x < opt.x || x == opt.x && y < opt.y; 36 } 37 Point operator - (const Point &opt) const { 38 return Point(x - opt.x, y - opt.y); 39 } 40 constructInt2(Point, x, y); 41 void inp() { 42 scanf("%d %d", &x, &y); 43 } 44 void outp() { 45 printf("(%d, %d), ", x, y); 46 } 47 }; 48 49 struct Trie { 50 const static int char_size = 26; 51 int cc; 52 int cht[100010][char_size]; 53 int mark[100010]; 54 Trie() { cc = 0; mem0(mark); mem0(cht); } 55 int Idex(char ch) { return ch - '0'; } 56 void Insert(char s[], int v) { 57 int pos = 0; 58 for(int i = 0; s[i]; i++) { 59 int id = Idex(s[i]); 60 if(!cht[pos][id]) cht[pos][id] = ++cc; 61 pos = cht[pos][id]; 62 } 63 mark[pos] = v; 64 } 65 bool Find(char s[]) { 66 int pos = 0; 67 for(int i = 0; s[i]; i++) { 68 int id = Idex(s[i]); 69 if(!cht[pos][id]) return 0; 70 pos = cht[pos][id]; 71 } 72 return mark[pos]; 73 } 74 }; 75 76 struct KMP { 77 int next[1000010]; 78 void GetNext(char s[]) { 79 mem0(next); 80 next[0] = next[1] = 0; 81 for(int i = 1; s[i]; i++) { 82 int j = next[i]; 83 while(j && s[i] != s[j]) j = next[j]; 84 next[i + 1] = s[j] == s[i]? j + 1 : 0; 85 } 86 } 87 void Match(char s[], char t[]) { 88 int j = 0, len = strlen(t); 89 for(int i = 0; s[i]; i++) { 90 while(j && s[i] != t[j]) j = next[j]; 91 if(s[i] == t[j]) j++; 92 if(j == len) printf("%d\n", i - len + 1); 93 } 94 } 95 }; 96 97 struct Matrix { 98 int a[3][3]; 99 Matrix operator * (const Matrix &_A) const { 100 Matrix tmp; 101 mem0(tmp.a); 102 for(int i = 0; i < 3; i++) { 103 for(int j = 0; j < 3; j++) { 104 for(int k = 0; k < 3; k++) { 105 tmp.a[i][j] = ((LL)a[i][k] * _A.a[k][j] + tmp.a[i][j]) % MD; 106 } 107 } 108 } 109 return tmp; 110 } 111 }; 112 113 struct Edge { 114 int u, v; 115 constructInt2(Edge, u, v); 116 }; 117 118 struct Segment { 119 Point a, b; 120 void inp() { 121 scanf("%d%d%d%d", &a.x, &a.y, &b.x, &b.y); 122 if(a.x > b.x) { 123 swap(a.x, b.x); 124 swap(a.y, b.y); 125 } 126 } 127 }; 128 129 Matrix CalcMatrix(Matrix a, int n) { 130 if(n == 1) return a; 131 Matrix tmp = CalcMatrix(a, n >> 1); 132 tmp = tmp * tmp; 133 if(n & 1) tmp = tmp * a; 134 return tmp; 135 } 136 137 inline int ReadInt() { 138 char c = getchar(); 139 while(!isdigit(c)) c = getchar(); 140 141 int x = 0; 142 while(isdigit(c)) { 143 x = x * 10 + c - '0'; 144 c = getchar(); 145 } 146 return x; 147 } 148 149 inline void WriteInt(int i) { 150 int p = 0; 151 static int buf[10]; 152 if(i == 0) p++; 153 else while(i) { 154 buf[p++] = i % 10; 155 i /= 10; 156 } 157 for(int j = p - 1; j; j--) putchar('0' + buf[j]); 158 } 159 160 int Cross(Point a, Point b) { 161 return a.x * b.y - a.y * b.x; 162 } 163 164 int Dist2(Point a, Point b) { 165 int x = a.x - b.x, y = a.y - b.y; 166 return x * x + y * y; 167 } 168 int ConvexHull(Point *p, int n, Point *ch) { 169 sort(p, p + n); 170 int m = 0; 171 for (int i = 0; i < n; i++) { 172 while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; 173 ch[m++] = p[i]; 174 } 175 int k = m; 176 for (int i = n - 2; i >= 0; i--) { 177 while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; 178 ch[m++] = p[i]; 179 } 180 if (n > 1) m--; 181 return m; 182 } 183 184 template<class edge> struct Graph { 185 vector<vector<edge> > adj; 186 Graph(int n) { adj.clear(); adj.resize(n + 5); } 187 Graph() { adj.clear(); } 188 void resize(int n) { adj.resize(n + 5); } 189 void add(int s, edge e){ adj[s].push_back(e); } 190 void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); } 191 void clear() { adj.clear(); } 192 vector<edge>& operator [](int t) { return adj[t]; } 193 };
--------------------------------------------------------------------------------------
update @: 2015.03.30
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #include <map> 7 #include <queue> 8 #include <deque> 9 #include <cmath> 10 #include <vector> 11 #include <ctime> 12 #include <cctype> 13 #include <set> 14 15 using namespace std; 16 17 #define mem0(a) memset(a, 0, sizeof(a)) 18 #define lson l, m, rt << 1 19 #define rson m + 1, r, rt << 1 | 1 20 #define define_m int m = (l + r) >> 1 21 #define Rep(a, b) for(int a = 0; a < b; a++) 22 #define lowbit(x) ((x) & (-(x))) 23 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 24 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 25 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 26 27 typedef double db; 28 typedef long long LL; 29 typedef pair<int, int> pii; 30 typedef multiset<int> msi; 31 typedef multiset<int>::iterator msii; 32 typedef set<int> si; 33 typedef set<int>::iterator sii; 34 35 const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; 36 const int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1}; 37 const int maxn = 1e6 + 7; 38 const int maxm = 1e5 + 7; 39 const int MD = 1e9 +7; 40 const int INF = 1e9 + 7; 41 42 const int maxI = 1e8; 43 const int Len = 8; 44 45 struct BigInt { 46 47 vi num; 48 bool symbol; 49 50 BigInt() { num.clear(); symbol = 0; } 51 BigInt(int x) { symbol = 0; if (x < 0) { symbol = 1; x = -x; } num.push_back(x % maxI); if (x >= maxI) num.push_back(x / maxI); } 52 BigInt(bool s, vi x) { symbol = s; num = x; } 53 BigInt(char s[]) { 54 int len = strlen(s), x = 1, sum = 0, p = s[0] == '-'; 55 symbol = p; 56 for (int i = len - 1; i >= p; i--) { 57 sum += (s[i] - '0') * x; 58 x *= 10; 59 if (x == 1e8 || i == p) { 60 num.push_back(sum); 61 sum = 0; 62 x = 1; 63 } 64 } 65 while (num.back() == 0 && num.size() > 1) num.pop_back(); 66 } 67 68 void push(int x) { num.push_back(x); } 69 70 BigInt abs() const { return BigInt(false, num); } 71 72 bool smaller(const vi &a, const vi &b) const { 73 if (a.size() != b.size()) return a.size() < b.size(); 74 for (int i = a.size() - 1; i >= 0; i--) { 75 if (a[i] != b[i]) return a[i] < b[i]; 76 } 77 return 0; 78 } 79 80 bool operator < (const BigInt &p) const { 81 if (symbol && !p.symbol) return true; 82 if (!symbol && p.symbol) return false; 83 if (symbol && p.symbol) return smaller(p.num, num); 84 return smaller(num, p.num); 85 } 86 87 bool operator > (const BigInt &p) const { 88 return p < *this; 89 } 90 91 bool operator == (const BigInt &p) const { 92 return !(p < *this) && !(*this < p); 93 } 94 95 bool operator >= (const BigInt &p) const { 96 return !(*this < p); 97 } 98 99 bool operator <= (const BigInt &p) const { 100 return !(p < *this); 101 } 102 103 vi add(const vi &a, const vi &b) const { 104 vi c; 105 c.clear(); 106 int x = 0; 107 for (int i = 0; i < a.size(); i++) { 108 x += a[i]; 109 if (i < b.size()) x += b[i]; 110 c.push_back(x % maxI); 111 x /= maxI; 112 } 113 for (int i = a.size(); i < b.size(); i++) { 114 x += b[i]; 115 c.push_back(x % maxI); 116 x /= maxI; 117 } 118 if (x) c.push_back(x); 119 while (c.back() == 0 && c.size() > 1) c.pop_back(); 120 return c; 121 } 122 123 vi sub(const vi &a, const vi &b) const { 124 vi c; 125 c.clear(); 126 int x = 1; 127 for (int i = 0; i < b.size(); i++) { 128 x += maxI + a[i] - b[i] - 1; 129 c.push_back(x % maxI); 130 x /= maxI; 131 } 132 for (int i = b.size(); i < a.size(); i++) { 133 x += maxI + a[i] - 1; 134 c.push_back(x % maxI); 135 x /= maxI; 136 } 137 while (c.back() == 0 && c.size() > 1) c.pop_back(); 138 return c; 139 } 140 141 vi mul(const vi &a, const vi &b) const { 142 vi c; 143 c.resize(a.size() + b.size()); 144 for (int i = 0; i < a.size(); i++) { 145 for (int j = 0; j < b.size(); j++) { 146 LL tmp = (LL)a[i] * b[j] + c[i + j]; 147 c[i + j + 1] += tmp / maxI; 148 c[i + j] = tmp % maxI; 149 } 150 } 151 while (c.back() == 0 && c.size() > 1) c.pop_back(); 152 return c; 153 } 154 155 vi div(const vi &a, const vi &b) const { 156 vi c(a.size()), x(1, 0), y(1, 0), z(1, 0), t(1, 0); 157 y.push_back(1); 158 for (int i = a.size() - 1; i >= 0; i--) { 159 z[0] = a[i]; 160 x = add(mul(x, y), z); 161 if (smaller(x, b)) continue; 162 int l = 1, r = maxI - 1; 163 while (l < r) { 164 int m = (l + r + 1) >> 1; 165 t[0] = m; 166 if (smaller(x, mul(b, t))) r = m - 1; 167 else l = m; 168 } 169 c[i] = l; 170 t[0] = l; 171 x = sub(x, mul(b, t)); 172 } 173 while (c.back() == 0 && c.size() > 1) c.pop_back(); 174 return c; 175 } 176 177 BigInt operator + (const BigInt &p) const{ 178 if (!symbol && !p.symbol) return BigInt(false, add(num, p.num)); 179 if (!symbol && p.symbol) return *this >= p.abs()? BigInt(false, sub(num, p.num)) : BigInt(true, sub(p.num, num)); 180 if (symbol && !p.symbol) return (*this).abs() > p? BigInt(true, sub(num, p.num)) : BigInt(false, sub(p.num, num)); 181 return BigInt(true, add(num, p.num)); 182 } 183 184 BigInt operator - (const BigInt &p) const { 185 return *this + BigInt(!p.symbol, p.num); 186 } 187 188 BigInt operator * (const BigInt &p) const { 189 BigInt res(symbol ^ p.symbol, mul(num, p.num)); 190 if (res.symbol && res.num.size() == 1 && res.num[0] == 0) res.symbol = false; 191 return res; 192 } 193 194 BigInt operator / (const BigInt &p) const { 195 if (p == BigInt(0)) return p; 196 BigInt res(symbol ^ p.symbol, div(num, p.num)); 197 if (res.symbol && res.num.size() == 1 && res.num[0] == 0) res.symbol = false; 198 return res; 199 } 200 201 BigInt operator % (const BigInt &p) const { 202 return *this - *this / p * p; 203 } 204 205 void show() const { 206 if (symbol) putchar('-'); 207 printf("%d", num[num.size() - 1]); 208 for (int i = num.size() - 2; i >= 0; i--) { 209 printf("%08d", num[i]); 210 } 211 putchar('\n'); 212 } 213 214 int TotalDigit() const { 215 int x = num[num.size() - 1] / 10, t = 1; 216 while (x) { 217 x /= 10; 218 t++; 219 } 220 return t + (num.size() - 1) * Len; 221 } 222 223 }; 224 225 BigInt mi(BigInt a, int b, BigInt md) { 226 if (b == 0) return BigInt(1) % md; 227 BigInt tmp = mi(a, b >> 1, md); 228 tmp = tmp * tmp % md; 229 if (b & 1) tmp = tmp * a % md; 230 return tmp; 231 } 232 233 struct Point { 234 int x, y; 235 bool operator < (const Point &opt) const { 236 return x < opt.x || x == opt.x && y < opt.y; 237 } 238 Point operator - (const Point &opt) const { 239 return Point(x - opt.x, y - opt.y); 240 } 241 constructInt2(Point, x, y); 242 void inp() { 243 scanf("%d %d", &x, &y); 244 } 245 void outp() { 246 printf("(%d, %d), ", x, y); 247 } 248 }; 249 250 struct Trie { 251 const static int char_size = 26; 252 int cc; 253 int cht[100010][char_size]; 254 int mark[100010]; 255 Trie() { cc = 0; mem0(mark); mem0(cht); } 256 int Idex(char ch) { return ch - '0'; } 257 void Insert(char s[], int v) { 258 int pos = 0; 259 for(int i = 0; s[i]; i++) { 260 int id = Idex(s[i]); 261 if(!cht[pos][id]) cht[pos][id] = ++cc; 262 pos = cht[pos][id]; 263 } 264 mark[pos] = v; 265 } 266 bool Find(char s[]) { 267 int pos = 0; 268 for(int i = 0; s[i]; i++) { 269 int id = Idex(s[i]); 270 if(!cht[pos][id]) return 0; 271 pos = cht[pos][id]; 272 } 273 return mark[pos]; 274 } 275 }; 276 277 struct KMP { 278 int next[1000010]; 279 void GetNext(char s[]) { 280 mem0(next); 281 next[0] = next[1] = 0; 282 for(int i = 1; s[i]; i++) { 283 int j = next[i]; 284 while(j && s[i] != s[j]) j = next[j]; 285 next[i + 1] = s[j] == s[i]? j + 1 : 0; 286 } 287 } 288 void Match(char s[], char t[]) { 289 int j = 0, len = strlen(t); 290 for(int i = 0; s[i]; i++) { 291 while(j && s[i] != t[j]) j = next[j]; 292 if(s[i] == t[j]) j++; 293 if(j == len) printf("%d\n", i - len + 1); 294 } 295 } 296 }; 297 298 struct Matrix { 299 int a[3][3]; 300 Matrix operator * (const Matrix &_A) const { 301 Matrix tmp; 302 mem0(tmp.a); 303 for(int i = 0; i < 3; i++) { 304 for(int j = 0; j < 3; j++) { 305 for(int k = 0; k < 3; k++) { 306 tmp.a[i][j] = ((LL)a[i][k] * _A.a[k][j] + tmp.a[i][j]) % MD; 307 } 308 } 309 } 310 return tmp; 311 } 312 }; 313 314 struct Edge { 315 int u, v; 316 constructInt2(Edge, u, v); 317 }; 318 319 struct Segment { 320 Point a, b; 321 void inp() { 322 scanf("%d%d%d%d", &a.x, &a.y, &b.x, &b.y); 323 if(a.x > b.x) { 324 swap(a.x, b.x); 325 swap(a.y, b.y); 326 } 327 } 328 }; 329 330 Matrix CalcMatrix(Matrix a, int n) { 331 if(n == 1) return a; 332 Matrix tmp = CalcMatrix(a, n >> 1); 333 tmp = tmp * tmp; 334 if(n & 1) tmp = tmp * a; 335 return tmp; 336 } 337 338 inline int ReadInt() { 339 char c = getchar(); 340 while(!isdigit(c)) c = getchar(); 341 342 int x = 0; 343 while(isdigit(c)) { 344 x = x * 10 + c - '0'; 345 c = getchar(); 346 } 347 return x; 348 } 349 350 inline void WriteInt(int i) { 351 int p = 0; 352 static int buf[10]; 353 if(i == 0) p++; 354 else while(i) { 355 buf[p++] = i % 10; 356 i /= 10; 357 } 358 for(int j = p - 1; j; j--) putchar('0' + buf[j]); 359 } 360 361 int Cross(Point a, Point b) { 362 return a.x * b.y - a.y * b.x; 363 } 364 365 int Dist2(Point a, Point b) { 366 int x = a.x - b.x, y = a.y - b.y; 367 return x * x + y * y; 368 } 369 int ConvexHull(Point *p, int n, Point *ch) { 370 sort(p, p + n); 371 int m = 0; 372 for (int i = 0; i < n; i++) { 373 while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; 374 ch[m++] = p[i]; 375 } 376 int k = m; 377 for (int i = n - 2; i >= 0; i--) { 378 while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; 379 ch[m++] = p[i]; 380 } 381 if (n > 1) m--; 382 return m; 383 } 384 385 template<class edge> struct Graph { 386 vector<vector<edge> > adj; 387 Graph(int n) { adj.clear(); adj.resize(n + 5); } 388 Graph() { adj.clear(); } 389 void resize(int n) { adj.resize(n + 5); } 390 void add(int s, edge e){ adj[s].push_back(e); } 391 void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); } 392 void clear() { adj.clear(); } 393 vector<edge>& operator [](int t) { return adj[t]; } 394 }; 395 396 template<class T> struct TreeArray { 397 vector<T> c; 398 int maxn; 399 TreeArray(int n) { c.resize(n + 5); maxn = n; } 400 TreeArray() { c.clear(); maxn = 0; } 401 void clear() { memset(&c[0], 0, sizeof(T) * maxn); } 402 void resize(int n) { c.resize(n + 5); maxn = n; } 403 void add(int p, T x) { while (p < maxn) { c[p] += x; p += lowbit(p); } } 404 T get(int p) { T res = 0; while (p) { res += c[p]; p -= lowbit(p); } return res; } 405 T range(int a, int b) { return get(b) - get(a - 1); } 406 }; 407 408 template<class T> struct MonotoneQueue{ 409 deque<T> Q; 410 MonotoneQueue<T>() { Q.clear(); } 411 void clear() { Q.clear(); } 412 bool empty() { return Q.empty(); } 413 void add_back(T x) { while (!Q.empty() && !(Q.back() < x)) Q.pop_back(); Q.push_back(x); } 414 void pop_front() { Q.pop_front(); } 415 T back2() { if(Q.size() < 2) return return *(Q.end() - 2); } 416 T front() { return Q.front(); } 417 };