#include <bits/stdc++.h> n using namespace std; n void read(long long &x) { scanf( %lld , &x); } n const long long N = 1000005; n struct city { n   long long cnt, id; n   bool operator<(const city b) const { return cnt < b.cnt; } n } a[N]; n struct que { n   long long x, id; n   bool operator<(const que b) const { return x < b.x; } n } qq[N]; n long long up[N], ans[N]; n long long n, m, q; n struct node { n   long long l, r, val; n   long long cnt, sz, rnd; n } tr[N]; n long long tot, rt; n void update(long long rt) { n   tr[rt].sz = tr[tr[rt].l].sz + tr[tr[rt].r].sz + tr[rt].cnt; n } n void lturn(long long &rt) { n   long long tmp = tr[rt].r; n   tr[rt].r = tr[tmp].l; n   tr[tmp].l = rt; n   tr[tmp].sz = tr[rt].sz; n   update(rt); n   rt = tmp; n } n void rturn(long long &rt) { n   long long tmp = tr[rt].l; n   tr[rt].l = tr[tmp].r; n   tr[tmp].r = rt; n   tr[tmp].sz = tr[rt].sz; n   update(rt); n   rt = tmp; n } n void Insert(long long &rt, long long x) { n   if (!rt) { n     rt = ++tot; n     tr[rt].sz = tr[rt].cnt = 1; n     tr[rt].val = x, tr[rt].rnd = rand(); n     return; n   } n   tr[rt].sz++; n   if (tr[rt].val == x) n     tr[rt].cnt++; n   else if (tr[rt].val < x) { n     Insert(tr[rt].r, x); n     if (tr[tr[rt].r].rnd < tr[rt].rnd) lturn(rt); n   } else { n     Insert(tr[rt].l, x); n     if (tr[tr[rt].l].rnd < tr[rt].rnd) rturn(rt); n   } n } n void Delete(long long &rt, long long x) { n   if (!rt) return; n   if (tr[rt].val == x) { n     if (tr[rt].cnt > 1) n       tr[rt].cnt--, tr[rt].sz--; n     else { n       if (tr[rt].l * tr[rt].r == 0) n         rt = tr[rt].l + tr[rt].r; n       else if (tr[tr[rt].l].rnd < tr[tr[rt].r].rnd) n         rturn(rt), Delete(rt, x); n       else n         lturn(rt), Delete(rt, x); n     } n   } else if (tr[rt].val < x) n     tr[rt].sz--, Delete(tr[rt].r, x); n   else n     tr[rt].sz--, Delete(tr[rt].l, x); n } n long long findnum(long long rt, long long x) { n   if (!rt) return -1; n   if (x <= tr[tr[rt].l].sz) return findnum(tr[rt].l, x); n   x -= tr[tr[rt].l].sz; n   if (x <= tr[rt].cnt) return tr[rt].val; n   x -= tr[rt].cnt; n   return findnum(tr[rt].r, x); n } n void doit() { n   long long kind = 0; n   for (long long _ = 1; _ <= q; _++) { n     while (kind < m && up[kind] < qq[_].x) { n       kind++; n       Insert(rt, a[kind].id); n     } n     long long dis = qq[_].x - up[kind - 1]; n     dis = (dis - 1) % kind + 1; n     ans[qq[_].id] = findnum(rt, dis); n   } n } n signed main() { n   read(n), read(m), read(q); n   for (long long i = 1; i <= m; i++) a[i].id = i; n   for (long long i = 1; i <= n; i++) { n     long long x; n     scanf( %d , &x); n     a[x].cnt++; n   } n   sort(a + 1, a + m + 1); n   for (long long i = 1; i <= m; i++) { n     up[i] = (long long)i * (a[i + 1].cnt - a[i].cnt); n     up[i] += up[i - 1]; n   } n   for (long long _ = 1; _ <= q; _++) { n     long long x; n     scanf( %lld , &x); n     x -= n; n     qq[_] = (que){x, _}; n   } n   sort(qq + 1, qq + q + 1); n   doit(); n   for (long long i = 1; i <= q; i++) printf( %lld n , ans[i]); n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n template <class T1, class T2> n ostream &operator<<(ostream &os, const pair<T1, T2> &p) { n   os <<  {  << p.first <<  ,  << p.second <<  } ; n   return os; n } n const int N = 3e5 + 5; n const int oo = 1e9 + 7; n const int LOGN = 30; n long long bigmod(long long a, long long p, long long m) { n   long long res = 1; n   long long x = a; n   while (p) { n     if (p & 1) { n       res = (res * x) % m; n     } n     x = (x * x) % m; n     p = p >> 1; n   } n   return res; n } n int basis[N][LOGN]; n void insert_vec(int t, int x) { n   for (int i = LOGN - 1; i >= 0; i--) basis[t][i] = basis[t - 1][i]; n   for (int i = LOGN - 1; i >= 0; i--) { n     if ((x >> i) & 1) { n       if (!basis[t][i]) { n         basis[t][i] = x; n         return; n       } else { n         x ^= basis[t][i]; n       } n     } n   } n } n int ans(int t, int x) { n   int tot = t; n   for (int i = LOGN - 1; i >= 0; i--) { n     if (basis[t][i]) { n       tot--; n       if ((x >> i) & 1) x ^= basis[t][i]; n     } else { n       if ((x >> i) & 1) return 0; n     } n   } n   return bigmod(2, tot, oo); n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     insert_vec(i, x); n   } n   while (q--) { n     int l, x; n     cin >> l >> x; n     cout << ans(l, x) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1LL << 61; n const long long mod = 1000000007; n const int maxn = 404; n bitset<maxn> cb[30][maxn]; n bitset<maxn> ans[maxn]; n bitset<maxn> shift(bitset<maxn> b, int len, int sh); n int main() { n   std::ios::sync_with_stdio(false); n   int i = 0, j = 0, k = 0; n   int n, m, c, r; n   scanf( %d%d , &n, &m); n   getchar(); n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       char ch = getchar(); n       cb[ch -  a ][i][j] = true; n     } n     getchar(); n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       ans[i][j] = true; n     } n   } n   scanf( %d%d , &r, &c); n   getchar(); n   for (i = 0; i < r; i++) { n     for (j = 0; j < c; j++) { n       char ch = getchar(); n       if (ch ==  ? ) continue; n       int shiftx = ((-i) % n + n) % n; n       int shifty = j % m; n       for (k = 0; k < n; k++) { n         int nk = k + shiftx; n         if (nk >= n) nk -= n; n         ans[nk] &= shift(cb[ch -  a ][k], m, shifty); n       } n     } n     getchar(); n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       printf( %d , ans[i][j] ? 1 : 0); n     } n     puts(  ); n   } n   return 0; n } n bitset<maxn> shift(bitset<maxn> b, int len, int sh) { n   return (b >> sh) | (b << (len - sh)); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)3e5 + 10; n struct node { n   int nxt, to; n } edg[maxn * 2]; n int head[maxn], num; n int f[maxn], n, a[maxn]; n void add(int a, int b) { n   edg[++num].to = b; n   edg[num].nxt = head[a]; n   head[a] = num; n } n int k = 0; n void dfs(int root) { n   if (head[root] == 0) { n     f[root] = 1; n     k++; n     return; n   } n   if (a[root] == 1) f[root] = 1e7; n   int t = head[root]; n   while (t != 0) { n     dfs(edg[t].to); n     if (a[root] == 1) { n       f[root] = min(f[root], f[edg[t].to]); n     } else { n       f[root] += f[edg[t].to]; n     } n     t = edg[t].nxt; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 2; i <= n; i++) { n     int fat; n     cin >> fat; n     add(fat, i); n   } n   dfs(1); n   cout << k + 1 - f[1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, k, tt; n int tree1[800000], tree2[800000]; n void updata1(int L, int R, int ro, int x, int v) { n   if (x < L || x > R) return; n   if (L == R && L == x) { n     tree1[ro] = min(v + tree1[ro], b); n     return; n   } n   int Mid = (L + R) / 2; n   updata1(L, Mid, ro * 2, x, v); n   updata1(Mid + 1, R, ro * 2 + 1, x, v); n   tree1[ro] = tree1[ro * 2] + tree1[ro * 2 + 1]; n } n void updata2(int L, int R, int ro, int x, int v) { n   if (x < L || x > R) return; n   if (L == R && L == x) { n     tree2[ro] = min(v + tree2[ro], a); n     return; n   } n   int Mid = (L + R) / 2; n   updata2(L, Mid, ro * 2, x, v); n   updata2(Mid + 1, R, ro * 2 + 1, x, v); n   tree2[ro] = tree2[ro * 2] + tree2[ro * 2 + 1]; n } n int quary1(int ro, int L, int R, int l, int r) { n   if (r < L || l > R) return 0; n   if (l <= L && R <= r) return tree1[ro]; n   int Mid = (L + R) / 2; n   int x1 = quary1(ro * 2, L, Mid, l, r), n       x2 = quary1(ro * 2 + 1, Mid + 1, R, l, r); n   return x1 + x2; n } n int quary2(int ro, int L, int R, int l, int r) { n   if (r < L || l > R) return 0; n   if (l <= L && R <= r) return tree2[ro]; n   int Mid = (L + R) / 2; n   int x1 = quary2(ro * 2, L, Mid, l, r), n       x2 = quary2(ro * 2 + 1, Mid + 1, R, l, r); n   return x1 + x2; n } n int main() { n   scanf( %d%d%d%d%d , &n, &k, &a, &b, &tt); n   for (int i = 1; i <= tt; i++) { n     int a1, b1, c1; n     scanf( %d , &a1); n     if (a1 == 1) { n       scanf( %d%d , &b1, &c1); n       updata1(1, n, 1, b1, c1); n       updata2(1, n, 1, b1, c1); n     } else if (a1 == 2) { n       scanf( %d , &b1); n       printf( %d n , quary1(1, 1, n, 1, b1 - 1) + quary2(1, 1, n, b1 + k, n)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fa2[200007], fb2[200007]; n bool f(int i, int j, long long y) { n   long long x1, x2; n   if ((y - fb2[i]) % fa2[i] == 0) { n     x1 = (y - fb2[i]) / fa2[i]; n   } else n     x1 = (y - fb2[i]) / fa2[i] + 1; n   if ((y - fb2[j]) % fa2[j] == 0) { n     x2 = (y - fb2[j]) / fa2[j]; n   } else n     x2 = (y - fb2[j]) / fa2[j] + 1; n   if (x1 == x2) { n     return (x1 * fa2[i] + fb2[i]) <= (x2 * fa2[j] + fb2[j]); n   } n   return x1 > x2; n } n long long fcheck2(int i1, int i2, int i3) { n   return (fa2[i2] - fa2[i1]) * (fb2[i3] - fb2[i2]) >= n          (fb2[i2] - fb2[i1]) * (fa2[i3] - fa2[i2]); n } n long long f2(int i, long long x) { return fa2[i] * x + fb2[i]; } n long long ff(int i, long long y) { n   long long x1, x2; n   if ((y - fb2[i]) % fa2[i] == 0) { n     x1 = (y - fb2[i]) / fa2[i]; n   } else n     x1 = (y - fb2[i]) / fa2[i] + 1; n   return x1; n } n struct Factory { n   long long v, c; n   bool operator<(const Factory &p) const { n     if (c == p.c) return v > p.v; n     return c < p.c; n   } n } a[200007]; n long long dpt[200007]; n long long dpm[200007]; n int dep[200007]; n int dep2[200007]; n int n; n long long s; n void solve() { n   int l = 0, l2 = 0, r2 = 1; n   fa2[0] = a[0].v; n   fb2[0] = 0; n   dep2[0] = 0; n   for (int i = 0; i < n - 1; i++) { n     while (l + 1 < r2 && f(dep2[l], dep2[l + 1], a[i + 1].c)) l++; n     dpt[i + 1] = ff(dep2[l], a[i + 1].c); n     while (l2 + 1 < r2 && n            f2(dep2[l2], dpt[i + 1]) < f2(dep2[l2 + 1], dpt[i + 1])) n       l2++; n     dpm[i + 1] = f2(dep2[l2], dpt[i + 1]) - a[i + 1].c; n     fa2[i + 1] = a[i + 1].v; n     fb2[i + 1] = dpm[i + 1] - a[i + 1].v * dpt[i + 1]; n     while (l2 + 1 < r2 && fcheck2(dep2[r2 - 2], dep2[r2 - 1], i + 1)) r2--; n     dep2[r2++] = i + 1; n   } n } n int main() { n   scanf( %d%I64d , &n, &s); n   for (int i = 0; i < n; i++) scanf( %I64d%I64d , &a[i].v, &a[i].c); n   a[n].c = s; n   a[n].v = 2000000000; n   n++; n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n   } n   int cnt = 0; n   for (int i = 1; i < n; i++) { n     if (a[i].v > a[cnt].v) { n       cnt++; n       a[cnt] = a[i]; n     } n   } n   cnt++; n   n = cnt; n   dpt[0] = 0; n   dpm[0] = 0; n   solve(); n   for (int i = 0; i < n; i++) { n     if (a[i].c == s) { n       printf( %I64d n , dpt[i]); n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a, b, x, y, z, ans, g[3010 * 3010], h[3010][3010], n     f[3010][3010]; n deque<long long> q, zr; n int main() { n   scanf( %lld%lld%lld%lld%lld%lld%lld%lld , &n, &m, &a, &b, g, &x, &y, &z); n   for (int i = 1; i <= n * m; i++) g[i] = (g[i - 1] * x + y) % z; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) h[i][j] = g[(i - 1) * m + j - 1]; n   for (int i = 1; i <= n; i++) { n     q = zr; n     for (int j = 1; j <= m; j++) { n       while (!q.empty() && h[i][q.back()] >= h[i][j]) q.pop_back(); n       while (!q.empty() && q.front() + b - 1 < j) q.pop_front(); n       q.push_back(j), f[i][j] = h[i][q.front()]; n     } n   } n   for (long long j = 1; j <= m; j++) { n     while (!q.empty()) q.pop_front(); n     for (long long i = 1; i <= n; i++) { n       while (!q.empty() && f[q.back()][j] >= f[i][j]) q.pop_back(); n       if (!q.empty() && q.front() + a - 1 < i) q.pop_front(); n       q.push_back(i); n       if (i >= a && j >= b) ans += f[q.front()][j]; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pie = 3.14159265358979323846; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   double d, h, v, e, t = 0; n   cin >> d >> h >> v >> e; n   if (v <= (pie * (d / 2) * (d / 2) * e)) { n     cout <<  NO n ; n   } else { n     t = (pie * (d / 2) * (d / 2) * h) / (v - (pie * (d / 2) * (d / 2) * e)); n     if (t <= 10000) { n       cout <<  YES n  << fixed << setprecision(15) << t <<   n ; n     } else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 35; n const int N = 2e5; n const long long mod = 1e7; n const double eps = 1e-6; n int a[MAX]; n long long n; n long long ans; n pair<long long, long long> ch(long long x) { n   long long x2 = n - x; n   long long s = x; n   s += (x2 * (x2 - 1)) / 2; n   if (((s + 1) / 2) >= x) { n     pair<long long, long long> k; n     k.first = 1; n     k.second = min(x * 2, s); n     return k; n   } else n     return {0, 0}; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   int t; n   cin >> t; n   while (t--) { n     ans = 0; n     cin >> n; n     if (n == 1) { n       cout <<  0 n ; n       continue; n     } n     long long l = 0; n     long long r = n - 1; n     while (l <= r) { n       long long mid = (l + r) / 2; n       if (ch(mid).first) { n         ans = max(ans, ch(mid).second); n         l = mid + 1; n       } else n         r = mid - 1; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   static int r, sign; n   static char c; n   r = 0, sign = 1; n   do c = getchar(); n   while (c !=  -  && (c <  0  || c >  9 )); n   if (c ==  - ) sign = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) r = r * 10 + (int)(c -  0 ), c = getchar(); n   return sign * r; n } n template <typename T> n inline void print(T *a, int n) { n   for (int i = 1; i < n; ++i) cout << a[i] <<    ; n   cout << a[n] << endl; n } n struct inedge { n   int a, b, w; n   inline bool operator<(const inedge &x) const { return w < x.w; } n } ie[100000 + 1], p[100000 + 1]; n struct edge { n   int next, node; n } e[100000 << 1 | 1]; n int head[100000 + 1], tot = 0; n inline void addedge(int a, int b) { n   e[++tot].next = head[a]; n   head[a] = tot, e[tot].node = b; n } n int n, m, a[100000 + 1]; n int f[100000 + 1], size[100000 + 1]; n int find(int x) { n   if (f[x] == x) return x; n   int fa = f[x]; n   f[x] = find(fa); n   return f[x]; n } n void fusion(int a, int b) { n   a = find(a), b = find(b); n   f[a] = b, size[b] += size[a]; n } n int main(int argc, char *argv[]) { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   for (int i = 1; i <= m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     ie[i].a = x, ie[i].b = y, ie[i].w = min(a[x], a[y]); n   } n   sort(ie + 1, ie + m + 1); n   for (int i = 1; i <= n; ++i) f[i] = i; n   int cnt = 0; n   for (int i = m; i > 0; --i) n     if (find(ie[i].a) != find(ie[i].b)) { n       f[find(ie[i].a)] = find(ie[i].b); n       addedge(ie[i].a, ie[i].b); n       addedge(ie[i].b, ie[i].a); n       p[++cnt] = ie[i]; n     } n   long long ans = 0LL; n   for (int i = 1; i <= n; ++i) f[i] = i, size[i] = 1; n   for (int i = 1; i <= cnt; ++i) { n     ans += (long long)p[i].w * size[find(p[i].a)] * size[find(p[i].b)]; n     fusion(p[i].a, p[i].b); n   } n   cout << fixed << setprecision(10) << (double)ans * 2LL / n / (n - 1) << endl; n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n const int dy[] = {-1, 1, 0, 0}; n const int dx[] = {0, 0, -1, 1}; n int n, m; n string s[1010]; n int L[1010][1010]; n int R[1010][1010]; n int U[1010][1010]; n int D[1010][1010]; n int usedX[1010][1010], usedY[1010][1010]; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   cout << fixed << setprecision(10); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   for (int i = 0; i < n; i++) { n     L[i][0] = s[i][0] ==  * ; n     for (int j = 1; j < m; j++) { n       if (s[i][j] ==  . ) n         L[i][j] = 0; n       else n         L[i][j] = L[i][j - 1] + 1; n     } n     R[i][m - 1] = s[i][m - 1] ==  * ; n     for (int j = m - 2; j >= 0; j--) { n       if (s[i][j] ==  . ) n         R[i][j] = 0; n       else n         R[i][j] += R[i][j + 1] + 1; n     } n   } n   for (int j = 0; j < m; j++) { n     U[0][j] = s[0][j] ==  * ; n     for (int i = 1; i < n; i++) { n       if (s[i][j] ==  . ) n         U[i][j] = 0; n       else n         U[i][j] = U[i - 1][j] + 1; n     } n     D[n - 1][j] = s[n - 1][j] ==  * ; n     for (int i = n - 2; i >= 0; i--) { n       if (s[i][j] ==  . ) n         D[i][j] = 0; n       else n         D[i][j] += D[i + 1][j] + 1; n     } n   } n   using P = tuple<int, int, int>; n   vector<P> vs; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int mx = min({U[i][j], R[i][j], D[i][j], L[i][j]}); n       if (mx > 1) { n         vs.emplace_back(i, j, mx - 1); n         if (j - mx + 1 >= 0) usedX[i][j - mx + 1]++; n         if (j + mx <= m) usedX[i][j + mx]--; n         if (i - mx + 1 >= 0) usedY[i - mx + 1][j]++; n         if (i + mx <= n) usedY[i + mx][j]--; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       usedX[i][j + 1] += usedX[i][j]; n     } n   } n   for (int j = 0; j < m; j++) { n     for (int i = 0; i < n; i++) { n       usedY[i + 1][j] += usedY[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  *  && !(usedY[i][j] || usedX[i][j])) { n         cout <<  -1  << endl; n         return 0; n       } n     } n   } n   cout << vs.size() << endl; n   for (auto v : vs) { n     int y, x, p; n     tie(y, x, p) = v; n     cout << y + 1 <<     << x + 1 <<     << p << endl; n   } n   return 0; n } n


