#include <bits/stdc++.h> n using namespace std; n int m = 10000000; n int n; n int a[1000005]; n int p[10000005]; n long long gcd(long long a, long long b) { n   if (!b) return a; n   return gcd(b, a % b); n } n int get(int x) { return p[x] == x ? x : p[x] = get(p[x]); } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     p[a[i]] = a[i]; n   } n   int k = n; n   int x, y; n   for (long long j = 1; j <= m; j++) n     for (long long i = j + 1; 2 * i * j <= m && i * i - j * j <= m; i++) n       if (gcd(2 * i * j, i * i - j * j) == 1) { n         x = 2 * i * j; n         y = i * i - j * j; n         if (p[x] != 0 && p[y] != 0) { n           x = get(x); n           y = get(y); n           if (x != y) { n             k--; n             p[x] = y; n           } n         } n         if (i * i + j * j <= m) { n           x = 2 * i * j; n           y = i * i + j * j; n           if (p[x] != 0 && p[y] != 0) { n             x = get(x); n             y = get(y); n             if (x != y) { n               k--; n               p[x] = y; n             } n           } n         } n         if (i * i + j * j <= m) { n           x = i * i + j * j; n           y = i * i - j * j; n           if (p[x] != 0 && p[y] != 0) { n             x = get(x); n             y = get(y); n             if (x != y) { n               k--; n               p[x] = y; n             } n           } n         } n       } n   cout << k << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1100010; n const double pai = acos(-1.0); n char s[maxn], t[maxn]; n int n, m, a[maxn], b[maxn], ans[maxn], fa[maxn][6], rev[maxn], bin[1100010]; n struct node { n   double a, b; n   node(double t0 = 0.0, double t1 = 0.0) { n     a = t0; n     b = t1; n   } n } A[maxn], B[maxn]; n node operator+(node a, node b) { n   node c; n   c.a = a.a + b.a; n   c.b = a.b + b.b; n   return c; n } n node operator-(node a, node b) { n   node c; n   c.a = a.a - b.a; n   c.b = a.b - b.b; n   return c; n } n node operator*(node a, node b) { n   node c; n   c.a = a.a * b.a - a.b * b.b; n   c.b = a.b * b.a + a.a * b.b; n   return c; n } n void FFT(node *t, int n, int id) { n   int len = bin[n]; n   for (int i = 0; i < n; i++) n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1)); n   for (int i = 0; i < n; i++) n     if (i < rev[i]) swap(t[i], t[rev[i]]); n   for (int i = 1; i < n; i <<= 1) { n     node Wn; n     Wn.a = cos(pai / i); n     Wn.b = id * sin(pai / i); n     for (int j = 0; j < n; j += (i << 1)) { n       node W; n       W.a = 1; n       W.b = 0; n       for (int k = j; k < j + i; k++) { n         node t1 = t[k], t2 = t[k + i] * W; n         t[k] = t1 + t2; n         t[k + i] = t1 - t2; n         W = W * Wn; n       } n     } n   } n } n int find(int *f, int x) { n   if (f[x] == x) return x; n   return f[x] = find(f, f[x]); n } n void solve(int x, int y) { n   int len = 1; n   while (len <= 2 * (n + m)) len <<= 1; n   for (int i = 0; i < len; i++) A[i].a = A[i].b = B[i].a = B[i].b = 0; n   for (int i = 0; i < n; i++) n     if (s[i] -  a  == x) A[i].a = 1; n   for (int i = 0; i < m; i++) n     if (t[i] -  a  == y) B[i].a = 1; n   reverse(B, B + m); n   FFT(A, len, 1), FFT(B, len, 1); n   for (int i = 0; i < len; i++) A[i] = A[i] * B[i]; n   FFT(A, len, -1); n   for (int i = m - 1; i < n; i++) { n     int temp = (int)(A[i].a / len + 0.5); n     if (!temp) continue; n     if (find(fa[i], x) != find(fa[i], y)) { n       fa[i][fa[i][x]] = fa[i][y]; n       ans[i]++; n     } n   } n } n int main() { n   for (int i = 0; i <= 20; i++) bin[1 << i] = i; n   scanf( %s , s); n   scanf( %s , t); n   n = strlen(s); n   m = strlen(t); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 6; j++) fa[i][j] = j; n   for (int i = 0; i < 6; i++) n     for (int j = 0; j < 6; j++) n       if (i != j) solve(i, j); n   for (int i = m - 1; i < n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n void file() {} n int main() { n   fast(); n   file(); n   int n, idx = 0; n   cin >> n; n   vector<vector<int>> v(n, vector<int>(n)); n   vector<bool> ans(n, 1); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> v[i][j]; n       if (v[i][j] == 3) { n         ans[i] = 0; n         ans[j] = 0; n       } else if (v[i][j] == 1) n         ans[i] = 0; n       else if (v[i][j] == 2) n         ans[j] = 0; n     } n   } n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (ans[i]) cnt++; n   } n   cout << cnt << endl; n   for (int i = 0; i < n; i++) { n     if (ans[i]) cout << i + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void sc(T &x) { n   char r; n   bool start = false, neg = false; n   T ret = 0; n   while (true) { n     r = getchar(); n     if ((r -  0  < 0 || r -  0  > 9) && r !=  -  && !start) continue; n     if ((r -  0  < 0 || r -  0  > 9) && r !=  -  && start) break; n     if (start) ret *= 10; n     start = true; n     ret += r -  0 ; n   } n   if (!neg) n     x = ret; n   else n     x = -ret; n } n template <typename T> n T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   map<int, int> m; n   int i, j = 0, k = 0; n   i = 6; n   while (i--) { n     cin >> j; n     if (m.find(j) != m.end()) n       m[j]++; n     else n       m[j] = 1; n   } n   i = j = k = 0; n   if (m.size() == 1) { n     cout <<  Elephant n ; n   } else if (m.size() > 3) { n     cout <<  Alien n ; n   } else if (m.size() == 2) { n     for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) { n       if ((*it).second >= 4) k++; n       if ((*it).second == 2) j++; n     } n     if (k & j) { n       cout <<  Elephant n ; n     } else if (k) { n       cout <<  Bear n ; n     } else { n       cout <<  Alien n ; n     } n   } else if (m.size() == 3) { n     for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) { n       if ((*it).second == 4) k++; n     } n     if (k) { n       cout <<  Bear n ; n     } else { n       cout <<  Alien n ; n     } n   } n } n


