#include <bits/stdc++.h> n int ct, n, m, tct, cnt[540010], mp[540010], t[540010], a[50010][4], n     lmax = 27 * 27 * 27 * 27, B, pool[200010], *pt(pool), n     *v[540010], ans[70010][402]; n char s[50010], x[10], y[10]; n int nd(const char* st, const char* ed) { n   int res = 0; n   while (st != ed) res = res * 27 + (*st++ -  a  + 1); n   return res; n } n int leng(int i) { n   if (i < 27) return 1; n   if (i < 27 * 27) return 2; n   if (i < 27 * 27 * 27) return 3; n   return 4; n } n int main() { n   scanf( %s%d , s, &m); n   n = strlen(s); n   B = 500; n   for (int i = 0; i < n; i++) { n     if (i <= n - 4) ++cnt[a[i][3] = nd(s + i, s + i + 4)]; n     if (i <= n - 3) ++cnt[a[i][2] = nd(s + i, s + i + 3)]; n     if (i <= n - 2) ++cnt[a[i][1] = nd(s + i, s + i + 2)]; n     ++cnt[a[i][0] = nd(s + i, s + i + 1)]; n   } n   for (int i = 1; i < lmax; i++) n     if (cnt[i]) { n       mp[i] = ++ct; n       v[mp[i]] = pt, pt += cnt[i]; n       cnt[i] = 0; n     } n   for (int k = 0; k <= 3; k++) n     for (int j = 0; j < n - k; j++) { n       int p = mp[a[j][k]]; n       v[p][cnt[p]++] = j; n     } n   for (int i = 1; i <= lmax; i++) n     if (cnt[mp[i]] >= B) { n       t[mp[i]] = ++tct; n       int q = t[mp[i]], d = leng(i); n       for (int j = 1; j <= ct; j++) ans[j][q] = 0x3f3f3f3f; n       int rl = -5, ll = -5; n       for (int k = 0; k <= 3; k++) { n         rl = -5; n         for (int j = 0; j < n - k; j++) { n           int p = mp[a[j][k]]; n           if (a[j][d - 1] == i) ll = j - 1, rl = j + d - 1; n           if (rl != -5) n             ans[p][q] = std::min(ans[p][q], std::max(j + k, rl) - ll); n         } n       } n       for (int k = 0; k <= 3; k++) { n         rl = -5; n         for (int j = n - k - 1; ~j; j--) { n           int p = mp[a[j][k]]; n           if (a[j][d - 1] == i) ll = j, rl = j + d; n           if (rl != -5) n             ans[p][q] = n                 std::min(ans[p][q], std::max(rl, j + k + 1) - std::min(ll, j)); n         } n       } n       ans[mp[i]][q] = d; n     } n   while (m--) { n     scanf( %s%s , x, y); n     int xw = strlen(x), yw = strlen(y); n     int x1 = nd(x, x + xw), y1 = nd(y, y + yw); n     if (!mp[x1] || !mp[y1]) { n       puts( -1 ); n     } else { n       x1 = mp[x1], y1 = mp[y1]; n       if (cnt[x1] > cnt[y1]) x1 ^= y1 ^= x1 ^= y1, xw ^= yw ^= xw ^= yw; n       if (cnt[y1] >= B) { n         printf( %d n , ans[x1][t[y1]]); n       } else { n         int i = 0, j = 0, ans = 0x3f3f3f3f; n         while (i < cnt[x1] && j < cnt[y1]) { n           if (v[x1][i] < v[y1][j]) n             ans = std::min(ans, n                            std::max(v[x1][i] + xw, v[y1][j] + yw) - v[x1][i]), n             ++i; n           else n             ans = std::min(ans, n                            std::max(v[x1][i] + xw, v[y1][j] + yw) - v[y1][j]), n             ++j; n         } n         printf( %d n , ans); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t = 1; n   while (t--) { n     string s; n     cin >> s; n     sort(s.begin(), s.end()); n     long long n = s.size(), ans = 0; n     long long l = 0, r = n - 1; n     while (l <= r) { n       long long m = l + (r - l) / 2; n       if (s[m] == s[n - 1]) { n         if (s[m - 1] != s[n - 1] || m == 0) { n           ans = m; n           break; n         } else n           r = m - 1; n       } else { n         l = m + 1; n       } n     } n     cout << s.substr(ans) << endl; n   } n } n