#include <cstdio> n #include <cstdlib> n #include <cstring> n #include <cmath> n #include <ctime> n #include <iostream> n #include <algorithm> n #include <queue> n #include <map> n #include <set> n #include <vector> n #include <string> n #include <stack> n #include <numeric> n #include <cassert> n  n #define endl   n  n #define sp     n  n #define pb push_back n #define mp make_pair n #define ff first n #define ss second n  n using namespace std; n  n typedef long long ll; n typedef pair<int,int> pii; n typedef pair<ll,ll> pll; n  n const int maxn = 2e5+5; n set<pair<int,char>> adj[maxn]; n  n int main(){ n      n     ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);cerr.tie(nullptr); n     int n,m;cin>>n>>m; n     set<pii> cift, samecift; n     for(int q=0;q<m;q++){ n         char t;cin>>t; n         if(t ==  + ){ n             int u,v;char c;cin>>u>>v>>c; n             pair<int,char> p = {v,c}; n             adj[u].emplace(v,c); n             auto it = adj[v].lower_bound({u,0}); n             if(it != adj[v].end() && it->ff == u){ n                 int setu = min(u,v); n                 int setv = max(u,v); n                 cift.emplace(setu,setv); n                 if(it->ss == c){ n                     samecift.emplace(setu,setv); n                 } n             } n         } n         else if(t ==  - ){ n             int u,v;cin>>u>>v; n             auto it = adj[u].lower_bound({v,0}); n             assert(it != adj[u].end()); n             adj[u].erase(it); n             int setu = min(u,v); n             int setv = max(u,v); n             auto it2 = cift.find({setu,setv}); n             if(it2 != cift.end()){ n                 cift.erase(it2); n                 it2 = samecift.find({setu,setv}); n                 if(it2 != samecift.end()){ n                     samecift.erase(it2); n                 } n             } n         } n         else{ n             int k;cin>>k; n             if(k&1){ n                 if(cift.size()){ n                     cout <<  YES  << endl; n                 } n                 else n                     cout <<  NO  << endl; n             } n             else{ n                 if(samecift.size()){ n                     cout <<  YES  << endl; n                 } n                 else{ n                     cout <<  NO  << endl; n                 } n             }        n         } n     } n      n } n  n  n  n  n  n  n  n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 10; n int rd() { n   int x = 0, w = 1; n   char ch = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + (ch ^ 48); n     ch = getchar(); n   } n   return x * w; n } n int to[N], nt[N], hd[N], tot = 1; n void adde(int x, int y) { ++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot; } n char cc[N]; n int n, ans, f[N], g[N], s[N * 50], ch[N * 50][2], rt[N], tz; n void inst(int o, int x) { n   s[o] = x; n   int l = 1, r = n; n   while (l < r) { n     int mid = (l + r) >> 1; n     if (x <= mid) n       o = ch[o][0] = ++tz, r = mid; n     else n       o = ch[o][1] = ++tz, l = mid + 1; n     s[o] = x; n   } n } n int merg(int o1, int o2) { n   if (!o1 || !o2) return o1 + o2; n   int o = ++tz; n   s[o] = max(s[o1], s[o2]); n   ch[o][0] = merg(ch[o1][0], ch[o2][0]); n   ch[o][1] = merg(ch[o1][1], ch[o2][1]); n   return o; n } n int quer(int o, int l, int r, int ll, int rr) { n   if (!o || ll > rr) return s[0]; n   if (ll <= l && r <= rr) return s[o]; n   int an = s[0], mid = (l + r) >> 1; n   if (ll <= mid) an = max(an, quer(ch[o][0], l, mid, ll, rr)); n   if (rr > mid) an = max(an, quer(ch[o][1], mid + 1, r, ll, rr)); n   return an; n } n int fa[N], tn[N][26], len[N], la = 1, tt = 1; n void extd(int xx, int i) { n   int np = ++tt, p = la; n   len[np] = len[p] + 1, inst(rt[np] = ++tz, i), la = np; n   while (p && !tn[p][xx]) tn[p][xx] = np, p = fa[p]; n   if (!p) n     fa[np] = 1; n   else { n     int q = tn[p][xx]; n     if (len[q] == len[p] + 1) n       fa[np] = q; n     else { n       int nq = ++tt; n       fa[nq] = fa[q], len[nq] = len[p] + 1, n       memcpy(tn[nq], tn[q], sizeof(int) * 26); n       fa[np] = fa[q] = nq; n       while (p && tn[p][xx] == q) tn[p][xx] = nq, p = fa[p]; n     } n   } n } n void dfs1(int x) { n   for (int i = hd[x]; i; i = nt[i]) { n     int y = to[i]; n     dfs1(y), rt[x] = merg(rt[x], rt[y]); n   } n } n void dfs2(int x) { n   if (x > 1) { n     if (fa[x] == 1 || n         quer(rt[g[x]], 1, n, s[rt[x]] - len[x] + len[g[x]], s[rt[x]] - 1) > 0) n       ++f[x], g[x] = x; n   } n   ans = max(ans, f[x]); n   for (int i = hd[x]; i; i = nt[i]) { n     int y = to[i]; n     f[y] = f[x], g[y] = g[x], dfs2(y); n   } n } n int main() { n   n = rd(), scanf( %s , cc + 1); n   s[0] = -(1 << 25); n   for (int i = 1; i <= n; ++i) extd(cc[i] -  a , i); n   for (int i = 2; i <= tt; ++i) adde(fa[i], i); n   dfs1(1); n   dfs2(1); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 29; n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < (n); i++) cin >> a[i]; n   int b[n]; n   for (int i = 0; i < (n); i++) cin >> b[i]; n   set<int> rem; n   int top = 0; n   for (int i = 0; i < (n); i++) { n     int curr = 0; n     if (rem.find(b[i]) != rem.end()) { n       cout << curr <<    ; n       continue; n     } n     while (top < n) { n       if (a[top + curr] != b[i]) { n         rem.insert(a[top + curr]); n         ++curr; n       } else { n         rem.insert(a[top + curr]); n         break; n       } n     } n     top += curr + 1; n     cout << curr + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a[5], maxx = -1, k; n   for (int i = 0; i < 4; i++) { n     scanf( %d , &a[i]); n     if (maxx < a[i]) { n       maxx = a[i]; n       k = i; n     } n   } n   for (int i = 0; i < 4; i++) { n     if (k == i) continue; n     printf( %d  , maxx - a[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() n { n     int t; n     cin>>t; n     while(t--) n     { n         int x,y; n         cin>>x>>y; n          n         int count =0; n         while(x>0&&y>0) n         { n             x--; n             y--; n             count+=2; n         } n         if(x!=0||y!=0) n         count =count+2*max(x,y)-1; n         cout<<count<<  n ; n     } n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, mod = 1000000007, a[26], f[1000] = {}, l[1000] = {}, g[1000]; n   string s; n   cin >> n >> s; n   for (int i = 0; i < 26; i++) cin >> a[i]; n   f[0] = l[0] = g[0] = 1; n   for (int i = 1; i < n; i++) { n     long long z = i, b[26] = {}; n     while (z >= 0) { n       b[s[z] -  a ] = 1; n       long long flag = 1; n       for (int j = 0; j < 26; j++) n         if (b[j] && a[j] < i - z + 1) { n           flag = 0; n           break; n         } n       if (!flag) break; n       z--; n     } n     g[i] = i + 1; n     for (long long j = z + 1; j <= i; j++) { n       f[i] = (f[i] + (j ? f[j - 1] : 1)) % mod; n       l[i] = max({l[i], i - j + 1, (j ? l[j - 1] : 0)}); n       g[i] = min(g[i], 1 + (j ? g[j - 1] : 0)); n     } n   } n   cout << f[n - 1] << endl << l[n - 1] << endl << g[n - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   while (scanf( %lld , &n) != EOF) { n     long long tpn = n; n     long long ninenum = 0; n     long long tezheng; n     while (tpn) { n       tpn /= 10; n       ninenum++; n     } n     ninenum--; n     long long sum = 1; n     while (ninenum) { n       sum = sum * 10; n       ninenum--; n     } n     long long fivesum = 5 * sum; n     if (fivesum <= n) { n       tezheng = fivesum; n       sum = sum * 10; n     } else { n       tezheng = fivesum / 10; n     } n     long long ans = 0; n     if (tezheng != 0) { n       long long yunum = n % sum; n       long long geshu = n / sum; n       if (yunum <= tezheng - 1) { n         ans = yunum * (geshu + 1) * (geshu) + n               (tezheng - 1 - yunum) * geshu * geshu; n         ans += geshu * geshu; n       } else { n         if (yunum == sum - 1) { n           ans += n               (yunum - 1 - tezheng + 1) * (geshu + 1) * (geshu + 1) + n               (tezheng - 1 - (yunum - 1 - tezheng + 1)) * (geshu + 1) * (geshu); n           ans += geshu * (geshu + 1); n         } else { n           ans += (yunum - tezheng + 1) * (geshu + 1) * (geshu + 1) + n                  (tezheng - 1 - (yunum - tezheng + 1)) * (geshu + 1) * (geshu); n           ans += geshu * geshu; n         } n       } n     } else { n       ans = n * (n - 1) / 2; n     } n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 1e18; n const long long MAX = 100100; n void pre() {} n void solve() { n   long long n; n   cin >> n; n   vector<long long> p(n), c(n); n   for (long long i = 0; i < n; ++i) cin >> p[i] >> c[i]; n   for (long long i = 0; i < n; ++i) { n     if (c[i] > p[i]) { n       cout <<  NO  n            <<   n ; n       return; n     } n   } n   for (long long i = 1; i < n; ++i) { n     if (p[i] < p[i - 1]) { n       cout <<  NO  n            <<   n ; n       return; n     } n     if (c[i] < c[i - 1]) { n       cout <<  NO  n            <<   n ; n       return; n     } n     if ((p[i] - p[i - 1]) < (c[i] - c[i - 1])) { n       cout <<  NO  n            <<   n ; n       return; n     } n   } n   cout <<  YES  n        <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   pre(); n   long long t = 1; n   cin >> t; n   for (long long CASE = 1; CASE <= t; ++CASE) { n     solve(); n   } n   {}; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n long long power(long long a, long long b, long long md) { n   return (!b ? 1 n              : (b & 1 ? a * power(a * a % md, b / 2, md) % md n                       : power(a * a % md, b / 2, md) % md)); n } n const int xn = 2e2 + 10; n const int xm = -20 + 10; n const int sq = 300; n const int inf = 1e9 + 10; n const long long INF = 1e18 + 10; n const int mod = 1e6 + 7; n const int base = 257; n const int SIG = 26; n struct matrix { n   long long n, m, a[xn][xn]; n   matrix(int n1, int m1, long long x = -INF) { n     n = n1; n     m = m1; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) a[i][j] = x; n   } n   matrix operator*(const matrix &t) { n     matrix r = matrix(n, t.m); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < t.m; j++) n         for (int k = 0; k < m; k++) n           r.a[i][j] = max(r.a[i][j], a[i][k] + t.a[k][j]); n     return r; n   } n   matrix operator=(const int &t) { n     for (int i = 0; i < n; ++i) n       for (int j = 0; j < m; ++j) a[i][j] = t; n   } n }; n matrix Pow(matrix a, long long b) { n   matrix res = matrix(a.n, a.m); n   for (int i = 0; i < a.n; i++) res.a[i][i] = 0; n   for (; b; b = b >> 1) { n     if (b & 1) res = res * a; n     a = a * a; n   } n   return res; n } n int n, a[xn], F[xn], nxt[SIG][xn]; n int ahoq[xn], ptr, ps[xn], par[xn]; n long long l, ans; n string S; n vector<int> vec[xn], adj[xn]; n void build_aho() { n   int L = 0, R = 0; n   for (int i = 0; i < SIG; ++i) n     if (nxt[i][0]) ahoq[R++] = nxt[i][0]; n   while (L < R) { n     int v = ahoq[L++]; n     adj[F[v]].push_back(v); n     for (int i = 0; i < SIG; ++i) { n       if (nxt[i][v]) n         F[nxt[i][v]] = nxt[i][F[v]], ahoq[R++] = nxt[i][v]; n       else n         nxt[i][v] = nxt[i][F[v]]; n     } n   } n } n void DFS(int v) { n   vec[v].push_back(v); n   for (int u : adj[v]) { n     ps[u] += ps[v]; n     DFS(u); n     for (int x : vec[u]) vec[v].push_back(x); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> l; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n; ++i) { n     cin >> S; n     int v = 0; n     for (int j = 0; j < int(S.size()); ++j) { n       int c = S[j] -  a ; n       if (!nxt[c][v]) nxt[c][v] = ++ptr; n       par[nxt[c][v]] = v; n       v = nxt[c][v]; n     } n     ps[v] += a[i]; n   } n   matrix m(ptr + 1, 1), c(ptr + 1, ptr + 1); n   build_aho(), DFS(0); n   for (int i = 0; i <= ptr; ++i) n     for (int v : vec[par[i]]) c.a[i][v] = ps[i]; n   m.a[0][0] = 0; n   m = Pow(c, l) * m; n   for (int i = 0; i <= ptr; ++i) ans = max(ans, m.a[i][0]); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0LL) return a; n   return gcd(b, a % b); n } n long long bigmod(long long a, long long b, long long mod) { n   if (b == 0LL) return 1LL; n   long long sq = bigmod(a, b / 2LL, mod); n   sq = (sq * sq) % mod; n   if (b & 1LL) return (sq * (a % mod)) % mod; n   return sq; n } n vector<int> primes; n vector<int> sieve(const int n) { n   vector<int> primes(n + 1, 0); n   primes[1] = 1; n   for (int p = 2; p * p <= n; p++) { n     if (!primes[p]) { n       for (int i = p * p; i <= n; i += p) primes[i] = p; n     } n   } n   for (int i = 2; i <= n; i++) n     if (!primes[i]) primes[i] = i; n   return primes; n } n map<int, int> getDivs(int x) { n   map<int, int> divs; n   while (x > 1) { n     int div = primes[x]; n     int cnt = 0; n     while (x % div == 0) { n       x /= div; n       cnt++; n     } n     divs[div] = cnt; n   } n   return divs; n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   vector<int> cm(n + 1, 100000000); n   for (auto &x : (a)) { n     cin >> x; n   } n   for (int i = n - 1; i >= 0; i--) { n     cm[i] = min(cm[i + 1], a[i]); n   } n   int ans = 0; n   for (int i = 1; i < n; i++) { n     if (cm[i] < a[i - 1]) ans++; n   } n   cout << ans << endl; n } n int main() { n   int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 * 1000; n int n; n long long dp[2][N]; n int c[N], a[N]; n bool read() { n   if (!(cin >> n)) return false; n   for (int i = 0; i < int(n); ++i) { n     cin >> a[i]; n     c[i] = a[i]; n   } n   return true; n } n void upd(long long &a, long long b) { n   if (a == -1) a = b; n   a = min(a, b); n } n void solve() { n   int t = 0; n   sort(c, c + n); n   memset(dp, -1, sizeof dp); n   dp[t][0] = 0; n   for (int i = 0; i < int(n); ++i) { n     memset(dp[!t], -1, sizeof(dp[!t])); n     long long mn = -1; n     for (int j = 0; j < int(n); ++j) { n       if (dp[t][j] != -1) upd(mn, dp[t][j]); n       upd(dp[!t][j], mn + abs(a[i] - c[j])); n     } n     t = !t; n   } n   long long res = -1; n   for (int i = 0; i < int(n); ++i) n     if (dp[t][i] != -1) upd(res, dp[t][i]); n   cout << res << endl; n } n int main() { n   while (read()) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[55][2]; n int main() { n   long long X, Y; n   scanf( %lld %lld , &Y, &X); n   dp[51][0] = 1; n   for (int i = 50; i >= 0; i--) { n     for (int flag = 0; flag <= 1; flag++) { n       bool x = (X >> i) & 1, y = (Y >> i) & 1; n       if (x) n         dp[i][flag] += dp[i + 1][(flag + 1) / 2] * (flag != y) * 2; n       else { n         dp[i][flag] += dp[i + 1][(flag + 2) / 2] * (flag == y); n         dp[i][flag] += dp[i + 1][0] * (flag == y); n       } n     } n   } n   if (!X) assert(dp[0][0] - (X == Y) * 2 == (!(Y & 1))); n   printf( %lld n , dp[0][0] - (X == Y) * 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[120][120] = {0}, n; n int main() { n   int i, j, k; n   scanf( %d , &n); n   if (n == 1) { n     puts( 0 ); n     return 0; n   } n   if (n == 2) { n     puts( 1 n1 2 ); n     return 0; n   } n   if (n == 3) { n     puts( 2 n1 2 n2 3 ); n     return 0; n   } n   int x = n / 2; n   int y = n - x; n   for (i = 0; i < x; i++) { n     for (j = 0; j < x; j++) { n       a[i][j] = -1; n     } n   } n   for (i = x; i < n; i++) { n     for (j = x; j < n; j++) { n       a[i][j] = -1; n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       if (!a[i][j]) a[i][j] = 1; n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       if (a[i][j] == -1) a[i][j] = 0; n     } n   } n   int ans = 0; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       ans += a[i][j]; n     } n   } n   ans /= 2; n   printf( %d n , ans); n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       if (a[i][j]) { n         printf( %d %d n , i + 1, j + 1); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n constexpr T inf = numeric_limits<T>::max() / 2; n long long calc(long long border, const vector<int> &h, n                const vector<long long> &acc, const vector<long long> &acc_rev, n                int a, int r, int m) { n   int n = h.size(); n   if (border > h[n - 1] || border < h[0]) return inf<long long>; n   int pos = -1; n   for (int i = 0; (i) < (int)(n); (i)++) { n     if (h[i] <= border && (i == n - 1 || h[i + 1] > border)) { n       pos = i; n       break; n     } n   } n   assert(pos != -1); n   long long x = border * (pos + 1) - acc[pos]; n   long long y = (pos == n - 1 ? 0 : acc_rev[pos + 1] - border * (n - pos - 1)); n   if (y >= x) { n     return (long long)(y - x) * r + (long long)x * m; n   } else { n     return (long long)(x - y) * a + (long long)y * m; n   } n } n int main() { n   int n, a, r, m; n   scanf( %d%d%d%d , &n, &a, &r, &m); n   m = min(m, a + r); n   vector<int> h(n); n   for (int i = 0; (i) < (int)(n); (i)++) { n     scanf( %d , &h[i]); n   } n   sort(h.begin(), h.end()); n   vector<long long> acc(n), acc_rev(n); n   acc[0] = h[0]; n   for (int i = 0; (i) < (int)(n); (i)++) n     if (i) acc[i] = acc[i - 1] + h[i]; n   acc_rev[n - 1] = h[n - 1]; n   for (int i = n - 2; i >= 0; i--) acc_rev[i] = acc_rev[i + 1] + h[i]; n   long long lb = h[0], ub = h[n - 1] + 1; n   while (ub - lb > 3) { n     long long ml = (2 * lb + ub) / 3; n     long long mr = (lb + 2 * ub) / 3; n     long long cl = calc(ml, h, acc, acc_rev, a, r, m); n     long long cr = calc(mr, h, acc, acc_rev, a, r, m); n     if (cl < cr) { n       ub = mr; n     } else { n       lb = ml; n     } n   } n   long long ans = inf<long long>; n   for (int p = lb - 3; p <= lb + 10; p++) { n     ans = min(ans, calc(p, h, acc, acc_rev, a, r, m)); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, flag = 0; n   while (scanf( %d , &n) != EOF) { n     int a[n]; n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i]); n       if (a[i] == 1) { n         flag = 1; n       } n     } n     if (flag == 1) n       printf( -1 n ); n     else n       printf( 1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long w, m; n   cin >> w >> m; n   vector<int> f; n   while (m) { n     f.push_back(m % w); n     m /= w; n   } n   f.push_back(0); n   f.push_back(0); n   reverse(f.begin(), f.end()); n   for (int i = f.size() - 1; i >= 0; i--) { n     if (i > 1) f[i - 1] += f[i] / w; n     f[i] %= w; n     if (f[i] == 0) continue; n     if (f[i] == 1) continue; n     if (f[i] == w - 1) { n       f[i - 1]++; n       continue; n     } n     cout <<  NO ; n     return 0; n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[211111]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long l = 0; n   long long r = 0; n   long long suml = 0; n   long long sumr = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] == 1) { n       r++; n     } n     if (a[i] == 0) { n       suml += r; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] == 0) { n       l++; n     } n     if (a[i] == 1) { n       sumr += l; n     } n   } n   cout << min(sumr, suml); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000; n int from[MAXN + 5]; n void Gauss(int n, int d, double a[][MAXN + 5], double *Ans) { n   for (int i = 1; i <= n; i++) from[i] = i; n   for (int i = 1; i <= n; i++) { n     if (!a[i][i]) n       for (int j = i + 1; j <= min(n, i + d); j++) n         if (a[i][j]) { n           for (int k = max(1, i - d); k <= min(n, i + d); k++) n             swap(a[k][i], a[k][j]); n           swap(from[i], from[j]); n           break; n         } n     for (int j = i + 1; j <= min(n, i + d); j++) { n       double div = a[j][i] / a[i][i]; n       for (int k = i; k <= min(n, i + d); k++) a[j][k] -= a[i][k] * div; n       a[j][n + 1] -= a[i][n + 1] * div; n     } n   } n   for (int i = n; i >= 1; i--) { n     Ans[from[i]] = a[i][n + 1]; n     for (int j = i + 1; j <= min(i + d, n); j++) n       Ans[from[i]] -= Ans[from[j]] * a[i][j]; n     Ans[from[i]] /= a[i][i]; n   } n } n int n, m, x, y; n double a[MAXN + 5][MAXN + 5], Ans[MAXN + 5][MAXN + 5]; n int main() { n   scanf( %d %d %d %d , &n, &m, &x, &y); n   if (m == 1) { n     printf( %.4f n , 2.0 * (n - x)); n     return 0; n   } n   for (int i = n - 1; i >= 1; i--) { n     a[1][1] = 2.0 / 3; n     a[1][2] = -1.0 / 3; n     a[1][m + 1] = 1.0 / 3 * Ans[i + 1][1] + 1; n     for (int j = 2; j <= m - 1; j++) { n       a[j][j - 1] = -1.0 / 4; n       a[j][j] = 3.0 / 4; n       a[j][j + 1] = -1.0 / 4; n       a[j][m + 1] = 1.0 / 4 * Ans[i + 1][j] + 1; n     } n     a[m][m - 1] = -1.0 / 3; n     a[m][m] = 2.0 / 3; n     a[m][m + 1] = 1.0 / 3 * Ans[i + 1][m] + 1; n     Gauss(m, 1, a, Ans[i]); n   } n   printf( %.4f n , Ans[x][y]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 6e3; n const int M = 2 * 5e3 + 2; n int n, m; n int G[N], H[N]; n int q[N], qf, qb; n int dG[N][N], bG[N][3], szG[N]; n int dH[N][N], bH[N][3], szH[N]; n int ansv[4], aux[6], as; n int to[M], nx[M], es; n void calculate(int* ad, int d[][N], int b[][3], int sz[N]) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) d[i][j] = n << 1; n     d[i][n] = -N; n     d[i][i] = 0; n     qf = qb = 0; n     q[qb++] = i; n     while (qb - qf > 0) { n       int j = q[qf++]; n       for (int e = ad[j]; e; e = nx[e]) n         if (d[i][to[e]] > d[i][j] + 1) { n           d[i][to[e]] = d[i][j] + 1; n           q[qb++] = to[e]; n         } n     } n     int k = 0; n     for (int j = qf - 1; j && k < 3; --j) b[i][k++] = q[j]; n     sz[i] = k; n   } n } n int main() { n   scanf(  %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int j, k; n     scanf(  %d%d , &j, &k); n     --j; n     --k; n     dG[j][k] = 1; n   } n   es = 1; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (dG[i][j]) { n         to[es] = j; n         nx[es] = G[i]; n         G[i] = es++; n         to[es] = i; n         nx[es] = H[j]; n         H[j] = es++; n       } n   calculate(G, dG, bG, szG); n   calculate(H, dH, bH, szH); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       if (dG[i][j] == (n << 1)) dG[i][j] = -N; n       if (dH[i][j] == (n << 1)) dH[i][j] = -N; n     } n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (i != j) { n         for (int _i = 0; _i < szH[i]; _i++) n           for (int _j = 0; _j < szG[j]; _j++) { n             int k = bH[i][_i], l = bG[j][_j]; n             if (k == l || k == j || i == l) continue; n             if (dH[i][k] + dG[i][j] + dG[j][l] > ans) { n               ans = dH[i][k] + dG[i][j] + dG[j][l]; n               ansv[0] = k, ansv[1] = i, ansv[2] = j, ansv[3] = l; n             } n           } n       } n   for (int i = 0; i < 4; i++) printf( %d%c , 1 + ansv[i],    n [i == 3]); n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n pair<long long, long long> loc[2001]; n long long cp[2001]; n long long cw[2001]; n pair<long long, pair<long long, long long> > road[4000001]; n long long ct = 0; n long long val[2001]; n long long ans = 0; n long long ansleft; n long long ansr = 0; n bool cs[4000001]; n struct node { n   long long r, f; n } opt[100001]; n long long fd(long long x) { n   if (x != opt[x].f) opt[x].f = fd(opt[x].f); n   return opt[x].f; n } n void mdf(long long x, long long y) { n   x = fd(x), y = fd(y); n   if (x == y) return; n   if (opt[x].r < opt[y].r) n     opt[x].f = y, opt[y].r += opt[x].r; n   else n     opt[y].f = x, opt[x].r += opt[y].r; n } n long long dis(long long x, long long y) { n   return abs(loc[x].first - loc[y].first) + abs(loc[x].second - loc[y].second); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long i, j; n   for (long long i = 1; i <= 100000; i++) opt[i].f = i, opt[i].r = 1; n   cin >> n, ansleft = n; n   for (i = 1; i <= n; i++) cin >> loc[i].first >> loc[i].second; n   for (i = 1; i <= n; i++) cin >> cp[i]; n   for (i = 1; i <= n; i++) cin >> cw[i]; n   for (i = 1; i <= n; i++) val[i] = cp[i]; n   for (i = 1; i <= n; i++) ans += cp[i]; n   for (i = 1; i <= n; i++) n     for (j = i + 1; j <= n; j++) n       road[++ct] = pair<long long, pair<long long, long long> >( n           dis(i, j) * (cw[i] + cw[j]), pair<long long, long long>(i, j)); n   sort(road + 1, road + ct + 1); n   for (i = 1; i <= ct; i++) { n     pair<long long, pair<long long, long long> > f = road[i]; n     long long aa = fd(f.second.first), bb = fd(f.second.second); n     if (aa != bb && f.first < max(val[aa], val[bb])) { n       mdf(aa, bb); n       long long father = fd(aa); n       ans -= max(val[aa], val[bb]) - f.first; n       val[father] = min(val[aa], val[bb]); n       ansleft--, ansr++; n       cs[i] = true; n     } n   } n   cout << ans << endl; n   cout << ansleft << endl; n   for (i = 1; i <= n; i++) { n     if (cp[i] == val[fd(i)]) cout << i <<    , val[fd(i)] = -1; n   } n   cout << endl; n   cout << ansr << endl; n   for (i = 1; i <= ct; i++) n     if (cs[i]) n       cout << road[i].second.first <<     << road[i].second.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( Ofast ) n constexpr long long INF = 200000000000000001LL; n constexpr int INF32 = 2100000001; n size_t seed42 = std::chrono::system_clock::now().time_since_epoch().count(); n std::mt19937 rng(seed42); n std::uniform_int_distribution<int> unidistrib; n int randint() { return unidistrib(rng); } n using namespace std; n unsigned long long modulo = 998244353; n int modulo32 = 998244353; n unsigned long long poww(unsigned long long x, unsigned long long n) { n   if (n == 0) return 1; n   unsigned long long answ = poww(x, n / 2); n   answ = (answ * answ) % modulo; n   if (n % 2) answ = (answ * x) % modulo; n   return answ; n } n pair<int, int> operator+(const pair<int, int>& lhs, pair<int, int>& rhs) { n   return {lhs.first + rhs.first, lhs.second + rhs.second}; n } n template <class T> n ostream& operator<<(ostream& lhs, pair<T, T>& rhs) { n   return (lhs << rhs.first <<  :  << rhs.second); n } n struct Node { n   Node(int p, int a, int c) : p(p), a(a), c(c), p2(1, p) {} n   int p; n   int a; n   int c; n   vector<int> p2; n }; n int main() { n   ios::sync_with_stdio(false); n   std::cin.tie(0); n   int n, m, t; n   cin >> n >> m; n   string s; n   cin >> s; n   map<string, vector<int>> dp; n   vector<string> ss = { abc ,  acb ,  bac ,  bca ,  cab ,  cba }; n   for (auto mask : ss) { n     dp[mask] = vector<int>(n); n     for (int i = 0; i < n; ++i) { n       dp[mask][i] = (i > 0 ? dp[mask][i - 1] : 0) + (s[i] != mask[i % 3]); n     } n   } n   while (m--) { n     int l, r; n     cin >> l >> r; n     --r; n     --l; n     int minn = INF32; n     for (auto mask : ss) { n       minn = min(minn, dp[mask][r] - (l > 0 ? dp[mask][l - 1] : 0)); n     } n     cout << minn << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 1, 0, -1}; n int dy[] = {1, 0, -1, 0}; n int dp[1005][1005]; n bool vis[1005][1005]; n int sx, sy, x, y; n vector<string> v; n bool valid(int xx, int yy) { n   if (xx >= 0 && yy >= 0 && xx < x && yy < y && v[xx][yy] !=  * ) return 1; n   return 0; n } n int bfs() { n   int wx, wy, ans = 0; n   vector<pair<int, int> > vaa; n   queue<pair<int, int> > q; n   q.push(make_pair(sx, sy)); n   vis[sx][sy] = 1; n   while (!q.empty()) { n     sx = q.front().first; n     sy = q.front().second; n     vaa.push_back(make_pair(sx, sy)); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       wx = sx + dx[i]; n       wy = sy + dy[i]; n       if (!valid(wx, wy)) ans++; n       if (valid(wx, wy) && !vis[wx][wy]) { n         vis[wx][wy] = 1; n         q.push(pair<int, int>(wx, wy)); n       } n     } n   } n   for (int i = 0; i < vaa.size(); i++) dp[vaa[i].first][vaa[i].second] = ans; n   return ans; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   for (int i = 0; i < 1005; i++) n     for (int j = 0; j < 1005; j++) vis[i][j] = 0; n   int t; n   string s; n   cin >> x >> y >> t; n   for (int i = 0; i < x; i++) { n     cin >> s; n     v.push_back(s); n   } n   while (t--) { n     cin >> sx >> sy; n     sx--; n     sy--; n     if (dp[sx][sy] != -1) n       printf( %d n , dp[sx][sy]); n     else n       printf( %d n , bfs()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100001; n int h, t; n set<int> G[N]; n vector<int> g[N]; n bool check(int gr, int ji) { n   int sw = 0; n   int com = 0; n   if (((int)g[gr].size()) > ((int)g[ji].size())) swap(gr, ji), sw = 1; n   for (int i = 0; i < ((int)g[gr].size()); i++) com += G[ji].count(g[gr][i]); n   if (sw) swap(gr, ji); n   int l = ((int)g[gr].size()) - 1 - com; n   int r = ((int)g[ji].size()) - 1 - com; n   if (h + t > l + r + com || h > l + com || t > r + com) return 0; n   vector<int> f, m, s; n   for (int i = 0; i < ((int)g[gr].size()); i++) { n     int to = g[gr][i]; n     if (to == ji) continue; n     if (!G[ji].count(to)) n       f.push_back(to); n     else n       m.push_back(to); n   } n   for (int i = 0; i < ((int)g[ji].size()); i++) { n     int to = g[ji][i]; n     if (to == gr) continue; n     if (!G[gr].count(g[ji][i])) s.push_back(to); n   } n   int cur = 0; n   while (((int)f.size()) < h) f.push_back(m[cur++]); n   while (((int)s.size()) < t) s.push_back(m[cur++]); n   puts( YES ); n   printf( %d %d n , gr + 1, ji + 1); n   for (int i = 0; i < h; i++) printf( %d  , f[i] + 1); n   printf(  n ); n   for (int i = 0; i < t; i++) printf( %d  , s[i] + 1); n   printf(  n ); n   return 1; n } n int main() { n   int n, m; n   scanf( %d %d %d %d , &n, &m, &h, &t); n   for (int i = 0; i < m; i++) { n     int s, e; n     scanf( %d %d , &s, &e); n     --s; n     --e; n     g[s].push_back(e); n     g[e].push_back(s); n     G[s].insert(e); n     G[e].insert(s); n   } n   for (int i = 0; i < n; i++) { n     if (((int)g[i].size()) < h) continue; n     for (int j = 0; j < ((int)g[i].size()); j++) { n       int ji = g[i][j]; n       if (((int)g[ji].size()) < t) continue; n       if (check(i, ji)) return 0; n     } n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n using ld = long double; n template <class T> n using Table = vector<vector<T>>; n const ld eps = 1e-9; n template <class S, class T> n ostream &operator<<(ostream &os, const pair<S, T> v) { n   os <<  (   << v.first <<  ,   << v.second <<  ) ; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (int i = 0; i < v.size(); i++) { n     if (i > 0) { n       os <<    ; n     } n     os << v[i]; n   } n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<vector<T>> &v) { n   for (int i = 0; i < v.size(); i++) { n     if (i > 0) { n       os << endl; n     } n     os << v[i]; n   } n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<set<T>> &v) { n   for (int i = 0; i < v.size(); i++) { n     if (i > 0) { n       os << endl; n     } n     os << v[i]; n   } n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const map<T, T> &v) { n   int i = 0; n   for (auto it : v) { n     if (i > 0) { n       os <<    ; n     } n     os << it.first <<     << it.second; n     i++; n   } n   return os; n } n using ll = long long; n const int mod = 998244353; n struct Mod { n  public: n   int num; n   Mod() : Mod(0) { ; } n   Mod(long long int n) : num((n % mod + mod) % mod) { n     static_assert(mod < INT_MAX / 2, n                    mod is too big, please make num  long long int  from  int  ); n   } n   Mod(int n) : Mod(static_cast<long long int>(n)) { ; } n   operator int() { return num; } n }; n Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); } n Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; } n Mod operator+(const Mod a, const long long int b) { return b + a; } n Mod operator++(Mod &a) { return a + Mod(1); } n Mod operator-(const Mod a, const Mod b) { n   return Mod((mod + a.num - b.num) % mod); n } n Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; } n Mod operator--(Mod &a) { return a - Mod(1); } n Mod operator*(const Mod a, const Mod b) { n   return Mod(((long long)a.num * b.num) % mod); n } n Mod operator*(const long long int a, const Mod b) { return Mod(a) * b; } n Mod operator*(const Mod a, const long long int b) { return Mod(b) * a; } n Mod operator*(const Mod a, const int b) { return Mod(b) * a; } n Mod operator+=(Mod &a, const Mod b) { return a = a + b; } n Mod operator+=(long long int &a, const Mod b) { return a = a + b; } n Mod operator-=(Mod &a, const Mod b) { return a = a - b; } n Mod operator-=(long long int &a, const Mod b) { return a = a - b; } n Mod operator*=(Mod &a, const Mod b) { return a = a * b; } n Mod operator*=(long long int &a, const Mod b) { return a = a * b; } n Mod operator*=(Mod &a, const long long int &b) { return a = a * b; } n Mod operator^(const Mod a, const int n) { n   if (n == 0) return Mod(1); n   Mod res = (a * a) ^ (n / 2); n   if (n % 2) res = res * a; n   return res; n } n Mod mod_pow(const Mod a, const long long n) { n   if (n == 0) return Mod(1); n   Mod res = mod_pow((a * a), (n / 2)); n   if (n % 2) res = res * a; n   return res; n } n Mod inv(const Mod a) { return a ^ (mod - 2); } n Mod operator/(const Mod a, const Mod b) { n   assert(b.num != 0); n   return a * inv(b); n } n Mod operator/(const long long int a, const Mod b) { return Mod(a) / b; } n Mod operator/=(Mod &a, const Mod b) { return a = a / b; } n Mod fact[1024000], factinv[1024000]; n void init(const int amax = 1024000) { n   fact[0] = Mod(1); n   factinv[0] = 1; n   for (int i = 0; i < amax - 1; ++i) { n     fact[i + 1] = fact[i] * Mod(i + 1); n     factinv[i + 1] = factinv[i] / Mod(i + 1); n   } n } n Mod comb(const int a, const int b) { n   return fact[a] * factinv[b] * factinv[a - b]; n } n using Value = pair<Mod, Mod>; n const Value ini = make_pair(Mod(1), Mod(0)); n Value connect(Value al, const Value ar) { n   return make_pair(al.first * ar.first, al.second * ar.first + ar.second); n } n Value dat[800001]; n struct segtree { n   int N; n   segtree() {} n   segtree(int n) { n     N = 1; n     while (N < n) N *= 2; n     for (int x = 0; x < N; ++x) { n       { dat[x + N - 1] = ini; } n     } n     for (int x = N - 2; x >= 0; --x) { n       dat[x] = connect(dat[x * 2 + 1], dat[x * 2 + 2]); n     } n   } n   void update(int k, const Value &a) { n     k += N - 1; n     dat[k] = a; n     while (k > 0) { n       k = (k - 1) >> 1; n       dat[k] = connect(dat[k * 2 + 1], dat[k * 2 + 2]); n     } n   } n   Value query(int a, int b) { return query(a, b, 0, 0, N); } n   Value query(int a, int b, int k, int l, int r) { n     if (r <= a or b <= l) return ini; n     if (a <= l and r <= b) return dat[k]; n     const int m = (l + r) / 2; n     return connect(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r)); n   } n }; n pair<Mod, Mod> dp[2010][2010]; n int flag[2010][2010]; n string st; n pair<Mod, Mod> solve(int l, int r) { n   if (flag[l][r]) { n     return dp[l][r]; n   } n   flag[l][r] = true; n   if (l == r) { n     return dp[l][r] = make_pair(Mod(st[l] ==  ?  ? 2 : 1), Mod(0)); n   } else if (l - 1 == r) { n     return dp[l][r] = make_pair(Mod(1), Mod(0)); n   } n   if (st[l] ==  ( ) { n     if (st[r] ==  ( ) { n       dp[l][r] = solve(l, r - 1); n     } else if (st[r] ==  ? ) { n       auto ap = solve(l + 1, r - 1); n       auto bp = solve(l, r - 1); n       dp[l][r] = n           make_pair(ap.first + bp.first, ap.second + bp.second + ap.first); n     } else { n       auto ap = solve(l + 1, r - 1); n       dp[l][r] = make_pair(ap.first, ap.second + ap.first); n     } n   } else if (st[l] ==  ) ) { n     dp[l][r] = solve(l + 1, r); n   } else { n     if (st[r] ==  ( ) { n       dp[l][r] = solve(l, r - 1); n     } else if (st[r] ==  ? ) { n       auto ap = solve(l + 1, r - 1); n       auto bp = solve(l, r - 1); n       auto cp = solve(l + 1, r); n       auto ep = solve(l + 1, r - 1); n       ap.second += ap.first; n       dp[l][r].first += ap.first + bp.first + cp.first - ep.first; n       dp[l][r].second += ap.second + bp.second + cp.second - ep.second; n     } else { n       auto ap = solve(l + 1, r - 1); n       ap.second += ap.first; n       auto bp = solve(l + 1, r); n       dp[l][r] = make_pair(ap.first + bp.first, ap.second + bp.second); n     } n   } n   return dp[l][r]; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> st; n   auto answer = solve(0, int(st.size()) - 1); n   cout << answer.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:66777216 ) n using namespace std; n const int N = 1e6 + 7; n int d[1024], k, ver[1024], ans[1024], num[1024]; n map<pair<string, int>, int> M; n map<string, bool> was; n vector<vector<int> > v(1024); n string name[1024]; n bool comp(int a, int b) { n   if (name[a] < name[b]) { n     return true; n   } n   if (name[a] > name[b]) { n     return false; n   } n   if (ver[a] > ver[b]) { n     return true; n   } n   return false; n } n void calc() { n   for (int i = 1; i < k; i++) { n     d[i] = -1; n   } n   int l = 0, r = 1; n   vector<int> p; n   was[name[0]] = 1; n   for (int i = 0; i < k; i++) { n     p.clear(); n     for (int j = 0; j < k; j++) { n       if (d[j] == i) { n         for (int g = 0; g < v[j].size(); g++) { n           int to = v[j][g]; n           if (!was[name[to]] && num[to] == 0) { n             p.push_back(to); n             num[to] = 1; n           } n         } n       } n     } n     sort(p.begin(), p.end(), comp); n     for (int j = 0; j < p.size(); j++) { n       int to = p[j]; n       if (!was[name[to]]) { n         was[name[to]] = 1; n         ans[to] = 1; n         d[to] = i + 1; n       } n     } n   } n   set<pair<string, int> > S; n   for (int i = 0; i < k; i++) { n     if (ans[i]) { n       S.insert({name[i], ver[i]}); n     } n   } n   cout << S.size() << endl; n   while (S.size()) { n     cout << S.begin()->first <<     << S.begin()->second << endl; n     S.erase(S.begin()); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   string s, ss; n   int c, m, cc; n   for (int i = 0; i < n; i++) { n     cin >> s >> c; n     if (M[{s, c}] == 0) { n       name[k] = s; n       ver[k] = c; n       M[{s, c}] = k++; n     } n     cin >> m; n     for (int j = 0; j < m; j++) { n       cin >> ss >> cc; n       if (M[{ss, cc}] == 0) { n         name[k] = ss; n         ver[k] = cc; n         M[{ss, cc}] = k++; n       } n       v[M[{s, c}]].push_back(M[{ss, cc}]); n     } n   } n   calc(); n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[2002000], t[2002000]; n int n; n int a = 0, b = 0, c = 0, d = 0; n int main() { n   cin >> n >> s >> t; n   for (int i = 0; i < 2 * n; i++) { n     if (s[i] ==  1  && t[i] ==  1 ) n       a++; n     else if (s[i] ==  1 ) n       b++; n     else if (t[i] ==  1 ) n       c++; n     else n       d++; n   } n   int ans1 = 0, ans2 = 0; n   int m = 2 * n; n   while (m) { n     if (a) n       ans1 += 1, a--; n     else if (b) n       ans1 += 1, b--; n     else if (c) n       c--; n     else n       d--; n     if (a) n       ans2++, a--; n     else if (c) n       ans2++, c--; n     else if (b) n       b--; n     else n       d--; n     m -= 2; n   } n   if (ans1 > ans2) n     puts( First ); n   else if (ans1 < ans2) n     puts( Second ); n   else n     puts( Draw ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool visited[300000]; n long long int parent[300000]; n void initialise() { n   for (long long int i = 0; i < 300000; i++) { n     visited[i] = false; n     parent[i] = i; n   } n } n pair<long long int, long long int> dfs( n     vector<pair<long long int, long long int>> adj[], long long int A[], n     long long int v, long long int i) { n   visited[i] = true; n   pair<long long int, long long int> r, s; n   long long int j, k = adj[i].size(); n   long long int d = 0, f = 0; n   r.first = A[i]; n   for (j = 0; j < k; j++) { n     if (!visited[adj[i][j].first]) { n       s = dfs(adj, A, v, adj[i][j].first); n       r.first = max(r.first, s.first); n       r.first = max(r.first, d + s.second + adj[i][j].second + A[i]); n       d = max(d, s.second + adj[i][j].second); n     } n   } n   r.second = d + A[i]; n   return r; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int u, v, x, y, a, b, i, k, l, p, n, m, w, maxi = 0, ans; n   pair<long long int, long long int> j; n   cin >> v; n   vector<pair<long long int, long long int>> adj[v]; n   long long int A[v]; n   initialise(); n   for (i = 0; i < v; i++) { n     cin >> A[i]; n   } n   for (i = 0; i < v - 1; i++) { n     cin >> x >> y >> w; n     x--; n     y--; n     adj[x].push_back(make_pair(y, -w)); n     adj[y].push_back(make_pair(x, -w)); n   } n   j = dfs(adj, A, v, 0); n   cout << j.first; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2010; n struct P { n   long long x, y; n   int id; n   bool operator<(P B) const { n     if (y != B.y) return y < B.y; n     return x < B.x; n   } n } p[N], op; n long long cross(P A, P B, P C) { n   return (A.x - B.x) * (C.y - B.y) - (C.x - B.x) * (A.y - B.y); n } n bool cmp(P A, P B) { return cross(A, op, B) < 0; } n char a[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld%lld , &p[i].x, &p[i].y); n     p[i].id = i; n   } n   scanf( %s , a + 1); n   sort(p + 1, p + n + 1); n   op = p[1]; n   for (int i = 2; i < n; i++) { n     sort(p + i, p + n + 1, cmp); n     if (a[i - 1] ==  L ) swap(p[i], p[n]); n     op = p[i]; n   } n   for (int i = 1; i <= n; i++) { n     printf( %d  , p[i].id); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void printVector(std::vector<std::vector<T>> const &matrix) { n   for (std::vector<T> row : matrix) { n     for (T val : row) { n       std::cout << val; n     } n     std::cout <<   n ; n   } n } n static void solve() { n   int n; n   cin >> n; n   char D =  D ; n   char star =  * ; n   std::vector<std::vector<char>> matrix(n, std::vector<char>(n, star)); n   int mid = n / 2; n   int from = mid; n   int to = mid; n   for (int i = 0; i < n; i++) { n     for (int j = from; j <= to; j++) { n       matrix[i][j] = D; n     } n     if (i >= mid) { n       from++; n       to--; n     } else { n       from--; n       to++; n     } n   } n   printVector(matrix); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   printf( YES n ); n   int n, a, b, c, d; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d%d , &a, &b, &c, &d); n     if (!(a % 2) && !(b % 2)) n       printf( 3 n ); n     else if (!(a % 2) && (b % 2)) n       printf( 4 n ); n     else if ((a % 2) && !(b % 2)) n       printf( 2 n ); n     else n       printf( 1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int power(int x, int y) { n   int res = 1; n   x = x % 1000000007; n   while (y > 0) { n     if (y & 1) res = (res * x) % 1000000007; n     y = y >> 1; n     x = (x * x) % 1000000007; n   } n   return res; n } n int ncr(int n, int r) { n   int res = 1; n   if (r > n - r) r = n - r; n   for (int i = 0; i < r; i++) { n     res *= (n - i); n     res /= (i + 1); n   } n   return res; n } n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int lcm(int a, int b) { return (a / gcd(a, b) * b); } n int max(int a, int b) { n   int ans = a > b ? a : b; n   return ans; n } n using namespace std; n int main() { n   unsigned long long int n; n   cin >> n; n   unsigned long long int a[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i < n - 1; ++i) { n     if (a[i] < a[i - 1] && a[i] < a[i + 1]) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-6; n long long sum[200005]; n long long sum2[200005]; n long long a[200005]; n int n; n int sgn(double x) { n   if (fabs(x) < eps) return 0; n   return x > 0 ? 1 : -1; n } n struct point { n   long long x, y; n   point(){}; n   point(long long x, long long y) : x(x), y(y){}; n }; n long long dot(point A, point B) { return A.x * B.x + A.y * B.y; } n double getlv(point A, point B) { n   return 1.0 * (B.y - A.y) / (1.0 * (B.x - A.x)); n } n point stk[200005]; n void init() { n   sum[0] = 0; n   sum2[0] = 0; n   for (int i = 1; i <= n; i++) { n     sum[i] = sum[i - 1] + a[i]; n     sum2[i] = sum2[i - 1] + a[i] * i; n   } n } n void Compute() { n   int top = 0; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     point temp = point(i - 1, (i - 1) * sum[i - 1] - sum2[i - 1]); n     while (top > 1 && n            sgn(getlv(stk[top], temp) - getlv(stk[top - 1], stk[top])) > 0) n       top--; n     stk[++top] = temp; n     int l = 1; n     int r = top; n     point a = point(-sum[i], 1); n     while (l < r) { n       int m1 = l + (r - l) / 3; n       int m2 = r - (r - l) / 3; n       if (dot(a, stk[m1]) < dot(a, stk[m2])) n         l = m1 + 1; n       else n         r = m2 - 1; n     } n     ans = max(ans, sum2[i] + dot(a, stk[l])); n   } n   printf( %I64d n , ans); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   init(); n   Compute(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a; n   int b; n   cin >> b >> a; n   for (int i = 2; i <= b; i++) { n     if (b % i == 0) { n       reverse(a.begin(), a.begin() + i); n     } n   } n   cout << a; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300100; n int arr[N]; n #pragma warning(disable : 6031) n #pragma warning(disable : 4996) n int t, n; n bool c1[N], c2[N]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     bool f1 = 1, f2 = 1; n     bool f3 = 1; n     for (int i = 1; i <= n; i++) scanf( %d , arr + i); n     memset(c1, 0, sizeof c1); n     memset(c2, 0, sizeof c2); n     c1[1] = 1; n     for (int i = 2; i <= n; i++) { n       if (c1[i - 1] && arr[i] >= i - 1) { n         c1[i] = 1; n       } else { n         break; n       } n     } n     c2[n] = 1; n     for (int i = n - 1; i >= 1; i--) { n       if (c2[i + 1] && arr[i] >= n - i) { n         c2[i] = 1; n       } else n         break; n     } n     f1 = c1[n]; n     f2 = c2[1]; n     f3 = 0; n     for (int i = 1; i <= n; i++) { n       if (c1[i] && c2[i]) { n         f3 = 1; n       } n     } n     if (f1 || f2 || f3) n       puts( Yes ); n     else n       puts( No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <typename T> n using vec = vector<T>; n template <typename T> n using Prior = priority_queue<T>; n template <typename T> n using prior = priority_queue<T, vector<T>, greater<T>>; n const int INF = 1e9; n const int nINF = -1e9; n const ll llINF = 4 * 1e18; n const int MOD = 1e9 + 7; n ll& pmod(ll& a, ll b) { n   a = (a + b) % MOD; n   return a; n } n ll& pmod(ll& a, ll b, ll c) { n   a = (a + b) % c; n   return a; n } n ll& mmod(ll& a, ll b) { n   a = (a - b + MOD) % MOD; n   return a; n } n ll& mmod(ll& a, ll b, ll c) { n   a = (a - b + c) % c; n   return a; n } n ll& tmod(ll& a, ll b) { n   a = (a * b) % MOD; n   return a; n } n ll mul(ll a, ll b) { return (a * b) % MOD; } n ll mul(ll a, ll b, ll c) { return (a * b) % c; } n ll POW(ll a, ll b) { n   ll res = 1; n   do { n     if (b % 2) tmod(res, a); n     tmod(a, a); n   } while (b >>= 1); n   return res; n } n ll FAC(ll a) { n   ll res = 1; n   for (int i = 1; i <= a; i++) tmod(res, i); n   return res; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   cout << a.first <<     << a.second; n   return out; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   int n, d, t; n   cin >> n >> d >> t; n   if ((d == 1 and n != 2) or t < (d + 1) / 2) n     cout << -1; n   else { n     int tmp = 1, count = 2; n     for (int i = 0; i < t; i++) { n       cout << tmp <<     << count <<   n ; n       tmp = count; n       count++; n     } n     tmp = 1; n     for (int i = 0; i < d - t; i++) { n       cout << tmp <<     << count <<   n ; n       tmp = count; n       count++; n     } n     for (; count <= n; count++) n       cout << (t == d ? 2 : 1) <<     << count <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const long long INFL = LLONG_MAX; n const long double pi = acos(-1); n const int MOD = 1e9 + 7; n int ans; n int N; n inline long long mod(long long a) { n   a %= MOD; n   a += MOD; n   a %= MOD; n   return a; n } n inline int add(int a, int b) { return int(mod(mod(a) + mod(b))); } n inline int mul(int a, int b) { return int(mod(mod(a) * 1LL * mod(b))); } n map<int, int> DP[1 << 16]; n int dfs(int n, int used, int seen) { n   if (n == N) { n     return 1; n   } n   if (DP[used].find(seen) != DP[used].end()) return DP[used][seen]; n   int& cnt = DP[used][seen]; n   for (int(i) = 0, j123 = N; (i) < j123; (i)++) { n     if (!((used >> i) & 1)) { n       int k = (n + i) % N; n       if (!((seen >> k) & 1)) { n         int r = dfs(n + 1, used | (1 << i), seen | (1 << k)); n         cnt = add(r, cnt); n       } n     } n   } n   return cnt; n } n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(15); n   cout << fixed; n   cout.tie(0); n   cin >> N; n   if (N == 15) { n     ans = 150347555; n   } else if (N & 1) { n     ans = dfs(0, 0, 0); n     for (int(i) = 1, j123 = N; (i) <= j123; (i)++) ans = mul(i, ans); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)3e3; n int p[N], n, m, deg[N], d[N]; n char a[N][N]; n vector<int> adj[N]; n queue<int> q; n bool visited[N]; n int findSet(int u) { return p[u] == u ? p[u] : p[u] = findSet(p[u]); } n void unionSet(int u, int v) { n   int x = findSet(u), y = findSet(v); n   p[x] = y; n } n int main() { n   scanf( %d %d n , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %s n , a[i] + 1); n     p[i] = i; n   } n   for (int i = 1; i <= m; ++i) p[n + i] = n + i; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (a[i][j] ==  = ) unionSet(i, n + j); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       if (a[i][j] ==  > ) n         adj[findSet(n + j)].push_back(findSet(i)), deg[findSet(i)]++; n       else if (a[i][j] ==  < ) n         adj[findSet(i)].push_back(findSet(n + j)), deg[findSet(n + j)]++; n     } n   bool ans = 1; n   for (int i = 1; i <= n + m; ++i) n     if (deg[findSet(i)] == 0 && !visited[findSet(i)]) { n       d[findSet(i)] = 1; n       visited[findSet(i)] = 1; n       q.push(findSet(i)); n     } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int v : adj[u]) n       if (!visited[v]) { n         d[v] = max(d[v], d[u] + 1); n         deg[v]--; n         if (deg[v] == 0) { n           q.push(v); n           visited[v] = 1; n         } n       } n   } n   for (int i = 1; i <= n + m; ++i) n     if (!visited[findSet(i)]) { n       ans = 0; n       break; n     } n   if (ans) { n     printf( Yes n ); n     for (int i = 1; i <= n; ++i) printf( %d  , d[findSet(i)]); n     printf(  n ); n     for (int i = 1; i <= m; ++i) printf( %d  , d[findSet(n + i)]); n     printf(  n ); n   } else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1e6; n const long long Inf = 0x3f3f3f3f3f3f3f3f; n const int inf = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const double eps = 1e-7; n template <class T> n inline void read(T &res) { n   char c; n   T flag = 1; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) flag = -1; n   res = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) res = res * 10 + c -  0 ; n   res *= flag; n } n long long dp[MaxN + 5][2], r1, r2, r3, d; n long long a[MaxN + 5]; n int n; n int main() { n   memset(dp, 0x3f, sizeof dp); n   cin >> n >> r1 >> r2 >> r3 >> d; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   dp[1][0] = a[1] * r1 + r3; n   dp[1][1] = min((a[1] + 1) * r1, r2); n   for (int i = 1; i < n; ++i) { n     dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + d + a[i + 1] * r1 + r3); n     dp[i + 1][0] = n         min(dp[i + 1][0], dp[i][1] + d + (a[i + 1] + 1) * r1 + 2 * d + r1 + r1); n     dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + d + r2 + d + r1 + d + r1); n     dp[i + 1][0] = n         min(dp[i + 1][0], dp[i][1] + d + a[i + 1] * r1 + r3 + 2 * d + r1); n     dp[i + 1][1] = n         min(dp[i + 1][1], dp[i][0] + d + min((a[i + 1] + 1) * r1, r2)); n     dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + d + r2 + d + r1 + d); n     dp[i + 1][1] = n         min(dp[i + 1][1], dp[i][1] + d + 2 * d + r1 + (a[i + 1] + 1) * r1); n     if (i + 1 == n) { n       dp[i + 1][0] = n           min(dp[i + 1][0], dp[i][1] + d + a[i + 1] * r1 + r3 + d + r1); n     } n   } n   cout << dp[n][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int howfar(const std::string& s) { n   const int N = s.length(); n   int r(0); n   int d(1); n   for (int i = 0; i != N; ++i) { n     switch (s[i]) { n       case  F : n         r += d; n         break; n       case  T : n         d = -d; n         break; n       default: n         assert(0); n     } n   } n   return abs(r); n } n char invert(char c) { return c ==  T  ?  F  :  T ; } n int solve(const std::string& s, int n) { n   const int N = s.length(); n   if (n > 0 && n % 2 == 0) { n     int rm(-10000); n     std::string sm; n     std::string s2 = s; n     for (int i1 = 0; i1 != N; ++i1) { n       for (int i2 = 0; i2 != N; ++i2) { n         s2[i1] = invert(s2[i1]); n         s2[i2] = invert(s2[i2]); n         const int r2 = howfar(s2); n         if (r2 > rm) { n           rm = r2; n           sm = s2; n         } n         s2[i2] = invert(s2[i2]); n         s2[i1] = invert(s2[i1]); n       } n     } n     return solve(sm, n - 2); n   } else if (n > 0) { n     int rm(-10000); n     std::string sm = s; n     for (int i = 0; i != N; ++i) { n       std::string s2 = s; n       s2[i] = invert(s2[i]); n       const int r = howfar(s2); n       if (r > rm) { n         rm = r; n         sm = s2; n       } n     } n     return solve(sm, n - 1); n   } n   return howfar(s); n } n int main(int argc, char* argv[]) { n   std::string s; n   cin >> s; n   int n; n   cin >> n; n   cout << solve(s, n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n long long dp[15][(1 << 13) + 10][15]; n struct E { n   int a, b; n } e[20]; n struct Lca { n   int a, b, c; n } lca[100 + 10]; n int num[(1 << 13) + 10]; n bool il(int u, int state, int last) { n   for (int i = 1; i <= q; i++) { n     int xx = ((state >> lca[i].a - 1) & 1) + ((state >> lca[i].b - 1) & 1) + n              ((state >> lca[i].c - 1) & 1); n     if (xx == 2 && (u != lca[i].c || last == 0)) n       return true; n     else if (xx == 1 && ((state >> lca[i].c - 1) & 1) && last == 0) n       return true; n     else if (xx == 3 && (u == lca[i].a || u == lca[i].b) && u != lca[i].c) n       return true; n   } n   for (int i = 1; i <= m; i++) { n     int xx = ((state >> e[i].a - 1) & 1) + ((state >> e[i].b - 1) & 1); n     if (xx == 1 && u != e[i].a && u != e[i].b) return true; n   } n   return false; n } n long long dfs(int u, int state, int last) { n   if (dp[u][state][last] != -1) return dp[u][state][last]; n   dp[u][state][last] = 0; n   if (il(u, state, last)) return dp[u][state][last]; n   if (num[state] == 1) { n     return dp[u][state][last] = 1; n   } n   int full = state ^ (1 << u - 1); n   for (int x = full; x; x = (x - 1) & full) { n     int ok = true; n     int y = full ^ x; n     for (int i = 1; i <= m; i++) { n       if ((((x >> (e[i].a - 1)) & 1) && ((y >> (e[i].b - 1)) & 1)) || n           (((y >> (e[i].a - 1)) & 1) && ((x >> (e[i].b - 1)) & 1))) { n         ok = false; n         break; n       } n     } n     if (!ok) continue; n     for (int i = last + 1; i <= n; i++) n       if ((x >> (i - 1)) & 1) n         dp[u][state][last] += dfs(u, y | (1 << u - 1), i) * dfs(i, x, 0); n   } n   return dp[u][state][last]; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &e[i].a, &e[i].b); n   } n   for (int i = 1; i <= q; i++) { n     scanf( %d%d%d , &lca[i].a, &lca[i].b, &lca[i].c); n   } n   for (int i = 0; i < (1 << n); i++) { n     int x = i; n     while (x) { n       num[i] += x & 1; n       x >>= 1; n     } n   } n   memset(dp, -1, sizeof(dp)); n   printf( %I64d n , dfs(1, (1 << n) - 1, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 1e3 + 7; n int n, m, tot, root[N]; n long long p, ans; n struct node { n   int ls, rs; n   long long sum; n } t[N * 30]; n void insert(int x, int &y, int l, int r, int v) { n   y = ++tot; n   t[y] = t[x]; n   t[y].sum++; n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (v <= mid) n     insert(t[x].ls, t[y].ls, l, mid, v); n   else n     insert(t[x].rs, t[y].rs, mid + 1, r, v); n } n long long query(int l, int r, int x, int y, int L, int R) { n   if (L > R || l > r || L > r || R < l) return 0; n   if (l >= L && r <= R) return t[y].sum - t[x].sum; n   int mid = (l + r) >> 1; n   return query(l, mid, t[x].ls, t[y].ls, L, R) + n          query(mid + 1, r, t[x].rs, t[y].rs, L, R); n } n long long cal(long long x) { return x * (x - 1) / 2; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     insert(root[i - 1], root[i], 1, n, x); n   } n   while (m--) { n     int l, r, u, v; n     scanf( %d%d%d%d , &l, &r, &u, &v); n     ans = (long long)n * (long long)(n - 1) / 2; n     p = query(1, n, root[0], root[n], 1, r - 1); n     ans -= cal(p); n     p = query(1, n, root[0], root[n], v + 1, n); n     ans -= cal(p); n     p = query(1, n, root[0], root[l - 1], 1, n); n     ans -= cal(p); n     p = query(1, n, root[u], root[n], 1, n); n     ans -= cal(p); n     p = query(1, n, root[0], root[l - 1], 1, r - 1); n     ans += cal(p); n     p = query(1, n, root[0], root[l - 1], v + 1, n); n     ans += cal(p); n     p = query(1, n, root[u], root[n], 1, r - 1); n     ans += cal(p); n     p = query(1, n, root[u], root[n], v + 1, n); n     ans += cal(p); n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n char _buf[100000], *_op(_buf), *_ed(_buf); n inline int getint() { n   int _s = 0, _f = 1; n   char _ch = (_op == _ed && (_ed = (_op = _buf) + fread(_buf, 1, 100000, stdin), n                              _op == _ed) n                   ? EOF n                   : *_op++); n   while (!( 0  <= _ch && _ch <=  9 )) n     (_ch ==  - ) && (_f = -1), n         _ch = n             (_op == _ed && (_ed = (_op = _buf) + fread(_buf, 1, 100000, stdin), n                             _op == _ed) n                  ? EOF n                  : *_op++); n   while (( 0  <= _ch && _ch <=  9 )) n     _s = _s * 10 + _ch - 48, n     _ch = (_op == _ed && (_ed = (_op = _buf) + fread(_buf, 1, 100000, stdin), n                           _op == _ed) n                ? EOF n                : *_op++); n   return _s * _f; n } n const int N = 2e5 + 1, mod = 1e9 + 7; n inline void check(int& x) { x -= mod, x += x >> 31 & mod; } n inline void cmax(int& x, int y) { n   if (x - y >> 31) x = y; n } n inline void cmin(int& x, int y) { n   if (y - x >> 31) x = y; n } n int n, m, ans; n int Sx, Sy, Fx, Fy; n int X[N], Y[N], tag[N]; n int idx[N], idy[N], posx[N], posy[N]; n inline int dis(int x, int y, int p) { n   return min(abs(X[p] - x), abs(Y[p] - y)); n } n priority_queue<pair<int, int> > q; n inline void input() { n   n = getint(), m = getint(); n   Sx = getint(), Sy = getint(), Fx = getint(), Fy = getint(); n   for (int i = 1; i <= m; i++) X[i] = getint(), Y[i] = getint(); n   ans = abs(Fx - Sx) + abs(Fy - Sy); n   for (int i = 1; i <= m; i++) q.push(pair<int, int>(-dis(Sx, Sy, i), i)); n } n inline int cmpx(int x, int y) { return X[x] < X[y]; } n inline int cmpy(int x, int y) { return Y[x] < Y[y]; } n inline void init() { n   for (int i = 1; i <= m; i++) idx[i] = i; n   sort(idx + 1, idx + 1 + m, cmpx); n   for (int i = 1; i <= m; i++) posx[idx[i]] = i; n   for (int i = 1; i <= m; i++) idy[i] = i; n   sort(idy + 1, idy + 1 + m, cmpy); n   for (int i = 1; i <= m; i++) posy[idy[i]] = i; n } n inline void work() { n   for (int u, w, v; !q.empty();) { n     w = -q.top().first, u = q.top().second, q.pop(); n     if (tag[u]) continue; n     tag[u] = 1, ans = min(0ll + ans, 0ll + w + abs(X[u] - Fx) + abs(Y[u] - Fy)); n     if ((v = idx[posx[u] - 1]) && !tag[v]) n       q.push(pair<int, int>(-w - X[u] + X[v], v)); n     if ((v = idx[posx[u] + 1]) && !tag[v]) n       q.push(pair<int, int>(-w - X[v] + X[u], v)); n     if ((v = idy[posy[u] - 1]) && !tag[v]) n       q.push(pair<int, int>(-w - Y[u] + Y[v], v)); n     if ((v = idy[posy[u] + 1]) && !tag[v]) n       q.push(pair<int, int>(-w - Y[v] + Y[u], v)); n   } n   cout << ans <<   n ; n } n int main() { n   input(); n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, K; n char maze[2010][2010]; n int up[2010][2010], down[2010][2010]; n struct Segtree { n   int key[2010 * 4], ans[2010 * 4]; n   void add(deque<pair<int, int> > &que, int pos, int val) { n     while (!que.empty() && val < que.back().second) que.pop_back(); n     que.push_back(make_pair(pos, val)); n   } n   void remove(deque<pair<int, int> > &que, int pos) { n     while (!que.empty() && que.front().first <= pos) que.pop_front(); n   } n   void cal(int l, int r, int step) { n     int mid = l + r >> 1; n     deque<pair<int, int> > qU, qD; n     int i = 0, j = 0; n     key[step] = 0; n     for (; j < m; j++) { n       int a, b; n       add(qU, j, mid - max(l - 1, up[mid][j])); n       if (mid < r) n         add(qD, j, b = (min(r + 1, down[mid + 1][j]) - mid - 1)); n       else n         add(qD, j, b = 0); n       key[step] = n           max(key[step], min(j - i + 1, qU.front().second + qD.front().second)); n       while (i < j && j - i + 1 > qU.front().second + qD.front().second) { n         remove(qU, i); n         remove(qD, i); n         i++; n         key[step] = max(key[step], n                         min(j - i + 1, qU.front().second + qD.front().second)); n       } n     } n   } n   void build(int l, int r, int step) { n     cal(l, r, step); n     if (l == r) return; n     int mid = l + r >> 1; n     build(l, mid, step * 2); n     build(mid + 1, r, step * 2 + 1); n     ans[step] = max(key[step], max(ans[step * 2], ans[step * 2 + 1])); n   } n   void update(int l, int r, int pos, int step) { n     cal(l, r, step); n     if (l == r) return; n     int mid = l + r >> 1; n     if (pos <= mid) n       update(l, mid, pos, step * 2); n     else n       update(mid + 1, r, pos, step * 2 + 1); n     ans[step] = max(key[step], max(ans[step * 2], ans[step * 2 + 1])); n   } n } tree; n int main() { n   scanf( %d%d%d , &n, &m, &K); n   for (int i = 0; i < (n); i++) scanf( %s , maze[i]); n   for (int j = 0; j < (m); j++) up[0][j] = maze[0][j] ==  .  ? -1 : 0; n   for (int i = 1; i < n; i++) n     for (int j = 0; j < (m); j++) n       up[i][j] = maze[i][j] ==  .  ? up[i - 1][j] : i; n   for (int j = 0; j < (m); j++) n     down[n - 1][j] = maze[n - 1][j] ==  .  ? n : n - 1; n   for (int i = n - 2; i >= 0; i--) n     for (int j = 0; j < (m); j++) n       down[i][j] = maze[i][j] ==  .  ? down[i + 1][j] : i; n   tree.build(0, n - 1, 1); n   while (K--) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--, y--; n     maze[x][y] =  # ; n     up[0][y] = maze[0][y] ==  .  ? -1 : 0; n     for (int i = 1; i < n; i++) up[i][y] = maze[i][y] ==  .  ? up[i - 1][y] : i; n     down[n - 1][y] = maze[n - 1][y] ==  .  ? n : n - 1; n     for (int i = n - 2; i >= 0; i--) n       down[i][y] = maze[i][y] ==  .  ? down[i + 1][y] : i; n     tree.update(0, n - 1, x, 1); n     printf( %d n , tree.ans[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<pair<int, int> > v[1000007]; n vector<int> rv[1000007]; n int comp[1000007]; n vector<pair<int, int> > g[1000007]; n long long sm[1000007]; n int used[1000007]; n int stver; n stack<int> s; n vector<int> srt; n bool val[100000002]; n long long hval[1000007]; n long long dp[1000007]; n map<int, int> ZX; n void dfs(int vertex) { n   used[vertex] = 1; n   int i; n   int sz = v[vertex].size(); n   for (i = 0; i < sz; i++) { n     if (used[v[vertex][i].first] == 0) { n       dfs(v[vertex][i].first); n     } n   } n   s.push(vertex); n } n void revdfs(int vertex, int id) { n   comp[vertex] = id; n   int i; n   int sz = rv[vertex].size(); n   for (i = 0; i < sz; i++) { n     if (comp[rv[vertex][i]] == 0) { n       revdfs(rv[vertex][i], id); n     } n   } n } n void precalc() { n   int i; n   long long cur = 0; n   int br = 1; n   if (val[0] == true) { n     hval[0] = 1; n   } n   for (i = 1; i < 100000002; i++) { n     cur = cur + br; n     if (i >= (br * (br + 1) / 2)) { n       br++; n     } n     if (val[i] == true) { n       hval[ZX[i]] = cur; n     } n   } n } n void calc_dp(int vertex) { n   used[vertex] = 1; n   int i; n   int sz = g[vertex].size(); n   for (i = 0; i < sz; i++) { n     if (used[g[vertex][i].first] == 0) { n       calc_dp(g[vertex][i].first); n     } n     if (dp[vertex] < dp[g[vertex][i].first] + g[vertex][i].second) { n       dp[vertex] = dp[g[vertex][i].first] + g[vertex][i].second; n     } n   } n   dp[vertex] += sm[vertex]; n } n void input() { n   scanf( %d%d , &n, &m); n   int i; n   for (i = 1; i <= m; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     v[x].push_back(make_pair(y, z)); n     rv[y].push_back(x); n     srt.push_back(z); n     val[z] = true; n   } n   if (srt.size() != 0) { n     sort(srt.begin(), srt.end()); n     ZX[srt[0]] = 1; n     for (i = 1; i < m; i++) { n       if (srt[i] == srt[i - 1]) { n         continue; n       } n       ZX[srt[i]] = ZX[srt[i - 1]] + 1; n     } n   } n   scanf( %d , &stver); n   precalc(); n } n void solve() { n   int i, j; n   for (i = 1; i <= n; i++) { n     if (used[i] == 0) { n       dfs(i); n     } n   } n   int tp = 1; n   while (s.empty() == false) { n     int u = s.top(); n     s.pop(); n     if (comp[u] != 0) { n       continue; n     } n     revdfs(u, tp); n     tp++; n   } n   for (i = 1; i <= n; i++) { n     int sz = v[i].size(); n     for (j = 0; j < sz; j++) { n       if (comp[v[i][j].first] == comp[i]) { n         sm[comp[i]] += hval[ZX[v[i][j].second]]; n       } else { n         g[comp[i]].push_back(make_pair(comp[v[i][j].first], v[i][j].second)); n       } n     } n   } n   for (i = 1; i <= n; i++) { n     used[i] = 0; n   } n   stver = comp[stver]; n   calc_dp(stver); n   printf( %I64d n , dp[stver]); n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 2e18; n bool isPrime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long int i = 5; i * i <= n; i = i + 6) { n     if (n % i == 0 || n % (i + 2) == 0) return false; n   } n   return true; n } n long long int exp(long long int x, long long int n) { n   long long int res = 1; n   while (n > 0) { n     if (n % 2 == 1) res = (res * x) % 1000000007; n     x = (x * x) % 1000000007; n     n = n / 2; n   } n   return res; n } n void null() { n   long long int n, l, r; n   cin >> n >> l >> r; n   vector<long long int> a, b; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     a.push_back(x); n   } n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     b.push_back(x); n   } n   for (long long int i = 0; i < l - 1; i++) { n     if (a[i] != b[i]) { n       cout <<  LIE ; n       return; n     } n   } n   for (long long int i = r; i < n; i++) { n     if (a[i] != b[i]) { n       cout <<  LIE ; n       return; n     } n   } n   cout <<  TRUTH ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   clock_t start, end; n   start = clock(); n   cout << fixed << setprecision(20); n   while (t--) { n     null(); n   } n   end = clock(); n   double time_taken = double(end - start) / double(CLOCKS_PER_SEC); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[105], rev[105], b[105]; n long double ans = 0, cnt = 0, f[2][35][35], g[31][31]; n void dfs(int u) { n   if (!u) { n     for (int i = 1; i <= n; i++) n       for (int j = i + 1; j <= n; j++) n         if (a[i] > a[j]) ans++; n     cnt++; n     return; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       for (int k = i; k <= (i + j) / 2; k++) swap(a[k], a[j - (k - i)]); n       dfs(u - 1); n       for (int k = i; k <= (i + j) / 2; k++) swap(a[k], a[j - (k - i)]); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   long double kk = n * (n + 1) / 2.0; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (a[i] > a[j]) f[0][i][j] = 1; n   for (int o = 1; o <= m; o++) { n     int p = o & 1; n     memset(f[p], 0, sizeof(f[p])); n     for (int i = 1; i <= n; i++) { n       for (int j = i; j <= n; j++) { n         for (int k = 1; k <= n; k++) b[k] = k; n         for (int k = i; k <= (j + i) / 2; k++) swap(b[k], b[j - k + i]); n         for (int x = 1; x <= n; x++) n           for (int y = x + 1; y <= n; y++) n             if (b[x] < b[y]) n               f[p][b[x]][b[y]] += f[p ^ 1][x][y] / kk; n             else n               f[p][b[y]][b[x]] += (1 - f[p ^ 1][x][y]) / kk; n       } n     } n   } n   long double ans = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) ans += f[m & 1][i][j]; n   printf( %.10lf n , (double)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int inv[32][2]; n void rec(vector<int>& a, int b) { n   if (b < 0 || a.size() == 0) return; n   int c[2] = {0}; n   vector<int> v[2]; n   for (int x : a) { n     int val; n     if ((x & (1 << b)) > 0) n       val = 1; n     else n       val = 0; n     if (val == 0) n       inv[b][0] += c[1]; n     else n       inv[b][1] += c[0]; n     c[val]++; n     v[val].push_back(x); n   } n   rec(v[0], b - 1); n   rec(v[1], b - 1); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   rec(a, 30); n   long long int x = 0, ans = 0; n   for (int i = 0; i <= 30; i++) { n     ans += min(inv[i][0], inv[i][1]); n     if (inv[i][0] > inv[i][1]) x += (1 << i); n   } n   cout << ans <<     << x <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n vector<int> g[N]; n void dfs(int u, vector<int>& dep, int par = -1, int h = 0) { n   dep[u] = h; n   for (int v : g[u]) { n     if (v == par) continue; n     dfs(v, dep, u, h + 1); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, x; n   cin >> n >> x; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   vector<int> dep_r(n + 1); n   vector<int> dep_x(n + 1); n   dfs(1, dep_r); n   dfs(x, dep_x); n   int mx = -1; n   for (int i = 1; i <= n; i++) { n     if (dep_x[i] < dep_r[i]) { n       mx = max(mx, dep_r[i] << 1); n     } n   } n   cout << mx <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const long long inf = 1e18; n const int maxn = 1e5 + 10; n const double eps = 1e-7; n const double pi = 3.14159265359; n int n, m, t[maxn]; n int par[maxn]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n   } n   par[0] = t[0]; n   for (int i = 1; i < n; i++) { n     par[i] = par[i - 1] + t[i]; n   } n   for (int i = 0; i < n; i++) { n     if (i == 0) { n       cout << 0 <<    ; n       continue; n     } n     sort(t, t + i); n     int res = 0; n     int s = par[i]; n     int id = i - 1; n     while (s > m) { n       s -= t[id]; n       id--; n       res++; n     } n     cout << res <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n int b[10] = {0, 2, 3, 1}, a[maxn]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     long long n, ans = 1; n     cin >> n; n     int m = n % 3; n     n = (n + 2) / 3; n     while (n > ans) { n       n -= ans; n       ans *= 4; n     } n     int cnt = 0; n     long long ans1 = ans + n - 1, ans2 = ans * 2; n     n--; n     while (n) { n       a[++cnt] = n % 4; n       n /= 4; n     } n     long long now = 1; n     for (int i = 1; i <= cnt; i++) ans2 += b[a[i]] * now, now *= 4; n     if (m == 1) n       cout << ans1 << endl; n     else if (m == 2) n       cout << ans2 << endl; n     else if (m == 0) n       cout << (ans1 ^ ans2) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int q; n void solve(long long int x) { n   for (long long int i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       long long int temp = x / i; n       for (long long int j = 2; j * j <= temp; j++) { n         if (temp % j == 0) { n           cout << 1 <<   n ; n           cout << i * j; n           return; n         } n       } n       cout << 2; n       return; n     } n   } n   cout << 1 <<   n ; n   cout << 0; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> q; n   if (q == 1) { n     cout << 1 <<   n ; n     cout << 0; n     return 0; n   } n   solve(q); n   return 0; n } n