n #include <bits/stdc++.h> n  n using namespace std; n  n void code() n { n  int n; n  cin>>n; n  string s; n  cin>>s; n  n  set<char> used; n  char last= - ; n  bool possible=true; n  for(char& c:s) n  { n   if(c!=last) n   { n    if(used.find(c)!=used.end()) n    { n     possible=false; n     break; n    } n   } n   last=c; n   used.insert(c); n  } n  n  if(possible) n   cout<< YES ; n  else n   cout<< NO ; n  cout<<  n ; n } n  n int main() n { n  ios_base::sync_with_stdio(false); n  cin.tie(nullptr); n  n  int t=1; n  cin>>t; n  for(int tt=1;tt<=t;++tt) n  { n   code(); n  } n  n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 20; n int n; n int part[N][2]; n bool vis[N]; n vector<int> ans; n vector<pair<int, int> > G[N]; n void dfs(int u) { n   vis[u] = true; n   for (auto v : G[u]) n     if (!vis[v.first]) dfs(v.first); n } n bool check(int Mask) { n   for (int i = 0; i <= Mask; ++i) G[i].clear(), vis[i] = false; n   for (int i = 1; i <= n; ++i) { n     int u = part[i][0] & Mask; n     int v = part[i][1] & Mask; n     G[u].push_back({v, 2 * i - 1}); n     G[v].push_back({u, 2 * i - 2}); n   } n   int comps = 0; n   for (int i = 0; i <= Mask; ++i) { n     if (G[i].size() & 1) return false; n     if (!vis[i] && G[i].size() > 0) { n       ++comps; n       dfs(i); n     } n   } n   return comps == 1; n } n void go(int u, int prv = -1) { n   while (G[u].size()) { n     auto e = G[u].back(); n     G[u].pop_back(); n     if (vis[e.second / 2]) continue; n     vis[e.second / 2] = true; n     go(e.first, e.second); n   } n   if (prv != -1) { n     ans.push_back(prv); n     ans.push_back(prv ^ 1); n   } n } n void restore(int Mask) { n   for (int i = 0; i <= n; ++i) vis[i] = false; n   for (int i = 0; i <= Mask; ++i) n     if (G[i].size()) { n       go(i); n       break; n     } n   for (int i = 0; i < n + n; ++i) n     printf( %d%c , ans[i] + 1,    n [i + 1 == n + n]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d %d , &part[i][0], &part[i][1]); n   for (int i = 20; i >= 0; --i) n     if (check((1 << i) - 1)) { n       printf( %d n , i); n       restore((1 << i) - 1); n       exit(0); n     } n   assert(false); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   float d, l, v1, v2, y, z, p, q, i, j; n   scanf( %f %f %f %f , &d, &l, &v1, &v2); n   float x = (l - d) / (v1 + v2); n   printf( %f n , x); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1, c = getchar(); n   while (c < 48) c ==  -  && (f = -1), c = getchar(); n   while (c > 47) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n const int MAXN = 100005; n struct Node { n   int l, r, mx; n   double p; n } q[5005]; n std::vector<int> G[5005]; n int lg[MAXN], fac[20], st[18][MAXN]; n double f[5005][5005]; n int n, m; n bool operator<(Node a, Node b) { return a.l ^ b.l ? a.l < b.l : a.r > b.r; } n inline void dfs(int x) { n   double p = 1; n   for (int y : G[x]) dfs(y); n   for (int y : G[x]) p *= f[y][q[x].mx - q[y].mx]; n   f[x][0] = (1 - q[x].p) * p; n   for (int i = 1; i <= m; ++i) { n     double lp = 1, rp = 1; n     for (int y : G[x]) { n       lp *= f[y][min(q[x].mx + i - q[y].mx - 1, m)]; n       rp *= f[y][min(q[x].mx + i - q[y].mx, m)]; n     } n     f[x][i] = q[x].p * lp + (1 - q[x].p) * rp; n   } n } n inline int query(int l, int r) { n   int k = lg[r - l + 1]; n   return max(st[k][l], st[k][r - fac[k] + 1]); n } n inline void buildst() { n   for (int i = 1; i <= n; ++i) st[0][i] = read(); n   for (int i = fac[0] = 1; i < 20; ++i) fac[i] = fac[i - 1] << 1; n   for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1; n   for (int j = 1; j < 17; ++j) n     for (int i = 1; i + fac[j] - 1 <= n; ++i) n       st[j][i] = max(st[j - 1][i], st[j - 1][i + fac[j - 1]]); n } n int main(int argc, char const *argv[]) { n   n = read(); n   m = read(); n   buildst(); n   for (int i = 1; i <= m; ++i) { n     q[i].l = read(); n     q[i].r = read(); n     scanf( %lf , &q[i].p); n     q[i].mx = query(q[i].l, q[i].r); n   } n   q[++m] = Node{1, n, query(1, n), 0}; n   sort(q + 1, q + m + 1); n   for (int i = 2; i <= m; ++i) n     for (int j = i - 1; j; --j) n       if (q[j].l <= q[i].l && q[i].r <= q[j].r) { n         G[j].push_back(i); n         break; n       } n   dfs(1); n   double ans = f[1][0] * q[1].mx; n   for (int i = 1; i <= m; ++i) ans += (f[1][i] - f[1][i - 1]) * (q[1].mx + i); n   printf( %lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; ++i) { n     cin >> v[i]; n   } n   sort(v.begin(), v.end()); n   cout << min(v[n - 1] - v[1], v[n - 2] - v[0]); n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1e3 + 100; n const int LogN = 20; n long long Inf = 1e9 + 100; n const int dx[4] = {0, 1, 0, -1}; n const int dy[4] = {1, 0, -1, 0}; n const double eps = 1e-9; n int md = 2520; n struct el { n   int v, u, c; n }; n vector<el> Ans; n vector<int> Gr[MaxN]; n int Sz[MaxN]; n int Ct = -1; n int N; n int Next = 1; n int Add = 1; n int find(int v, int p = -1, int c = 0) { n   int Mx = 0; n   int Sum = 1; n   for (int to : Gr[v]) { n     if (to == p) continue; n     int k = find(to, v, c + 1); n     Sum += k; n     if (Ct != -1) return Sum; n     Sz[to] = k; n     Mx = ((Mx) > (Sz[to]) ? (Mx) : (Sz[to])); n   } n   Mx = ((Mx) > (N - Sum) ? (Mx) : (N - Sum)); n   if (Mx <= N / 2 && Ct == -1) { n     Ct = v; n     Sz[p] = N - Sum; n   } n   return Sum; n } n void dfs(int v, int k, int p = -1) { n   for (int to : Gr[v]) { n     if (p == to) continue; n     Ans.push_back({v, to, Next - k}); n     Next += Add; n     dfs(to, Next - Add, v); n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cout << fixed << setprecision(5); n   cin >> N; n   for (int i = 0; i < N - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     Gr[a].push_back(b); n     Gr[b].push_back(a); n   } n   find(0); n   vector<pair<int, int> > Mt; n   for (int to : Gr[Ct]) { n     Mt.push_back({Sz[to], to}); n   } n   sort(Mt.begin(), Mt.end()); n   vector<pair<int, int> > At; n   int k = 0; n   while (k < (N - 1) / 3 + ((N - 1) % 3 != 0)) { n     At.push_back(Mt.back()); n     Mt.pop_back(); n     k += At.back().first; n   } n   for (pair<int, int> a : At) { n     Ans.push_back({Ct, a.second, Next}); n     Next += Add; n     dfs(a.second, Next - Add, Ct); n   } n   Add = (k + 1); n   Next = k + 1; n   for (pair<int, int> a : Mt) { n     Ans.push_back({Ct, a.second, Next}); n     Next += Add; n     dfs(a.second, Next - Add, Ct); n   } n   for (el a : Ans) { n     cout << a.v + 1 <<     << a.u + 1 <<     << a.c <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = int(1e9) + 7, INF = 0x3fffffff, maxn = 1e5 + 12; n int n, sum, a[maxn]; n int main(void) { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d , &(a[i])), sum += a[i]; n   sort(a, a + n); n   int ans = 0; n   int l = sum / n, r; n   r = l + 1; n   int y = n - sum + l * n; n   for (int i = 0; i < n; i++) { n     while (i < y) { n       ans += abs(a[i] - l); n       i++; n     } n     while (i < n) { n       ans += abs(r - a[i]); n       i++; n     } n   } n   cout << ans / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[3500]; n int main(void) { n   int c; n   scanf( %d n , &c); n   for (int t = 0; t < c; t++) { n     int n, m, k; n     scanf( %d %d %d n , &n, &m, &k); n     for (int i = 0; i < n; i++) { n       scanf( %d  , &arr[i]); n     } n     scanf(  n ); n     vector<int> v; n     for (int i = 0; i < m; i++) { n       v.push_back(max(arr[i], arr[i + n - m])); n     } n     if (k >= m) { n       k = m - 1; n     } n     int ret = 0; n     for (int i = 0; i <= k; i++) { n       int mini = 2e9; n       for (int j = i; j < i + m - k; j++) { n         mini = min(v[j], mini); n       } n       ret = max(ret, mini); n     } n     printf( %d n , ret); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, sl, fh; n struct node { n   int s, m, r, t; n } a[100010]; n struct seg { n   vector<long long> tim, pre, suf; n   vector<node> a; n   int lt; n   void resize(int l) { n     pre.resize(l); n     suf.resize(l); n   } n   long long getans(int T) { n     if (!lt) { n       long long ret = 0; n       for (int i = 0; i < (int)a.size(); ++i) n         ret += min(0ll + a[i].m, a[i].s + 1ll * T * a[i].r); n       lt = T; n       return ret; n     } n     int t = T - lt, n         p = upper_bound(tim.begin(), tim.end(), t) - tim.begin() - 1; n     long long ret = 0; n     if (p >= 0) ret = pre[p]; n     if (p < (int)a.size() - 1) ret += t * suf[p + 1]; n     lt = T; n     return ret; n   } n } tr[100010 << 2]; n int rd() { n   sl = 0; n   fh = 1; n   char ch = getchar(); n   while (ch <  0  ||  9  < ch) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) sl = sl * 10 + ch -  0 , ch = getchar(); n   return sl * fh; n } n void merge(seg &res, seg &x, seg &y) { n   int sz, sx = x.a.size(), sy = y.a.size(); n   res.resize(sz = sx + sy); n   int i = 0, j = 0; n   for (; i < sx && j < sy;) n     if (x.tim[i] <= y.tim[j]) n       res.a.push_back(x.a[i]), res.tim.push_back(x.tim[i]), ++i; n     else n       res.a.push_back(y.a[j]), res.tim.push_back(y.tim[j]), ++j; n   for (; i < sx; ++i) res.a.push_back(x.a[i]), res.tim.push_back(x.tim[i]); n   for (; j < sy; ++j) res.a.push_back(y.a[j]), res.tim.push_back(y.tim[j]); n   res.pre[0] = res.a[0].m; n   for (int i = 1; i < sz; ++i) res.pre[i] = res.pre[i - 1] + res.a[i].m; n   res.suf[sz - 1] = res.a[sz - 1].r; n   for (int i = sz - 1; i; --i) res.suf[i - 1] = res.suf[i] + res.a[i - 1].r; n } n void build(int x, int l, int r) { n   if (l == r) { n     tr[x].a.push_back(a[l]); n     tr[x].tim.push_back(a[l].t); n     tr[x].pre.push_back(a[l].m); n     tr[x].suf.push_back(a[l].r); n     return; n   } n   build(x << 1, l, ((l + r) >> 1)); n   build(x << 1 | 1, ((l + r) >> 1) + 1, r); n   merge(tr[x], tr[x << 1], tr[x << 1 | 1]); n } n void pushdown(int x) { n   if (tr[x].lt >= 0) tr[x << 1].lt = tr[x << 1 | 1].lt = tr[x].lt, tr[x].lt = 0; n } n long long query(int x, int l, int r, int L, int R, int T) { n   if (L <= l && r <= R && tr[x].lt >= 0) return tr[x].getans(T); n   pushdown(x); n   tr[x].lt = -1; n   long long ret = 0; n   if (L <= ((l + r) >> 1)) ret = query(x << 1, l, ((l + r) >> 1), L, R, T); n   if (R > ((l + r) >> 1)) n     ret += query(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R, T); n   if (tr[x << 1].lt == tr[x << 1 | 1].lt) tr[x].lt = tr[x << 1].lt; n   return ret; n } n int main() { n   n = rd(); n   for (int i = 1; i <= n; ++i) { n     a[i].s = rd(); n     a[i].m = rd(); n     a[i].r = rd(); n     if (!a[i].r) n       a[i].t = 1e9; n     else n       a[i].t = (a[i].m - 1) / a[i].r + 1; n   } n   build(1, 1, n); n   for (int l, r, t, T = rd(); T; --T) { n     t = rd(); n     l = rd(); n     r = rd(); n     printf( %lld n , query(1, 1, n, l, r, t)); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n int n, x, y, final, a[100500], dep[100500], maxn, root, f[100500]; n std::vector<int> G[100500]; n priority_queue<int> Que; n void DFS1(int now, int fa) { n   for (int i = 0; i < (int)G[now].size(); i++) { n     if (G[now][i] == fa) continue; n     dep[G[now][i]] = dep[now] + 1; n     DFS1(G[now][i], now); n   } n   if (dep[now] > maxn) { n     root = now; n     maxn = dep[now]; n   } n } n void DFS2(int now, int fa) { n   f[now] = 1; n   int son = 0; n   for (int i = 0; i < (int)G[now].size(); i++) { n     if (G[now][i] == fa) continue; n     int to = G[now][i]; n     DFS2(to, now); n     if (f[to] + 1 > f[now]) { n       f[now] = f[to] + 1; n       son = to; n     } n   } n   for (int i = 0; i < (int)G[now].size(); i++) n     if (G[now][i] != fa && G[now][i] != son) Que.push(f[G[now][i]]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     G[x].push_back(y); n     G[y].push_back(x); n   } n   dep[1] = 1; n   DFS1(1, 0); n   DFS2(root, 0); n   Que.push(f[root]); n   printf( 1  ); n   for (int i = 2; i <= n; i++) { n     int add = 0; n     if (!Que.empty()) add = Que.top(), Que.pop(); n     final += add; n     printf( %d  , final); n   } n   return 0; n } n