#include <bits/stdc++.h> n const int maxn = 1011; n const int mod = 256; n char str[maxn]; n int main() { n   gets(str); n   int len = strlen(str); n   int sad = 0; n   for (int i = 0; i < len; i++) { n     int a[11] = {0}, j = 0, temp = 0; n     int c = str[i]; n     while (c) { n       a[j++] = c % 2; n       c /= 2; n     } n     for (int j = 7, k = 1; j >= 0; j--, k *= 2) { n       temp += a[j] * k; n     } n     int res = (sad - temp + mod) % mod; n     printf( %d n , res); n     sad = temp; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   register long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = getchar(); n   } n   return (f == 1) ? x : -x; n } n int cnt[10101010], X[10101010]; n bool vis[10101010]; n long long F[10101010], pw2[1101010]; n int tot, p[1010101], a[1010101], lim, mu[10101010]; n inline void seive(int n) { n   for (int i = 2; i <= n; i++) { n     if (!vis[i]) p[++tot] = i, X[i] = i, mu[i] = -1; n     for (int j = 1; j <= tot; j++) { n       if (1ll * p[j] * i > n) break; n       vis[p[j] * i] = 1, mu[p[j] * i] = -mu[i]; n       if (i % p[j] == 0) { n         mu[i * p[j]] = 0; n         X[i * p[j]] = X[i]; n         break; n       } else n         X[i * p[j]] = X[i] * p[j]; n     } n   } n } n inline void FMTand(long long *F, int lim) { n   for (int i = 1; i <= tot; i++) { n     for (int j = lim / p[i]; j; j--) { n       F[j] = (F[j] + F[j * p[i]]) % 1000000007; n     } n   } n } n inline void FMTor(long long *F, int lim) { n   for (int i = 1; i <= tot; i++) { n     for (int j = 1; j * p[i] <= lim; j++) { n       F[j * p[i]] = (F[j * p[i]] + F[j]) % 1000000007; n     } n   } n } n signed main() { n   long long n = read(); n   pw2[0] = 1; n   for (int i = 1; i <= n; i++) pw2[i] = 1ll * pw2[i - 1] * 2 % 1000000007; n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     cnt[a[i]]++; n     lim = max(lim, a[i]); n     F[a[i]] = cnt[a[i]]; n   } n   seive(lim); n   FMTand(F, lim); n   long long ONE = pw2[F[1]] - 1, _ = 0; n   for (int i = 2; i <= lim; i++) { n     ONE = n         (1000000007 + ONE + mu[i] * (pw2[F[i]] - 1) % 1000000007) % 1000000007; n   } n   for (int i = 1; i <= lim; i++) { n     F[i] = (1000000007 + (pw2[F[i]] - 1) * (1000000007 - mu[i]) % 1000000007) % n            1000000007; n   } n   FMTor(F, lim); n   for (int i = 1; i <= lim; i++) { n     _ = (_ + 1ll * cnt[i] * (pw2[n] - 1 - ONE - F[X[i]]) % 1000000007) % n         1000000007; n   } n   cout << (_ % 1000000007 + 1000000007) % 1000000007; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[101][3] = {}; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1, a; i <= n; ++i) { n     cin >> a; n     dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1], dp[i - 1][2])); n     if (a & 1) dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + 1; n     if (a & 2) dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + 1; n   } n   cout << n - max(dp[n][0], max(dp[n][1], dp[n][2])) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int a[maxn], m, n, ans, dp[maxn]; n inline bool check(int x) { n   memset(dp, 0, sizeof(dp)); n   for (int sta = 0; sta <= 1 && sta < n; sta++) { n     dp[sta] = sta ? max(a[0] + x, a[1]) : a[0]; n     for (int i = sta + 1; i < n; i++) { n       dp[i] = dp[i - 1]; n       if (dp[i - 1] >= a[i] - 1) dp[i] = max(dp[i], a[i] + x); n       if (dp[i - 1] >= a[i] - x - 1) dp[i] = max(dp[i], a[i]); n       if (dp[i - 2] >= a[i] - x - 1 && i >= 2) dp[i] = max(dp[i], a[i - 1] + x); n     } n     if (dp[n - 1] >= min(m - 1, m + a[sta] - x - 1)) return 1; n   } n   return 0; n } n int main() { n   scanf( %d%d , &m, &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   sort(a, a + n); n   pair<int, int> pos(a[0] + m - a[n - 1], 0); n   for (int i = 1; i < n; i++) { n     pos = max(pos, make_pair(a[i] - a[i - 1], i)); n   } n   rotate(a, a + pos.second, a + n); n   for (int i = n - 1; i >= 0; i--) { n     a[i] -= a[0]; n     if (a[i] < 0) { n       a[i] += m; n     } n   } n   int l = 0, r = a[0] - a[n - 1] + m - 1, ans = r; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid)) { n       ans = mid, r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n + 1); n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   int u = 0, v = 1; n   vector<vector<double>> d[2]; n   d[u].assign(n + 1, vector<double>(n + 1)); n   for (int i = 1; i <= n; ++i) { n     for (int j = i + 1; j <= n; ++j) { n       if (a[i] < a[j]) { n         d[u][i][j] = 1.0; n       } else { n         d[u][j][i] = 1.0; n       } n     } n   } n   double p = 2.0 / (n * (n + 1)); n   for (; k--; swap(u, v)) { n     d[v].assign(n + 1, vector<double>(n + 1)); n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= n; ++j) { n         if (i == j) { n           continue; n         } n         for (int x = 1; x <= n; ++x) { n           for (int y = x; y <= n; ++y) { n             int ni = i; n             if (x <= i && i <= y) { n               ni = x + y - i; n             } n             int nj = j; n             if (x <= j && j <= y) { n               nj = x + y - j; n             } n             d[v][ni][nj] += d[u][i][j] * p; n           } n         } n       } n     } n   } n   double ans = 0.0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j < i; ++j) { n       ans += d[u][i][j]; n     } n   } n   cout << ans << endl; n } n void init() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.precision(10); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f(int, int); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   int a[m + 1]; n   int c[m]; n   int b = 0; n   for (int i = 0; i < m; i++) { n     c[i] = 0; n   } n   for (int i = 0; i <= m; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < m; i++) { n     for (int j = 1; j <= n; j++) { n       if (f(a[i], j) != f(a[m], j)) c[i]++; n     } n     if (c[i] <= k) b++; n   } n   cout << b; n   return (0); n } n int f(int q, int w) { n   int e = q; n   for (int i = 1; i < w; i++) { n     e = e / 2; n   } n   return (e % 2); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& s, vector<T> t) { n   for (int i = 0; i < (int)(t.size()); i++) s << (i ?     :   ) << t[i]; n   return s; n } n template <typename T> n istream& operator>>(istream& s, vector<T>& t) { n   for (int i = 0; i < (int)(t.size()); i++) s >> t[i]; n   return s; n } n template <typename T, typename U> n ostream& operator<<(ostream& s, pair<T, U> t) { n   s <<  (  << t.first <<  ,  << t.second <<  ) ; n   return s; n } n template <typename T, typename U> n istream& operator>>(istream& s, pair<T, U>& t) { n   s >> t.first >> t.second; n   return s; n } n typedef pair<double, double> it; n const double EPS = 1e-15; n it intersect(it a, it b) { n   if (a.first == -1 || b.first == -1) return {-1, -1}; n   it res = {max(a.first, b.first), min(a.second, b.second)}; n   if (res.first + EPS >= res.second) return {-1, -1}; n   return res; n } n it get1D(double r, double v, double x1, double x2) { n   if (v == 0) { n     if (r > x1 && r < x2) n       return {0, 1e18}; n     else n       return {-1, -1}; n   } n   double t1 = (x1 - r) / v, t2 = (x2 - r) / v; n   if (t2 < t1) swap(t1, t2); n   if (t2 < 0) return {-1, -1}; n   if (t1 < 0) t1 = 0; n   return {t1, t2}; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   double x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   it res = {0, 1e18}; n   for (int i = 0; i < (int)(n); i++) { n     double rx, ry, vx, vy; n     cin >> rx >> ry >> vx >> vy; n     it xi = get1D(rx, vx, x1, x2); n     it yi = get1D(ry, vy, y1, y2); n     it cur = intersect(xi, yi); n     res = intersect(res, cur); n   } n   cout << fixed << setprecision(9); n   if (res.first == -1) { n     cout <<  -1  << endl; n   } else { n     cout << res.first << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int off = 1 << 18; n const int MOD = 1e9 + 7; n int q, n = 1; n int st[off << 1] = {}, sp[off], st2[off << 1]; n vector<int> g[200001]; n int qt[200000], qv[200000], qval[200000], ch[200001] = {}; n int tin[200001], tout[200001], timer = -1, par[200001]; n void dfs(int v) { n   tin[v] = ++timer; n   for (int to : g[v]) { n     dfs(to); n   } n   tout[v] = timer; n } n void stBuild() { n   for (int i = off - 1; i >= 1; --i) { n     st[i] = st[2 * i] + st[2 * i + 1]; n     sp[i] = 1; n   } n   for (int i = 0; i < (int)(2 * off); ++i) st2[i] = 1; n } n void stPush(int v) { n   for (int i = 2 * v; i <= 2 * v + 1; ++i) { n     st[i] = (long long)st[i] * sp[v] % MOD; n     if (i < off) { n       sp[i] = (long long)sp[i] * sp[v] % MOD; n     } n   } n   sp[v] = 1; n } n int mul; n void stMul(int v, int L, int R, int l, int r) { n   if (L == l && R == r) { n     st[v] = (long long)st[v] * mul % MOD; n     if (L != R) sp[v] = (long long)sp[v] * mul % MOD; n     return; n   } n   int mid = (L + R) >> 1; n   if (sp[v] != 1) { n     stPush(v); n   } n   if (l <= mid) stMul(2 * v, L, mid, l, min(r, mid)); n   if (r > mid) stMul(2 * v + 1, mid + 1, R, max(l, mid + 1), r); n   st[v] = st[2 * v] + st[2 * v + 1]; n   if (st[v] >= MOD) st[v] -= MOD; n } n int pos, val; n void stAssign(int v, int L, int R) { n   if (L == R) { n     st[v] = val; n     return; n   } n   int mid = (L + R) >> 1; n   if (sp[v] != 1) { n     stPush(v); n   } n   if (pos <= mid) n     stAssign(2 * v, L, mid); n   else n     stAssign(2 * v + 1, mid + 1, R); n   st[v] = st[2 * v] + st[2 * v + 1]; n   if (st[v] >= MOD) st[v] -= MOD; n } n int stGet(int v, int L, int R, int l, int r) { n   if (l > r) return 0; n   if (L == l && R == r) { n     return st[v]; n   } n   int mid = (L + R) >> 1; n   if (sp[v] != 1) { n     stPush(v); n   } n   int res = stGet(2 * v, L, mid, l, min(r, mid)) + n             stGet(2 * v + 1, mid + 1, R, max(l, mid + 1), r); n   if (res >= MOD) res -= MOD; n   return res; n } n void st2Mul(int v, int L, int R, int l, int r) { n   if (L == l && R == r) { n     st2[v] = (long long)st2[v] * mul % MOD; n     return; n   } n   int mid = (L + R) >> 1; n   if (l <= mid) st2Mul(2 * v, L, mid, l, min(r, mid)); n   if (r > mid) st2Mul(2 * v + 1, mid + 1, R, max(l, mid + 1), r); n } n int st2Get(int pos) { n   int res = 1; n   for (pos += off; pos >= 1; pos >>= 1) { n     res = (long long)res * st2[pos] % MOD; n   } n   return res; n } n int binPow(long long x, int p) { n   long long res = 1; n   while (p) { n     if (p & 1) res = res * x % MOD; n     p >>= 1; n     if (p) x = x * x % MOD; n   } n   return (int)res; n } n int main() { n   scanf( %d%d , st + off, &q); n   for (int i = 0; i < (int)(q); ++i) { n     scanf( %d%d , qt + i, qv + i), --qv[i]; n     if (qt[i] == 1) { n       scanf( %d , qval + i); n       g[qv[i]].push_back(n); n       par[n++] = qv[i]; n     } n   } n   dfs(0); n   stBuild(); n   n = 1; n   for (int i = 0; i < (int)(q); ++i) { n     if (qt[i] == 1) { n       val = (long long)st2Get(tin[qv[i]]) * qval[i] % MOD; n       pos = tin[n++]; n       stAssign(1, 0, off - 1); n       mul = (long long)(ch[qv[i]] + 2) * binPow(ch[qv[i]] + 1, MOD - 2) % MOD; n       ++ch[qv[i]]; n       stMul(1, 0, off - 1, tin[qv[i]], tout[qv[i]]); n       st2Mul(1, 0, off - 1, tin[qv[i]], tout[qv[i]]); n     } else { n       int res = stGet(1, 0, off - 1, tin[qv[i]], tout[qv[i]]); n       if (qv[i] != 0) { n         res = (long long)res * binPow(st2Get(tin[par[qv[i]]]), MOD - 2) % MOD; n       } n       printf( %d n , res); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _print(long long int x) { cerr << x <<    ; } n void _print(float x) { cerr << x <<    ; } n void _print(double x) { cerr << x <<    ; } n void _print(char x) { cerr << x <<    ; } n void _print(string x) { cerr << x <<    ; } n void _print(bool x) { cerr << x <<    ; } n template <class T, class V> n void _print(pair<T, V> x) { n   cerr <<  { ; n   _print(x.first); n   cerr <<  , ; n   _print(x.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> x) { n   cerr <<  [  ; n   for (T i : x) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(set<T> x) { n   cerr <<  [  ; n   for (T i : x) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> x) { n   cerr <<  [  ; n   for (T i : x) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> x) { n   cerr <<  [  ; n   for (auto i : x) { n     _print(i); n     cerr <<    ; n   } n   cerr <<   ] ; n } n void solve() { n   string s; n   cin >> s; n   long long int n = s.size(); n   if (n % 2 == 1) { n     cout <<  NO  << endl; n     return; n   } n   for (long long int i = 0; i <= (n / 2) - 1; i++) { n     if (s[i] != s[i + (n / 2)]) { n       cout <<  NO  << endl; n       return; n     } n   } n   cout <<  YES  << endl; n } n signed main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   clock_t start, end; n   start = clock(); n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   end = clock(); n   double time_taken = double(end - start) / double(CLOCKS_PER_SEC); n   cerr <<  Time taken by program is :   << fixed << time_taken n        << setprecision(5); n   cerr <<   sec   << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n char s[maxn]; n int vis[maxn]; n pair<char, int> sta[maxn]; n int main() { n   scanf( %s , s); n   int o = -1; n   for (int i = 0; s[i]; i++) { n     if (s[i] ==  0  && o >= 0 && sta[o].first ==  1 ) { n       vis[sta[o].second] = 1; n       o--; n     } else n       sta[++o] = make_pair(s[i], i); n   } n   for (int i = 0; s[i]; i++) printf( %d , vis[i]); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> prime; n unordered_set<long long int> stprime; n void sieve(long long int n) { n   bool p[n + 1]; n   memset(p, true, sizeof(p)); n   for (long long int i = 2; i * i <= n; i++) { n     if (p[i]) { n       for (int j = i * i; j <= n; j += i) { n         p[j] = false; n       } n     } n   } n   for (long long int i = 2; i <= n; i++) { n     if (p[i]) { n       prime.push_back(i); n       stprime.insert(i); n     } n   } n } n void solve() { n   long long int m; n   cin >> m; n   long long int arr[8]; n   memset(arr, 0, sizeof(arr)); n   for (int i = 0; i < m; i++) { n     long long int a; n     cin >> a; n     arr[a]++; n   } n   m /= 3; n   if (arr[1] != m || arr[5] | arr[7] || arr[2] < arr[4] || arr[6] < arr[3] || n       arr[2] + arr[3] != arr[4] + arr[6]) { n     cout <<  -1  << endl; n   } else { n     for (int i = 1; i <= arr[4]; i++) cout <<  1 2 4  << endl; n     for (int i = 1; i <= arr[2] - arr[4]; i++) cout <<  1 2 6  << endl; n     for (int i = 1; i <= arr[3]; i++) cout <<  1 3 6  << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int frd[2110][2110], rev[2110][2110]; n vector<int> v; n int main() { n   int a, b, c, d, e, i, j, p, q, mx, mx1, len; n   string x, y; n   cin >> x >> y; n   a = x.length(); n   b = y.length(); n   x =  %  + x; n   y =  *  + y; n   for (i = b; i >= 1; i--) { n     for (j = a; j >= 1; j--) { n       if (y[i] == x[j]) { n         frd[i][j] = frd[i + 1][j + 1] + 1; n       } n     } n     for (j = 1; j <= a; j++) { n       if (y[i] == x[j]) { n         rev[i][j] = rev[i + 1][j - 1] + 1; n       } n     } n   } n   for (i = 1; i <= b;) { n     p = -1; n     q = -1; n     mx = -1; n     mx1 = -1; n     for (j = 1; j <= a; j++) { n       if (frd[i][j] > mx) { n         mx = frd[i][j]; n         p = j; n       } n     } n     for (j = 1; j <= a; j++) { n       if (rev[i][j] > mx1) { n         mx1 = rev[i][j]; n         q = j; n       } n     } n     len = max(mx, mx1); n     if (len == 0) { n       cout <<  -1  << endl; n       return 0; n     } n     if (len == mx) { n       v.push_back(p); n       v.push_back(p + len - 1); n     } else { n       v.push_back(q); n       v.push_back(q - len + 1); n     } n     i = i + len; n   } n   cout << v.size() / 2 << endl; n   e = v.size(); n   for (i = 0; i < e; i = i + 2) { n     cout << v[i] <<     << v[i + 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[1000000]; n int main() { n   int n, x, counter = 0; n   cin >> n >> x; n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   sort(arr, arr + n); n   while (arr[((n + 1) / 2 - 1)] != x) { n     arr[n++] = x; n     sort(arr, arr + n); n     counter++; n   } n   cout << counter; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 305; n vector<int> adj[MX]; n vector<int> l, ans; n int dfs(int u, int v, vector<int>& p, int par = 0) { n   int sum = 0; n   for (int w : adj[u]) { n     if (w != par) { n       p.push_back(w); n       sum = max(sum, dfs(w, v, p, u)); n       p.pop_back(); n     } n   } n   if (u == v) { n     for (int i : p) ans.push_back(i); n     return p.size(); n   } n   return sum; n } n int main() { n   int n, k = 0; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 2; i <= n; i++) n     if (adj[i].size() == 1) k++; n   for (int i = 0; i < k; i++) { n     int u; n     cin >> u; n     l.push_back(u); n   } n   l.push_back(1); n   vector<int> vec[k + 1]; n   int sum = 0; n   int u = 1; n   for (int i = 0; i <= k; i++) { n     sum += dfs(u, l[i], vec[i], u); n     u = l[i]; n   } n   if (sum != 2 * n - 2) n     cout <<  -1  <<   n ; n   else { n     cout <<  1  <<    ; n     for (int i : ans) cout << i <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v); n template <typename A, typename B> n ostream &operator<<(ostream &cout, pair<A, B> const &p) { n   return cout <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v) { n   cout <<  [ ; n   for (int i = 0; i < v.size(); i++) { n     if (i) cout <<  ,  ; n     cout << v[i]; n   } n   return cout <<  ] ; n } n template <typename A, typename B> n istream &operator>>(istream &cin, pair<A, B> &p) { n   cin >> p.first; n   return cin >> p.second; n } n mt19937 rng(steady_clock::now().time_since_epoch().count()); n void usaco(string filename) { n   freopen((filename +  .in ).c_str(),  r , stdin); n   freopen((filename +  .out ).c_str(),  w , stdout); n } n const long double pi = 3.14159265358979323846; n const long long mod = 998244353; n long long n, m, k, q, Q, T, l, r, x, y, z; n long long a[1000005]; n long long b[1000005]; n long long c[1000005]; n string second, t; n long long ans = -mod * mod; n vector<pair<long long, long long> > edges[300005]; n long long dis[300005]; n long long bfirst[300005]; n long long blast[300005]; n bool has[300005]; n vector<long long> res; n void dfs1(int v, int p, long long d) { n   dis[v] = d; n   for (pair<long long, long long> x : edges[v]) n     if (x.first != p) dfs1(x.first, v, d + x.second); n } n long long why, why2; n long long why3 = -mod * mod; n bool dfs2(int v, int p, long long d, long long d2) { n   if (v == n - 1) { n     why = d2; n     why2 = p; n     return has[v] = 1; n   } n   bfirst[v] = d; n   blast[v] = dis[v]; n   int targ = -1; n   ans = max(ans, d + why3); n   for (pair<long long, long long> x : edges[v]) n     if (x.first != p) { n       has[v] |= dfs2(x.first, v, d + x.second, d); n       bfirst[v] = max(bfirst[v], bfirst[x.first]); n       blast[v] = max(blast[v], blast[x.first]); n       if (has[x.first]) { n         targ = x.first; n         res.push_back(x.second); n       } n     } n   if (has[v]) { n     for (pair<long long, long long> x : edges[v]) n       if (x.first != p && x.first != targ) { n         long long va = bfirst[x.first] + blast[targ]; n         ans = max(ans, va); n         ans = max(ans, blast[x.first] + d2); n       } n   } n   return has[v]; n } n int cc = 0; n void dfs3(int v, int p, long long d) { n   if (v != n - 1) ans = max(ans, why + d); n   ++cc; n   if (v != n - 1) why3 = max(why3, d); n   for (pair<long long, long long> x : edges[v]) n     if (x.first != p) dfs3(x.first, v, d + x.second); n } n bool first = 0; n void dfs4(int v, int p) { n   for (pair<long long, long long> x : edges[v]) n     if (x.first != p) { n       if (p != -1 && !has[v]) first = 1; n       dfs4(x.first, v); n     } n } n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n   }; n   cin >> n >> m; n   for (long long i = 0; i < (n - 1); i++) { n     cin >> x >> y >> z; n     --x; n     --y; n     edges[x].push_back(make_pair(y, z)); n     edges[y].push_back(make_pair(x, z)); n   } n   dfs1(n - 1, -1, 0); n   memset(bfirst, 0, sizeof(bfirst)); n   memset(blast, 0, sizeof(blast)); n   dfs2(0, -1, 0, -mod * mod); n   for (long long i = 0; i < (res.size() - 1); i++) n     ans = max(ans, dis[0] - res[i] - res[i + 1]); n   dfs3(n - 1, why2, 0); n   dfs2(0, -1, 0, -mod * mod); n   if (cc > 2) first = 1; n   dfs4(0, -1); n   for (long long i = 0; i < (m); i++) { n     cin >> x; n     cout << (first ? dis[0] : min(ans + x, dis[0])) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n long long ln, lk, lm; n const int MN = 10000; n int mod; n char s[505][505]; n pair<int, int> id[2][MN], rid[505][505]; n int sz[2]; n vector<int> mp[MN], mpc[MN]; n long long A[505][505]; n int col[MN], C, cnt; n long long ans; n void pp(int N) { n   for (int(i) = 1; (i) <= (int)(N); (i)++) { n     for (int(j) = 1; (j) <= (int)(N); (j)++) printf( %lld  , A[i][j]); n     puts(  ); n   } n   puts(  ); n } n long long fastPow(long long x, long long y) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gauss(int N) { n   long long tmp = 1; n   for (int(i) = 1; (i) <= (int)(N); (i)++) n     for (int(j) = 1; (j) <= (int)(N); (j)++) n       A[i][j] = (A[i][j] % mod + mod) % mod; n   for (int i = 2; i <= N; i++) { n     int j = i; n     while (j <= N && A[j][i] == 0) j++; n     if (j > N) { n       return 0; n     } n     if (i != j) { n       for (int(k) = 1; (k) <= (int)(N); (k)++) swap(A[i][k], A[j][k]); n       tmp = mod - tmp; n     } n     for (int j = i + 1; j <= N; j++) { n       long long base = A[j][i] * fastPow(A[i][i], mod - 2) % mod; n       for (int k = i; k <= N; k++) { n         A[j][k] = A[j][k] - A[i][k] * base % mod; n         A[j][k] = (A[j][k] % mod + mod) % mod; n       } n     } n     tmp *= A[i][i]; n     tmp %= mod; n   } n   return tmp; n } n void dfs(int x, int pa) { n   col[x] = C; n   for (int c : mp[x]) n     if (c != pa) { n       if (col[c] == 0) n         dfs(c, x); n       else { n         puts( 0 ); n         exit(0); n       } n     } n } n bool vis[MN]; n void dfs1(int x) { n   vis[x] = 1; n   cnt++; n   for (int c : mpc[x]) n     if (vis[c] == 0) dfs1(c); n } n void cal(int t) { n   for (int(i) = 0; (i) < (int)(MN); (i)++) mp[i].clear(); n   for (int(i) = 0; (i) < (int)(MN); (i)++) mpc[i].clear(); n   for (int(i) = 0; (i) < (int)(n); (i)++) n     for (int(j) = 0; (j) < (int)(m); (j)++) { n       int u, v; n       if (s[i][j] ==  /  && rid[i][j + 1].first == t) { n         u = rid[i][j + 1].second; n         v = rid[i + 1][j].second; n         mp[u].push_back(v); n         mp[v].push_back(u); n       } else if (s[i][j] ==      && rid[i][j].first == t) { n         u = rid[i][j].second; n         v = rid[i + 1][j + 1].second; n         mp[u].push_back(v); n         mp[v].push_back(u); n       } n     } n   C = 0; n   memset(col, 0, sizeof col); n   for (int(i) = 0; (i) < (int)(sz[t]); (i)++) n     if (col[i] == 0) { n       C++; n       dfs(i, -1); n     } n   for (int(i) = 0; (i) < (int)(505); (i)++) n     for (int(j) = 0; (j) < (int)(505); (j)++) A[i][j] = 0; n   for (int(i) = 0; (i) < (int)(n); (i)++) n     for (int(j) = 0; (j) < (int)(m); (j)++) n       if (s[i][j] ==  * ) { n         int u, v; n         if (rid[i][j + 1].first == t) { n           u = rid[i][j + 1].second; n           v = rid[i + 1][j].second; n         } else { n           assert(rid[i][j].first == t); n           u = rid[i][j].second; n           v = rid[i + 1][j + 1].second; n         } n         u = col[u]; n         v = col[v]; n         if (u == v) continue; n         A[u][u]++; n         A[v][v]++; n         A[u][v]--; n         A[v][u]--; n         mpc[u].push_back(v); n         mpc[v].push_back(u); n       } n   cnt = 0; n   for (int(i) = 0; (i) < (int)(MN); (i)++) vis[i] = 0; n   dfs1(1); n   if (cnt == C) { n     ans += gauss(C); n   } n   ans += mod; n   ans %= mod; n } n void fmain() { n   scanf( %d%d%d , &n, &m, &mod); n   for (int(i) = 0; (i) < (int)(n); (i)++) scanf( %s , s[i]); n   for (int(i) = 0; (i) < (int)(n + 1); (i)++) { n     int t = i % 2; n     for (int(j) = 0; (j) < (int)(m + 1); (j)++) { n       id[t][sz[t]++] = make_pair(i, j); n       rid[i][j] = {t, sz[t] - 1}; n       t ^= 1; n     } n   } n   for (int(i) = 0; (i) < (int)(2); (i)++) cal(i); n   printf( %lld n , ans); n } n int main() { n   fmain(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, x, y; n int main() { n   cin >> a >> b; n   if (a > b) { n     x = b; n     y = (a - b) / 2; n   } else { n     x = a; n     y = (b - a) / 2; n   } n   cout << x <<     << y; n } n