#include <bits/stdc++.h> n int n; n int x[200050]; n int f[200050]; n int ans = 0; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &x[i]); n   for (int i = 0; i < n; i++) scanf( %d , &f[i]); n   int k = 0, l = 0; n   while (l < n) { n     if (x[k] == f[l]) n       k++, l++; n     else n       ans++, l++; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n struct point { n   double x, y, z; n } P[5200]; n inline double sqr(double x) { return x * x; } n inline double dist(point a, point b) { n   return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y) + sqr(a.z - b.z)); n } n inline double len(point a, point b, point c) { n   return dist(a, b) + dist(a, c) + dist(b, c); n } n int main() { n   cin >> n; n   double ans = 1e100; n   for (int i = 0; i < n; i++) cin >> P[i].x >> P[i].y >> P[i].z; n   for (int i = 1; i < n; i++) n     for (int j = i + 1; j < n; j++) ans = min(ans, len(P[0], P[i], P[j])); n   cout.precision(11); n   cout << ans / 2 << endl; n } n


#include <bits/stdc++.h> n int main() { n   int m, n, i, j, p, q; n   scanf( %d %d , &m, &n); n   scanf( %d %d , &p, &q); n   int ar[m], br[n]; n   for (i = 0; i < m; i++) { n     scanf( %d , &ar[i]); n   } n   for (i = 0; i < n; i++) { n     scanf( %d , &br[i]); n   } n   if (ar[p - 1] < br[n - q]) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[100005]; n int F[100005]; n int main() { n   int n, x; n   cin >> n >> x; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &A[i]); n     F[A[i]]++; n   } n   int idx = (n + 1) / 2; n   bool wasPresent = true; n   if (!F[x]) { n     wasPresent = false; n     A[n + 1] = x; n     n++; n   } n   sort(A + 1, A + n + 1); n   int ans = 100005; n   for (int i = 1; i <= n; i++) { n     if (A[i] == x) { n       int add = 0; n       if ((i - 1) > (n - i)) { n         add = (i - 1) - (n - i); n       } else if ((i - 1) < (n - i)) { n         add = (n - i) - (i - 1) - 1; n       } n       ans = min(ans, add); n     } n   } n   if (!wasPresent) ans++; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000000000000000000; n map<pair<long long, int>, long long> memo; n long long solve(long long n, int x) { n   pair<long long, int> p = make_pair(n, x); n   if (memo.find(p) != memo.end()) return memo[p]; n   if (n == 0) return memo[p] = 0; n   if (n == 1) return memo[p] = (x == 0) ? 1 : 0; n   return memo[p] = solve((n + 1) / 2, x) + (x ? solve(n / 2, x - 1) : 0); n } n int main() { n   ios_base::sync_with_stdio(false); n   long long n, t; n   cin >> n >> t; n   for (int i = 0; (1ll << i) <= t; ++i) n     if (t == (1ll << i)) { n       cout << solve(n + 2, i + 1) - solve(2, i + 1); n       return 0; n     } n   cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   int a = 0, b = 0, c = 0, d = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] >=  A  && s[i] <=  Z ) n       a = 1; n     else if (s[i] >=  a  && s[i] <=  z ) n       b = 1; n     else if (s[i] >=  0  && s[i] <=  9 ) n       c = 1; n   } n   if (n >= 5) { n     if (a && b && c) n       cout <<  Correct  << endl; n     else n       cout <<  Too weak  << endl; n   } else n     cout <<  Too weak  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200010]; n int main() { n   int n; n   while (~scanf( %lld , &n)) { n     long long ans = 0; n     for (int i = 0; i < n; ++i) scanf( %lld , &a[i]); n     for (int i = 0; i < n;) { n       ans++; n       long long j = i; n       while (j < n && a[j] == -1) j++; n       if (j == n) break; n       long long k = j + 1; n       while (k < n && a[k] == -1) k++; n       if (k == n) break; n       long long d = k - j; n       if ((a[k] - a[j]) % d) { n         i = k; n         continue; n       } n       long long s = (a[k] - a[j]) / d; n       if (s > 0 && a[j] - s * (j - i) <= 0) { n         i = k; n         continue; n       } n       long long r = k + 1; n       while (r < n) { n         long long e = a[k] + s * (r - k); n         if (a[r] != -1 && a[r] != e) break; n         if (e <= 0) break; n         r++; n       } n       i = r; n     } n     printf( %lld n , ans); n   } n   return 0; n } n