#include <bits/stdc++.h> n int m, n = 0, num[(20)]; n long long f[(20)][(20)], cnt[(20)], ans = 0; n void CountBit(int m); n long long dfs(int bit, int value, bool limit); n void solve(int p, int s, int q, long long tot); n int main() { n   scanf( %d , &m); n   CountBit(m); n   memset(f, 0xff, sizeof(f)); n   for (int i = 0; i <= n; i++) cnt[i] = dfs(n, i, true); n   cnt[0]--; n   for (int i = 1; i <= n; i++) solve(0, 1, i, cnt[i]); n   printf( n        %I64d  n         n , n       ans); n   return 0; n } n void solve(int p, int s, int q, long long tot) { n   if (p >= q) return; n   if (s == 7) { n     if (q > p) ans += tot, ans %= (1000000007LL); n     return; n   } n   for (int i = 0; i < q; i++) { n     if (cnt[i]) { n       cnt[i]--; n       solve(p + i, s + 1, q, tot * (cnt[i] + 1) % (1000000007LL)); n       cnt[i]++; n     } n   } n } n long long dfs(int bit, int value, bool limit) { n   long long ret = 0, &save = f[bit][value]; n   if (!bit) { n     if (!value) return save = 1; n     return 0; n   } n   if (!limit && (~save)) return save; n   int border = limit ? num[bit] : 9; n   for (int i = 0; i <= border; i++) n     ret += dfs(bit - 1, value - (i == 4 || i == 7), limit && i == border); n   if (!limit) save = ret; n   return ret; n } n void CountBit(int m) { n   while (m) num[++n] = m % 10, m /= 10; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, a[] = {3, 15, 81, 6723, 50625, 2562991875LL}; n int sg[] = {0, 1, 2, 0, 3, 1, 2}, n, ans = 0; n int main() { n   cin >> n; n   while (n--) { n     cin >> x; n     ans ^= sg[lower_bound(a, a + 6, x) - a]; n   } n   cout << (ans ?  Furlo  :  Rublo ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   scanf( %d , &q); n   map<long long, long long> road; n   for (int i = 0; i < q; i++) { n     int s; n     scanf( %d , &s); n     if (s == 1) { n       long long u, v, w; n       scanf( %I64d %I64d %I64d , &u, &v, &w); n       while (u != v) { n         if (u > v) { n           road[u] += w; n           u /= 2; n         } else { n           road[v] += w; n           v /= 2; n         } n       } n     } else if (s == 2) { n       long long u, v; n       scanf( %I64d %I64d , &u, &v); n       long long result = 0; n       while (u != v) { n         if (u > v) { n           result += road[u]; n           u /= 2; n         } else { n           result += road[v]; n           v /= 2; n         } n       } n       printf( %I64d n , result); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 501; n long long matrix[maxn][maxn]; n long long sum_col[maxn]; n long long sum_row[maxn]; n long long sum_d1 = 0; n long long sum_d2 = 0; n int pos_col = 0; n int pos_row = 0; n int main() { n   int n, sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int tmp; n       cin >> tmp; n       if (tmp == 0) { n         pos_col = j; n         pos_row = i; n       } n       if (i == j) sum_d1 += tmp; n       if (i == n - j - 1) sum_d2 += tmp; n       sum_row[i] += tmp; n       sum_col[j] += tmp; n     } n   } n   if (n == 1) { n     cout << 1; n     return 0; n   } n   bool ans = true; n   int cor = 0; n   if (pos_col == cor) cor = 1; n   long long num = sum_col[cor] - sum_col[pos_col]; n   if (num <= 0) ans = false; n   sum_col[pos_col] += num; n   sum_row[pos_row] += num; n   if (pos_col == pos_row) sum_d1 += num; n   if (pos_row == n - pos_col - 1) sum_d2 += num; n   for (int i = 0; i < n; i++) { n     if (sum_col[i] != sum_col[cor]) ans = false; n     if (sum_row[i] != sum_col[cor]) ans = false; n   } n   if (sum_d1 != sum_col[cor]) ans = false; n   if (sum_d2 != sum_col[cor]) ans = false; n   if (ans) n     cout << num; n   else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n long long int n, remm[200010], actual_pos[200010]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   clock_t clk = clock(); n   int q; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> remm[i], actual_pos[i] = remm[i]; n   remm[n + 1] = 1e15; n   cin >> q; n   set<int> stations; n   for (int i = 1; i <= n + 1; i++) stations.insert(i); n   while (q--) { n     int type; n     cin >> type; n     if (type == 1) { n       int vess, pour; n       cin >> vess >> pour; n       while (pour != 0) { n         int go_to = *(stations.lower_bound(vess)); n         if (remm[go_to] > pour) { n           remm[go_to] -= pour; n           pour = 0; n         } else { n           pour -= remm[go_to]; n           remm[go_to] = 0; n           stations.erase(go_to); n         } n       } n     } else { n       int ves; n       cin >> ves; n       cout << actual_pos[ves] - remm[ves] << endl; n     } n   } n   cerr << endl n        << setprecision(10) << fixed << (double)(clock() - clk) / CLOCKS_PER_SEC; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> t[100000]; n int n; n int h[100000]; n int arr[100000]; n double fac[100001]; n bool cmp(int a, int b) { return h[a] < h[b]; } n void dfs(int cur, int he, int par = -1) { n   h[cur] = he; n   for (int i = 0; i < t[cur].size(); i++) { n     if (t[cur][i] == par) continue; n     dfs(t[cur][i], he + 1, cur); n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     t[a - 1].push_back(b - 1); n     t[b - 1].push_back(a - 1); n   } n   dfs(0, 1); n   for (int i = 0; i < n; i++) arr[i] = i; n   sort(arr, arr + n, cmp); n   double ans = 1; n   fac[0] = 1; n   fac[1] = 1; n   for (int i = 2; i <= n; i++) fac[i] = 1.0 / i; n   for (int i = 1; i < n; i++) { n     ans = (ans + 1) * fac[h[i]] + ans * (1 - fac[h[i]]); n   } n   printf( %.9lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void FAST_IO() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n template <class F, class S> n ostream& operator<<(ostream& os, pair<F, S>& p) { n   os <<  (  << p.first <<  ,  << p.second <<  ) ; n   return os; n } n template <class T, class S> n ostream& operator<<(ostream& os, map<T, S>& v) { n   os <<  { ; n   for (auto& i : v) cout <<  {  << i.first <<  ,  << i.second <<  } ; n   os <<  } ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, set<T>& v) { n   os <<  { ; n   for (auto& i : v) cout << i <<  , ; n   os <<  } ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, vector<T>& v) { n   os <<  [ ; n   if (v.size()) os << v[0]; n   for (int i = 1; i < v.size(); ++i) os <<  ,  << v[i]; n   os <<  ] ; n   return os; n } n int main() { n   int n; n   vector<bool> in(1002); n   while (cin >> n) { n     in[n] = true; n     getchar(); n   } n   int first = true; n   for (int i = 0; i < in.size(); ++i) { n     if (!in[i]) continue; n     int begin = i; n     while (in[i]) i++; n     if (!first) printf( , ); n     if (i - begin > 1) n       printf( %d-%d , begin, i - 1); n     else n       printf( %d , i - 1); n     first = false; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<long long> m(n); n   for (int i = 0; i < n; i++) cin >> m[i]; n   long long sum = 0; n   for (int i = 0; i < n; i++) sum += m[i]; n   sum += n; n   vector<long long> a(n + 1); n   for (int i = n - 1; i >= 0; i--) a[i] = max(a[i + 1] - 1, m[i] + 1); n   for (int i = 1; i < n; i++) a[i] = max(a[i], a[i - 1]); n   for (int i = 0; i < n; i++) sum -= a[i]; n   cout << -sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool chmax(T& a, const T& b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T& a, const T& b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n struct Edge { n   int from, to; n   Edge(){}; n   Edge(int from, int to) : from(from), to(to) {} n   void scanFrom(std::istream& strm, int base = 1) { n     strm >> from >> to; n     if (base == 1) --from, --to; n   } n   Edge reverse() const { return Edge(to, from); } n }; n inline std::istream& operator>>(std::istream& strm, Edge& t) { n   t.scanFrom(strm); n   return strm; n } n template <typename T> n struct WeightedEdge { n   int from, to; n   T _weight; n   WeightedEdge(){}; n   WeightedEdge(int from, int to, T weight) n       : from(from), to(to), _weight(weight) {} n   void scanFrom(std::istream& strm, int base = 1) { n     strm >> from >> to >> _weight; n     if (base == 1) --from, --to; n   } n   T weight() const { return _weight; } n   WeightedEdge reverse() const { return WeightedEdge(to, from, _weight); } n }; n template <typename T> n inline std::istream& operator>>(std::istream& strm, WeightedEdge<T>& t) { n   t.scanFrom(strm); n   return strm; n } n template <typename Edge> n class Digraph { n   int _E, _V; n   vector<vector<Edge>> _adjacencyList; n  n  public: n   int E() const { return _E; } n   int V() const { return _V; } n   int size() const { return _V; } n   Digraph() : _V(0), _E(0){}; n   Digraph(int V, int E = 0) : _V(V), _E(E) { _adjacencyList.resize(V); } n   void addEdge(const Edge& e) { n     _adjacencyList[e.from].push_back(e); n     ++_E; n   } n   void addUndirectedEdge(const Edge& e) { n     _adjacencyList[e.from].push_back(e); n     _adjacencyList[e.to].push_back(e.reverse()); n     ++_E; n   } n   void addNode() { n     ++_V; n     _adjacencyList.push_back(vector<Edge>()); n   } n   void scanFrom(std::istream& strm, int base = 1) { n     if (_V == 0) strm >> _V; n     if (_E == 0) strm >> _E; n     _adjacencyList.resize(_V); n     for (int i = 0; i < _E; ++i) { n       Edge e; n       e.scanFrom(cin, base); n       assert(0 <= e.from < _V); n       assert(0 <= e.to < _V); n       addEdge(e); n     } n   } n   vector<Edge> operator[](int u) const { return _adjacencyList[u]; } n   vector<vector<Edge>>& adjacencyList() { return _adjacencyList; } n }; n template <typename Edge> n inline std::istream& operator>>(std::istream& strm, Digraph<Edge>& t) { n   t.scanFrom(strm); n   return strm; n } n int main() { n   int V, E; n   cin >> V >> E; n   Digraph<Edge> G(V, E); n   for (int i = 0; i < (int)(E); ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     --u, --v; n     G.addUndirectedEdge(Edge(u, v)); n   } n   int sq = ceil(sqrt(V)); n   vector<int> depth(V, -1); n   vector<bool> independent(V, false); n   function<void(int, int)> printCycle = [&](int s, int u) { n     printf( %d  , u + 1); n     if (u == s) return; n     for (Edge& e : G[u]) { n       if (depth[e.to] == depth[u] - 1) printCycle(s, e.to); n     } n   }; n   function<void(int, int)> dfs = [&](int u, int d) { n     depth[u] = d; n     bool ind = true; n     int mind = d, anc = u; n     for (Edge& e : G[u]) { n       if (depth[e.to] == -1) dfs(e.to, d + 1); n       if (depth[e.to] < depth[u]) { n         if (chmin(mind, depth[e.to])) anc = e.to; n         continue; n       } n       ind &= !independent[e.to]; n     } n     if (d - mind >= sq - 1) { n       cout << 2 << endl; n       cout << d - mind + 1 << endl; n       printCycle(anc, u); n       exit(0); n     } n     independent[u] = ind; n   }; n   dfs(0, 0); n   cout << 1 << endl; n   int c = 0; n   for (int u = 0; u < (int)(V); ++u) { n     if (c < sq && independent[u]) { n       printf( %d  , u + 1); n       ++c; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class R { n  public: n   int l; n   int r; n   int t; n   int b; n   R(int l, int r, int t, int b) { n     this->l = l; n     this->r = r; n     this->b = b; n     this->t = t; n   } n }; n int ask(int x1, int x2, int y1, int y2) { n   cout <<  ?   << x1 <<     << y1 <<     << x2 <<     << y2 << endl; n   fflush(stdout); n   int ans; n   cin >> ans; n   return ans; n } n int askR(R r) { n   cout <<  ?   << r.l <<     << r.t <<     << r.r <<     << r.b << endl; n   fflush(stdout); n   int ans; n   cin >> ans; n   return ans; n } n bool check(R other, R border) { n   if (other.t >= border.t && other.l >= border.l && other.r <= border.r && n       other.b <= border.b) n     return true; n   return false; n } n void solveBot(R& r, R other) { n   int left = r.t; n   int right = r.b; n   while (right - left > 1) { n     int m = (right + left) / 2; n     int a = ask(r.l, r.r, r.t, m); n     if (a == 2 || (a == 1 && !check(other, R(r.l, r.r, r.t, m)))) { n       right = m; n       continue; n     } n     left = m; n   } n   int a = ask(r.l, r.r, r.t, left); n   if (a == 2 || (a == 1 && !check(other, R(r.l, r.r, r.t, left)))) { n     R rr = R(r.l, r.r, r.t, left); n     r = rr; n     return; n   } n   R rr = R(r.l, r.r, r.t, right); n   r = rr; n } n void solveRight(R& r, R other) { n   int left = r.l; n   int right = r.r; n   while (right - left > 1) { n     int m = (right + left) / 2; n     int a = ask(r.l, m, r.t, r.b); n     if (a == 2 || (a == 1 && !check(other, R(r.l, m, r.t, r.b)))) { n       right = m; n       continue; n     } n     left = m; n   } n   int a = ask(r.l, left, r.t, r.b); n   if (a == 2 || (a == 1 && !check(other, R(r.l, left, r.t, r.b)))) { n     R rr = R(r.l, left, r.t, r.b); n     r = rr; n     return; n   } n   R rr = R(r.l, right, r.t, r.b); n   r = rr; n } n void solveLeft(R& r, R other) { n   int left = r.l; n   int right = r.r; n   while (right - left > 1) { n     int m = (right + left) / 2; n     int a = ask(m, r.r, r.t, r.b); n     if (a == 2 || (a == 1 && !check(other, R(m, r.r, r.t, r.b)))) { n       left = m; n       continue; n     } n     right = m; n   } n   int a = ask(right, r.r, r.t, r.b); n   if (a == 2 || (a == 1 && !check(other, R(right, r.r, r.t, r.b)))) { n     R rr = R(right, r.r, r.t, r.b); n     r = rr; n     return; n   } n   R rr = R(left, r.r, r.t, r.b); n   r = rr; n } n void solveTop(R& r, R other) { n   int left = r.t; n   int right = r.b; n   while (right - left > 1) { n     int m = (right + left) / 2; n     int a = ask(r.l, r.r, m, r.b); n     if (a == 2 || (a == 1 && !check(other, R(r.l, r.r, m, r.b)))) { n       left = m; n       continue; n     } n     right = m; n   } n   int a = ask(r.l, r.r, right, r.b); n   if (a == 2 || (a == 1 && !check(other, R(r.l, r.r, right, r.b)))) { n     R rr = R(r.l, r.r, right, r.b); n     r = rr; n     return; n   } n   R rr = R(r.l, r.r, left, r.b); n   r = rr; n } n void solve(int n) { n   R r = R(1, n, 1, n); n   R fiks = R(-1, -1, -1, -1); n   solveBot(r, fiks); n   solveLeft(r, fiks); n   solveRight(r, fiks); n   solveTop(r, fiks); n   R s = R(1, n, 1, n); n   solveBot(s, r); n   solveLeft(s, r); n   solveRight(s, r); n   solveTop(s, r); n   cout <<  !   << r.l <<     << r.t <<     << r.r <<     << r.b; n   cout <<     << s.l <<     << s.t <<     << s.r <<     << s.b; n } n int main() { n   int n; n   cin >> n; n   solve(n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n int lca[400100][20]; n int len[400100]; n long long s[400100][20]; n int w[400100]; n int main() { n   ios::sync_with_stdio(false); n   int Q; n   cin >> Q; n   int cnt = 0; n   int last = 0; n   len[0] = 1; n   for (long long q = 0; q < (long long)(Q); ++q) { n     int t, x; n     long long y; n     cin >> t >> x >> y; n     x ^= last; n     --x; n     y ^= last; n     if (t == 1) { n       ++cnt; n       w[cnt] = y; n       for (int i = 20 - 1; i >= 0; --i) { n         if (w[lca[x][i]] < y) { n           x = lca[x][i]; n         } n       } n       if (w[x] < y) { n         x = lca[x][0]; n       } n       if (w[x] < y) { n         x = cnt; n       } n       lca[cnt][0] = x; n       len[cnt] = len[x] + 1; n       s[cnt][0] = y; n       for (long long i = 0; i < (long long)(20 - 1); ++i) { n         lca[cnt][i + 1] = lca[lca[cnt][i]][i]; n         s[cnt][i + 1] = s[cnt][i] + s[lca[cnt][i]][i]; n       } n     } else { n       last = 0; n       int upp = len[x]; n       for (int i = 20 - 1; i >= 0; --i) { n         if (s[x][i] <= y) { n           y -= s[x][i]; n           x = lca[x][i]; n           last += 1 << i; n         } n       } n       last = min(last, upp); n       cout << last << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> val(100000 + 5); n struct Gaussian { n   long long no_of_bits = 20; n   vector<long long> v; n   long long set, origsize = 0, redsize = 0; n   void push(long long val) { n     origsize++; n     if (val) v.push_back(val); n   } n   void clear() { n     v.clear(); n     set = 0, redsize = 0; n   } n   void eliminate() { n     set = redsize = 0; n     for (long long bit = 0; bit <= no_of_bits; bit++) { n       bool check = false; n       for (long long i = redsize; i < v.size(); i++) { n         if ((v[i] >> bit) & 1) { n           swap(v[i], v[redsize]); n           check = true; n           break; n         } n       } n       if (check) { n         for (long long i = redsize + 1; i < v.size(); i++) { n           if ((v[i] >> bit) & 1) v[i] ^= v[redsize]; n         } n         redsize++; n       } n     } n     v.resize(redsize); n     for (auto it : v) set |= it; n   } n   Gaussian& operator=(Gaussian& orig) { n     v = orig.v; n     set = orig.set; n     redsize = orig.redsize; n     origsize = orig.origsize; n     return *this; n   } n }; n long long modexpo(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b % 2) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b = b / 2; n   } n   return res; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, q; n   cin >> n >> q; n   Gaussian arr[n]; n   val.resize(n); n   for (long long i = 0; i < n; i++) cin >> val[i]; n   arr[0].push(val[0]); n   arr[0].eliminate(); n   for (long long i = 1; i < n; i++) { n     arr[i] = arr[i - 1]; n     arr[i].push(val[i]); n     arr[i].eliminate(); n   } n   while (q--) { n     long long l, x; n     cin >> l >> x; n     Gaussian temp = arr[--l]; n     temp.push(x); n     temp.eliminate(); n     long long ans = 0; n     if (temp.redsize == arr[l].redsize) n       ans = modexpo(2, arr[l].origsize - arr[l].redsize); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod1 = 19260817; n const long long mod2 = 19660813; n int n; n int m; n char s[200005]; n char opt[5]; n struct { n   int tree[200005]; n   int lowbit(int x) { return x & -x; } n   void ins(int pos, int val) { n     for (; pos <= n; pos += lowbit(pos)) tree[pos] += val; n   } n   int query(int pos) { n     int sum = 0; n     for (; pos; pos -= lowbit(pos)) sum += tree[pos]; n     return sum; n   } n } t[125]; n int find(int x) { n   int ret = 0; n   for (int i = 20; i >= 0; --i) { n     if (ret + (1 << i) > n) continue; n     int sum = 0; n     for (int j = 48; j < 125; ++j) sum += t[j].tree[ret + (1 << i)]; n     if (x >= sum) { n       x -= sum; n       ret += (1 << i); n     } n   } n   return ret; n } n set<int> book[125]; n set<int>::iterator it; n vector<int> memo; n bool check(int pos) { n   if (pos == 1) { n     if (t[s[pos]].query(pos)) return true; n   } else { n     if (t[s[pos]].query(pos) - t[s[pos]].query(pos - 1)) return true; n   } n   return false; n } n int main() { n   scanf( %d %d , &n, &m); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; ++i) { n     t[s[i]].ins(i, 1); n     book[s[i]].insert(i); n   } n   while (m--) { n     int l; n     int r; n     scanf( %d %d %s , &l, &r, opt + 1); n     int lcur = find(l - 1) + 1; n     int rcur = find(r - 1) + 1; n     it = book[opt[1]].lower_bound(lcur); n     memo.clear(); n     while (it != book[opt[1]].end()) { n       int pos = *it; n       if (pos > rcur) break; n       memo.push_back(pos); n       ++it; n     } n     for (auto p : memo) book[opt[1]].erase(p); n     for (auto p : memo) t[opt[1]].ins(p, -1); n   } n   for (int i = 1; i <= n; ++i) { n     if (check(i)) printf( %c , s[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, k, m, maxa, minb, tem; n int main() { n   cin >> a >> b >> k >> m; n   for (int i = 0; i < a; i++) { n     cin >> tem; n     if (i == k - 1) maxa = tem; n   } n   for (int i = 0; i < b; i++) { n     cin >> tem; n     if (i == b - m) minb = tem; n   } n   cout << (maxa < minb ?  YES  :  NO ) << endl; 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   long long w, h, u1, d1, u2, d2; n   cin >> w >> h >> u1 >> d1 >> u2 >> d2; n   for (int i = h; i >= 0; i--) { n     w += i; n     if (i == d1) w -= u1; n     if (i == d2) w -= u2; n     w = max(w, 0LL); n   } n   cout << w << endl; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_set<int> S; n int main() { n   int n; n   cin >> n; n   int A[n], dp[n + 1]; n   dp[0] = 0; n   S.clear(); n   for (int _n{n}, i{0}; i < _n; ++i) { n     cin >> A[i]; n     dp[i + 1] = dp[i]; n     if (!S.count(A[i])) { n       S.insert(A[i]); n       dp[i + 1]++; n     } n   } n   S.clear(); n   uint64_t ans = 0; n   for (int _n{n + 1}, i{1}; i < _n; ++i) { n     if (!S.count(A[n - i])) { n       ans += dp[n - i]; n       S.insert(A[n - i]); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(long long a, long long b) { return a > b; } n std::vector<long long> fac(500000); n void pre() { n   fac[0] = 1; n   for (int i = 1; i < 500000; i++) { n     fac[i] = i * fac[i - 1]; n     fac[i] %= 998244353; n   } n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long modInverse(long long n, long long p) { return power(n, p - 2, p); } n long long nCrModPFermat(long long n, long long r) { n   if (n <= 0 || n < r) return 0; n   if (r == 0) return 1; n   long long a = (fac[n] * modInverse(fac[r], 998244353) % 998244353 * n                  modInverse(fac[n - r], 998244353) % 998244353) % n                 998244353; n   return a; n } n void solve() { n   long long k, x, y, n; n   cin >> n >> k; n   std::vector<pair<long long, long long>> ar; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     long long l, r; n     cin >> l >> r; n     ar.push_back(make_pair(l, r)); n   } n   if (k == 1) { n     cout << n <<   n ; n     return; n   } n   sort(ar.begin(), ar.end()); n   multiset<long long> q; n   long long cnt; n   for (int i = 0; i < n; i++) { n     while (!q.empty() && *q.begin() < ar[i].first) { n       q.erase(q.begin()); n     } n     cnt = q.size(); n     ans += (nCrModPFermat(cnt, k - 1)); n     ans %= 998244353; n     q.insert(ar[i].second); n   } n   cout << ans <<   n ; n } n int main() { n   pre(); n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mx = 2e3 + 1, Mod = 1e9 + 7; n long long int k, Pa, Pb; n int pw(long long int a, int b) { n   long long int ans = 1; n   while (b) { n     if (b % 2) ans = (ans * a) % Mod; n     a = (a * a) % Mod; n     b /= 2; n   } n   return ans; n } n long long int dp[mx][mx]; n long long int dpp(int i, int j) { n   if (j >= k) return j; n   return dp[i][j]; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> k >> Pa >> Pb; n   long long int divm = pw(Pa + Pb, Mod - 2); n   long long int divb = (Pa * pw(Pb, Mod - 2)) % Mod; n   for (int i = k; i >= 0; i--) n     for (int j = k - 1; j >= 0; j--) { n       if (i == k) { n         dp[i][j] = (i + j + divb) % Mod; n         continue; n       } n       dp[i][j] = n           (((Pa * dpp(i + 1, j) + Pb * dpp(i, i + j)) % Mod) * divm) % Mod; n     } n   return cout << dp[1][0], 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[2000 + 5][2000 + 5]; n int v, res, n, m, k, cnt; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   bool flag = 0; n   if (k == 1) flag = 1; n   for (int i = 1; i <= n; i++) scanf( %s , str[i]); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < m; j++) { n       if (str[i][j] ==  * ) { n         if (v >= k) res += v - k + 1; n         v = 0; n       } else n         v++, cnt++; n     } n     if (v >= k) res += v - k + 1; n     v = 0; n   } n   if (flag == 1) { n     printf( %d , cnt); n     return 0; n   } n   for (int i = 0; i < m; i++) { n     for (int j = 1; j <= n; j++) { n       if (str[j][i] ==  * ) { n         if (v >= k) res += v - k + 1; n         v = 0; n       } else n         v++; n     } n     if (v >= k) res += v - k + 1; n     v = 0; n   } n   printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n #define ll long long int n #define mp make_pair n #define pb push_back n #define pf push_front n #define F first n #define S second n #define MOD 1000000007 n #define MAXN   1000001 n #define MODA 998244353 n #define all(a)      (a).begin(),(a).end() n #define allr(a)      (a).rbegin(),(a).rend() n  n ll power(ll a,ll b){ n     ll result=1; n     while(b>0){ n         int lastbit=(b&1); n         if(lastbit){ n             result=(result*a); n         } n         a=(a*a); n         b=b>>1; n     } n     return result; n } n  n ll power2(ll a,ll b,ll p){ n     ll result=1; n     while(b>0){ n         int lastbit=(b&1); n         if(lastbit){ n             result=(result*a)%p; n         } n         a=(a*a)%p; n         b=b>>1; n     } n     return result%p; n } n  n ll gcd(ll a, ll b){ n     if (b == 0) n         return a; n     return gcd(b, a % b); n } n  n vector<ll> Sieve(int n){ n     bool prime[n+1]; n     memset(prime, true, sizeof(prime)); n     for (int p=2; p*p<=n; p++){ n         if (prime[p] == true){ n             for (int i=p*p; i<=n; i += p) n                 prime[i] = false; n         } n     } n     vector<ll>v; n     for (int p=2; p<=n; p++) n         if (prime[p]) v.pb(p); n     return v; n } n  n vector<ll> SeiveOfEratosthenes(int n){ n     vector<ll>v(n+1,0); n     for(int i=0;i<=n;i++){ n         v[i]=i; n     } n  n     for (int p=2; (p*p)<=n; p++){ n         if (v[p] == p){ n             v[p]=p; n             for (int i=p*p; i<=n; i += p) { n                 v[i] = p; n             } n         } n     } n     return v; n } n  n vector<ll> SmallestPrimeFactors(int n){ n     vector<ll>spf(n+1,0); n     spf[1] = 1; n     for (int i=2; i<=n; i++){ n         spf[i] = i; n     } n     for (int i=4; i<=n; i+=2){ n         spf[i] = 2; n     } n     for (int i=3; (i*i)<=n; i++){ n         if (spf[i] == i){ n             for (int j=i*i; j<MAXN; j+=i) { n                 if (spf[j] == j){ n                     spf[j] = i; n                 } n             } n         } n     } n     return spf; n } n  n void shortest(map<int,vector<pair<int,ll>>>m,int src,int n,ll *dist){ n     bool visited[n+1]; n     set<pair<ll,int>>s; n     for(int i=0;i<=n;i++){ n         dist[i]=-1; n         visited[i]=false; n     } n  n     s.insert(mp(0ll,src)); n     dist[src]=0; n     while(!s.empty()){ n         auto t=s.begin(); n         ll dis=(*t).F; n         int u=(*t).S; n         s.erase(t); n         if(visited[u]){ n             continue; n         } n         visited[u]=true; n         for(auto tt:m[u]){ n             int v=tt.F; n             ll w=tt.S; n             if(!visited[v]){ n                 if(dist[v]!=-1 && dist[v]>dis+w){ n                     s.erase(mp(dist[v],v)); n                     dist[v]=dis+w; n                     s.insert(mp(dist[v],v)); n                 }else if(dist[v]==-1){ n                     dist[v]=dis+w; n                     s.insert(mp(dist[v],v)); n                 } n             } n         } n     } n } n  n int main(){ n  n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n  n     int tttttt=1; n     cin >> tttttt; n     for(int ttttt=1; ttttt <= tttttt; ttttt++){ n         ll n,l,r,s; n         cin>>n>>l>>r>>s; n         ll len=(r-l+1); n         vector<int>v; n         for(int i=0;i<len;i++){ n             v.pb(i+1); n             s-=(i+1); n         } n         if(s<0){ n             cout<<-1; n         }else{ n             ll t=n-len; n             int x=0; n  n             for(int i=1;i<=t;i++){ n                 if(s>=len){ n                     s-=len; n                     x++; n                 }else{ n                     for(int j=v.size()-1;j>=0 && s>0 ;j--){ n                         v[j]+=1; n                         s--; n                     } n                     break; n                 } n                 if(s==0){ n                     break; n                 } n             } n  n             for(int j=0;j<v.size();j++){ n                 v[j]+=x; n             } n  n             if(s!=0){ n                 cout<<-1; n             }else{ n  n                 int a[n]; n                 int j=0; n                 int st[n+1]; n  n                 for(int i=1;i<=n;i++){ n                     st[i]=0; n                 } n                 for(int i=l-1;i<r;i++){ n                     a[i]=v[j]; n                     st[v[j]]=-1; n                     j++; n                 } n  n                 j=1; n                 for(int i=0;i<(l-1);){ n                     if(st[j]==0){ n                         a[i]=j; n                         j++; n                         i++; n                     }else{ n                         j++; n                     } n                 } n                 for(int i=r;i<(n);){ n                     if(st[j]==0){ n                         a[i]=j; n                         j++; n                         i++; n                     }else{ n                         j++; n                     } n                 } n                 for(int i=0;i<n;i++){ n                     cout<<a[i]<<   ; n                 } n             } n         } n         cout<<endl; n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int arr[101], ans = 0, temp = 0; n   int i, j, n; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %I64d , &arr[i]); n   for (i = 0; i < n; i++) { n     for (j = i; j < n; j++) { n       temp = temp ^ arr[j]; n       ans = max(ans, temp); n     } n     temp = 0; n   } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2020; n int n, m, i, j, cnt[N]; n char grid[N][N]; n bool check(int s) { n   for (int i = 0; i < m; ++i) n     if (grid[s][i] ==  1  && cnt[i] <= 1) return 0; n   return 1; n } n int main() { n   cin >> n >> m; n   for (i = 0; i < n; ++i) { n     scanf( %s , grid + i); n     for (j = 0; j < m; ++j) { n       cnt[j] += (grid[i][j] ==  1 ); n     } n   } n   for (i = 0; i < n; ++i) { n     if (check(i)) { n       return cout <<  YES , 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include<string.h> n #include<bits/stdc++.h> n using namespace std; n int main() n { n  int t; n  cin>>t; n  //string a,b; n  while(t--) n  { n   string a,b; n   cin>>a; n   int n,m,g,maxx; n   cin>>b; n   string l,k; n   int len=a.size(),len1=b.size(); n   for(int i=1;i<=len;i++) n   { n    int r=0; n    for(int j=0;j<(len-i);) n    { n     if(a.substr(j,i)!=a.substr(j+i,i)) n     { n      r=1; n      break; n     } n     else j+=i; n    } n    if(!r)  n    { n    l=a.substr(0,i); n    break; n   } n  } n   for(int i=1;i<=len1;i++) n   { n    int r=0; n    for(int j=0;j<(len1-i);) n    { n     if(b.substr(j,i)!=b.substr(j+i,i)) n     { n      r=1; n      break; n     } n     else j+=i; n    } n    if(!r) n    { n      k=b.substr(0,i); n      break;} n   } n   int s,s1; n   s=l.size(); n   s1=k.size(); n   n=len/s,m=len1/s1; n   maxx=min(n,m); n   for(int i=maxx;i>=1;i--) n   { n    if(n%i==0&&m%i==0) n    { n     g=i; n     break; n    } n   } n   //cout<<l<<   <<k; n   if(l==k)  n   { n    for(int i=0;i<n*m/g;i++)  cout<<l; n    cout<<endl; n   } n   else cout<< -1 <<endl; n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 100 * 1000; n int ok[MaxN]; n int bad[MaxN]; n char TYPE[MaxN]; n int QUERY[MaxN]; n bool crime[MaxN]; n int main() { n   int n, m; n   scanf( %d%d n , &n, &m); n   int sumWrong = 0; n   for (int i = 0; i < n; i++) { n     char type; n     int x; n     scanf( %c%d , &type, &x); n     if (i != n - 1) scanf(  n ); n     TYPE[i] = type; n     QUERY[i] = x - 1; n     if (type ==  + ) { n       bad[x - 1]++; n     } else { n       ok[x - 1]++; n       sumWrong++; n     } n   } n   int sz = 0; n   for (int i = 0; i < n; i++) { n     int right = bad[i] + sumWrong - ok[i]; n     if (right == m) { n       crime[i] = true; n       sz++; n     } n   } n   for (int i = 0; i < n; i++) { n     if (TYPE[i] ==  - ) { n       if (!crime[QUERY[i]]) printf( Truth n ); n       if (crime[QUERY[i]] && sz > 1) printf( Not defined n ); n       if (crime[QUERY[i]] && sz == 1) printf( Lie n ); n     } else { n       if (!crime[QUERY[i]]) printf( Lie n ); n       if (crime[QUERY[i]] && sz > 1) printf( Not defined n ); n       if (crime[QUERY[i]] && sz == 1) printf( Truth n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   cin.sync_with_stdio(false); n   long long k[3]; n   int t[3]; n   int n, i, j; n   for (i = 0; i < 3; i++) cin >> k[i]; n   for (i = 0; i < 3; i++) cin >> t[i]; n   cin >> n; n   vector<long long> a(n); n   vector<long long> b(n); n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   for (j = 0; j < 3; j++) n     if (k[j] > n) k[j] = n; n   for (j = 0; j < 3; j++) { n     priority_queue<long long, vector<long long>, greater<long long> > Q; n     for (i = 0; i < k[j]; i++) { n       Q.push(0); n     } n     for (i = 0; i < n; i++) { n       long long p = Q.top(); n       Q.pop(); n       if (p < b[i]) p = b[i]; n       b[i] = p + t[j]; n       Q.push(b[i]); n     } n   } n   long long ans = 0; n   for (i = 0; i < n; i++) { n     if (b[i] - a[i] > ans) ans = b[i] - a[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define pb push_back n #define reg register n #define int long long n #define For(i,a,b) for(reg int i=a;i<=b;++i) n #define Down(i,a,b) for(reg int i=a;i>=b;--i)  n #define ull unsigned long long n #define rep(i,a,b) for(reg int i=a;i<=b;++i) n #define ll long long n inline int max(int x,int y){return x>y?x:y;} n inline int min(int x,int y){return x<y?x:y;} n inline void ckmin(int &x,int y){x=x<y?x:y; return ;} n inline void ckmax(int &x,int y){x=x>y?x:y; return ;} n inline int gcd(int n,int m){return m?gcd(m,n%m):n;} n inline int lcm(int x,int y){return x/gcd(x,y)*y;} n inline void swap(int &x,int &y){int t=x; x=y; y=t; return ;} n namespace yspm{ n  inline int read(){ n      int res=0,f=1; char k; n      while(!isdigit(k=getchar())) if(k== - ) f=-1; n      while(isdigit(k)) res=res*10+k- 0 ,k=getchar(); n      return res*f; n     } n     char out[100]; n     inline void print(int x){ n         if(!x) return putchar( 0 ),putchar(  n ),void(); n         if(x<0) putchar( - ),x=-x;  n         int cnt=0; while(x) out[++cnt]=x%10,x/=10;  n         Down(i,cnt,1) putchar(out[i]+ 0 ); putchar(  n ); return ; n     }  n     const int N=1e5+10,bl=510; n     vector<int>f[N<<2]; n     int n,a[N],p,v; n     inline void push_up(int x){ n         int rpos=0,lsz=f[x<<1].size(),rsz=f[x<<1|1].size(); f[x].clear(); n         for(reg int i=0;i<lsz;){ n             if(rpos==rsz||(f[x<<1][i]<f[x<<1|1][rpos]-i)) f[x].push_back(f[x<<1][i++]); n             else f[x].push_back(f[x<<1|1][rpos++]-i); n         } for(reg int j=rpos;j<rsz;++j) f[x].push_back(f[x<<1|1][j]-lsz); n         return ; n     } n     inline void build(int p,int l,int r){ n         if(l==r) return f[p].resize(1),f[p][0]=a[l],void(); int mid=(l+r)>>1;  n         build(p<<1,l,mid); build(p<<1|1,mid+1,r);  n         if(r-l+1<=bl) push_up(p); return ; n     } n     inline void modify(int x,int l,int r){ n         if(l==r) return f[x][0]=a[l],void(); int mid=(l+r)>>1;  n         if(p<=mid) modify(x<<1,l,mid); else modify(x<<1|1,mid+1,r);  n         if(r-l+1<=bl) push_up(x); return ; n     } n     inline void query(int x,int l,int r){ n         if(l>=p&&r-l+1<=bl) return v+=upper_bound(f[x].begin(),f[x].end(),v)-f[x].begin(),void(); int mid=(l+r)>>1;  n         if(mid>=p) query(x<<1,l,mid); return query(x<<1|1,mid+1,r);  n     } n  signed main(){ n      n=read(); rep(i,1,n) a[i]=i-read(); build(1,1,n);  n      int Q=read(); while(Q--){ n          if(read()==1){ n              int x=read(),y=read();  n              a[x]=x-y; p=x; modify(1,1,n); n          }else{ n              p=read(); if(p==n) print(a[n]);  n              else v=a[p],++p,query(1,1,n),print(v); n          } n      } return 0; n     } n }signed main(){return yspm::main();} n //Use The Time To Enrich This Selfclosing Youth


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, n, m, q, p; n   cin >> q; n   while (q--) { n     cin >> n; n     string s; n     cin >> s; n     p = 0; n     if (s.length() > 2) { n       cout <<  YES n2 n ; n       cout << s[0] <<    ; n       for (i = 1; i < n; i++) cout << s[i]; n       cout << endl; n     } else { n       if (s[0] < s[1]) n         cout <<  YES n2 n  << s[0] <<     << s[1] << endl; n       else n         cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 4000; n long long p[MAXN + 10], v[MAXN + 10], d[MAXN + 10]; n bool lft[MAXN + 10]; n int n; n void print(int n) { n   for (int i = 0; i < n; i++) cout << p[i] <<    ; n   cout << endl; n   for (int i = 0; i < n; i++) cout << lft[i] <<    ; n   cout << endl; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> v[i] >> d[i] >> p[i]; n   int res = 0; n   for (int i = 0; i < n; i++) { n     if (lft[i]) continue; n     res++; n     long long f = 0, effect = 0; n     for (int j = i + 1; j < n; j++) { n       if (lft[j]) continue; n       p[j] -= max(0ll, v[i] - f++); n       p[j] -= effect; n       if (p[j] < 0) { n         lft[j] = true; n         effect += d[j]; n       } n     } n   } n   cout << res << endl; n   for (int i = 0; i < n; i++) n     if (!lft[i]) cout << i + 1 <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 2; n const long long N1 = 1e2 + 2; n const long long mod = 1e9 + 7; n const int MASK = 1 << 17 + 1; n const int who = 6 * N * log2(N); n long long power(long long a, long long b, long long c) { n   long long ret = 1; n   while (b) { n     if (b % 2 == 1) { n       ret = (ret * a) % c; n     } n     b /= 2; n     a = (a * a) % c; n   } n   return ret; n } n long long n, m, r, val; n long long a[N + 3], b[N + 3]; n map<long long, long long> vis[N + 3]; n vector<double> v; n bool calc(long long x, long long y) { n   if (x < n && y < m && x >= 0 && y >= 0) { n     if (vis[x][y] == 0) { n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m >> r >> val; n   long long tot = n - r + 1; n   for (int i = 0; i < n; i++) { n     int sum = max(0, (int)(n - r - i)) + max(0, (int)(i - r + 1)); n     a[i] = tot - sum; n   } n   tot = m - r + 1; n   for (int i = 0; i < m; i++) { n     int sum = max(0, (int)(m - r - i)) + max(0, (int)(i - r + 1)); n     b[i] = tot - sum; n   } n   priority_queue<pair<double, pair<int, int> > > pq; n   long long x = n / 2, y = m / 2; n   vis[x][y] = 1; n   pq.push(make_pair(a[x] * b[y], make_pair(x, y))); n   while (!pq.empty() && v.size() < val) { n     pair<double, pair<int, int> > tm = pq.top(); n     pq.pop(); n     v.push_back(tm.first); n     long long test; n     if (calc(tm.second.first - 1, tm.second.second + 0)) { n       test = a[(tm.second.first - 1)] * b[(tm.second.second)]; n       vis[tm.second.first - 1][tm.second.second] = 1; n       pq.push( n           make_pair(test, make_pair(tm.second.first - 1, tm.second.second))); n     } n     if (calc(tm.second.first + 0, tm.second.second + 1)) { n       test = a[(tm.second.first)] * b[(tm.second.second + 1)]; n       vis[tm.second.first][tm.second.second + 1] = 1; n       pq.push( n           make_pair(test, make_pair(tm.second.first, tm.second.second + 1))); n     } n     if (calc(tm.second.first + 0, tm.second.second - 1)) { n       vis[tm.second.first][tm.second.second - 1] = 1; n       test = a[(tm.second.first)] * b[(tm.second.second - 1)]; n       pq.push( n           make_pair(test, make_pair(tm.second.first, tm.second.second - 1))); n     } n     if (calc(tm.second.first + 1, tm.second.second + 0)) { n       vis[tm.second.first + 1][tm.second.second] = 1; n       test = a[(tm.second.first + 1)] * b[(tm.second.second)]; n       pq.push( n           make_pair(test, make_pair(tm.second.first + 1, tm.second.second))); n     } n   } n   double res = 0, res1 = (n - r + 1) * (m - r + 1); n   for (auto it : v) { n     res += it; n   } n   printf( %.15lf n , res / res1); n } n


#include <bits/stdc++.h> n using namespace std; n int dr[4][2] = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}; n bool eq(double d1, double d2) { return fabs(d1 - d2) < 1e-7; } n int getState(double dx, double dy) { n   for (int i = 0; i < 4; i++) n     if (!eq(dx, 0) && !eq(dy, 0)) n       if (dx * dr[i][0] > 0 && dy * dr[i][1] > 0) return i; n   if (eq(dx, 0)) { n     if (dy > 0) n       return 1; n     else n       return 3; n   } n   if (eq(dy, 0)) { n     if (dx > 0) n       return 0; n     else n       return 2; n   } n   return 10000; n } n namespace std { n bool operator<(const complex<double>& a, const complex<double>& b) { n   return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); n } n }  // namespace std n double cross(const complex<double>& a, const complex<double>& b) { n   return imag(conj(a) * b); n } n double dot(const complex<double>& a, const complex<double>& b) { n   return real(conj(a) * b); n } n int ccw(complex<double> a, complex<double> b, complex<double> c) { n   b -= a; n   c -= a; n   if (cross(b, c) > 0) return +1; n   if (cross(b, c) < 0) return -1; n   if (dot(b, c) < 0) return +2; n   if (norm(b) < norm(c)) return -2; n   return 0; n } n bool isconvex(const vector<complex<double> >& P) { n   for (int i = 0; i < P.size(); ++i) { n     if (ccw(P[(i + P.size() - 1) % P.size()], P[(i) % P.size()], n             P[(i + 1) % P.size()]) > 0) n       return false; n   } n   return true; n } n vector<complex<double> > convex_hull(vector<complex<double> >& ps) { n   int n = ps.size(), k = 0; n   sort(ps.begin(), ps.end()); n   vector<complex<double> > ch(2 * n); n   for (int i = 0; i < n; ch[k++] = ps[i++]) n     while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; n   for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) n     while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; n   ch.resize(k - 1); n   return ch; n } n vector<complex<double> > poly, temp; n vector<complex<double> > cn; n int n, ptr = 0; n bool eqq(complex<double> p1, complex<double> p2) { n   return eq(p1.imag(), p2.imag()) && eq(p1.real(), p2.real()); n } n int main() { n   cin >> n; n   poly.resize(n); n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     poly[i] = complex<double>(x, y); n   } n   sort(poly.begin(), poly.end()); n   for (int i = 0; i < poly.size(); i++) n     if (i == 0 || poly[i] != poly[i - 1]) temp.push_back(poly[i]); n   poly = temp; n   n = poly.size(); n   if (n == 1) n     cout << 4 << endl; n   else { n     cn = convex_hull(poly); n     n = cn.size(); n     int res = 0, curS = getState(cn[1].real() - cn[0].real(), n                                  cn[1].imag() - cn[0].imag()); n     int fs = curS; n     for (int i = 0; i < cn.size(); i++) { n       int j = (i + 1) % cn.size(); n       int dx = (int)(cn[j].real() - cn[i].real()); n       int dy = (int)(cn[j].imag() - cn[i].imag()); n       int state = getState(dx, dy); n       res += max(abs(dx), abs(dy)); n       while (curS != state) res++, curS = (curS + 1) % 4; n     } n     while (curS != fs) res++, curS = (curS + 1) % 4; n     cout << res << endl; 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     long long int n; n     cin >> n; n     int fl = 0; n     long long int ans = 0; n     if (n == 2) { n       cout << 2 << endl; n       continue; n     } n     ans = pow(2, n); n     for (int i = 1; i < n; i++) { n       if (i < n / 2) n         ans += pow(2, i); n       else n         ans -= pow(2, i); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int ans = 0; n   int a, b; n   cin >> a >> b; n   if (a < b) swap(a, b); n   cout << b <<     << (a - b) / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n #define ll long long n #define db long double n #define x first n #define y second n #define mp make_pair n #define pb push_back n #define all(a) a.begin(), a.end() n  n using namespace std; n  n const int MAGIC = 300; n const int BLOCKS = 160000/MAGIC; n  n vector<int> tree; n vector<int> leave_block; n  n int add[BLOCKS]; n int cnt[BLOCKS]; n  n int n; n  n void recalc(int block) { n   for (int i = block * MAGIC; i < min(n, (block + 1) * MAGIC); ++i) { n     tree[i] = max(0, tree[i] - add[block]); n   } n   add[block] = 0; n  n   for (int i = block * MAGIC; i < min(n, (block + 1) * MAGIC); ++i) { n     int W = tree[i]; n     if (W < block * MAGIC) leave_block[i] = W; n     else leave_block[i] = leave_block[W]; n   } n  n } n  n int layer(int x) { n   return x/MAGIC; n } n  n int go_outer(int x) { n   return max(0, leave_block[x] - add[layer(x)]); n } n  n int go(int x) { n   return max(0, tree[x] - add[layer(x)]); n } n  n int main(){ n #ifdef LOCAL n  freopen( H_input.txt ,  r , stdin); n  //freopen( H_output.txt ,  w , stdout); n #endif n  ios_base::sync_with_stdio(0); n  cin.tie(0); n  n   int q; n   cin >> n >> q; n  n   tree.assign(n, 0); n   for (int i = 1; i < n; ++i) { n     cin >> tree[i]; n     tree[i]--; n   } n  n   leave_block.assign(n, 0); n  n   for (int i = 0; i * MAGIC < n; ++i) { n     recalc(i); n   } n  n   for (int i = 0; i < q; ++i) { n     int tp; n     cin >> tp; n  n     //cout <<   ===   << go_outer(4) << endl; n  n     if (tp == 1) { n       int l, r, x; n       cin >> l >> r >> x; n       l--, r--; n       for (int j = 0; j * MAGIC < n; ++j) { n         int L = j*MAGIC, R = min(n, (j+1)*MAGIC) - 1; n          n         int pl = max(L, l), pr = min(R, r); n         if (pl > pr) continue; n         if (pl != L || pr != R || cnt[j] <= MAGIC) { n           if (pl == L && pr == R) { n             cnt[j]++; n             add[j]+=x; n             add[j] = min(add[j], (int) 1e8); n           } n           else { n             for (int e = pl; e <= pr; ++e) tree[e] = max(0, tree[e] - x); n           } n         //cout <<   ???   << j << endl; n           recalc(j); n           //cout << leave_block[3] <<     << tree[3] <<     << pl <<      << pr  << endl; n         }  n         else { n           add[j]+=x; n           add[j] = min(add[j], (int) 1e8); n         } n  n       } n     } n  n     else { n       int u, v; n       cin >> u >> v; n  n       u--, v--; n       while (true) { n         if (u == v) break; n         //cout << u <<     << v << endl; n         //cout << go_outer(u) <<     << go_outer(v) << endl; n         if (layer(u) < layer(v)) { n           swap(u, v); n         } n         if (layer(u) > layer(v)) { n           u = go_outer(u); n         } n         else { n           int A = go_outer(u), B = go_outer(v); n           if (A!=B) { n             u = A, v = B; n           } n           else { n             while (u != v) { n               if (u > v) u = go(u); n               else v = go(v);  n             } n           } n         } n       } n  n       cout << u+1 <<   n ; n  n  n     } n   } n  n  n } n


#include <bits/stdc++.h> n using namespace std; n int n, t; n int s[100013]; n int g[17][100013]; n int lg[100013]; n vector<pair<int, int> > sorted; n int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } n int find_l(int gcf, int ind) { n   int left = 0, right = sorted.size() - 1; n   while (left < right) { n     int mid = (left + right) / 2; n     if (sorted[mid].first == gcf && sorted[mid].second == ind) { n       break; n     } n     if (sorted[mid].first < gcf || n         (sorted[mid].first == gcf && sorted[mid].second < ind)) { n       left = mid + 1; n     } else { n       right = mid; n     } n   } n   return (left + right) / 2; n } n int find_r(int gcf, int ind) { n   int left = 0, right = sorted.size() - 1; n   while (left < right) { n     int mid = (left + right + 1) / 2; n     if (sorted[mid].first == gcf && sorted[mid].second == ind) { n       break; n     } n     if (sorted[mid].first < gcf || n         (sorted[mid].first == gcf && sorted[mid].second < ind)) { n       left = mid; n     } else { n       right = mid - 1; n     } n   } n   return (left + right + 1) / 2; n } n int query(int l, int r) { n   int tmp = l; n   int gcf = 0; n   for (int i = 16; i >= 0; i--) n     if ((r - l + 1) & (1 << i)) { n       gcf = gcd(gcf, g[i][l]), l = l + (1 << i); n     } n   l = tmp; n   vector<pair<int, int> >::iterator left = n       lower_bound(sorted.begin(), sorted.end(), make_pair(gcf, l)); n   vector<pair<int, int> >::iterator right = n       upper_bound(sorted.begin(), sorted.end(), make_pair(gcf, r)); n   return r - l + 1 - max(0, (int)(right - left)); n } n int main() { n   scanf( %d , &n); n   int l = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &s[i]); n     sorted.push_back(make_pair(s[i], i)); n     if (i == (1 << (l + 1))) l++; n     lg[i] = l; n   } n   sort(sorted.begin(), sorted.end()); n   for (int i = 1; i <= n; i++) { n     g[0][i] = s[i]; n   } n   for (int i = 1; i <= 16; i++) { n     for (int j = 1; j <= n; j++) { n       g[i][j] = gcd(g[i - 1][j], g[i - 1][min(n, j + (1 << (i - 1)))]); n     } n   } n   scanf( %d , &t); n   for (int i = 0; i < t; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     printf( %d n , query(l, r)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using uint = unsigned int; n using ll = long long; n using ld = long double; n using ull = unsigned long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; 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 template <typename T1, typename T2> n ostream &operator<<(ostream &out, const pair<T1, T2> &item) { n   out <<  (  << item.first <<  ,   << item.second <<  ) ; n   return out; n } n template <typename T> n ostream &operator<<(ostream &out, const vector<T> &v) { n   for (const auto &item : v) out << item <<    ; n   return out; n } n const int N = 100010; n const int B = 666013; n const int SZ = 500; n const int NRB = N / SZ + 5; n struct Block { n   int i, l, r; n   Block(int i, int l, int r) : i(i), l(l), r(r) {} n }; n int bid[N], s[N], aux[SZ + 5]; n int nOpen[NRB], nClosed[NRB]; n ull pw[N], open[NRB][SZ + 5], closed[NRB][SZ + 5]; n bool ok[N]; n ull getHash(ull h[], int l, int r) { return h[r] - h[l] * pw[r - l]; } n int genStack(int l, int r) { n   if (l >= N) return -2; n   int top = -1; n   for (int p = l; p < r && s[p]; ++p) { n     if (s[p] > 0) n       aux[++top] = s[p]; n     else { n       if (top == -1 || aux[top] < 0) n         aux[++top] = s[p]; n       else if (aux[top] != -s[p]) n         return -2; n       else n         --top; n     } n   } n   return top; n } n void makeHashes(int i, int top) { n   int p; n   for (p = 0; p <= top && aux[p] < 0; ++p) n     closed[i][p + 1] = closed[i][p] * B - aux[p]; n   nClosed[i] = p; n   nOpen[i] = top + 1 - p; n   reverse(aux + p, aux + p + nOpen[i]); n   for (int j = 0; p + j <= top; ++j) n     open[i][j + 1] = open[i][j] * B + aux[p + j]; n } n void buildBlock(int i) { n   int top = genStack(i * SZ, (i + 1) * SZ); n   if (top == -2) return void(ok[i] = false); n   ok[i] = true; n   makeHashes(i, top); n } n void update(int p, int t) { n   s[p] = t; n   buildBlock(bid[p]); n } n bool process(int i, vector<Block> &v) { n   int a = 0, b = nClosed[i]; n   while (a < b && !v.empty()) { n     auto &lst = v.back(); n     int d = min(b - a, lst.r - lst.l); n     if (lst.r - lst.l <= b - a) { n       if (getHash(open[lst.i], lst.l, lst.r) == getHash(closed[i], a, a + d)) n         a += d, v.pop_back(); n       else n         return false; n     } else { n       if (getHash(open[lst.i], lst.l, lst.l + d) == getHash(closed[i], a, b)) n         lst.l += d, a = b; n       else n         return false; n     } n   } n   if (a < b) return false; n   if (nOpen[i]) v.emplace_back(i, 0, nOpen[i]); n   return true; n } n bool query(int l, int r) { n   if (bid[l] == bid[r - 1]) return genStack(l, r) == -1; n   int lid = NRB - 2, rid = NRB - 1; n   int top = genStack(bid[l] * SZ + l % SZ, (bid[l] + 1) * SZ); n   if (top == -2) return false; n   makeHashes(lid, top); n   top = genStack(bid[r] * SZ, bid[r] * SZ + r % SZ); n   if (top == -2) return false; n   makeHashes(rid, top); n   vector<Block> v; n   if (!process(lid, v)) return false; n   for (int i = bid[l] + 1; i < bid[r]; ++i) { n     if (!ok[i]) return false; n     if (!process(i, v)) return false; n   } n   if (!process(rid, v)) return false; n   return v.empty(); n } n int main() { n   ios_base::sync_with_stdio(false); n   pw[0] = 1; n   for (int i = 1; i < N; ++i) pw[i] = pw[i - 1] * B; n   for (int i = 0; i < N; ++i) bid[i] = i / SZ; n   int n, k, q, type, p, t, l, r; n   cin >> n >> k; n   for (int i = 0; i < n; ++i) cin >> s[i]; n   for (int i = 0; i < NRB; ++i) buildBlock(i); n   for (cin >> q; q; --q) { n     cin >> type; n     if (type == 1) { n       cin >> p >> t; n       update(p - 1, t); n     } else { n       cin >> l >> r; n       cout << (query(l - 1, r) ?  Yes  :  No ) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n using pii = pair<long long, long long>; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long q; n   cin >> q; n   while (q--) { n     long long n, m; n     cin >> n >> m; n     vector<pair<long long, pii>> a; n     long long len = 0; n     for (long long i = 0; i < n; i++) { n       long long x, y, z; n       cin >> x >> y >> z; n       if (i > 0 && x == a[len - 1].first) { n         a[len - 1].second.first = max(a[len - 1].second.first, y); n         a[len - 1].second.second = min(a[len - 1].second.second, z); n       } else { n         a.push_back({x, {y, z}}); n         len++; n       } n     } n     n = len; n     long long low = m, high = m, curt = 0; n     long long pos = 1; n     for (long long i = 0; i < n; i++) { n       long long t = a[i].first, l = a[i].second.first, h = a[i].second.second; n       if (l > h) { n         pos = 0; n         break; n       } n       low -= t - curt; n       high += t - curt; n       if (low > h || high < l) { n         pos = 0; n         break; n       } n       low = max(l, low); n       high = min(h, high); n       curt = t; n     } n     if (pos) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   cin >> n >> x; n   int s = 0; n   for (int i = 0; i < n; ++i) { n     int a; n     cin >> a; n     s += a; n   } n   s += n - 1; n   if (s == x) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> b1, b2; n   int n; n   cin >> n; n   int a[n]; n   long long s1 = 0, s2 = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] > 0) { n       s1 += a[i]; n       b1.push_back(a[i]); n     } else { n       s2 -= a[i]; n       b2.push_back(-a[i]); n     } n   } n   if (s1 > s2) { n     cout <<  first ; n     return 0; n   } n   if (s1 < s2) { n     cout <<  second ; n     return 0; n   } n   for (int i = 0; i < min(b1.size(), b2.size()); i++) { n     if (b1[i] > b2[i]) { n       cout <<  first ; n       return 0; n     } n     if (b1[i] < b2[i]) { n       cout <<  second ; n       return 0; n     } n   } n   if (b1.size() != b2.size()) { n     if (b1.size() > b2.size()) { n       cout <<  first ; n     } else { n       cout <<  second ; n     } n     return 0; n   } n   for (int i = 0; i < b1.size(); i++) { n     if (b1[i] != b2[i]) { n       if (b1[i] > b2[i]) { n         cout <<  first ; n       } else { n         cout <<  second ; n       } n       return 0; n     } n   } n   if (a[n - 1] > 0) { n     cout <<  first ; n   } else { n     cout <<  second ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000 * 1000 + 100; n int d[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   long long n, m; n   cin >> n >> m; n   long long ans = n * (n - 1) * (n - 2) / 6; n   ans -= m * (n - 2); n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     d[u]++; n     d[v]++; n   } n   for (int i = 1; i <= n; i++) ans += d[i] * (d[i] - 1) / 2; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n struct point{ n public: n     int x, y; n }; n  n bool cmpx(point a, point b){ n     return a.x<b.x; n } n  n bool cmpy(point a, point b){ n     return a.y<b.y; n } n  n int main(){ n     int t, i, j, x1, x2, y1, y2, d; n     point ptsx[4], ptsy[4]; n     cin>>t; n     for(j=0; j<t; j++){ n         for(i=0; i<4; i++) cin>>ptsx[i].x>>ptsx[i].y; n         sort(ptsx, ptsx+4, cmpx); n         for(i=0; i<4; i++) ptsy[i]=ptsx[i]; n         sort(ptsy, ptsy+4, cmpy); n         x1=ptsx[1].x; n         x2=ptsx[2].x; n         y1=ptsy[1].y; n         y2=ptsy[2].y; n         if(x2-x1>=y2-y1){ n             d=x2-x1-y2+y1; n             y2=min(y2+d, ptsy[3].y); n             d=x2-x1-y2+y1; n             y1-=d; n             cout<<x1-ptsx[0].x+ptsx[3].x-x2+min(abs(ptsx[0].y-y1)+abs(ptsx[1].y-y2), abs(ptsx[0].y-y2)+abs(ptsx[1].y-y1))+min(abs(ptsx[2].y-y1)+abs(ptsx[3].y-y2), abs(ptsx[2].y-y2)+abs(ptsx[3].y-y1))<<endl; n         } n         else{ n             d=y2-y1-x2+x1; n             x2=min(x2+d, ptsx[3].x); n             d=y2-y1-x2+x1; n             x1-=d; n             cout<<y1-ptsy[0].y+ptsy[3].y-y2+min(abs(ptsy[0].x-x1)+abs(ptsy[1].x-x2), abs(ptsy[0].x-x2)+abs(ptsy[1].x-x1))+min(abs(ptsy[2].x-x1)+abs(ptsy[3].x-x2), abs(ptsy[2].x-x2)+abs(ptsy[3].x-x1))<<endl; n         } n     } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void amin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n inline void amax(T &a, T b) { n   if (a < b) a = b; n } n int gcd(int x, int y) { n   while (y) { n     x %= y; n     swap(x, y); n   } n   return x; n } n int GCD[111][111]; n int N, B[111]; n pair<int, int> A[111]; n int best; n vector<int> v, best_v; n int sums[111]; n void update() { n   int sum = 0; n   sum = sums[N - v.size()]; n   for (int i = 0; i < int(v.size()); i++) n     sum += abs(A[i + N - v.size()].first - v[i]); n   if (best > sum) { n     best = sum; n     best_v = v; n   } n } n void rec(int x) { n   update(); n   if ((int)v.size() == N || x == 59) { n     return; n   } n   bool ok = true; n   for (int i = 0; i < int(v.size()); i++) n     if (GCD[v[i]][x] != 1) { n       ok = false; n       break; n     } n   if (ok) { n     v.push_back(x); n     rec(x + 1); n     v.pop_back(); n   } n   rec(x + 1); n } n int main() { n   for (int i = 0; i < int(111); i++) n     for (int j = 0; j < int(111); j++) GCD[i][j] = gcd(i, j); n   scanf( %d , &N); n   for (int i = 0; i < int(N); i++) { n     int x; n     scanf( %d , &x); n     A[i] = make_pair(x, i); n   } n   sort(A, A + N); n   for (int i = 0; i < int(N); i++) sums[i + 1] = sums[i] + A[i].first; n   best = 1 << 29; n   rec(2); n   for (int i = 0; i < int(N); i++) B[i] = 1; n   for (int i = 0; i < int(best_v.size()); i++) n     B[A[i + N - best_v.size()].second] = best_v[i]; n   for (int i = 0; i < int(N); i++) n     printf( %d%c , B[i], i + 1 == N ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10; n int n, m; n struct bian { n   int from, to; n }; n struct tree { n   bian eg[N << 2]; n   int num, front[N << 1]; n   int zong; n   int fa[N << 1], siz[N << 1]; n   void add(int x, int y) { n     eg[++num].to = y; n     eg[num].from = front[x]; n     front[x] = num; n   } n   void init() { n     zong = n; n     for (int i = 1; i <= zong; ++i) fa[i] = i, siz[i] = 1; n   } n   int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n   int tot, now; n   int son[N << 4][2]; n   long long tag[N << 4]; n   int root[N]; n   void merge(int x, int y) { n     int d1 = find(x), d2 = find(y); n     fa[++zong] = zong; n     add(zong, d1); n     add(zong, d2); n     siz[zong] = siz[find(x)] + siz[find(y)]; n     fa[d1] = zong; n     fa[d2] = zong; n   } n   int ll[N << 1], rr[N << 1], ids[N], p; n   void dfs(int o) { n     if (o <= n) { n       ids[o] = ++p; n       ll[o] = rr[o] = ids[o]; n       return; n     } n     ll[o] = 1e9; n     for (int i = front[o]; i; i = eg[i].from) { n       int to = eg[i].to; n       dfs(to); n       ll[o] = min(ll[o], ll[to]); n       rr[o] = max(rr[o], rr[to]); n     } n   } n   void opt1(int be, int &o, int l, int r, int L, int R, int w) { n     if (o == 0) o = ++tot; n     tag[o] = tag[be]; n     if (L <= l && r <= R) { n       tag[o] += w; n       son[o][0] = son[be][0]; n       son[o][1] = son[be][1]; n       return; n     } n     int mid = (l + r) >> 1; n     if (mid >= L) n       opt1(son[be][0], son[o][0], l, mid, L, R, w); n     else n       son[o][0] = son[be][0]; n     if (mid < R) n       opt1(son[be][1], son[o][1], mid + 1, r, L, R, w); n     else n       son[o][1] = son[be][1]; n   } n   long long opt2(int o, int l, int r, int i, long long now) { n     if (o == 0) return now; n     if (l == r) return now + tag[o]; n     int mid = (l + r) >> 1; n     if (mid >= i) n       return opt2(son[o][0], l, mid, i, now + tag[o]); n     else n       return opt2(son[o][1], mid + 1, r, i, now + tag[o]); n   } n } T1; n struct tree_ { n   bian eg[N << 2]; n   int num, front[N << 1]; n   int zong; n   int fa[N << 1]; n   void add(int x, int y) { n     eg[++num].to = y; n     eg[num].from = front[x]; n     front[x] = num; n   } n   void init() { n     zong = n; n     for (int i = 1; i <= zong; ++i) fa[i] = i; n   } n   int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n   void merge(int x, int y) { n     int d1 = find(x), d2 = find(y); n     fa[++zong] = zong; n     add(zong, d1); n     add(zong, d2); n     fa[d1] = zong; n     fa[d2] = zong; n   } n   int ll[N << 1], rr[N << 1], ids[N], p; n   void dfs(int o) { n     if (o <= n) { n       ids[o] = ++p; n       ll[o] = rr[o] = ids[o]; n       return; n     } n     ll[o] = 1e9; n     for (int i = front[o]; i; i = eg[i].from) { n       int to = eg[i].to; n       dfs(to); n       ll[o] = min(ll[o], ll[to]); n       rr[o] = max(rr[o], rr[to]); n     } n   } n   int col[N << 2]; n   void opt3(int o, int l, int r, int L, int R, int w) { n     if (L <= l && r <= R) { n       col[o] = w; n       return; n     } n     int mid = (l + r) >> 1; n     if (col[o]) { n       col[o << 1] = col[o << 1 | 1] = col[o]; n       col[o] = 0; n     } n     if (mid >= L) opt3(o << 1, l, mid, L, R, w); n     if (mid < R) opt3(o << 1 | 1, mid + 1, r, L, R, w); n   } n   int opt4(int o, int l, int r, int i) { n     if (l == r) return col[o]; n     int mid = (l + r) >> 1; n     if (col[o]) { n       col[o << 1] = col[o << 1 | 1] = col[o]; n       col[o] = 0; n     } n     if (mid >= i) n       return opt4(o << 1, l, mid, i); n     else n       return opt4(o << 1 | 1, mid + 1, r, i); n   } n } T2; n int que[N][3]; n struct b { n   int fa[N << 1], zong; n   void init() { n     zong = n; n     for (int i = 1; i <= n; ++i) fa[i] = i; n   } n   int find(int o) { return fa[o] == o ? o : fa[o] = find(fa[o]); } n   void merge(int x, int y) { n     int d1 = find(x), d2 = find(y); n     fa[++zong] = zong; n     fa[d1] = zong; n     fa[d2] = zong; n   } n } B1, B2; n int main() { n   scanf( %d%d , &n, &m); n   T1.init(); n   T2.init(); n   for (int i = 1; i <= m; ++i) { n     char s[10]; n     scanf( %s , s); n     if (s[0] ==  U ) { n       int x, y; n       scanf( %d%d , &x, &y); n       que[i][0] = 1; n       que[i][1] = x; n       que[i][2] = y; n       T1.merge(x, y); n     } n     if (s[0] ==  M ) { n       int x, y; n       scanf( %d%d , &x, &y); n       que[i][0] = 2; n       que[i][1] = x; n       que[i][2] = y; n       T2.merge(x, y); n     } n     if (s[0] ==  A ) { n       int x; n       scanf( %d , &x); n       que[i][0] = 3; n       que[i][1] = x; n     } n     if (s[0] ==  Z ) { n       int x; n       scanf( %d , &x); n       que[i][0] = 4; n       que[i][1] = x; n     } n     if (s[0] ==  Q ) { n       int x; n       scanf( %d , &x); n       que[i][0] = 5; n       que[i][1] = x; n     } n   } n   for (int i = 1; i <= T1.zong; ++i) n     if (T1.find(i) == i) T1.dfs(i); n   for (int i = 1; i <= T2.zong; ++i) n     if (T2.find(i) == i) T2.dfs(i); n   B1.init(); n   B2.init(); n   for (int i = 1; i <= m; ++i) { n     if (que[i][0] == 1) { n       B1.merge(que[i][1], que[i][2]); n     } n     if (que[i][0] == 2) { n       B2.merge(que[i][1], que[i][2]); n     } n     if (que[i][0] == 3) { n       ++T1.now; n       T1.opt1(T1.root[T1.now - 1], T1.root[T1.now], 1, n, n               T1.ll[B1.find(que[i][1])], T1.rr[B1.find(que[i][1])], n               T1.siz[B1.find(que[i][1])]); n     } n     if (que[i][0] == 4) { n       T2.opt3(1, 1, n, T2.ll[B2.find(que[i][1])], T2.rr[B2.find(que[i][1])], n               T1.now); n     } n     if (que[i][0] == 5) { n       printf( %lld n , T1.opt2(T1.root[T1.now], 1, n, T1.ids[que[i][1]], 0) - n                            T1.opt2(T1.root[T2.opt4(1, 1, n, T2.ids[que[i][1]])], n                                    1, n, T1.ids[que[i][1]], 0)); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, i; n int oma = 0, omi = 0, ma, mi; n int a[101]; n string s; n int main() { n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     cin >> s; n     a[s.size()]++; n   } n   cin >> s; n   for (i = 1; i < s.size(); i++) omi = omi + a[i]; n   mi = omi; n   ma = oma; n   omi = 5 * (omi / k) + 1 + omi; n   oma = a[s.size()] + ((a[s.size()] - 1 + mi) / k) * 5 + mi; n   cout << omi <<     << oma << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n string s; n void solve() {} n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long test = 1; n   cin >> test; n   while (test--) { n     cin >> n >> k; n     cin >> s; n     int ans = 0; n     int cnt = 0; n     for (long long i = 0; i < (n); ++i) { n       if (s[i] ==  0 ) n         cnt++; n       else { n         if (i == cnt && cnt >= k + 1) n           ans += (cnt - 1 - k) / (k + 1) + 1; n         else n           ans += (cnt - k) / (k + 1); n         cnt = 0; n       } n     } n     if (cnt == n) n       cout << 1 + (cnt - 1) / (k + 1); n     else n       cout << ans + cnt / (k + 1); n     solve(); n     cout <<   n ; n   } n } n


#include<cmath> n #include<cstdio> n #include<vector> n #include<algorithm> n #include<map> n using namespace std; n  n int main() { n     int n, M=1000000007, tmp, maxc=100; n     vector<int> c; n     scanf( %d , &n); n  n     int sumc=0; n     for(int i=0;i<n;++i) { n         scanf( %d , &tmp); n         c.push_back(tmp); n         sumc+=tmp; n     } n  n     int sumb=0, global_bi_over_i_ub=0; n     vector<int> b(n, 0); n     for(int i=0;i<n-1;++i) { n         scanf( %d , &tmp); n         sumb = sumb+tmp; n         b[i+1] = b[i] + sumb; n         global_bi_over_i_ub = min(global_bi_over_i_ub, -(b[i+1] / (i+2))); n     } n     int global_x_ub = maxc+global_bi_over_i_ub; n  n     int q; n     vector<int> x; n     scanf( %d , &q); n     for(int i=0;i<q;++i) { n         scanf( %d , &tmp); n         x.push_back(max(global_x_ub-maxc-5, min(global_x_ub+5, tmp))); n     } n     //printf( ub: %d , global_x_ub); n  n     map<int,int> x2ans; n     for(int xval=global_x_ub+5;xval>=global_x_ub-maxc-5;--xval) { n         vector<int> *p_ans=nullptr, *p_ans_last=nullptr; n         p_ans_last = new vector<int>(sumc+2,0); n         (*p_ans_last)[0]=1; n         for(int i=0;i<n;++i) { n             p_ans = new vector<int>(sumc+2, 0); n             int sum_last = 0, lower_bound=xval*(i+1)+b[i]; n             //printf( lb=%d, xval=%d, mul=%d, b=%d n , lower_bound, xval, xval*(i+1), b[i]); n             for(int j=0;j<=c[i];++j) { n                 sum_last = (sum_last + (*p_ans_last)[j])%M; n                 (*p_ans)[j] = sum_last; n             } n             for(int j=c[i]+1;j<=sumc;++j) { n                 sum_last = (sum_last - (*p_ans_last)[j-c[i]-1] + M) % M; n                 sum_last = (sum_last + (*p_ans_last)[j]) % M; n                 (*p_ans)[j] = sum_last; n             } n             for(int j=0;j<lower_bound && j<=sumc+1;++j) n                 (*p_ans)[j]=0; n             swap(p_ans, p_ans_last); n             delete p_ans; n         } n         int cnt=0; n         for(int i=0;i<=sumc;++i) n             cnt = (cnt + (*p_ans_last)[i])%M; n         x2ans[xval] = cnt; n         delete p_ans_last; n     } n  n     for(int i=0;i<q;++i) { n         printf( %d n , x2ans[x[i]]); n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n struct query { n   int n, str_l, id; n   friend bool operator<(query a, query b) { return a.str_l < b.str_l; } n }; n const long long MOD = 1e9 + 7; n const int maxn = 1e5 + 5; n int n, str_len; n char str[maxn]; n long long ans[maxn], fac[maxn], ifac[maxn], inv[maxn], q25[maxn], q26[maxn], n     iq26[maxn]; n long long s[maxn]; n query q[maxn]; n int q_len; n int read_int() { n   int t = 1, m = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) t = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) n     m = (m << 3) + (m << 1) + ch -  0 , ch = getchar(); n   return t * m; n } n void init() { n   fac[0] = fac[1] = ifac[0] = ifac[1] = inv[1] = 1; n   q25[0] = q26[0] = iq26[0] = 1; n   for (int i = 2; i < maxn; i++) { n     fac[i] = fac[i - 1] * i % MOD; n     inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD; n     ifac[i] = ifac[i - 1] * inv[i] % MOD; n   } n   for (int i = 1; i < maxn; i++) n     iq26[i] = iq26[i - 1] * inv[26] % MOD, q25[i] = q25[i - 1] * 25 % MOD, n     q26[i] = q26[i - 1] * 26 % MOD; n } n long long qpow(long long a, long long b) { n   long long c = 1; n   while (b) { n     if (b & 1) c = c * a % MOD; n     b >>= 1, a = a * a % MOD; n   } n   return c; n } n inline long long C(long long a, long long b) { n   return fac[a] * ifac[b] % MOD * ifac[a - b] % MOD; n } n int main() { n   init(); n   int Q = read_int(); n   scanf( %s , str), str_len = strlen(str); n   while (Q--) { n     int t = read_int(); n     if (t == 1) n       scanf( %s , str), str_len = strlen(str); n     else n       q_len++, q[q_len].str_l = str_len, q[q_len].n = read_int(), n                q[q_len].id = q_len; n   } n   sort(q + 1, q + 1 + q_len); n   for (int t = 1; t <= q_len; t++) { n     n = q[t].n, str_len = q[t].str_l; n     if (str_len != q[t - 1].str_l) { n       memset(s, 0, sizeof(s)); n       for (int i = str_len; i < maxn; i++) n         s[i] = (s[i - 1] + C(i - 1, str_len - 1) * iq26[i] % MOD * n                                q25[i - str_len] % MOD) % n                MOD; n     } n     ans[q[t].id] = q26[n] * s[n] % MOD; n   } n   for (int i = 1; i <= q_len; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x > 0 ? x : -x; n } n int n; n int m; n int x[100]; n int main() { n   scanf( %d%d , &n, &m); n   int s = 0, f = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &x[i]); n     s += x[i]; n     f = max(f, x[i]); n   } n   if (s + m < f * n) n     printf( -1 n ); n   else n     for (int i = 0; i < n; i++) printf( %.10f n , (s + m + 0.0) / n - x[i]); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 110; n using namespace std; n inline int read() { n   int x = 0, f = 1; n   int ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) { n       f = -1; n     } n     ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, a[N]; n int main() { n   int q = read(); n   while (q--) { n     bool flg = 0; n     n = read(); n     for (int i = 1; i <= n; i++) { n       a[i] = read(); n     } n     sort(a + 1, a + 1 + n); n     for (int i = 2; i <= n; i++) { n       if (a[i] == a[i - 1] + 1) { n         cout << 2 << endl; n         flg = 1; n         break; n       } n     } n     if (!flg) cout << 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n void pre() {} n void solve() {} n int p[100]; n int vis[109]; n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cin.exceptions(cin.failbit); n   pre(); n   int n; n   cin >> n; n   for (int i = 0; i < (n); ++i) { n     cin >> p[i]; n     vis[p[i]] = 1; n   } n   vector<pair<int, int> > v; n   for (int i = 0; i < (n); ++i) n     if (p[i]) p[i] = p[i] % 2 + 2; n   v.push_back(make_pair(p[0], 1)); n   for (int i = 1; i <= (n - 1); ++i) { n     if (p[i] == p[i - 1]) n       v.back().second++; n     else n       v.push_back(make_pair(p[i], 1)); n   } n   if ((int)(v).size() == 1) { n     if (p[0] == 0) { n       if (n == 1) n         cout << 0; n       else n         cout << 1; n     } else n       cout << 0; n     return 0; n   } n   vector<int> cnt[2]; n   int fns = 0; n   for (int i = 0; i < ((int)(v).size()); ++i) { n     if (v[i].first == 0) { n       if (i == 0) n         ; n       else if (i == (int)(v).size() - 1) n         ; n       else if (v[i - 1].first % 2 == v[i + 1].first % 2) n         cnt[v[i - 1].first % 2].push_back(v[i].second); n       else n         fns++; n     } else if (i != (int)(v).size() - 1 && v[i + 1].first != 0 && n                v[i].first != v[i + 1].first) { n       fns++; n     } n   } n   int val[2]; n   val[0] = val[1] = 0; n   for (int i = 1; i <= (n); ++i) n     if (!vis[i]) val[i % 2]++; n   sort(cnt[0].begin(), cnt[0].end()); n   sort(cnt[1].begin(), cnt[1].end()); n   reverse(cnt[0].begin(), cnt[0].end()); n   reverse(cnt[1].begin(), cnt[1].end()); n   int gns = n; n   int st[2]; n   st[0] = val[0], st[1] = val[1]; n   vector<int> gg1 = cnt[0], gg2 = cnt[1]; n   int cns = fns; n   for (int q = 0; q < (4); ++q) { n     val[0] = st[0]; n     val[1] = st[1]; n     cnt[0] = gg1; n     cnt[1] = gg2; n     fns = cns; n     if (v[(int)(v).size() - 1].first == 0) { n       if (q & 2) { n         if (val[v[(int)(v).size() - 2].first % 2] >= n             v[(int)(v).size() - 1].second) { n           val[v[(int)(v).size() - 2].first % 2] -= n               v[(int)(v).size() - 1].second; n         } else n           fns++; n       } else n         fns++; n     } n     if (v[0].first == 0) { n       if (q & 1) { n         if (val[v[1].first % 2] >= v[0].second) { n           val[v[1].first % 2] -= v[0].second; n         } else n           fns++; n       } else n         fns++; n     } n     while ((int)(cnt[1]).size() && cnt[1].back() <= val[1]) { n       val[1] -= cnt[1].back(); n       cnt[1].pop_back(); n     } n     while ((int)(cnt[0]).size() && cnt[0].back() <= val[0]) { n       val[0] -= cnt[0].back(); n       cnt[0].pop_back(); n     } n     gns = min(gns, fns + 2 * ((int)(cnt[0]).size() + (int)(cnt[1]).size())); n   } n   cout << gns; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0; n   int answer = 0; n   cin >> n; n   int last = -1; n   int xs = 0, ys = 0, x = 0, y = 0; n   for (int i = 0; i < n; i++) { n     int px = x, py = y; n     char c; n     cin >> c; n     if (c ==  L ) n       x--; n     else if (c ==  R ) n       x++; n     else if (c ==  U ) n       y++; n     else n       y--; n     if (abs(x - xs) + abs(y - ys) != i - last) n       answer++, last = i - 1, xs = px, ys = py; n   } n   answer++; n   cout << answer; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1000 * 1000 * 1000 * 1000 * 1000 * 100; n const int MOD = 1000 * 1000 * 1000 + 9; n const int SIZE = 5000; n int main() { n   std::ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> a(n); n   vector<vector<int> > p(100500); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     a[i] = abs(a[i]); n     p[a[i]].push_back(i); n   } n   int cur = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) n       if (a[j] < a[i]) ++cur; n   } n   for (int k = 1; k < 100500; ++k) { n     if (p[k].size() == 0) continue; n     int best = 0; n     int before = 0; n     int after = 0; n     int tmp = 0; n     for (int c = 0; c < n; ++c) { n       if (a[c] > -k && a[c] < k) ++after; n     } n     int i = 0; n     for (int cnt = 0; cnt < p[k].size(); ++cnt) { n       for (; i != p[k][cnt]; ++i) { n         if (a[i] > -k && a[i] < k) ++before, --after; n       } n       tmp += before - after; n       if (tmp < best) best = tmp; n     } n     cur += best; n   } n   cout << cur << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n const int inf = 1e9; n int n, l[maxn], c[maxn], ans, cnt, pri[maxn], lst, cur, p[maxn], st[maxn], n     f[maxn]; n bool tf[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , l + i); n   for (int i = 1; i <= n; i++) scanf( %d , c + i); n   for (int i = 2; i <= 100000; i++) { n     if (!tf[i]) pri[++cnt] = i; n     for (int j = 1; j <= cnt && i * pri[j] <= 100000; j++) { n       tf[i * pri[j]] = true; n       if (i % pri[j] == 0) break; n     } n   } n   ans = inf; n   for (int i = 1; i <= n; i++) { n     lst = l[i]; n     cur = 0; n     for (int j = 1; j <= cnt && pri[j] * pri[j] <= lst; j++) n       if (lst % pri[j] == 0) { n         p[++cur] = pri[j]; n         while (lst % pri[j] == 0) lst /= pri[j]; n       } n     if (lst != 1) p[++cur] = lst; n     for (int j = 1; j <= n; j++) { n       st[j] = 0; n       for (int k = 1; k <= cur; k++) n         if (l[j] % p[k] == 0) st[j] |= (1 << (k - 1)); n     } n     for (int i = 0; i < (1 << 10); i++) f[i] = inf; n     f[(1 << cur) - 1] = c[i]; n     for (int i = 1; i <= n; i++) n       for (int j = 0; j < (1 << cur); j++) n         f[st[i] & j] = min(f[st[i] & j], f[j] + c[i]); n     ans = min(ans, f[0]); n   } n   if (ans >= inf) n     printf( -1 n ); n   else n     printf( %d n , ans); n   return 0; n } n