#include <bits/stdc++.h> n using namespace std; n int t[110], w[110]; n int main() { n   int n, cnt = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &t[i], &w[i]); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i != j) { n         if (t[i] == w[j]) { n           cnt++; n           break; n         } n       } n     } n   } n   printf( %d , n - cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void rd(T& ret) { n   ret = 0; n   bool ok = 0, u = 0; n   for (;;) { n     int c = getchar(); n     if (c >=  0  && c <=  9 ) n       ret = (ret << 3) + (ret << 1) + c -  0 , ok = 1; n     else if (c ==  - ) n       u = 1; n     else if (ok) { n       if (u) ret *= -1; n       return; n     } n   } n } n long long powmod(long long p, long long n, long long mod) { n   long long ret = 1; n   for (; n; n >>= 1) { n     if (n & 1) ret = ret * p % mod; n     p = p * p % mod; n   } n   return ret; n } n template <class T> n bool chmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool chmax(T& a, const T& b) { n   return b > a ? a = b, 1 : 0; n } n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n const long long mod = (long long)1e9 + 7; n const int N = 1 << 21; n pair<pair<int, int>, int> qn[N]; n int stk[N], top, an[N], ans[N]; n bool has[N]; n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = (0); i < (n); i++) scanf( %d , an + i); n   for (int i = (0); i < (q); i++) { n     int l, first; n     scanf( %d%d , &l, &first); n     --l; n     qn[i].first = pair<int, int>(l, first); n     qn[i].second = i; n   } n   sort(qn, qn + q); n   has[0] = 1; n   stk[top++] = 0; n   long long t = 1, it = 0; n   for (int i = (0); i < (n); i++) { n     if (has[an[i]]) n       t = (t << 1) % mod; n     else { n       int tn = top, second; n       for (int j = (0); j < (tn); j++) { n         second = an[i] ^ stk[j]; n         stk[top++] = second; n         has[second] = 1; n       } n       has[an[i]] = 1; n       stk[top++] = an[i]; n     } n     while (it < q && qn[it].first.first == i) { n       int val = qn[it].first.second; n       ans[qn[it].second] = has[val] ? t : 0; n       ++it; n     } n   } n   for (int i = (0); i < (q); i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const long long MAX = 100005; n const long double PI = 3.14159265359; n const long double G = 9.807; n const long long INF = 1e18; n const long double EPS = 1e-6; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n bool isprime(long long a) { n   if (a == 2) return 1; n   if (!(a & 1)) return 0; n   for (long long i = 3; i * i <= a; i += 2) n     if (a % i == 0) return 0; n   return 1; n } n long long mpow(long long base, long long exponent, long long modulus) { n   if (modulus == 1) return 0; n   long long result = 1; n   base = base % modulus; n   while (exponent) { n     if (exponent % 2 == 1) result = (result * base) % modulus; n     exponent = exponent >> 1; n     base = (base * base) % modulus; n   } n   return result; n } n long long minv(long long a, long long mod) { n   long long _gcd = gcd(a, mod); n   assert(_gcd == 1); n   return mpow(a, mod - 2, mod); n } n long long n; n long long ans; n long long a[1005]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 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     if (a[i - 1] > a[i] and a[i] < a[i + 1]) n       ans++; n     else if (a[i - 1] < a[i] and a[i] > a[i + 1]) n       ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int maxn = 3e5 + 10, lg = 20; n vector<pair<long long, long long> > v[maxn]; n long long n, w[maxn], rmq[lg + 2][maxn], dp[maxn], up[maxn], W[maxn], n     par[lg + 2][maxn], h[maxn], q; n void dfs_down(int i, int j) { n   h[i] = h[j] + 1; n   dp[i] = w[i]; n   for (int y = 0; y < v[i].size(); y++) { n     int x = v[i][y].first; n     if (x - j) { n       W[x] = v[i][y].second; n       dfs_down(x, i); n       dp[i] += max(0LL, dp[x] - 2 * W[x]); n     } n   } n } n long long get_dp(int pes) { n   long long s = 0; n   if (dp[pes] - 2 * W[pes] >= 0 && pes != 1) s = -(dp[pes] - 2 * W[pes]); n   return s; n } n void dfs_up(int i, int j) { n   if (i != 1) { n     up[i] = max(0LL, up[j] + dp[j] + get_dp(i) - 2 * W[i]); n   } n   for (int y = 0; y < v[i].size(); y++) { n     int x = v[i][y].first; n     if (x - j) { n       dfs_up(x, i); n     } n   } n } n void get_par(int i, int j) { n   par[0][i] = j; n   rmq[0][i] = -W[i] + dp[j] + get_dp(i); n   for (int y = 1; y <= lg; y++) { n     int p = par[y - 1][i]; n     par[y][i] = par[y - 1][p]; n     rmq[y][i] = rmq[y - 1][i] + rmq[y - 1][p]; n   } n   for (int y = 0; y < v[i].size(); y++) { n     int x = v[i][y].first; n     if (x - j) { n       get_par(x, i); n     } n   } n } n long long query(int i, int j) { n   if (i == j) return up[i] + dp[i]; n   if (h[i] > h[j]) swap(i, j); n   int o = h[j] - h[i]; n   int I = i; n   int J = j; n   long long Ans = 0; n   for (int y = 0; y <= lg; y++) { n     if (o & (1 << y)) { n       Ans += rmq[y][j]; n       j = par[y][j]; n     } n   } n   if (j == i) return Ans + dp[J] + up[i]; n   Ans += dp[I] + dp[J]; n   for (int y = lg; y > -1; y--) { n     if (par[y][i] != par[y][j]) n       Ans += rmq[y][j] + rmq[y][i], i = par[y][i], j = par[y][j]; n   } n   int p = par[0][j]; n   Ans += up[p] + rmq[0][i] + rmq[0][j] + get_dp(i) + get_dp(j) - n          (dp[p] + get_dp(i) + get_dp(j)); n   return Ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   for (int y = 1; y <= n; y++) cin >> w[y]; n   for (int y = 1; y < n; y++) { n     int i, j, p; n     cin >> i >> j >> p; n     v[i].push_back(pair<long long, long long>(j, p)); n     v[j].push_back(pair<long long, long long>(i, p)); n   } n   dfs_down(1, 0); n   dfs_up(1, 0); n   get_par(1, 0); n   while (q--) { n     int i, j; n     cin >> i >> j; n     cout << query(i, j) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:100000000 ) n int mod = 1000000007; n int mas[1010]; n int pow2(int b) { n   if (b == -1) return 0; n   int res = 1; n   for (int i = 1; i <= b; i++) { n     res <<= 1; n     res %= mod; n   } n   return res; n } n int GCD(int a, int b) { n   if (b == 0) n     return a; n   else n     return GCD(b, a % b); n } n vector<int> v; n vector<int> C; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) scanf( %d , &mas[i]); n   sort(mas, mas + m); n   if (mas[0] != 1) v.push_back(mas[0] - 1); n   if ((n > 1 || m != 1) && mas[m - 1] != n) v.push_back(n - mas[m - 1]); n   for (int i = 1; i < m; i++) { n     int a = pow2(mas[i] - mas[i - 1] - 2); n     if (mas[i] - 1 != mas[i - 1]) v.push_back(mas[i] - mas[i - 1] - 1); n     a = max(a, 1); n     C.push_back(a); n   } n   sort(v.begin(), v.end()); n   int S = 0; n   vector<int> F; n   for (int i = 0, maxi = (int)(v).size(); i < maxi; i++) S += v[i]; n   for (int i = 2; i <= S; i++) F.push_back(i); n   vector<int> VVV; n   for (int i = 0, maxi = (int)(v).size(); i < maxi; i++) n     for (int j = 2; j <= v[i]; j++) VVV.push_back(j); n   for (int i = 0, maxi = (int)(VVV).size(); i < maxi; i++) { n     for (int j = 0, maxj = (int)(F).size(); j < maxj; j++) { n       if (VVV[i] == 1) break; n       int G = GCD(F[j], VVV[i]); n       VVV[i] /= G; n       F[j] /= G; n     } n   } n   long long ans = 1; n   for (int i = 0, maxi = (int)(F).size(); i < maxi; i++) n     ans *= (long long)F[i], ans %= (long long)mod; n   for (int i = 0, maxi = (int)(C).size(); i < maxi; i++) n     ans *= (long long)C[i], ans %= (long long)mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   int a[1000]{}; n   for (int i = 0; i < t; i++) { n     int n, s, k; n     cin >> n >> s >> k; n     set<int> b; n     for (int j = 0; j < k; j++) { n       cin >> a[j]; n       b.insert(a[j]); n     } n     int ans = n; n     for (int i = max(1, s - k); i <= min(n, s + k + 1); i++) { n       if (b.count(i) == 0) { n         ans = min(ans, abs(s - i)); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const double PI = acos(-1.0); n const long long MOD = 1000000007LL; n template <class T> n inline bool chmax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n int dp[500][500]; n void solve() { n   string S, T; n   cin >> S >> T; n   int N = S.size(); n   int M = T.size(); n   for (int i = 0; i < (int)(M); ++i) { n     for (int j = 0; j < (int)(N + 1); ++j) n       for (int k = 0; k < (int)(M + 1); ++k) dp[j][k] = -1; n     for (int j = 0; j < (int)(N + 1); ++j) dp[j][0] = 0; n     for (int j = 0; j < (int)(N); ++j) { n       for (int k = 0; k < (int)(i + 1); ++k) { n         chmax(dp[j + 1][k], dp[j][k]); n         if (k < i && S[j] == T[k]) { n           chmax(dp[j + 1][k + 1], dp[j][k]); n         } n         if (dp[j][k] >= 0 && dp[j][k] < M - i && S[j] == T[i + dp[j][k]]) { n           chmax(dp[j + 1][k], dp[j][k] + 1); n         } n       } n     } n     if (dp[N][i] == M - i) { n       cout <<  YES  <<   n ; n       return; n     } n   } n   cout <<  NO  <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int Q; n   cin >> Q; n   while (Q--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t, n; n int hmz, hmbz, bsz = -100000, sum; n int main() { n   cin >> t; n   for (; t; t--) { n     cin >> n; n     hmz = 0; n     hmbz = 0; n     bsz = -100000; n     sum = 0; n     for (int i = 1; i <= n; i++) { n       int x; n       cin >> x; n       if (x == 0) n         hmz++; n       else if (x < 0) n         bsz = max(bsz, x); n       else n         hmbz++; n       sum += x; n     } n     int sol = 0; n     if (hmz) sum += hmz, sol += hmz; n     if (sum == 0) { n       if (hmbz) n         sol++; n       else n         sol += (1 - hmbz); n     } n     cout << sol <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e3 + 5; n int times[maxn][maxn], Time, cost[maxn][maxn], n, m; n string k[maxn]; n int dfs(int x, int y) { n   if (!min(x, y) or x > n or y > m) return 0; n   times[x][y] = ++Time; n   int now = k[x][y - 1]; n   int nextx, nexty; n   if (now ==  R ) n     nextx = x, nexty = y + 1; n   else if (now ==  L ) n     nextx = x, nexty = y - 1; n   else if (now ==  U ) n     nextx = x - 1, nexty = y; n   else n     nextx = x + 1, nexty = y; n   if (times[nextx][nexty]) { n     cost[x][y] = Time - times[nextx][nexty] + 1; n     times[x][y] = 0; n     return cost[x][y] - 1; n   } n   if (cost[nextx][nexty]) { n     cost[x][y] = cost[nextx][nexty] + 1; n     times[x][y] = 0; n     return 0; n   } n   int gg = dfs(nextx, nexty); n   if (!gg) { n     cost[x][y] = cost[nextx][nexty] + 1; n     times[x][y] = 0; n     return 0; n   } else { n     cost[x][y] = cost[nextx][nexty]; n     times[x][y] = 0; n     return gg - 1; n   } n } n void solve() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> k[i]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       Time = 0; n       if (!cost[i][j]) dfs(i, j); n     } n   } n   int ans = 0, ansx, ansy; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (cost[i][j] > ans) ans = cost[i][j], ansx = i, ansy = j; n   cout << ansx <<     << ansy <<     << ans <<   n ; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cost[i][j] = times[i][j] = 0; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   for (int i = 1; i <= t; i++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> graf[1000001]; n vector<int> chains[1000001]; n vector<int> chainsbit[1000001]; n int chainwhere[1000001], chainposition[1000001]; n void update(int chainidx, int x, int val) { n   while (x <= chainsbit[chainidx].size() - 1) { n     chainsbit[chainidx][x] += val; n     x += (x & -x); n   } n } n int read(int chainidx, int x) { n   if (chainsbit[chainidx].size() < x) n     x = min(x, (int)(chainsbit[chainidx].size()) - 1); n   int ret = 0; n   while (x > 0) { n     ret += chainsbit[chainidx][x]; n     x -= (x & -x); n   } n   return ret; n } n void DFS(int u, int chainidx, int prev) { n   chains[chainidx].push_back(u); n   chainsbit[u].push_back(0); n   chainwhere[u] = chainidx; n   chainposition[u] = chains[chainidx].size() - 1; n   for (int i = 0; i < graf[u].size(); i++) n     if (graf[u][i] != prev) DFS(graf[u][i], chainidx, u); n } n int rootvalue = 0; n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     graf[u].push_back(v); n     graf[v].push_back(u); n   } n   int chainnum = graf[1].size(); n   for (int i = 0; i < n + 15; i++) chainsbit[0].push_back(0); n   for (int i = 0; i < graf[1].size(); i++) { n     chains[i + 1].push_back(0); n     chainsbit[i + 1].push_back(0); n     DFS(graf[1][i], i + 1, 1); n     int ln = chains[i + 1].size() * 100; n     for (int j = 0; j < ln; j++) { n       chains[i + 1].push_back(0); n       chainsbit[i + 1].push_back(0); n     } n   } n   while (q--) { n     int t; n     scanf( %d , &t); n     if (t == 0) { n       int v, x, d; n       scanf( %d%d%d , &v, &x, &d); n       if (v == 1) { n         rootvalue += x; n         update(0, 1, +x); n         update(0, d + 1, -x); n       } else { n         int ch = chainwhere[v]; n         int depth = chainposition[v]; n         if (depth - d >= 1) { n           update(ch, depth - d, +x); n           update(ch, depth + d + 1, -x); n         } else { n           update(ch, 1, +x); n           update(ch, depth + d + 1, -x); n           int rest = d - depth; n           rootvalue += x; n           if (rest > 0) { n             update(0, 1, +x); n             update(0, rest + 1, -x); n             update(ch, 1, -x); n             update(ch, rest + 1, +x); n           } n         } n       } n     } else { n       int v; n       scanf( %d , &v); n       if (v == 1) n         printf( %d n , rootvalue); n       else { n         int ret = n             read(0, chainposition[v]) + read(chainwhere[v], chainposition[v]); n         printf( %d n , ret); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     vector<int> v(n); n     for (int i = 0; i < n; i++) cin >> v[i]; n     vector<int> f(n, 0); n     unordered_map<int, vector<int>> mp; n     for (int i = 0; i < n; i++) mp[v[i]].push_back(i); n     auto it = mp.begin(); n     int fg = 0; n     bool flg = 0; n     int ts = 0; n     for (it; it != mp.end(); it++) { n       auto vv = it->second; n       if (vv.size() < k) continue; n       ts += vv.size(); n       if (vv.size() >= k) { n         int tt = 0; n         while (tt < k) { n           f[vv[tt]] = tt + 1; n           tt++; n         } n       } n     } n     it = mp.begin(); n     for (it; it != mp.end(); it++) { n       auto vv = it->second; n       if (vv.size() >= k) n         continue; n       else { n         int ii = 0; n         while (ii < vv.size()) { n           if (fg == 0 && (n - ts < k)) { n             flg = 1; n             break; n           } n           ts++; n           f[vv[ii]] = fg + 1; n           fg = (fg + 1) % k; n           ii++; n         } n         if (flg) break; n       } n     } n     for (int i = 0; i < n; i++) cout << f[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   return (b != 0 ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n template <class T> n inline bool read(T &x) { n   int c = getchar(); n   int sgn = 1; n   while (~c && c <  0  || c >  9 ) { n     if (c ==  - ) sgn = -1; n     c = getchar(); n   } n   for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   x *= sgn; n   return ~c; n } n const int INF = 2000000009; n const int MX = 100005; n const long double EPS = 1e-9; n const int MOD = 1000000007; n struct PT { n   long double x, y; n   PT() {} n   PT(long double x, long double y) : x(x), y(y) {} n   PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); } n   PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); } n   PT operator*(long double c) const { return PT(x * c, y * c); } n   PT operator/(long double c) const { return PT(x / c, y / c); } n }; n long double cross(PT a, PT b) { return a.x * b.y - a.y * b.x; } n long double dot(PT a, PT b) { return a.x * b.x + a.y * b.y; } n long double area2(PT a, PT b, PT c) { n   return cross(a, b) + cross(b, c) + cross(c, a); n } n long double dist2(PT a, PT b) { return dot(b - a, b - a); } n inline int sign(long double a) { n   int ret = (a < -EPS) ? -1 : ((a > EPS) ? 1 : 0); n   return ret; n } n struct Line { n   PT v; n   long double a, b, c; n   Line() {} n   Line(PT p, PT q) : v(q - p), a(-v.y), b(v.x), c(cross(v, p)) {} n   long double side(PT p) { return cross(v, p) - c; } n }; n long double commonLength(vector<PT> &p, PT a, PT b) { n   Line l = Line(a, b); n   long double ans = 0.0; n   int n = p.size(); n   p.push_back(p[0]); n   vector<pair<long double, int> > vec; n   for (int i = 0; i < n; i++) { n     int s1 = sign(cross(b - a, p[i] - a)); n     int s2 = sign(cross(b - a, p[i + 1] - a)); n     if (s1 == s2) continue; n     Line t = Line(p[i], p[i + 1]); n     PT inter = (t.v * l.c - l.v * t.c) / cross(l.v, t.v); n     long double tmp = dot(inter, l.v); n     int f; n     if (s1 > s2) n       f = s1 && s2 ? 2 : 1; n     else n       f = s1 && s2 ? -2 : -1; n     vec.push_back(make_pair(tmp, f)); n   } n   sort(vec.begin(), vec.end()); n   for (int i = 0, j = 0; i + 1 < (int)vec.size(); i++) { n     j += vec[i].second; n     if (j) ans += vec[i + 1].first - vec[i].first; n   } n   ans = ans / sqrt(dot(l.v, l.v)); n   p.pop_back(); n   return ans; n } n int main() { n   int n = ({ n     int a; n     read(a); n     a; n   }), n       m = ({ n         int a; n         read(a); n         a; n       }); n   vector<PT> pol; n   for (int i = 0; i < n; i++) { n     long double x = ({ n       long double a; n       scanf( %Lf , &a); n       a; n     }), n                 y = ({ n                   long double a; n                   scanf( %Lf , &a); n                   a; n                 }); n     pol.push_back(PT(x, y)); n   } n   while (m--) { n     PT a, b; n     a.x = ({ n       long double a; n       scanf( %Lf , &a); n       a; n     }), n     a.y = ({ n       long double a; n       scanf( %Lf , &a); n       a; n     }), n     b.x = ({ n       long double a; n       scanf( %Lf , &a); n       a; n     }), n     b.y = ({ n       long double a; n       scanf( %Lf , &a); n       a; n     }); n     printf( %.8Lf n , commonLength(pol, a, b)); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 1e2 + 5, M = 1e5 + 5; n int lim = (1 << 10) - 1; n bool vis[M]; n vector<int> primes; n int idx[M]; n void pre() { n   for (int i = 2; primes.size() < 110; ++i) { n     if (vis[i]) continue; n     primes.emplace_back(i); n     for (int j = i; j < M; j += i) vis[j] = true; n   } n   for (int i = 0; i < 110; ++i) idx[primes[i]] = i; n } n vector<int> tmp; n void fac(int x) { n   tmp.clear(); n   for (int i = 2; i * i <= x; ++i) { n     if (x % i) continue; n     tmp.emplace_back(idx[i]); n     while (x % i == 0) x /= i; n   } n   if (x > 1) tmp.emplace_back(idx[x]); n } n int n, a[N]; n int dp[N][1 << 10][N + 10]; n int solve(int i, int msk, int j) { n   if (i == n) return 0; n   if (msk == lim && j == 110) return (int)1e7; n   int &ret = dp[i][msk][j]; n   if (~ret) return ret; n   ret = (int)1e7; n   if (j < 110 && a[i] > primes[j]) n     cout <<  yala n2fl ya 3mna kda mb2tsh nf3a  << endl; n   if (j < 110) ret = min(ret, solve(i + 1, msk, j + 1) - a[i] + primes[j]); n   for (int f = 0; f < 10; ++f) { n     if ((msk & (1 << f))) continue; n     int nw = primes[f]; n     for (int k = 0; k <= 15 && (int)1e7 / nw >= primes[f]; ++k, nw *= primes[f]) n       ret = min(ret, solve(i + 1, (msk | (1 << f)), j) + abs(a[i] - nw)); n   } n   for (int hna = 1; hna <= 30; ++hna) { n     bool baz = false; n     int nmsk = msk; n     fac(hna); n     for (auto f : tmp) { n       nmsk |= (1 << f); n       baz |= ((msk & (1 << f)) > 0); n     } n     if (baz) continue; n     ret = min(ret, solve(i + 1, nmsk, j) + abs(a[i] - hna)); n   } n   return ret; n } n vector<int> out; n void build(int i, int msk, int j) { n   if (i == n) { n     for (auto x : out) printf( %d%c , x,    n [&x == &out.back()]); n     exit(0); n   } n   if (msk == lim && j == 110) return; n   int m3lsh = solve(i, msk, j); n   if (j < 110 && m3lsh == solve(i + 1, msk, j + 1) - a[i] + primes[j]) { n     out.emplace_back(primes[j]); n     build(i + 1, msk, j + 1); n     return; n   } n   for (int f = 0; f < 10; ++f) { n     if ((msk & (1 << f))) continue; n     int nw = primes[f]; n     for (int k = 0; k <= 15 && (int)1e7 / nw >= primes[f]; n          ++k, nw *= primes[f]) { n       if (m3lsh == solve(i + 1, (msk | (1 << f)), j) + abs(a[i] - nw)) { n         out.emplace_back(nw); n         build(i + 1, (msk | (1 << f)), j); n         return; n       } n     } n   } n   for (int hna = 1; hna <= 30; ++hna) { n     bool baz = false; n     int nmsk = msk; n     fac(hna); n     for (auto f : tmp) { n       nmsk |= (1 << f); n       baz |= (msk & (1 << f)); n     } n     if (baz) continue; n     if (m3lsh == solve(i + 1, nmsk, j) + abs(a[i] - hna)) { n       out.emplace_back(hna); n       build(i + 1, nmsk, j); n       return; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   pre(); n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   memset(dp, -1, sizeof dp); n   solve(0, 0, 10); n   build(0, 0, 10); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long a, b; n   cin >> a >> b; n   if (a == 0 || b == 0) { n     cout << 0 << endl; n     return; n   } n   cout << min((a + b) / 3, min(a, b)) << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tc = 1; n   cin >> tc; n   while (tc--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n bool isvowel(char c) { n   c = tolower(c); n   return (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ); n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long fastpow(long long base, long long p) { n   if (p == 0) return 1; n   long long num = fastpow(base, p / 2); n   num *= num; n   if (p % 2) num *= base; n   return num; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, m, x, q, ans = 0; n   cin >> n >> m >> x; n   string KeyBoard[n + 5], t; n   map<char, bool> check; n   vector<pair<int, int>> S; n   for (int i = 1; i <= n; i++) { n     cin >> KeyBoard[i]; n     for (int j = 0; j < m; j++) { n       if (KeyBoard[i][j] ==  S ) S.push_back(make_pair(i, j + 1)); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < m; j++) { n       if (KeyBoard[i][j] ==  S ) continue; n       check[KeyBoard[i][j]] = true; n       for (int k = 0; k < (int)S.size(); k++) { n         if (sqrt(pow(i - S[k].first, 2) + pow(j - S[k].second + 1, 2)) <= x) { n           check[toupper(KeyBoard[i][j])] = true; n           break; n         } n       } n     } n   } n   cin >> q >> t; n   for (int i = 0; i < (int)t.size(); i++) { n     if (!check[t[i]]) { n       if (islower(t[i]) || !check[tolower(t[i])] || !S.size()) { n         cout << -1; n         return 0; n       } n       ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a, b; n   while (cin >> n >> m >> a >> b) { n     a--; n     b--; n     int oa, pa, ob, pb; n     oa = a / m; n     pa = a % m; n     ob = b / m; n     pb = b % m; n     if (b == n - 1) { n       pb = m - 1; n     } n     if (oa == ob) { n       cout <<  1 ; n       continue; n     } n     if (pa == 0 && pb == m - 1) { n       cout <<  1 ; n       continue; n     } n     if (pa - pb == 1) { n       cout <<  2 ; n       continue; n     } n     int hang = ob - oa; n     if (pa > 0 && pb < m - 1) { n       if (hang == 1) { n         cout <<  2 ; n         continue; n       } else { n         cout <<  3 ; n         continue; n       } n     } n     cout <<  2 ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m, k; n   cin >> n >> m >> k; n   int arr[n][m]; n   int row[n]; n   int col[m]; n   for (int i = 0; i < n; i++) { n     row[i] = i; n     for (int j = 0; j < m; j++) { n       col[j] = j; n       cin >> arr[i][j]; n     } n   } n   while (k--) { n     char c; n     int x, y; n     cin >> c >> x >> y; n     if (c ==  c ) { n       x -= 1; n       y -= 1; n       swap(col[x], col[y]); n     } else if (c ==  r ) { n       x -= 1; n       y -= 1; n       swap(row[x], row[y]); n     } else { n       x = row[x - 1]; n       y = col[y - 1]; n       cout << arr[x][y] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const long long SIZE = 100000; n const int INF = 0x3f3f3f3f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const long double PI = acos(-1); n const long long MAXN = numeric_limits<long long>::max(); n template <class T> n T maxT(T a, T b) { n   return (a > b ? a : b); n } n template <class T> n T minT(T a, T b) { n   return (a < b ? a : b); n } n template <class T> n void setmin(T &a, T b) { n   a = (a > b ? a : b); n } n template <class T> n void setmax(T &a, T b) { n   a = (a < b ? a : b); n } n void fileio() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n   freopen( debug.txt ,  w , stderr); n } n void ofileio() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n void clock_out() { n   cerr <<   nTime Elapsed :   << 1.0 * clock() / CLOCKS_PER_SEC <<   s n ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << H; n   debug_out(T...); n } n void solve() { n   string s; n   cin >> s; n   string temp =   ; n   vector<int> cnt; n   int c = 1; n   temp += s[0]; n   for (int i = 1; i < s.size(); i++) { n     if (s[i] == s[i - 1]) { n       c++; n     } else { n       temp += s[i]; n       cnt.push_back(c); n       c = 1; n     } n   } n   cnt.push_back(c); n   string w = temp; n   reverse((w).begin(), (w).end()); n   if (w != temp) { n     cout << 0; n     return; n   } else { n     int pos = (cnt.size()) / 2; n     for (long long i = 0; i < pos; i++) { n       if ((cnt[i] + cnt[cnt.size() - i - 1]) < 3) { n         cout << 0; n         return; n       } n     } n     if (cnt[pos] >= 2) n       cout << cnt[pos] + 1; n     else n       cout << 0; n     return; n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   srand(time(NULL)); n   1; n   long long t = 1; n   while (t--) { n     solve(); n   } n   1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   char c[2] = { w ,  b }; n   cin >> n; n   if (n % 2 == 1) n     cout <<  -1 ; n   else { n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j < n; j++) { n         for (int k = 0; k < n; k++) { n           if ((j / 2) % 2 == 0) n             if ((k / 2) % 2 == 0) n               cout << c[i % 2]; n             else n               cout << c[!(i % 2)]; n           if ((j / 2) % 2 == 1) n             if ((k / 2) % 2 == 0) n               cout << c[!(i % 2)]; n             else n               cout << c[i % 2]; n         } n         cout << endl; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   for (auto i : v) os << i <<    ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& v) { n   for (auto i : v) os << i <<    ; n   return os; n } n template <typename T> n T read() { n   char c = getchar(); n   T sgn = 1; n   T t = 0; n   while (!isdigit(c)) { n     if (c ==  - ) sgn = -sgn; n     c = getchar(); n   } n   while (isdigit(c)) { n     t *= 10; n     t += c -  0 ; n     c = getchar(); n   } n   return t * sgn; n } n const int N = 1e5; n vector<int> atk, def, had; n bool used[N]; n int main() { n   int n = read<int>(); n   int m = read<int>(); n   for (int i = 0; i < n; ++i) { n     char s[10]; n     int val; n     scanf( %s %d , s, &val); n     getchar(); n     if (s[0] ==  A ) atk.push_back(val); n     if (s[0] ==  D ) def.push_back(val); n   } n   for (int i = 0; i < m; ++i) had.push_back(read<int>()); n   sort(atk.begin(), atk.end()); n   sort(def.begin(), def.end()); n   sort(had.begin(), had.end()); n   int ans = 0; n   for (int i = m - 1; i >= 0; --i) { n     int cur = 0; n     for (int j = 0, t = atk.size(); j < m - i && j < t; ++j) n       if (had[i + j] >= atk[j]) cur += had[i + j] - atk[j]; n     ans = max(ans, cur); n   } n   int p = 0; n   for (auto i : def) { n     while (p < m && had[p] <= i) p++; n     if (p == m) { n       printf( %d n , ans); n       exit(0); n     } n     used[p] = true; n     p++; n   } n   p = m - 1; n   int ans2 = 0; n   for (int i = atk.size() - 1; i >= 0; --i) { n     int j = atk[i]; n     while (p >= 0 && used[p]) p--; n     if (p == -1 || had[p] < j) { n       printf( %d n , ans); n       exit(0); n     } n     ans2 += had[p] - j; n     used[p] = true; n     p--; n   } n   for (int i = 0; i < m; ++i) n     if (!used[i]) ans2 += had[i]; n   printf( %d n , max(ans, ans2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int lazy[600005]; n bool haslazy[600005]; n int tree[600005]; n int arr[200005]; n int lft(int x) { return 2 * x + 1; } n int ryt(int x) { return 2 * x + 2; } n struct lol { n   int x; n   int y; n   int z; n }; n void updt(int x, int l, int r, int value) { n   tree[x] = (value | tree[x]); n   if (haslazy[x]) n     lazy[x] = (lazy[x] | value); n   else n     lazy[x] = value; n   haslazy[x] = true; n } n void laze(int x, int l, int r) { n   if (!haslazy[x]) return; n   int mid = (l + r) / 2; n   updt(ryt(x), mid + 1, r, lazy[x]); n   updt(lft(x), l, mid, lazy[x]); n   lazy[x] = 0; n   haslazy[x] = false; n } n void prop(int x, int l, int r, int a, int b, int value) { n   if (r < a || l > b) return; n   if (l >= a && r <= b) { n     updt(x, l, r, value); n     return; n   } n   laze(x, l, r); n   int mid = (l + r) / 2; n   prop(lft(x), l, mid, a, b, value); n   prop(ryt(x), mid + 1, r, a, b, value); n   int ta = tree[lft(x)]; n   int tb = tree[ryt(x)]; n   tree[x] = (ta & tb); n } n int query(int x, int l, int r, int a, int b) { n   if (l > b || r < a) return ((long long)1 << (long long)31) - 1; n   if (l >= a && r <= b) { n     return tree[x]; n   } n   laze(x, l, r); n   int mid = (l + r) / 2; n   int ta = query(lft(x), l, mid, a, b); n   int tb = query(ryt(x), mid + 1, r, a, b); n   return (ta & tb); n } n void build(int x, int a, int b) { n   if (a == b) { n     tree[x] = 0; n     return; n   } n   int mid = (a + b) / 2; n   build(lft(x), a, mid); n   build(ryt(x), mid + 1, b); n   tree[x] = 0; n } n int main() { n   cin >> n; n   build(0, 0, n - 1); n   int m; n   cin >> m; n   vector<lol> q; n   for (int i = 0; i < m; i++) { n     lol temp; n     cin >> temp.x >> temp.y >> temp.z; n     prop(0, 0, n - 1, temp.x - 1, temp.y - 1, temp.z); n     q.push_back(temp); n   } n   bool flag = false; n   for (int i = 0; i < m; i++) { n     if (query(0, 0, n - 1, q[i].x - 1, q[i].y - 1) != q[i].z) { n       flag = true; n       break; n     } n   } n   if (flag) { n     cout <<  NO ; n   } else { n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++) { n       int temp = query(0, 0, n - 1, i, i); n       cout << temp <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int i; n double a, b, c, d, base, top, mid, x11, x22, y11, y22, minim1, minim2, maxim1, n     maxim2; n int main() { n   scanf( %lf%lf%lf%lf , &a, &b, &c, &d); n   base = 0.0; n   top = 1000000000.0; n   for (i = 1; i <= 100; i++) { n     mid = (base + top) / 2.0; n     x11 = a - mid; n     x22 = a + mid; n     y11 = d - mid; n     y22 = d + mid; n     minim1 = min(min(x11 * y11, x11 * y22), min(x22 * y11, x22 * y22)); n     maxim1 = max(max(x11 * y11, x11 * y22), max(x22 * y11, x22 * y22)); n     x11 = b - mid; n     x22 = b + mid; n     y11 = c - mid; n     y22 = c + mid; n     minim2 = min(min(x11 * y11, x11 * y22), min(x22 * y11, x22 * y22)); n     maxim2 = max(max(x11 * y11, x11 * y22), max(x22 * y11, x22 * y22)); n     if (maxim1 < minim2 || maxim2 < minim1) n       base = mid; n     else n       top = mid; n   } n   printf( %.9lf n , top); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n pair<int, int> point[int(2e5 + 10)]; n map<int, int> mark_x, mark_y; n long long res; n void sol() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> point[i].first >> point[i].second; n     mark_x[point[i].first]++; n     mark_y[point[i].second]++; n   } n   sort(point, point + n); n   for (int i = 0; i < n; i++) { n     if (i == 0 || point[i].first != point[i - 1].first) { n       for (int j = i; point[j].first == point[i].first && j < n; j++) { n         mark_y[point[j].second]--; n       } n     } n     mark_x[point[i].first]--; n     res += mark_x[point[i].first]; n     res += mark_y[point[i].second]; n   } n   cout << res; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool isPrime(T x) { n   if (x <= 1) return false; n   T i; n   for (i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n template <class T> n class Prime { n  public: n   vector<T> z; n   Prime() { n     z.resize(1e5 + 7); n     for (int i = (0); i < (((int)z.size())); i++) z[i] = 1; n     z[0] = 0; n     z[1] = 0; n     T j; n     for (int i = (2); i < (((int)z.size())); i++) { n       if (z[i]) { n         j = i + i; n         while (j < ((int)z.size())) { n           z[j] = 0; n           j += i; n         } n       } n     } n   } n }; n template <class T> n double dist(T x1, T y1, T x2, T y2) { n   return sqrt(1. * (x2 - x1) * (x2 - x1) + 1. * (y2 - y1) * (y2 - y1)); n } n template <class T> n class Point { n  public: n   T x, y; n   Point() {} n   Point(T a, T b) : x(a), y(b) {} n   bool operator==(const Point& tmp) const { return (x == tmp.x && y == tmp.y); } n   Point operator-(const Point& tmp) const { n     return Point<T>(x - tmp.x, y - tmp.y); n   } n }; n char toLowerCase(char x) { return (x + 32); } n char toUpperCase(char x) { return (x - 32); } n bool isUpperCase(char x) { return (65 <= x && x <= 90) ? 1 : 0; } n bool isLowerCase(char x) { return (97 <= x && x <= 122) ? 1 : 0; } n bool isAlpha(char x) { return (isUpperCase(x) || isLowerCase(x)) ? 1 : 0; } n bool isDigit(char x) { return ( 0  <= x && x <=  9 ) ? 1 : 0; } n template <class T> n T toDec(string s) { n   stringstream is(s); n   T res; n   is >> res; n   return res; n } n template <class T> n string toStr(T n) { n   string s; n   stringstream is; n   is << n; n   is >> s; n   return s; n } n template <class T> n void checkmin(T& a, T b) { n   if (a > b) a = b; n } n template <class T> n void checkmax(T& a, T b) { n   if (a < b) a = b; n } n vector<int> res; n string s; n void parse(int ind) { n   string tmp = s.substr(ind + 7); n   int p = tmp.find( <table> ); n   if (p != -1) { n     parse(ind + 7 + p); n     tmp = s.substr(ind + 7); n   } n   p = tmp.find( </table> ) + ind + 14; n   int i, tot; n   i = ind; n   tot = 0; n   while (i <= p) { n     if (i + 3 <= p && s.substr(i, 4) ==  <td> ) tot++; n     s[i] =  1 ; n     i++; n   } n   res.push_back(tot); n } n int main() { n   int c; n   while ((c = getchar()) != EOF) { n     if (c !=   n ) s += c; n   } n   parse(0); n   sort(res.begin(), res.end()); n   for (int i = (0); i < (((int)res.size())); i++) cout << res[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout << 3 * n - 1 <<     << 2 << endl; n   cout <<  1 3  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool ispalindrom(string s) { n   string rs; n   reverse(s.begin(), s.end()); n   rs = s; n   reverse(s.begin(), s.end()); n   if (rs == s) n     return true; n   else n     return false; n } n int main() { n   string s; n   int k; n   cin >> s >> k; n   int n = s.length(); n   if (n % k != 0) { n     cout <<  NO ; n     return 0; n   } n   int i = n / k, count = 0; n   for (int j = 0; j + i <= n; j += i) { n     string temp = s.substr(j, i); n     if (!ispalindrom(temp)) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300001; n int f[maxn], a[maxn]; n inline void add(int x, int val) { n   for (int i = x + 1; i < maxn; i += i & (-i)) f[i] += val; n } n inline int get(int x) { n   int ans = 0; n   for (int i = x; i > 0; i -= i & (-i)) ans += f[i]; n   return ans; n } n inline int sum(int x, int y) { return get(y) - get(x); } n int main() { n   int q, l, r, n, m, rev = 0; n   scanf( %d%d , &n, &m); n   int cr = n - 1, cl = 0; n   int len = cr - cl + 1; n   for (int i = 0; i < n; ++i) add(i, a[i] = 1); n   for (int t = 0; t < m; ++t) { n     scanf( %d , &q); n     if (q == 1) { n       scanf( %d , &l); n       if (l > len - l) rev ^= 1, l = len - l; n       if (rev) { n         cr = cr - l; n         for (int i = cr; i >= cr - l + 1; --i) n           add(i, a[2 * cr - i + 1]), a[i] += a[2 * cr - i + 1]; n       } else { n         cl = cl + l; n         for (int i = cl; i <= cl + l - 1; ++i) n           add(i, a[2 * cl - i - 1]), a[i] += a[2 * cl - i - 1]; n       } n       len = cr - cl + 1; n     } n     if (q == 2) { n       scanf( %d%d , &l, &r); n       int ln = (r - l); n       if (rev) n         l = cr - l + 1, r = l - ln, swap(l, r); n       else n         l += cl, r += cl; n       printf( %d n , sum(l, r)); n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n int n, s; n int frq[1002]; n int t; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> s; n   for (int i = 1; i <= n; ++i) { n     int a, b; n     cin >> a >> b; n     frq[a] = max(frq[a], b); n   } n   for (int i = s; i >= 0; --i) { n     t = max(t, frq[i]); n     if (i != 0) ++t; n   } n   cout << t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long gcd(unsigned long long a, unsigned long long b) { n   return b == 0 ? a : gcd(b, a % b); n } n const int MAXTREE = 50; n const int MOD = 998244353; n const int INV6 = 166374059; n const int MINCOORD = -1000000000; n const int MAXCOORD = +1000000000; n struct P { n   int x, y; n   P() {} n   P(int x, int y) : x(x), y(y) {} n }; n P operator-(const P &a, const P &b) { return P(a.x - b.x, a.y - b.y); } n bool operator==(const P &a, const P &b) { return a.x == b.x && a.y == b.y; } n struct Pol { n   vector<P> p; n }; n int ntree, tlast; n P t[MAXTREE]; n void rotate(P &p) { p = P(-p.y, +p.x); } n void rotate(Pol &pol) { n   for (int i = (0); i < (((int)(pol.p).size())); ++i) rotate(pol.p[i]); n } n void flip(P &p) { swap(p.x, p.y); } n void flip(Pol &pol) { n   for (int i = (0); i < (((int)(pol.p).size())); ++i) flip(pol.p[i]); n   reverse(pol.p.begin(), pol.p.end()); n } n P infleftup(const P &p) { n   int t = min(p.x - MINCOORD, MAXCOORD - p.y); n   return P(p.x - t, p.y + t); n } n P infleftdown(const P &p) { n   int t = min(p.x - MINCOORD, p.y - MINCOORD); n   return P(p.x - t, p.y - t); n } n P infrightup(const P &p) { n   int t = min(MAXCOORD - p.x, MAXCOORD - p.y); n   return P(p.x + t, p.y + t); n } n P infrightdown(const P &p) { n   int t = min(MAXCOORD - p.x, p.y - MINCOORD); n   return P(p.x + t, p.y - t); n } n int getdelta(const P &a, const P &b) { n   P ba = b - a; n   assert(ba.y == 0 || abs(ba.y) == abs(ba.x)); n   return ba.y / ba.x; n } n int getdir(const P &a, const P &b) { n   P ba = b - a; n   int mag = max(abs(ba.x), abs(ba.y)); n   assert(mag != 0 && ba.x % mag == 0 && ba.y % mag == 0); n   ba.x /= mag, ba.y /= mag; n   if (ba == P(1, 1)) return 0; n   if (ba == P(1, 0)) return 1; n   if (ba == P(1, -1)) return 2; n   if (ba == P(0, -1)) return 3; n   if (ba == P(-1, -1)) return 4; n   if (ba == P(-1, 0)) return 5; n   if (ba == P(-1, 1)) return 6; n   if (ba == P(0, 1)) return 7; n   assert(false); n   return -1; n } n int gety(int x, const vector<P> &path) { n   int idx = 0; n   while (idx + 1 < ((int)(path).size()) && path[idx + 1].x <= x) ++idx; n   assert(idx + 1 < ((int)(path).size())); n   int v = getdelta(path[idx], path[idx + 1]); n   return path[idx].y + (x - path[idx].x) * v; n } n vector<pair<P, P>> segmentsbeloworequal(P a, P b, const vector<P> &path) { n   bool swapab = false; n   if (a.x > b.x) swapab = true, swap(a, b); n   P ba = b - a; n   vector<pair<P, P>> ret; n   if (ba.x == 0) { n     int x = a.x, py = gety(x, path); n     assert(!swapab); n     if (a.y <= py) n       ret.push_back(make_pair(a, P(x, min(py, b.y)))); n     else if (b.y <= py) n       ret.push_back(make_pair(P(x, py), b)); n   } else { n     int x = a.x, y = a.y, u = getdelta(a, b), idx = 0; n     P oth = a; n     while (true) { n       while (idx + 1 < ((int)(path).size()) && path[idx + 1].x <= x) ++idx; n       assert(idx + 1 < ((int)(path).size())); n       int v = getdelta(path[idx], path[idx + 1]); n       int py = path[idx].y + (x - path[idx].x) * v; n       if (x == b.x) { n         if (y <= py) ret.push_back(make_pair(oth, b)); n         break; n       } n       int dx = min(b.x - x, path[idx + 1].x - x); n       if (y <= py) { n         if (u - v >= 1) { n           int t = (py - y) / (u - v); n           if (t < dx) ret.push_back(make_pair(oth, P(x + t, y + u * t))); n         } n       } else { n         if (v - u >= 1) { n           int t = (y - py - 1) / (v - u); n           if (t < dx) oth = P(x + t + 1, y + u * (t + 1)); n         } n       } n       x += dx, y += u * dx; n     } n   } n   if (swapab) { n     swapab = false, swap(a, b), reverse(ret.begin(), ret.end()); n     for (int i = (0); i < (((int)(ret).size())); ++i) n       swap(ret[i].first, ret[i].second); n   } n   return ret; n } n bool isbeloworequal(const P &a, const vector<P> &path) { n   return a.y <= gety(a.x, path); n } n bool isinside(const P &p, const Pol &pol) { n   if (((int)(pol.p).size()) == 1) return pol.p[0] == p; n   bool ret = false; n   for (int i = (0); i < (((int)(pol.p).size())); ++i) { n     P a = pol.p[i], b = pol.p[(i + 1) % ((int)(pol.p).size())], n       c = pol.p[(i + 2) % ((int)(pol.p).size())], n       d = pol.p[(i + 3) % ((int)(pol.p).size())]; n     int pdir = getdir(a, b), cdir = getdir(b, c), ndir = getdir(c, d); n     int sgn, delta; n     P l, r; n     bool includefirst, includelast; n     if (cdir < 4) { n       includefirst = pdir < 3 || pdir >= cdir + 4, n       includelast = cdir != 3 && ndir >= 4 && ndir <= cdir + 4; n       if (cdir == 3) { n         sgn = includefirst ? +1 : 0, delta = 0, l = r = b; n         ++l.y, ++r.y; n       } else { n         sgn = +1, delta = getdelta(b, c); n         l = b, r = c; n         ++l.y, ++r.y; n         if (!includefirst) ++l.x, l.y += delta; n         if (!includelast) --r.x, r.y -= delta; n       } n     } else { n       includefirst = pdir != 7 && pdir >= cdir - 4, n       includelast = cdir != 7 && ndir <= cdir - 4; n       if (cdir == 7) { n         sgn = includefirst ? -1 : 0, delta = 0, l = r = b; n       } else { n         sgn = -1, delta = getdelta(c, b), l = c, r = b; n         if (!includefirst) --r.x, r.y -= delta; n         if (!includelast) ++l.x, l.y += delta; n       } n     } n     if (sgn != 0 && l.x <= p.x && p.x <= r.x && p.y < l.y + (p.x - l.x) * delta) n       ret = !ret; n   } n   return ret; n } n void updatepol(Pol &pol, bool prvonpol, const P p, const vector<P> &path, n                const Pol &orig) { n   if (((int)(pol.p).size()) >= 1 && p == pol.p[((int)(pol.p).size()) - 1]) n     return; n   if (!prvonpol && ((int)(pol.p).size()) >= 1) { n     P a = pol.p[((int)(pol.p).size()) - 1], b = p; n     bool swapab = false; n     if (a.x > b.x) swapab = true, swap(a, b); n     int ay = gety(a.x, path), by = gety(b.x, path); n     if (ay != a.y) { n       assert(ay == a.y + 1); n       if (isinside(P(a.x, a.y + 1), orig)) n         ++a.y; n       else n         ++a.x, --ay; n     } n     if (by != b.y) { n       assert(by == b.y + 1); n       if (isinside(P(b.x, b.y + 1), orig)) n         ++b.y; n       else n         --b.x, --by; n     } n     vector<P> extra; n     if (!(a == pol.p[((int)(pol.p).size()) - 1])) extra.push_back(a); n     for (int i = (0); i < (((int)(path).size())); ++i) n       if (a.x < path[i].x && path[i].x < b.x) extra.push_back(path[i]); n     if (!(b == p) && !(b == a)) extra.push_back(b); n     if (swapab) reverse(extra.begin(), extra.end()); n     for (int i = (0); i < (((int)(extra).size())); ++i) n       pol.p.push_back(extra[i]); n   } n   if (((int)(pol.p).size()) == 0 || !(p == pol.p[((int)(pol.p).size()) - 1])) n     pol.p.push_back(p); n } n Pol cutabove(Pol pol, const vector<P> &path) { n   if (((int)(pol.p).size()) == 0) return pol; n   Pol ret; n   if (((int)(pol.p).size()) == 1) n     return isbeloworequal(pol.p[0], path) ? pol : ret; n   bool prvonpol = false; n   for (int i = (0); i < (((int)(pol.p).size())); ++i) { n     P a = pol.p[i], b = pol.p[(i + 1) % ((int)(pol.p).size())]; n     vector<pair<P, P>> segments = segmentsbeloworequal(a, b, path); n     if (((int)(segments).size()) == 0 || !(segments[0].first == a)) n       prvonpol = false; n     for (int k = (0); k < (((int)(segments).size())); ++k) { n       if (segments[k].first == b) { n         assert(segments[k].second == b); n         assert(k == ((int)(segments).size()) - 1); n         prvonpol = false; n       } else { n         updatepol(ret, prvonpol, segments[k].first, path, pol); n         if (segments[k].first == segments[k].second) { n           prvonpol = false; n         } else if (segments[k].second == b) { n           prvonpol = true; n         } else { n           updatepol(ret, true, segments[k].second, path, pol); n           prvonpol = false; n         } n       } n     } n   } n   if (((int)(ret.p).size()) >= 1) { n     if (!(ret.p[0] == pol.p[0])) prvonpol = false; n     updatepol(ret, prvonpol, ret.p[0], path, pol); n     if (((int)(ret.p).size()) >= 2 && n         ret.p[((int)(ret.p).size()) - 1] == ret.p[0]) n       ret.p.pop_back(); n   } n   return ret; n } n Pol cut(Pol pol, P a, P b, bool winequal) { n   int nrot = 0, nflip = 0; n   while (b.y <= a.y || b.x < a.x) rotate(pol), rotate(a), rotate(b), ++nrot; n   if (b.y - a.y < b.x - a.x) flip(pol), flip(a), flip(b), ++nflip; n   P d = b - a; n   int between = d.y - 1; n   int asteps = between / 2, bsteps = between / 2, turn = winequal ? 0 : 1; n   if (between % 2 == 1) n     if (turn == 0) n       ++asteps, turn = 1; n     else n       ++bsteps, turn = 0; n   vector<P> path; n   int y = a.y + asteps; n   if (d.x == d.y) { n     P m(a.x + asteps + (turn == 0 ? 1 : 0), y); n     path.push_back(infleftup(m)); n     path.push_back(infrightdown(m)); n   } else if (d.x == 0) { n     if (asteps < bsteps) { n       assert(turn == 0); n       ++asteps, turn = 1; n     } n     if (bsteps < asteps) { n       assert(turn == 1); n       ++bsteps, turn = 0; n     } n     assert(asteps == bsteps); n     P l(a.x - asteps, y), r(a.x + asteps, y); n     path.push_back(turn == 0 ? infleftup(l) : infleftdown(l)); n     path.push_back(l); n     if (l.x != r.x || l.y != r.y) path.push_back(r); n     path.push_back(turn == 0 ? infrightup(r) : infrightdown(r)); n   } else { n     P l(a.x + d.x - bsteps - (turn == 0 ? 0 : 1), y), n         r(a.x + asteps + (turn == 0 ? 1 : 0), y); n     path.push_back(infleftup(l)); n     path.push_back(l); n     path.push_back(r); n     path.push_back(infrightdown(r)); n   } n   pol = cutabove(pol, path); n   while (nflip % 2 != 0) flip(pol), flip(a), flip(b), ++nflip; n   while (nrot % 4 != 0) rotate(pol), rotate(a), rotate(b), ++nrot; n   return pol; n } n int sum(int a, int b) { return (long long)(a + b) * (b - a + 1) / 2 % MOD; } n int sumsq(int a, int b) { n   return ((long long)2 * a * a + (long long)2 * b * b + (long long)2 * a * b + n           b - a) % n          MOD * (b - a + 1) % MOD * INV6 % MOD; n } n int calcsum(const P &l, const P &r, int delta) { n   int y = l.y % MOD; n   if (y < 0) y += MOD; n   int x = l.x % MOD; n   if (x < 0) x += MOD; n   if (delta == 0) { n     return (long long)sum(l.x, r.x) * y % MOD; n   } else if (delta == 1) { n     return ((long long)sum(l.x, r.x) * (y + MOD - x) + sumsq(l.x, r.x)) % MOD; n   } else if (delta == -1) { n     return ((long long)sum(l.x, r.x) * (y + x) + MOD - sumsq(l.x, r.x)) % MOD; n   } else { n     assert(false); n   } n } n int xcalc(Pol pol) { n   int ret = 0; n   if (((int)(pol.p).size()) >= 2) n     for (int i = (0); i < (((int)(pol.p).size())); ++i) { n       P a = pol.p[i], b = pol.p[(i + 1) % ((int)(pol.p).size())], n         c = pol.p[(i + 2) % ((int)(pol.p).size())], n         d = pol.p[(i + 3) % ((int)(pol.p).size())]; n       int pdir = getdir(a, b), cdir = getdir(b, c), ndir = getdir(c, d); n       int sgn, delta; n       P l, r; n       bool includefirst, includelast; n       if (cdir < 4) { n         includefirst = pdir < 3 || pdir >= cdir + 4, n         includelast = cdir != 3 && ndir >= 4 && ndir <= cdir + 4; n         if (cdir == 3) { n           sgn = includefirst ? +1 : 0, delta = 0, l = r = b; n           ++l.y, ++r.y; n         } else { n           sgn = +1, delta = getdelta(b, c); n           l = b, r = c; n           ++l.y, ++r.y; n           if (!includefirst) ++l.x, l.y += delta; n           if (!includelast) --r.x, r.y -= delta; n         } n       } else { n         includefirst = pdir != 7 && pdir >= cdir - 4, n         includelast = cdir != 7 && ndir <= cdir - 4; n         if (cdir == 7) { n           sgn = includefirst ? -1 : 0, delta = 0, l = r = b; n         } else { n           sgn = -1, delta = getdelta(c, b), l = c, r = b; n           if (!includefirst) --r.x, r.y -= delta; n           if (!includelast) ++l.x, l.y += delta; n         } n       } n       int cur = sgn != 0 && l.x <= r.x ? calcsum(l, r, delta) : 0; n       if (sgn == +1) n         ret = (ret + cur) % MOD; n       else n         ret = (ret + MOD - cur) % MOD; n     } n   if (((int)(pol.p).size()) == 1) ret = (ret + pol.p[0].x) % MOD; n   return ret; n } n Pol cutbelow(Pol pol, vector<P> path) { n   for (int rep = (0); rep < (2); ++rep) { n     rotate(pol); n     for (int i = (0); i < (((int)(path).size())); ++i) rotate(path[i]); n   } n   reverse(path.begin(), path.end()); n   pol = cutabove(pol, path); n   for (int rep = (0); rep < (2); ++rep) { n     rotate(pol); n     for (int i = (0); i < (((int)(path).size())); ++i) rotate(path[i]); n   } n   reverse(path.begin(), path.end()); n   return pol; n } n int calc(Pol pol) { n   int ret = 0; n   for (int rep = (0); rep < (4); ++rep) { n     vector<P> upath; n     upath.push_back(infleftdown(P(0, 0))); n     upath.push_back(infrightup(P(0, 0))); n     vector<P> lpath; n     lpath.push_back(P(MINCOORD, 0)); n     lpath.push_back(P(1, 0)); n     lpath.push_back(infrightdown(P(1, 0))); n     Pol cpol = cutbelow(cutabove(pol, upath), lpath); n     ret = (ret + xcalc(cpol)) % MOD; n     rotate(pol); n   } n   return ret; n } n int solve() { n   if (tlast == 0) return 0; n   int ret = 0; n   for (int i = (0); i < (ntree); ++i) { n     Pol pol; n     pol.p.push_back(P(t[i].x - tlast, t[i].y + tlast)); n     pol.p.push_back(P(t[i].x + tlast, t[i].y + tlast)); n     pol.p.push_back(P(t[i].x + tlast, t[i].y - tlast)); n     pol.p.push_back(P(t[i].x - tlast, t[i].y - tlast)); n     for (int j = (0); j < (ntree); ++j) n       if (j != i) { n         pol = cut(pol, t[i], t[j], i < j); n       } n     for (int k = (0); k < (((int)(pol.p).size())); ++k) n       pol.p[k] = pol.p[k] - t[i]; n     ret = (ret + calc(pol)) % MOD; n   } n   return ret; n } n void run() { n   scanf( %d%d , &ntree, &tlast); n   for (int i = (0); i < (ntree); ++i) scanf( %d%d , &t[i].x, &t[i].y); n   printf( %d n , solve()); n } n int solvestupid() { n   int lx = INT_MAX, hx = INT_MIN, ly = INT_MAX, hy = INT_MIN; n   for (int i = (0); i < (ntree); ++i) n     lx = min(lx, t[i].x - tlast), hx = max(hx, t[i].x + tlast), n     ly = min(ly, t[i].y - tlast), hy = max(hy, t[i].y + tlast); n   vector<vector<int>> d(hx - lx + 1, vector<int>(hy - ly + 1, INT_MAX)); n   for (int i = (0); i < (ntree); ++i) n     for (int x = (t[i].x - tlast); x <= (t[i].x + tlast); ++x) n       for (int y = (t[i].y - tlast); y <= (t[i].y + tlast); ++y) { n         int &v = d[x - lx][y - ly]; n         v = min(v, max(abs(x - t[i].x), abs(y - t[i].y))); n       } n   int ret = 0; n   for (int x = (lx); x <= (hx); ++x) n     for (int y = (ly); y <= (hy); ++y) { n       int v = d[x - lx][y - ly]; n       if (v != INT_MAX) ret += v; n     } n   return ret; n } n void stress() { n   mt19937 rnd(12344); n   int mxn = 100, mxt = 100, mncoord = 0, mxcoord = 100; n   for (int rep = (0); rep < (100000); ++rep) { n     ntree = rnd() % mxn + 1; n     tlast = rnd() % (mxt + 1); n     set<pair<int, int>> seen; n     for (int i = (0); i < (ntree); ++i) n       while (true) { n         t[i].x = rnd() % (mxcoord - mncoord + 1) + mncoord, n         t[i].y = rnd() % (mxcoord - mncoord + 1) + mncoord; n         if (seen.count(make_pair(t[i].x, t[i].y))) continue; n         seen.insert(make_pair(t[i].x, t[i].y)); n         break; n       } n     int have = solve(); n     int want = solvestupid(); n     if (have == want) { n       if (rep % 100 == 99) printf( . ); n       continue; n     } n     puts(  ); n     printf( %d %d n , ntree, tlast); n     for (int i = (0); i < (ntree); ++i) printf( %d %d n , t[i].x, t[i].y); n     printf( err have=%d want=%d n , have, want); n     break; n   } n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T& a) { n   char c = getchar(); n   int f = 1; n   a = 0; n   for (; c >  9  || c <  0 ; c = getchar()) n     if (c ==  - ) f = -1; n   for (; c <=  9  && c >=  0 ; c = getchar()) a = a * 10 + c - 48; n   a *= f; n } n const int o = 1e5 + 10; n inline long long Max(long long a, long long b) { return a > b ? a : b; } n long long T, n, k, a[o]; n pair<long long, long long> p[o]; n signed main() { n   for (read(T); T--;) { n     read(n); n     for (long long i = 1; i <= n; ++i) { n       read(k); n       for (long long j = 0; j < k; ++j) read(a[j]); n       a[k] = 0; n       for (long long j = k - 1; j + 1; --j) a[j] = Max(a[j + 1], a[j] - j); n       p[i] = make_pair(a[0], k); n     } n     sort(p + 1, p + 1 + n); n     for (long long i = 2, j = p[1].second; i <= n; j += p[i++].second) n       p[i].first = Max(p[i - 1].first, p[i].first - j); n     printf( %lld n , p[n].first + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> dif; n map<int, int> g; n int a[120000]; n int main() { n   int n, flag; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   for (int i = 2; i <= n; i++) { n     if (g[a[i] - a[i - 1]] == 0) dif.push_back(a[i] - a[i - 1]); n     g[a[i] - a[i - 1]]++; n   } n   int sz = dif.size(); n   if (sz > 2 || n == 1) { n     if (sz > 2) n       cout << 0 << endl; n     else n       cout << -1 << endl; n     return 0; n   } n   if (sz == 1) { n     int D = dif[0]; n     if (dif[0] == 0) { n       cout << 1 << endl; n       cout << a[1] << endl; n       return 0; n     } n     if (n == 2) { n       if ((a[2] - a[1]) % 2 == 0) { n         cout << 3 << endl; n         cout << a[1] - D <<     << (a[2] + a[1]) / 2 <<     << a[2] + D << endl; n         return 0; n       } n     } n     cout << 2 << endl; n     cout << a[1] - D <<     << a[n] + D << endl; n     return 0; n   } n   int D1 = min(dif[0], dif[1]), D2 = max(dif[0], dif[1]); n   if (D1 * 2 != D2 || g[D2] > 1) { n     cout << 0 << endl; n     return 0; n   } n   cout << 1 << endl; n   for (int i = 2; i <= n; i++) { n     if (a[i] - a[i - 1] == D2) { n       cout << a[i] - D1 << endl; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void init_code() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n set<pair<int, int>> es; n map<int, list<int>> m; n void add(int a, int b) { n   m[a].push_back(b); n   m[b].push_back(a); n } n int v[1001]; n void dfs(int node) { n   v[node] = 1; n   for (int i : m[node]) { n     if (v[i] == 0) { n       es.erase({node, i}); n       es.erase({i, node}); n       dfs(i); n     } n   } n } n int main() { n   init_code(); n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     add(a, b); n     es.insert({a, b}); n   } n   vector<int> ans; n   for (int i = 1; i <= n; i++) { n     if (v[i] == 0) { n       ans.push_back(i); n       dfs(i); n     } n   } n   cout << es.size() << endl; n   vector<pair<int, int>> ans2, ans3; n   for (auto i : es) { n     ans2.push_back(i); n   } n   for (int i = 1; i < ans.size(); i++) { n     ans3.push_back({ans[i], ans[i - 1]}); n   } n   for (int i = 0; i < ans2.size(); i++) { n     cout << ans2[i].first <<     << ans2[i].second <<     << ans3[i].first n          <<     << ans3[i].second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4040, inF = 1e9; n int n, a, b, c, dp[N]; n int main() { n   cin >> n >> a >> b >> c; n   dp[0] = 0; n   for (int i = 1; i <= n; i++) { n     dp[i] = -inF; n     if (i >= a) dp[i] = max(dp[i], dp[i - a] + 1); n     if (i >= b) dp[i] = max(dp[i], dp[i - b] + 1); n     if (i >= c) dp[i] = max(dp[i], dp[i - c] + 1); n   } n   cout << dp[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long K = 10001; n signed main() { n   long long n, l, r, ans = 0; n   cin >> n >> l >> r; n   vector<long long> a(n), b(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (long long i = 0; i < n; i++) cin >> b[i]; n   vector<long long> bad, good; n   for (long long i = 0; i < n; i++) { n     if (b[i] == 0) n       bad.push_back(a[i]); n     else n       good.push_back(a[i]); n   } n   if (good.size() == 0) { n     cout << 0 << endl; n     return 0; n   } n   sort(good.begin(), good.end()); n   long long S = 0; n   for (long long i = 0; i < good.size(); i++) S += good[i]; n   bool knapbad[K], knapgood[K]; n   for (long long i = 0; i < K; i++) knapbad[i] = false; n   for (long long i = 0; i < K; i++) knapgood[i] = false; n   knapbad[0] = true; n   knapgood[0] = true; n   for (long long i = 0; i < bad.size(); i++) { n     for (long long j = K - 1; j >= bad[i]; j--) n       knapbad[j] |= knapbad[j - bad[i]]; n   } n   long long pref[K + 1]; n   pref[0] = 0; n   for (long long i = 1; i <= K; i++) pref[i] = pref[i - 1] + knapbad[i - 1]; n   vector<vector<long long> > close(good.size()); n   long long INF = 1e9; n   close[good.size() - 1].push_back(0); n   for (long long i = good.size() - 1; i >= 0; i--) { n     for (long long j = K - 1; j >= good[i]; j--) { n       if (!knapgood[j] && knapgood[j - good[i]]) { n         if (i != 0) close[i - 1].push_back(j); n         knapgood[j] = true; n       } n     } n   } n   for (long long i = l; i <= r; i++) n     if (knapgood[i] && i != S) ans = max(ans, (long long)1); n   long long Kn[K]; n   for (long long i = 0; i < K; i++) Kn[i] = -INF; n   Kn[0] = 0; n   for (long long i = 0; i < good.size(); i++) { n     for (long long j = K - 1; j >= good[i]; j--) { n       Kn[j] = max(Kn[j], Kn[j - good[i]] + 1); n     } n     for (long long j = 0; j < close[i].size(); j++) { n       long long N = close[i][j]; n       for (long long p = 0; p < K; p++) { n         if (Kn[p] < 0) continue; n         long long L = max((long long)0, l - N), R = r - N - p; n         if (L > R) continue; n         if (pref[R + 1] - pref[L] == 0) continue; n         if (N + p == S) n           ans = max(ans, Kn[p]); n         else n           ans = max(ans, Kn[p] + 1); n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int mnx = 1e5 + 9; n long long q, n; n long long a[mnx], k; n map<int, int> m; n int main() { n   cin >> q; n   while (q--) { n     cin >> n; n     for (int j = 1; j * j <= n; j++) { n       int jk = n / j; n       if (j == n / jk) { n         m[n / j]++; n         m[j]++; n       } n     } n     cout << m.size() + 1 <<   n ; n     cout << 0 <<    ; n     for (auto i : m) { n       cout << i.first <<    ; n     } n     m.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n int main() { n   string s; n   cin >> s; n   int index = s.size() - 1; n   vector<char> din3(s.size(), 0); n   vector<char> din2(s.size(), 0); n   if (index - 2 >= 5) din3[index - 2] = 1; n   if (index - 1 >= 5) din2[index - 1] = 1; n   index -= 3; n   set<string> res; n   while (index >= 5) { n     if (din2[index + 2] == 1 and s.substr(index, 2) != s.substr(index + 2, 2)) { n       din2[index] = 1; n     } n     if (din3[index + 3] == 1 and s.substr(index, 3) != s.substr(index + 3, 3)) { n       din3[index] = 1; n     } n     if (din2[index + 3] == 1) { n       din3[index] = 1; n     } n     if (din3[index + 2] == 1) { n       din2[index] = 1; n     } n     --index; n   } n   for (auto i(0); i < din2.size(); ++i) { n     if (din2[i] == 1) { n       res.insert(s.substr(i, 2)); n     } n   } n   for (auto i(0); i < din3.size(); ++i) { n     if (din3[i] == 1) { n       res.insert(s.substr(i, 3)); n     } n   } n   cout << res.size() << endl; n   while (!res.empty()) { n     cout << *res.begin() << endl; n     res.erase(res.begin()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, l, tmp; n long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x * f; n } n long long cal(long long x, long long y, long long z) { n   if (x < y) return 0; n   long long p = min(z, x - y); n   return (p + 2) * (p + 1) / 2; n } n int main() { n   a = read(); n   b = read(); n   c = read(); n   l = read(); n   for (int i = 0; i <= l; i++) n     tmp += cal(a + i, b + c, l - i) + cal(b + i, a + c, l - i) + n            cal(c + i, a + b, l - i); n   printf( %I64d n , (l + 1) * (l + 2) * (l + 3) / 6 - tmp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 50; n long long mod = 998244353; n int n, m, u, v; n vector<int> adj[N]; n long long toAdd[N + N]; n vector<pair<int, int> > q[N]; n long long ans[N]; n void dfs(int src, int par, int dep, long long sum) { n   for (auto i : q[src]) { n     toAdd[dep] += i.second; n     toAdd[dep + i.first + 1] -= i.second; n   } n   sum += toAdd[dep]; n   ans[src] = sum; n   for (auto i : adj[src]) n     if (i != par) dfs(i, src, dep + 1, sum); n   for (auto i : q[src]) { n     toAdd[dep] -= i.second; n     toAdd[dep + i.first + 1] += i.second; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; ++i) { n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   scanf( %d , &m); n   int d, x; n   while (m--) { n     scanf( %d%d%d , &v, &d, &x); n     d = min(d, n); n     q[v].push_back(make_pair(d, x)); n   } n   dfs(1, 0, 0, 0); n   for (int i = 1; i <= n; ++i) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long a[200010] = {}; n vector<int> e[200010]; n long long t[200010] = {}; n long long q[200010] = {}; n int s, b; n long long ans = -222147483600ll; n bool cmp(long long x, long long y) { return x > y; } n void dfs(int x, int y) { n   vector<long long> w; n   t[x] += a[x]; n   q[x] = -2221483600ll; n   for (int i = 0; i < e[x].size(); i++) { n     if (e[x][i] == y) continue; n     dfs(e[x][i], x); n     t[x] += t[e[x][i]]; n     q[x] = max(q[x], q[e[x][i]]); n     w.push_back(q[e[x][i]]); n   } n   sort(w.begin(), w.end(), cmp); n   q[x] = max(t[x], q[x]); n   if (w.size() >= 2) { n     ans = max(w[0] + w[1], ans); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &s, &b); n     e[s].push_back(b); n     e[b].push_back(s); n   } n   dfs(1, 0); n   if (ans == -222147483600ll) n     cout <<  Impossible  << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 5; n struct ooo { n   int v, id; n } p[N << 1]; n bool operator<(const ooo& a, const ooo& b) { return a.v < b.v; } n int n, ans, id, h, m, k, x; n int main() { n   scanf( %d%d%d%d , &n, &h, &m, &k); n   m /= 2; n   for (int i = 1; i <= n; i++) n     scanf( %d%d , &x, &p[i].v), p[i].v %= m, p[i].id = i; n   std::sort(p + 1, p + 1 + n); n   for (int i = 1; i <= n; i++) p[i + n] = p[i], p[i + n].v += m; n   ans = 1e9; n   x = -1; n   for (int i = 1, j = 1; i <= n * 2; i++) { n     while (p[i].v - p[j].v >= k) j++; n     if (i > n && i - j < ans) ans = i - j, x = p[i].v; n   } n   printf( %d %d n , ans, x % m); n   for (int i = 1; i <= n * 2; i++) n     if (x - k < p[i].v && p[i].v < x) printf( %d  , p[i].id); n } n