Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.53 KB | None | 0 0
  1. // Stress test
  2.  
  3. from os import system as cmd
  4.  
  5. cmd("g++ slow.cpp -std=c++17 -oslow")
  6. cmd("g++ wrong.cpp -std=c++17 -owrong")
  7. print("Compiled")
  8.  
  9. for i in range(10000):
  10.     if i % 10 == 0:
  11.         print(i)
  12.     cmd("python3 gen.py > test.txt")
  13.     cmd("./slow < test.txt > slow.txt")
  14.     cmd("./wrong < test.txt > wrong.txt")
  15.     if open("slow.txt").read().rstrip().split() != open("wrong.txt").read().rstrip().split():
  16.         exit(0)
  17.  
  18.  
  19.  
  20.  
  21. // Pragmas Collection
  22.  
  23. #pragma comment(linker, "/stack:200000000")
  24. #pragma GCC optimize("Ofast,no-stack-protector")
  25. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
  26. #pragma GCC optimize("unroll-loops")
  27. #pragma GCC optimize("fast-math")
  28.  
  29. // Fast Alloc
  30.  
  31. const int MAX_MEM = 1e8;
  32. int mpos = 0;
  33. char mem[MAX_MEM];
  34. inline void * operator new ( size_t n ) {
  35. char *res = mem + mpos;
  36. mpos += n;
  37. assert(mpos <= MAX_MEM);
  38. return (void *)res;
  39. }
  40. inline void operator delete ( void * ) { }
  41.  
  42.  
  43. // Policy based treap
  44.  
  45. #include <ext/pb_ds/assoc_container.hpp>
  46. using namespace __gnu_pbds;
  47. tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> s;
  48. s.insert(x);
  49. s.erase(x);
  50. s.count(x);
  51. s.find_by_order(k); // k-й по величине элемент в множестве
  52. s.order_of_key(x); // сколько элементов в множестве меньше x
  53.  
  54.  
  55. // Policy based hashtable
  56.  
  57. #include <ext/pb_ds/assoc_container.hpp>
  58. using namespace __gnu_pbds;
  59. typedef gp_hash_table<int, int, hash<int>, equal_to<int>, direct_mod_range_hashing<int>, linear_probe_fn<>,
  60.                       hash_standard_resize_policy<hash_prime_size_policy, hash_load_check_resize_trigger<true>, true>>
  61.     gp;
  62.  
  63. // Bitset
  64.  
  65. bitset<N> a;
  66. for (int i = a._Find_first(); i != a.size(); i = a._Find_next(i)) {
  67.     cout << i << endl;
  68. }
  69.  
  70. //  Bit Gauss
  71.  
  72. int basis[d];
  73. int sz;
  74.  
  75. void insertVector(int mask) {
  76.     for (int i = 0; i < d; i++) {
  77.         if ((mask & 1 << i) == 0) continue;
  78.         if (!basis[i]) {
  79.             basis[i] = mask;
  80.             ++sz;
  81.             return;
  82.         }
  83.  
  84.         mask ^= basis[i];
  85.     }
  86. }
  87.  
  88. // Fedya the Potter
  89.  
  90. // sum(i=0..n-1) (a+b*i) div m
  91. ll solve(ll n, ll a, ll b, ll m) {
  92.     if (b == 0) return n * (a / m);
  93.     if (a >= m) return n * (a / m) + solve(n, a % m, b, m);
  94.     if (b >= m) return n * (n - 1) / 2 * (b / m) + solve(n, a, b % m, m);
  95.     return solve((a + b * n) / m, (a + b * n) % m, m, b);
  96. }
  97.  
  98. int CRT(int a1, int m1, int a2, int m2) {
  99.     return (a1 - a2 % m1 + m1) * (ll)rev(m2, m1) % m1 * m2 + a2;
  100. }
  101.  
  102. // ExtGcd
  103.  
  104. int gcd (int a, int b, int & x, int & y) {
  105.     if (a == 0) {
  106.         x = 0; y = 1;
  107.         return b;
  108.     }
  109.     int x1, y1;
  110.     int d = gcd (b%a, a, x1, y1);
  111.     x = y1 - (b / a) * x1;
  112.     y = x1;
  113.     return d;
  114. }
  115. // Suffix array and stuff
  116.  
  117.  
  118. vector<int> suffix_array(string s){
  119.     int n = len(s);
  120.     vector<int> cl(n); forn (i, n) cl[i] = s[i];
  121.     for (int gap = 1;; gap *= 2) {
  122.         vector<pair<int, int>> nw(n); forn (i, n) nw[i] = make_pair(cl[i], cl[(i + gap) % n]);
  123.         auto old = nw; stable_sort(all(nw)); nw.resize(unique(all(nw)) - nw.begin());
  124.         forn (i, n)  cl[i] = lower_bound(all(nw), old[i]) - nw.begin();
  125.         if (len(nw) == n) break;
  126.     }
  127.     vector<int> sa(n); forn (i, n) sa[cl[i]] = i;
  128.     return sa;
  129. }
  130.  
  131. struct D {
  132.     int x, y, z;
  133.     bool operator<(const D& a) const { return x < a.x || x == a.x && y < a.y; }
  134. } d[1 << 21];
  135. int j, k, c[(1 << 19) + 7], b[100], a[100], i, n;
  136. char s[(1 << 19) + 7];
  137. int main() {
  138.     scanf("%s", s);
  139.     n = strlen(s);
  140.     s[n++] = 47;
  141.     R(i, n) a[s[i] - 47]++;
  142.     b[0] = 1;
  143.     R(i, 100) b[i] = b[i - 1] + (a[i] > 0);
  144.     R(i, n) c[i] = b[s[i] - 47];
  145.     for (k = 0; (1 << k) < n; k++) {
  146.         R(i, n) d[i] = (D){ c[i], c[i + (1 << k)], i };
  147.         std::stable_sort(d, d + n);
  148.         j = c[d[0].z] = 1;
  149.         R(i, n - 1) { c[d[i + 1].z] = d[i] < d[i + 1] ? ++j : j; }
  150.         if (j == n)
  151.             break;
  152.     }
  153.     printf("%d ", n - 1);
  154.     R(i, n - 1) printf("%d ", d[i + 1].z );
  155. }
  156.  
  157.  
  158.   void BuildLCP( int n, T *s ) { // O(N), works only for s with no nontrivial period
  159.     int lcp = 0;
  160.     forn(i, n) {
  161.       int j = p2[i];
  162.       if (--lcp < 0)
  163.         lcp = 0;
  164.       if (j != n - 1)
  165.         while (lcp < n && s[(p[j] + lcp) % n] == s[(p[j + 1] + lcp) % n])
  166.           lcp++;
  167.       len[j] = lcp;
  168.     }
  169.  
  170.  
  171.  
  172. // SetCht online
  173.  
  174.  
  175.  
  176. bool QUERY;
  177. struct Line {
  178.     mutable T a, b, p;
  179.     T Eval(T x) const { return a * x + b; }
  180.     bool operator<(const Line& o) const {
  181.         return QUERY ? p < o.p : a < o.a;
  182.     }
  183. };
  184.  
  185. struct LineContainer : multiset<Line> {
  186.     // for doubles, use kInf = 1/.0, div(a, b) = a / b
  187.     const T kInf = numeric_limits<T>::max();
  188.     T div(T a, T b) { // floored division
  189.         return a / b - ((a ^ b) < 0 && a % b); }
  190.     bool isect(iterator x, iterator y) {
  191.         if (y == end()) { x->p = kInf; return false; }
  192.         if (x->a == y->a) x->p = x->b > y->b ? kInf : -kInf;
  193.         else x->p = div(y->b - x->b, x->a - y->a);
  194.         return x->p >= y->p;
  195.     }
  196.     void InsertLine(T a, T b) {
  197.         auto nx = insert({a, b, 0}), it = nx++, pv = it;
  198.         while (isect(it, nx)) nx = erase(nx);
  199.         if (pv != begin() && isect(--pv, it)) isect(pv, it = erase(it));
  200.         while ((it = pv) != begin() && (--pv)->p >= it->p)
  201.             isect(pv, erase(it));
  202.     }
  203.     T EvalMax(T x) {
  204.         assert(!empty());
  205.         QUERY = 1; auto it = lower_bound({0,0,x}); QUERY = 0;
  206.         return it->Eval(x);
  207.     }
  208. };
  209. // Treap
  210.  
  211.  
  212.  
  213. struct node {
  214.     node *left, *right;
  215.     int val;
  216.     int pri;
  217.     int sz;
  218.     int min_val;
  219.     bool rev;
  220.  
  221.     node(int nval){
  222.         left = right = nullptr;
  223.         val = nval;
  224.         min_val = val;
  225.         pri = rnd();
  226.         rev = false;
  227.         sz = 1;
  228.     }
  229. };
  230.  
  231. int get_sz(node* v){
  232.     return (v == nullptr ? 0 : v->sz);
  233. }
  234. const int inf = (int) 1e9 + 7;
  235. int get_mval(node*v){
  236.     return v == nullptr ? inf : v->min_val;
  237. }
  238. void push(node* v){
  239.     if (v == nullptr) return;
  240.     if (v->rev){
  241.         swap(v->left, v->right);
  242.         if (v->left != nullptr) v->left->rev ^= 1;
  243.         if (v->right != nullptr) v->right->rev ^= 1;
  244.         v->rev = false;
  245.     }
  246. }
  247.  
  248.  
  249. void upd(node* v){
  250.     if (v == nullptr) return;
  251.     push(v);
  252.     v->sz = 1 + get_sz(v->left) + get_sz(v->right);
  253.     v->min_val = min(v->val, min(get_mval(v->left), get_mval(v->right)));
  254. }
  255.  
  256.  
  257. node* merge(node* A, node* B){
  258.     if (A == nullptr || B == nullptr)
  259.         return A == nullptr ? B : A;
  260.     push(A); push(B);
  261.     if (A->pri > B->pri){
  262.         A->right = merge(A->right, B);
  263.         upd(A);
  264.         return A;
  265.     }
  266.     B->left = merge(A, B->left);
  267.     upd(B);
  268.     return B;
  269. }
  270.  
  271.  
  272. pair<node*, node*> split(node* v, int sz){
  273.     if (v == nullptr){
  274.         return {nullptr, nullptr};
  275.     }
  276.     push(v);
  277.     if (sz <= get_sz(v->left)){
  278.         auto splitted = split(v->left, sz);
  279.         v->left = splitted.second;
  280.         upd(v);
  281.         return {splitted.first, v};
  282.     }
  283.     auto splitted = split(v->right, sz - get_sz(v->left) - 1);
  284.     v->right = splitted.first;
  285.     upd(v);
  286.     return {v, splitted.second};
  287. }
  288.  
  289. node* push_back(node* tree, int val){
  290.     tree = merge(tree, new node(val));
  291.     return tree;
  292. }
  293.  
  294. node* reverse(node* tree, int l, int r){ // [l, r], 0-indexing
  295.     auto splitted1 = split(tree, l);
  296.     auto splitted2 = split(splitted1.second, r - l + 1);
  297.     if (splitted2.first != nullptr){
  298.         splitted2.first->rev ^= 1;
  299.         push(splitted2.first);
  300.     }
  301.     return merge(splitted1.first, merge(splitted2.first, splitted2.second));
  302. }
  303.  
  304. void print_tree(node* v){
  305.     if (v == nullptr)
  306.         return;
  307.     print_tree(v->left);
  308.     cout << v->val << ' ';
  309.     print_tree(v->right);
  310. }
  311.  
  312. pair<node*, int> get_min(node* tree, int l, int r){
  313.     auto splitted1 = split(tree, l);
  314.     auto splitted2 = split(splitted1.second, r - l + 1);
  315.     int ans = get_mval(splitted2.first);
  316.     // print_tree(splitted2.first); cout << endl;
  317.     return {merge(splitted1.first, merge(splitted2.first, splitted2.second)), ans};
  318. }
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328. struct FFTSolver {
  329.   using Complex = complex<double>;
  330.   const double kPi = 4.0 * atan(1.0);
  331.   vector<int> rev;
  332.  
  333.   int __lg(int n) { return n == 1 ? 0 : 1 + __lg(n / 2); }
  334.  
  335.   void compute_rev(int n, int lg) {
  336.     rev.resize(n); rev[0] = 0;
  337.     for (int i = 1; i < n; ++i) {
  338.       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lg - 1));
  339.     }
  340.   }
  341.  
  342.   vector<Complex> fft(vector<Complex> V, bool invert) {
  343.     int n = V.size(), lg = __lg(n);
  344.     if ((int)rev.size() != n) compute_rev(n, lg);
  345.    
  346.     for (int i = 0; i < n; ++i) {
  347.       if (i < rev[i])
  348.         swap(V[i], V[rev[i]]);
  349.     }
  350.    
  351.     for (int step = 2; step <= n; step *= 2) {
  352.       const double ang = 2 * kPi / step;
  353.       Complex eps(cos(ang), sin(ang));
  354.       if (invert) eps = conj(eps);
  355.      
  356.       for (int i = 0; i < n; i += step) {
  357.         Complex w = 1;
  358.         for (int a = i, b = i+step/2; b < i+step; ++a, ++b) {
  359.           Complex aux = w * V[b];
  360.           V[b] = V[a] - aux;
  361.           V[a] = V[a] + aux;
  362.           w *= eps;
  363.         }
  364.       }
  365.     }
  366.    
  367.     return V;
  368.   }
  369.  
  370.   vector<Complex> transform(vector<Complex> V) {
  371.     int n = V.size();
  372.     vector<Complex> ret(n);
  373.     Complex div_x = Complex(0, 1) * (4.0 * n);
  374.    
  375.     for (int i = 0; i < n; ++i) {
  376.       int j = (n - i) % n;
  377.       ret[i] = (V[i] + conj(V[j]))
  378.         * (V[i] - conj(V[j])) / div_x;
  379.     }
  380.    
  381.     return ret;
  382.   }
  383.  
  384.   vector<int> Multiply(vector<int> A, vector<int> B) {
  385.     int n = A.size() + B.size() - 1;
  386.     vector<int> ret(n);
  387.     while (n != (n & -n)) ++n;
  388.    
  389.     A.resize(n); B.resize(n);
  390.     vector<Complex> V(n);
  391.     for (int i = 0; i < n; ++i) {
  392.       V[i] = Complex(A[i], B[i]);
  393.     }
  394.    
  395.     V = fft(move(V), false);
  396.     V = transform(move(V));
  397.     V = fft(move(V), true);
  398.    
  399.     for (int i = 0; i < (int)ret.size(); ++i)
  400.       ret[i] = round(real(V[i]));
  401.     return ret;
  402.   }
  403. };
  404.  
  405.  
  406.  
  407.  
  408. // Bridges
  409.  
  410. void dfs (int v, int p = -1) {
  411.     used[v] = true;
  412.     tin[v] = fup[v] = timer++;
  413.     for (size_t i=0; i<g[v].size(); ++i) {
  414.         int to = g[v][i];
  415.         if (to == p)  continue;
  416.         if (used[to])
  417.             fup[v] = min (fup[v], tin[to]);
  418.         else {
  419.             dfs (to, v);
  420.             fup[v] = min (fup[v], fup[to]);
  421.             if (fup[to] > tin[v])
  422.                 IS_BRIDGE(v,to);
  423.         }
  424.     }
  425. }
  426.  
  427. void find_bridges() {
  428.     timer = 0;
  429.     for (int i=0; i<n; ++i)
  430.         used[i] = false;
  431.     for (int i=0; i<n; ++i)
  432.         if (!used[i])
  433.             dfs (i);
  434. }
  435.  
  436. // DCP offline
  437.  
  438.  
  439. class DynamicConnectivity {
  440. private:
  441.     struct Query {
  442.         int type, v, w, otherTime;
  443.     };
  444.  
  445.     int V, curTime = 0, cnt;
  446.     vector<Query> queries;
  447.     vector<int> ans;
  448.     vector<int> presentEdges;
  449.  
  450.     DSU ds;
  451.  
  452.  
  453.     void solve(int l, int r) {
  454.         if (l >= r) {
  455.             if (l == r && queries[r].type == 0) ans.push_back(ds.get());
  456.             return;
  457.         }
  458.         int m = l + (r - l) / 2;
  459.         int cur = ds.curver;
  460.         for (int i = m + 1; i <= r; i++) if (queries[i].otherTime < l) ds.add(queries[i].v, queries[i].w);
  461.         solve(l, m);
  462.         while (ds.curver > cur) ds.rollback();
  463.         for (int i = l; i <= m; i++) if (queries[i].otherTime > r) ds.add(queries[i].v, queries[i].w);
  464.         solve(m + 1, r);
  465.         while (ds.curver > cur) ds.rollback();
  466.     }
  467.  
  468. public:
  469.     DynamicConnectivity(int V): V(V), curTime(0), cnt(V), presentEdges(V) {}
  470.  
  471.     void addEdge(int v, int w, int id) {
  472.         presentEdges[id] = curTime++;
  473.         queries.push_back({1, v, w, INT_MAX});
  474.     }
  475.  
  476.     void removeEdge(int v, int w, int id) {
  477.         int insTime = presentEdges[id];
  478.         queries.push_back({-1, v, w, insTime});
  479.         queries[insTime].otherTime = curTime++;
  480.         presentEdges[id] = -100;
  481.     }
  482.  
  483.     void query() {
  484.         queries.push_back({0, -1, -1, curTime++});
  485.     }
  486.  
  487.     vector<int> solve() {
  488.         ds.build();
  489.         solve(0, curTime - 1);
  490.         return ans;
  491.     }
  492. };
  493.  
  494. // Aho-Corasick
  495.  
  496. template <const int maxV, const int E>
  497. struct AhoCorasic {
  498.   struct Vertex {
  499.     int next[E], p, dep, ch, suf, end;
  500.   };
  501.  
  502.   int vn;
  503.   Vertex a[maxV];
  504.   int qst, qen, q[maxV];
  505.  
  506.   int newV( int P, int D, int K ) {
  507.     assert(vn < maxV);
  508.     Vertex &v = a[vn];
  509.     v.dep = D, v.ch = K, v.p = P, v.end = 0;
  510.     zero(v.next);
  511.     return vn++;
  512.   }
  513.  
  514.   AhoCorasic() {
  515.     vn = 0, newV(-1, 0, -1);
  516.   }
  517.  
  518.   void add( const char *s ) {
  519.     int v = 0;
  520.     while (*s) {
  521.       int ch = *s++ - 'a', &x = a[v].next[ch];
  522.       if (!x)
  523.         x = newV(v, a[v].dep + 1, ch);
  524.       v = x;
  525.     }
  526.     a[v].end = 1;
  527.   }
  528.  
  529.   void build() {
  530.     qst = qen = 0;
  531.     q[qen++] = 0;
  532.     while (qst < qen) {
  533.       int v = q[qst++];
  534.       Vertex &V = a[v];
  535.       V.suf = (V.dep <= 1 ? 0 : a[a[V.p].suf].next[V.ch]);
  536.       forn(i, 26)
  537.         if (V.next[i])
  538.           q[qen++] = V.next[i];
  539.         else
  540.           V.next[i] = a[V.suf].next[i];
  541.       V.end |= a[V.suf].end;
  542.     }
  543.   }
  544. };
  545.  
  546. AhoCorasic<(int)1e5, 26> m;
  547.  
  548.  
  549. // Tree Compression
  550.  
  551.  
  552. vector<int> CompressTree(vector<int> v, LCA& lca,
  553.                          vector<int>& link) {
  554.   auto cmp = [&](int a, int b) {
  555.     return lca.enter[a] < lca.enter[b];
  556.   };
  557.   sort(v.begin(), v.end(), cmp);
  558.   v.erase(unique(v.begin(), v.end()), v.end());
  559.  
  560.   for (int i = (int)v.size() - 1; i > 0; --i)
  561.     v.push_back(lca.Query(v[i - 1], v[i]));
  562.  
  563.   sort(v.begin(), v.end(), cmp);
  564.   v.erase(unique(v.begin(), v.end()), v.end());
  565.  
  566.   for (int i = 0; i < (int)v.size(); ++i)
  567.     link[v[i]] = (i == 0 ? -1 : lca.Query(v[i - 1], v[i]));
  568.   return v;
  569. }
  570.  
  571. vector<int> g[MAXN];
  572. bool used[MAXN];
  573. int timer, tin[MAXN], fup[MAXN];
  574.  
  575. void dfs (int v, int p = -1) {
  576.     used[v] = true;
  577.     tin[v] = fup[v] = timer++;
  578.     int children = 0;
  579.     for (size_t i=0; i<g[v].size(); ++i) {
  580.         int to = g[v][i];
  581.         if (to == p)  continue;
  582.         if (used[to])
  583.             fup[v] = min (fup[v], tin[to]);
  584.         else {
  585.             dfs (to, v);
  586.             fup[v] = min (fup[v], fup[to]);
  587.             if (fup[to] >= tin[v] && p != -1)
  588.                 IS_CUTPOINT(v);
  589.             ++children;
  590.         }
  591.     }
  592.     if (p == -1 && children > 1)
  593.         IS_CUTPOINT(v);
  594. }
  595.  
  596.  
  597.  
  598.  
  599. struct Dinic {
  600.   struct Edge { int to, cap, flow, nxt; };
  601.  
  602.   vector<Edge> edges;
  603.   vector<int> graph, at, dist;
  604.   int src = 0, dest = 1;
  605.  
  606.   Dinic(int n) : graph(n, -1), dist(n, -1) {}
  607.  
  608.   void add_edge(int from, int to, int cap) {
  609.     edges.push_back(Edge {to, cap, 0, graph[from]});
  610.     graph[from] = edges.size() - 1;
  611.   }
  612.   void AddEdge(int from, int to, int cap) {
  613.     add_edge(from, to, cap);
  614.     add_edge(to, from, 0);
  615.   }
  616.  
  617.   bool bfs() {
  618.     queue<int> q;
  619.     fill(dist.begin(), dist.end(), -1);
  620.     dist[src] = 0; q.push(src);
  621.    
  622.     while (!q.empty()) {
  623.       int node = q.front(); q.pop();
  624.       for (int i = graph[node]; i >= 0; i = edges[i].nxt) {
  625.         const auto &e = edges[i];
  626.         if (dist[e.to] == -1 && e.flow < e.cap) {
  627.           dist[e.to] = dist[node] + 1;
  628.           q.push(e.to);
  629.         }
  630.       }
  631.     }
  632.    
  633.     return dist[dest] != -1;
  634.   }
  635.  
  636.   int dfs(int node, int flow) {
  637.     if (flow == 0) return 0;
  638.     if (node == dest) return flow;
  639.    
  640.     while (at[node] != -1) {
  641.       int eid = at[node]; const auto &e = edges[eid];
  642.      
  643.       if (dist[e.to] == dist[node] + 1) {
  644.         if (int ret = dfs(e.to, min(flow, e.cap - e.flow))) {
  645.           edges[ eid ].flow += ret;
  646.           edges[eid^1].flow -= ret;
  647.           return ret;
  648.         }
  649.       }
  650.      
  651.       at[node] = e.nxt;
  652.     }
  653.     return 0;
  654.   }
  655.  
  656.   int Compute(int src, int dest) {
  657.     this->src = src; this->dest = dest; int ret = 0;
  658.     while (bfs()) {
  659.       at = graph;
  660.       while (int flow = dfs(src, 2e9))
  661.         ret += flow;
  662.     }
  663.     return ret;
  664.   }
  665. };
  666.  
  667.  
  668.  
  669. // Centroid
  670.  
  671. vector<int> good;
  672. vector<vector<int>> g;
  673. string city_l;
  674. vector<int> ans;
  675.  
  676.  
  677. const int MAXN = (int) 2e5 + 7;
  678. int level[MAXN];
  679. int parent[MAXN];
  680.  
  681. const static int maxn = 4e6;
  682. int root_masks[maxn], cur_mask[maxn];
  683. int root_masks_clear[maxn], root_masks_cur = 0;
  684. int cur_mask_clear[maxn], cur_mask_cur = 0;
  685.  
  686. void dfs_root_masks(int v, int mask, int p){
  687.     mask = mask ^ ((1ll << (city_l[v] - 'a')));
  688.     root_masks[mask]++;
  689.     root_masks_clear[root_masks_cur++] = mask;
  690.     for (auto u : g[v])
  691.         if (u != p && level[u] == -1)
  692.             dfs_root_masks(u, mask, v);
  693. }
  694.  
  695.  
  696.  
  697. void dfs_cur_mask(int v, int mask, int p){
  698.     mask = mask ^ ((1ll << (city_l[v] - 'a')));
  699.     cur_mask[mask]++;
  700.     cur_mask_clear[cur_mask_cur++] = mask;
  701.     for (auto u : g[v])
  702.         if (u != p && level[u] == -1)
  703.             dfs_cur_mask(u, mask, v);
  704. }
  705.  
  706. int dfs_calc(int v, int center, int mask, int p) {
  707.     mask = mask ^ ((1ll << (city_l[v] - 'a')));
  708.     int res = 0;
  709.     for (auto i : good){
  710.         res += (root_masks[mask ^ i] - cur_mask[mask ^ i  ^ (1ll << (city_l[center] - 'a'))]);
  711.     }
  712.     for (auto u : g[v]){
  713.         if (u != p && level[u] == -1)
  714.             res += dfs_calc(u, center, mask, v);
  715.     }
  716.  
  717.     ans[v] += res;
  718.     return res;
  719. }
  720.  
  721.  
  722. int dfs_(int v, int cur_mask = 0, int p = -1){
  723.     cur_mask = cur_mask ^ (1ll << (city_l[v] - 'a'));
  724.     int cur = 0;
  725.     if (count(all(good), cur_mask))
  726.         ++cur;
  727.     for (auto u : g[v])
  728.         if (u != p && level[u] == -1)
  729.             cur += dfs_(u, cur_mask, v);
  730.     return cur;
  731. }
  732.  
  733. void process(int center){
  734.     vector<int> sons;
  735.     for (auto u : g[center]){
  736.         if (level[u] == -1)
  737.             sons.push_back(u);
  738.     }
  739.  
  740.     while (root_masks_cur > 0){ root_masks[root_masks_clear[--root_masks_cur]] = 0;}
  741.  
  742.  
  743.     dfs_root_masks(center, 0, -1);
  744.     int to_add = 0;
  745.     forn (u, len(sons)){
  746.  
  747.         while (cur_mask_cur > 0){ cur_mask[cur_mask_clear[--cur_mask_cur]] = 0;}
  748.        
  749.         dfs_cur_mask(sons[u], 0, center);
  750.         to_add += dfs_calc(sons[u], center, 0, center);
  751.     }
  752.     ans[center] += (to_add + dfs_(center)) / 2 + 1;
  753. }
  754.  
  755. int dfs(int v, int size, int &center, int p = -1) {
  756.     int sum = 1;
  757.     for (int x : g[v])
  758.         if (level[x] == -1 && x != p)
  759.             sum += dfs(x, size, center, v);
  760.     if (center == -1 && (2 * sum >= size || p == -1))
  761.         center = v;
  762.     return sum;
  763. }
  764.  
  765. void build(int v, int size, int depth, int last) {
  766.     int center = -1;
  767.     dfs(v, size, center);
  768.     process(center);
  769.     level[center] = depth;
  770.     parent[center] = last;
  771.     for (int x : g[center])
  772.         if (level[x] == -1)
  773.             build(x, size / 2, depth + 1, center);
  774. };
  775.  
  776.  
  777. // SCC
  778.  
  779. vector < vector<int> > g, gr;
  780. vector<char> used;
  781. vector<int> order, component;
  782.  
  783. void dfs1 (int v) {
  784.     used[v] = true;
  785.     for (size_t i=0; i<g[v].size(); ++i)
  786.         if (!used[ g[v][i] ])
  787.             dfs1 (g[v][i]);
  788.     order.push_back (v);
  789. }
  790.  
  791. void dfs2 (int v) {
  792.     used[v] = true;
  793.     component.push_back (v);
  794.     for (size_t i=0; i<gr[v].size(); ++i)
  795.         if (!used[ gr[v][i] ])
  796.             dfs2 (gr[v][i]);
  797. }
  798.  
  799. int main() {
  800.     int n;
  801.     ... чтение n ...
  802.     for (;;) {
  803.         int a, b;
  804.         ... чтение очередного ребра (a,b) ...
  805.         g[a].push_back (b);
  806.         gr[b].push_back (a);
  807.     }
  808.  
  809.     used.assign (n, false);
  810.     for (int i=0; i<n; ++i)
  811.         if (!used[i])
  812.             dfs1 (i);
  813.     used.assign (n, false);
  814.     for (int i=0; i<n; ++i) {
  815.         int v = order[n-1-i];
  816.         if (!used[v]) {
  817.             dfs2 (v);
  818.             ... вывод очередной component ...
  819.             component.clear();
  820.         }
  821.     }
  822. }
  823.  
  824.  
  825.  
  826. // SegTree
  827.  
  828.  
  829. const int inf = (int) 1e9 + 7;
  830. struct node {
  831.     node *left, *right;
  832.     int l, r;
  833.     int max_val;
  834.     int add;
  835.  
  836.     static int allocated; static char* buf; void* operator new(size_t) { return buf + (allocated++ * sizeof(node));}
  837. };
  838.  
  839. int node::allocated = 0;
  840. char* node::buf = new char[(int) 1e8];
  841.  
  842.  
  843. node* build(int l, int r){
  844.     node* v = new node(); v->l = l; v->r = r; v->max_val = v->add = 0;
  845.     if (l == r){
  846.         v->left = v->right = nullptr;
  847.         return v;
  848.     }
  849.     int m = (l + r) / 2;
  850.     v->left = build(l, m);
  851.     v->right = build(m + 1, r);
  852.     return v;
  853. }
  854.  
  855. int plus1(node* v, int l, int r, int val){
  856.     if (l <= v-> l && v->r <= r){
  857.         v->add += val;
  858.         return v->max_val + v->add;
  859.     }
  860.     if (v->l > r || l > v->r){
  861.         return v->max_val + v->add;
  862.     }
  863.     v->max_val = max(plus1(v->left, l, r, val), plus1(v->right, l, r, val));
  864.     return v->max_val + v->add;
  865. }
  866.  
  867. int get(node* v, int l, int r){
  868.     if (l <= v-> l && v->r <= r){
  869.         return v->max_val + v->add;
  870.     }
  871.     if (v->l > r || l > v->r){
  872.         return -inf;
  873.     }
  874.     return max(get(v->left, l, r), get(v->right, l, r)) + v->add;
  875. }
  876.  
  877.  
  878. typedef int ftype;
  879. typedef complex<ftype> point;
  880. #define x real
  881. #define y imag
  882.  
  883. ftype dot(point a, point b) {
  884.     return (conj(a) * b).x();
  885. }
  886.  
  887. ftype cross(point a, point b) {
  888.     return (conj(a) * b).y();
  889. }
  890.  
  891. vector<point> hull, vecs;
  892.  
  893. void add_line(ftype k, ftype b) {
  894.     point nw = {k, b};
  895.     while(!vecs.empty() && dot(vecs.back(), nw - hull.back()) < 0) {
  896.         hull.pop_back();
  897.         vecs.pop_back();
  898.     }
  899.     if(!hull.empty()) {
  900.         vecs.push_back(1i * (nw - hull.back()));
  901.     }
  902.     hull.push_back(nw);
  903. }
  904.  
  905. int get(ftype x) {
  906.     point query = {x, 1};
  907.     auto it = lower_bound(vecs.begin(), vecs.end(), query, [](point a, point b) {
  908.         return cross(a, b) > 0;
  909.     });
  910.     return dot(query, hull[it - vecs.begin()]);
  911. }
  912.  
  913.  
  914.  
  915.  
  916. mt19937 rnd(2007);
  917. namespace GeneralMatchingCheat {
  918.     int n;
  919.     vector<vector<int>> g;
  920.     vector<int> mt;
  921.     vector<bool> used;
  922.  
  923.  
  924.     bool dfs(int v) {
  925.         used[v] = true;
  926.         shuffle(all(g[v]), rnd);
  927.         for (auto u : g[v]) {
  928.             if (mt[u] == -1) {
  929.                 mt[v] = u;
  930.                 mt[u] = v;
  931.                 return true;
  932.             }
  933.             int to = mt[u];
  934.             mt[v] = u;
  935.             mt[u] = v;
  936.             mt[to] = -1;
  937.             if (!used[to] && dfs(to)) {
  938.                 return true;
  939.             } else {
  940.                 mt[v] = -1;
  941.                 mt[u] = to;
  942.                 mt[to] = u;
  943.             }
  944.         }
  945.         return false;
  946.     }
  947.  
  948.     vector<pair<int, int>> solve(int _n, vector<pair<int, int>> edges) {
  949.         n = _n; g.assign(n, vector<int>(0));
  950.         for (auto e : edges){
  951.             g[e.first].push_back(e.second);
  952.             g[e.second].push_back(e.first);
  953.         }
  954.         mt.assign(n, -1);
  955.         forn (qwe, 50) {
  956.             vector<int> a;
  957.             forn (i, n) if (mt[i] == -1) a.push_back(i);
  958.             if (len(a) == 0)
  959.                 break;
  960.             shuffle(all(a), rnd);
  961.             used.assign(n, false);
  962.             for (auto i : a) {
  963.                 if (mt[i] == -1) {
  964.                     dfs(i);
  965.                 }
  966.             }
  967.         }
  968.  
  969.         vector<pair<int, int>> ans;
  970.         forn (i, n){
  971.             if (mt[i] > i)
  972.                 ans.push_back({i, mt[i]});
  973.         }
  974.         return ans;
  975.     }
  976. }
  977.  
  978.  
  979. vector<int> z_function (string s) {
  980.     int n = (int) s.length();
  981.     vector<int> z (n);
  982.     for (int i=1, l=0, r=0; i<n; ++i) {
  983.         if (i <= r)
  984.             z[i] = min (r-i+1, z[i-l]);
  985.         while (i+z[i] < n && s[z[i]] == s[i+z[i]])
  986.             ++z[i];
  987.         if (i+z[i]-1 > r)
  988.             l = i,  r = i+z[i]-1;
  989.     }
  990.     return z;
  991. }
  992.  
  993.  
  994. vector<int> prefix_function (string s) {
  995.     int n = (int) s.length();
  996.     vector<int> pi (n);
  997.     for (int i=1; i<n; ++i) {
  998.         int j = pi[i-1];
  999.         while (j > 0 && s[i] != s[j])
  1000.             j = pi[j-1];
  1001.         if (s[i] == s[j])  ++j;
  1002.         pi[i] = j;
  1003.     }
  1004.     return pi;
  1005. }
  1006.  
  1007.  
  1008.  
  1009. vector<pt> convex_hull(vector<pt> pts) {
  1010.     vector<pt> res;
  1011.     sort(pts.begin(), pts.end(), [](pt a, pt b) -> bool { return a.x != b.x ? a.x < b.x : a.y < b.y; });
  1012.     pts.resize(unique(all(pts)) - pts.begin());
  1013.     sort(pts.begin() + 1, pts.end(), [&](pt a, pt b) -> bool {
  1014.         if (((a - pts[0]) % (b - pts[0])) != 0) return ((a - pts[0]) % (b - pts[0])) > 0;
  1015.         return (a - pts[0]).sz() < (b - pts[0]).sz();
  1016.     });
  1017.     for (pt u : pts) {
  1018.         while (len(res) >= 2 && ((u - res[len(res) - 2]) % (res[len(res) - 1] - res[len(res) - 2])) >= 0)
  1019.             res.pop_back();
  1020.         res.push_back(u);
  1021.     }
  1022.     return res;
  1023. }
  1024.  
  1025.  
  1026.  
  1027.  
  1028. bool try_kuhn (int v) {
  1029.     if (used[v])  return false;
  1030.     used[v] = true;
  1031.     for (size_t i=0; i<g[v].size(); ++i) {
  1032.         int to = g[v][i];
  1033.         if (mt[to] == -1 || try_kuhn (mt[to])) {
  1034.             mt[to] = v;
  1035.             return true;
  1036.         }
  1037.     }
  1038.     return false;
  1039. }
  1040.  
  1041. for (int m=0; m<(1<<n); ++m)
  1042.     for (int s=m; s; s=(s-1)&m)
  1043.  
  1044.    
  1045. for (int k = 0; k < n; k++)
  1046.         for (int mask = 0; mask < (1 << n); mask++)
  1047.             if ((mask >> k) & 1)
  1048.                 a[mask] += a[mask ^ (1 << k)];
  1049.  
  1050.  
  1051.  
  1052.  
  1053. // MinCost
  1054.  
  1055.  
  1056. using namespace std;
  1057. const int N = 401;
  1058. struct edge {
  1059.     int to, cap, val, rev;
  1060. };
  1061. vector<edge> G[N];
  1062. int d[N], flow[N], pre[N][2];
  1063. bool v[N];
  1064. queue<int> Q;
  1065. int main() {
  1066.     int n, m;
  1067.     scanf("%d%d", &n, &m);
  1068.     for (int i = 1; i <= m; ++i) {
  1069.         int u, v, w, f;
  1070.         scanf("%d%d%d%d", &u, &v, &w, &f);
  1071.         G[u].push_back({ v, w, f, G[v].size() });
  1072.         G[v].push_back({ u, 0, -f, G[u].size() - 1 });
  1073.     }
  1074.     int ans0 = 0, ans1 = 0;
  1075.     for (;;) {
  1076.         for (int i = 1; i <= n; ++i) d[i] = 2147483647;
  1077.         memset(flow, -1, sizeof flow);
  1078.         memset(v, 0, sizeof v);
  1079.         d[1] = 0;
  1080.         flow[1] = 2147483647;
  1081.         v[1] = 1;
  1082.         Q.push(1);
  1083.         memset(pre, -1, sizeof pre);
  1084.         for (; !Q.empty();) {
  1085.             int x = Q.front();
  1086.             Q.pop();
  1087.             v[x] = 0;
  1088.             for (int i = 0; i < G[x].size(); ++i) {
  1089.                 edge e = G[x][i];
  1090.                 if (e.cap && d[x] + e.val < d[e.to]) {
  1091.                     d[e.to] = d[x] + e.val;
  1092.                     flow[e.to] = flow[x] < e.cap ? flow[x] : e.cap;
  1093.                     pre[e.to][0] = x;
  1094.                     pre[e.to][1] = i;
  1095.                     if (!v[e.to])
  1096.                         v[e.to] = 1, Q.push(e.to);
  1097.                 }
  1098.             }
  1099.         }
  1100.         if (flow[n] == -1)
  1101.             break;
  1102.         ans0 += flow[n];
  1103.         ans1 += d[n] * flow[n];
  1104.         for (int i = n; pre[i][0] != -1; i = pre[i][0]) {
  1105.             G[pre[i][0]][pre[i][1]].cap -= flow[n];
  1106.             G[i][G[pre[i][0]][pre[i][1]].rev].cap += flow[n];
  1107.         }
  1108.     }
  1109.     printf("%d %d", ans0, ans1);
  1110.  
  1111.  
  1112. // Some useless geometry
  1113.  
  1114. struct pt {
  1115.     int x, y;
  1116. };
  1117.  
  1118. struct ang {
  1119.     int a, b;
  1120. };
  1121.  
  1122. bool operator < (const ang & p, const ang & q) {
  1123.     if (p.b == 0 && q.b == 0)
  1124.         return p.a < q.a;
  1125.     return p.a * 1ll * q.b < p.b * 1ll * q.a;
  1126. }
  1127.  
  1128. long long sq (pt & a, pt & b, pt & c) {
  1129.     return a.x*1ll*(b.y-c.y) + b.x*1ll*(c.y-a.y) + c.x*1ll*(a.y-b.y);
  1130. }
  1131.  
  1132. int main() {
  1133.  
  1134.     int n;
  1135.     cin >> n;
  1136.     vector<pt> p (n);
  1137.     int zero_id = 0;
  1138.     for (int i=0; i<n; ++i) {
  1139.         scanf ("%d%d", &p[i].x, &p[i].y);
  1140.         if (p[i].x < p[zero_id].x || p[i].x == p[zero_id].x && p[i].y < p[zero_id].y)
  1141.             zero_id = i;
  1142.     }
  1143.     pt zero = p[zero_id];
  1144.     rotate (p.begin(), p.begin()+zero_id, p.end());
  1145.     p.erase (p.begin());
  1146.     --n;
  1147.  
  1148.     vector<ang> a (n);
  1149.     for (int i=0; i<n; ++i) {
  1150.         a[i].a = p[i].y - zero.y;
  1151.         a[i].b = p[i].x - zero.x;
  1152.         if (a[i].a == 0)
  1153.             a[i].b = a[i].b < 0 ? -1 : 1;
  1154.     }
  1155.  
  1156.     for (;;) {
  1157.         pt q; // очередной запрос
  1158.         bool in = false;
  1159.         if (q.x >= zero.x)
  1160.             if (q.x == zero.x && q.y == zero.y)
  1161.                 in = true;
  1162.             else {
  1163.                 ang my = { q.y-zero.y, q.x-zero.x };
  1164.                 if (my.a == 0)
  1165.                     my.b = my.b < 0 ? -1 : 1;
  1166.                 vector<ang>::iterator it = upper_bound (a.begin(), a.end(), my);
  1167.                 if (it == a.end() && my.a == a[n-1].a && my.b == a[n-1].b)
  1168.                     it = a.end()-1;
  1169.                 if (it != a.end() && it != a.begin()) {
  1170.                     int p1 = int (it - a.begin());
  1171.                     if (sq (p[p1], p[p1-1], q) <= 0)
  1172.                         in = true;
  1173.                 }
  1174.             }
  1175.         puts (in ? "INSIDE" : "OUTSIDE");
  1176.     }
  1177.  
  1178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement