#include <bits/stdc++.h> n using namespace std; n int n, lcnt; n long long m, p, q; n int a[100005], lsum[100005]; n long long sum[100005], rmax[100005], lmin[100005], ls[100005]; n long long ans1, ans2, ans; n set<int> unu; n inline long long Min(long long x, long long y) { return x < y ? x : y; } n inline long long Max(long long x, long long y) { return x > y ? x : y; } n inline int lowbit(int k) { return k & -k; } n struct ta { n   long long t[100005]; n   void add(int k, long long x) { n     for (; k <= lcnt; k += lowbit(k)) t[k] += x; n   } n   inline long long query(int k) { n     long long ans = 0; n     for (; k; k -= lowbit(k)) ans += t[k]; n     return ans; n   } n } tsum, tnum; n struct ta2 { n   int t[100005]; n   void build() { n     for (int i = 1; i <= lcnt; i++) t[i] = n + 1; n   } n   void add(int k, int x) { n     for (; k <= lcnt; k += lowbit(k)) t[k] = Min(t[k], x); n   } n   inline int query(int k) { n     int ans = n + 1; n     for (; k; k -= lowbit(k)) ans = Min(ans, t[k]); n     return ans; n   } n } tmin; n struct subs { n   int l, r; n   long long w; n   bool operator>(const subs k) const { n     if (w != k.w) n       return w > k.w; n     else if (r != k.r) n       return r > k.r; n     else n       return l > k.l; n   } n   bool operator<(const subs k) const { n     if (w != k.w) n       return w < k.w; n     else if (r != k.r) n       return r < k.r; n     else n       return l < k.l; n   } n   bool operator==(const subs k) const { n     return (k.l == l) && (k.r == r) && (k.w == w); n   } n   bool operator!=(const subs k) const { n     return (k.l != l) || (k.r != r) || (k.w != w); n   } n }; n struct heap { n   subs h[100005]; n   int top; n   void pushdown(int k) { n     while ((k << 1) <= top) { n       int v = k << 1; n       if (v + 1 <= top) n         if (h[v | 1] > h[v]) v++; n       if (h[v] > h[k]) n         swap(h[v], h[k]); n       else n         break; n       k = v; n     } n   } n   void pushup(int k) { n     while (k > 1) { n       int v = k >> 1; n       if (h[v] < h[k]) n         swap(h[v], h[k]); n       else n         break; n       k = v; n     } n   } n   void push(subs k) { n     h[++top] = k; n     pushup(top); n   } n   void pop() { n     h[1] = h[top--]; n     pushdown(1); n   } n } subsq; n void del(int k); n struct sg1 { n   bool b[400005]; n   void cover(int L, int R, int l, int r, int index) { n     if (L > R) return; n     if (b[index]) return; n     if (l == r) { n       del(l); n       b[index] = true; n       return; n     } n     if (L <= ((l + r) >> 1)) cover(L, R, l, ((l + r) >> 1), index << 1); n     if (R > ((l + r) >> 1)) cover(L, R, ((l + r) >> 1) + 1, r, index << 1 | 1); n     b[index] = b[index << 1] && b[index << 1 | 1]; n   } n } s1; n struct t { n   long long max[100005][17], min[100005][17]; n   int lo[100005]; n   void build(int l, int r, int index) { n     lo[1] = 0; n     for (int i = 2; i <= n; i++) n       if ((1 << (lo[i - 1] + 1)) == i) n         lo[i] = lo[i - 1] + 1; n       else n         lo[i] = lo[i - 1]; n     for (int i = 0; i <= n; i++) max[i][0] = min[i][0] = sum[i]; n     for (int i = 1; i <= lo[n]; i++) n       for (int j = 0; j + (1 << i) - 1 <= n; j++) n         max[j][i] = Max(max[j][i - 1], max[j + (1 << (i - 1))][i - 1]), n         min[j][i] = Min(min[j][i - 1], min[j + (1 << (i - 1))][i - 1]); n   } n   long long query(int l, int r) { n     int tmp = lo[r - l + 1]; n     return Max(max[l][tmp], max[r - (1 << tmp) + 1][tmp]) - n            Min(min[l][tmp], min[r - (1 << tmp) + 1][tmp]); n   } n } s2; n struct node3 { n   int l, r, s; n }; n struct sg3 { n   node3 t[4000005]; n   int root[100005], cnt; n   void ins(int &rt, int l, int r, int k) { n     if (rt == 0) rt = ++cnt; n     t[rt].s++; n     if (l == r) return; n     if (k <= ((l + r) >> 1)) n       ins(t[rt].l, l, ((l + r) >> 1), k); n     else n       ins(t[rt].r, ((l + r) >> 1) + 1, r, k); n   } n   void build() { n     for (int i = 0; i <= n; i++) ins(root[lsum[i]], 0, n, i); n   } n   inline int qsum(int rt, int l, int r, int k) { n     if (rt == 0) return 0; n     if (r == k) return t[rt].s; n     if (k <= ((l + r) >> 1)) n       return qsum(t[rt].l, l, ((l + r) >> 1), k); n     else n       return t[t[rt].l].s + qsum(t[rt].r, ((l + r) >> 1) + 1, r, k); n   } n   inline int query(int rt, int l, int r, int k) { n     if (l == r) return r; n     if (k <= t[t[rt].l].s) n       return query(t[rt].l, l, ((l + r) >> 1), k); n     else n       return query(t[rt].r, ((l + r) >> 1) + 1, r, k - t[t[rt].l].s); n   } n } s3; n struct sg4 { n   node3 t[4000005]; n   int cnt, root[100005]; n   void ins(int &rt, int Rt, int l, int r, int k) { n     rt = ++cnt; n     if (l == r) return; n     if (k <= ((l + r) >> 1)) { n       t[rt].r = t[Rt].r; n       ins(t[rt].l, t[Rt].l, l, ((l + r) >> 1), k); n     } else { n       t[rt].l = t[Rt].l; n       ins(t[rt].r, t[Rt].r, ((l + r) >> 1) + 1, r, k); n     } n   } n   void build() { n     ins(root[0], 0, 1, lcnt, lsum[0]); n     for (int i = 1; i <= n; i++) ins(root[i], root[i - 1], 1, lcnt, lsum[i]); n   } n   inline int query(int rt, int l, int r, int k) { n     if ((k > r) || (rt == 0)) return 0; n     if (l == r) return l; n     if (k > ((l + r) >> 1)) n       return query(t[rt].r, ((l + r) >> 1) + 1, r, k); n     else { n       int tmp = query(t[rt].l, l, ((l + r) >> 1), k); n       if (tmp) n         return tmp; n       else n         return query(t[rt].r, ((l + r) >> 1) + 1, r, k); n     } n   } n } s4; n struct splaynode { n   int l, r, size, fa; n   subs s; n   long long sum; n }; n struct Splay { n   splaynode t[100005]; n   int root, top, cnt, stack[100005]; n   inline int newnode() { return stack[top--]; } n   void pushup(int rt) { n     t[rt].size = t[t[rt].l].size + t[t[rt].r].size + 1; n     t[rt].sum = t[t[rt].l].sum + t[t[rt].r].sum + t[rt].s.w; n   } n   void rorate(int k, int i) { n     int tmp = t[k].fa; n     t[k].fa = t[tmp].fa; n     if (t[t[tmp].fa].l == tmp) n       t[t[tmp].fa].l = k; n     else n       t[t[tmp].fa].r = k; n     t[tmp].fa = k; n     if (i == 0) { n       if (t[k].l > 0) t[t[k].l].fa = tmp; n       t[tmp].r = t[k].l; n       t[k].l = tmp; n     } else { n       if (t[k].r > 0) t[t[k].r].fa = tmp; n       t[tmp].l = t[k].r; n       t[k].r = tmp; n     } n     pushup(tmp); n     pushup(k); n   } n   void splay(int k, int g) { n     while (t[k].fa != g) { n       int tmp = t[k].fa; n       if (t[tmp].fa == g) n         if (t[tmp].r == k) n           rorate(k, 0); n         else n           rorate(k, 1); n       else { n         int Tmp = t[tmp].fa; n         if (t[Tmp].l == tmp) { n           if (t[tmp].l == k) n             rorate(tmp, 1); n           else n             rorate(k, 0); n           rorate(k, 1); n         } else { n           if (t[tmp].r == k) n             rorate(tmp, 0); n           else n             rorate(k, 1); n           rorate(k, 0); n         } n       } n     } n     if (g == 0) root = k; n   } n   void build(int l, int r, int &rt) { n     rt = ++cnt; n     t[rt].s = (subs){((l + r) >> 1) + 1, ((l + r) >> 1), 0}; n     if (((l + r) >> 1) > l) { n       build(l, ((l + r) >> 1) - 1, t[rt].l); n       t[t[rt].l].fa = rt; n     } n     if (r > ((l + r) >> 1)) { n       build(((l + r) >> 1) + 1, r, t[rt].r); n       t[t[rt].r].fa = rt; n     } n     pushup(rt); n   } n   inline long long query(int k) { n     if (k > t[root].size) return -8333583335000000000ll; n     if (k == t[root].size) return t[root].sum; n     int p = root; n     while (t[t[p].r].size != k) { n       if (k < t[t[p].r].size) n         p = t[p].r; n       else n         k -= t[t[p].r].size + 1, p = t[p].l; n     } n     splay(p, 0); n     return t[t[p].r].sum; n   } n   void ins(subs k) { n     if (root == 0) { n       root = newnode(); n       t[root].l = t[root].r = t[root].fa = 0; n       t[root].s = k; n       pushup(root); n       return; n     } n     int p = root, v; n     while (true) { n       if (k < t[p].s) n         if (t[p].l) n           p = t[p].l; n         else { n           t[p].l = v = newnode(); n           break; n         } n       else if (t[p].r) n         p = t[p].r; n       else { n         t[p].r = v = newnode(); n         break; n       } n     } n     t[v].fa = p; n     t[v].l = t[v].r = 0; n     t[v].s = k; n     pushup(v); n     splay(v, 0); n   } n   void del(subs k) { n     if (t[root].size == 1) { n       root = 0; n       return; n     } n     int p = root; n     while (t[p].s != k) { n       if (k < t[p].s) n         p = t[p].l; n       else n         p = t[p].r; n     } n     splay(p, 0); n     int tmp = t[p].l; n     if (tmp == 0) { n       t[t[p].r].fa = 0; n       root = t[p].r; n       stack[++top] = p; n       return; n     } n     while (t[tmp].r) tmp = t[tmp].r; n     splay(tmp, root); n     t[tmp].fa = 0; n     root = tmp; n     if (t[p].r) t[t[p].r].fa = tmp, t[tmp].r = t[p].r; n     pushup(tmp); n     stack[++top] = p; n   } n } s; n inline long long read() { n   long long ret = 0, p = 1; n   char c = getchar(); n   while ((c <  0 ) || (c >  9 )) { n     if (c ==  - ) p = -1; n     c = getchar(); n   } n   while ((c >=  0 ) && (c <=  9 )) n     ret = (ret << 1) + (ret << 3) + c -  0 , c = getchar(); n   return ret * p; n } n inline int find(long long k) { n   int l = 0, r = lcnt + 1; n   while (r - l > 1) { n     if (ls[((l + r) >> 1)] > k) n       r = ((l + r) >> 1); n     else n       l = ((l + r) >> 1); n   } n   return l; n } n inline long long checknum(long long std) { n   long long ans = 0; n   for (int i = 0; i <= n; i++) { n     ans += tnum.query(find(sum[i] - std)); n     tnum.add(lsum[i], 1); n   } n   for (int i = 0; i <= n; i++) tnum.add(lsum[i], -1); n   return ans; n } n void del(int k) { n   set<int>::iterator p = unu.find(k), st = unu.begin(), ed = unu.end(); n   ed--; n   if (p != st) { n     set<int>::iterator pre = p; n     pre--; n     s.del((subs){(*pre) + 1, k - 1, s2.query((*pre), k - 1)}); n   } n   if (p != ed) { n     set<int>::iterator nex = p; n     nex++; n     s.del((subs){k + 1, (*nex) - 1, s2.query(k, (*nex) - 1)}); n   } n   if ((p != st) && (p != ed)) { n     set<int>::iterator pre = p, nex = p; n     pre--; n     nex++; n     s.ins((subs){(*pre) + 1, (*nex) - 1, s2.query((*pre), (*nex) - 1)}); n   } n   unu.erase(k); n } n inline subs Next(subs now) { n   int l = now.l, r = now.r, L, R = r; n   int tmp = s3.qsum(s3.root[lsum[l]], 0, n, l); n   if (tmp == 1) { n     int S = s4.query(s4.root[r - 1], 1, lcnt, lsum[l] + 1); n     if (S == 0) return (subs){0, R, -8333583335000000000ll}; n     int Sum = s3.qsum(s3.root[S], 0, n, r - 1); n     L = s3.query(s3.root[S], 0, n, Sum); n   } else n     L = s3.query(s3.root[lsum[l]], 0, n, tmp - 1); n   return (subs){L, R, sum[R] - sum[L]}; n } n inline long long checksum(long long std, long long rest) { n   long long ans = 0; n   tmin.build(); n   for (int i = 0; i <= n; i++) { n     int t = find(sum[i] - std); n     rest -= tnum.query(t); n     ans += tnum.query(t) * sum[i]; n     ans += tsum.query(t); n     s1.cover(tmin.query(t) + 1, i, 1, n, 1); n     tnum.add(lsum[i], 1); n     tsum.add(lsum[i], -sum[i]); n     tmin.add(lsum[i], i); n   } n   for (int i = 0; i <= n; i++) tnum.add(lsum[i], -1); n   std--; n   ans += rest * std; n   tnum.add(lsum[0], 1); n   for (int i = 1; i <= n; i++) { n     int Tmp = find(sum[i] - std); n     if (ls[Tmp] != sum[i] - std) { n       Tmp++; n       tnum.add(lsum[i], 1); n       int p = s4.query(s4.root[i - 1], 1, lcnt, Tmp); n       if (p == 0) continue; n       int L = s3.query(s3.root[p], 0, n, s3.qsum(s3.root[p], 0, n, i - 1)); n       subsq.push((subs){L, i, sum[i] - sum[L]}); n     } else { n       int tmp = tnum.query(Tmp) - tnum.query(Tmp - 1); n       tnum.add(lsum[i], 1); n       if (tmp == 0) { n         Tmp++; n         int p = s4.query(s4.root[i - 1], 1, lcnt, Tmp); n         if (p == 0) continue; n         int L = s3.query(s3.root[p], 0, n, s3.qsum(s3.root[p], 0, n, i - 1)); n         subsq.push((subs){L, i, sum[i] - sum[L]}); n       } else if (tmp <= rest) { n         int L = s3.query(s3.root[Tmp], 0, n, 1); n         s1.cover(L + 1, i, 1, n, 1); n         rest -= tmp; n         Tmp++; n         int p = s4.query(s4.root[i - 1], 1, lcnt, Tmp); n         if (p == 0) continue; n         L = s3.query(s3.root[p], 0, n, s3.qsum(s3.root[p], 0, n, i - 1)); n         subsq.push((subs){L, i, sum[i] - sum[L]}); n       } else { n         int L; n         if (rest) { n           L = s3.query(s3.root[Tmp], 0, n, tmp - rest + 1); n           s1.cover(L + 1, i, 1, n, 1); n         } n         L = s3.query(s3.root[Tmp], 0, n, tmp - rest); n         subsq.push((subs){L, i, sum[i] - sum[L]}); n         rest = 0; n       } n     } n   } n   return ans; n } n void upans2() { n   if (unu.empty()) { n     ans2 = -8333583335000000000ll; n     return; n   } n   ans2 = s.query(q - 1); n   if (ans2 == -8333583335000000000ll) return; n   set<int>::iterator p = unu.begin(); n   ans2 -= lmin[(*p) - 1]; n   p = unu.end(); n   p--; n   ans2 += rmax[(*p)]; n } n void work() { n   for (int i = 1; i <= n; i++) unu.insert(i); n   long long l = ls[1] - ls[lcnt] - 5, r = ls[lcnt] - ls[1] + 5; n   while (r - l > 1) { n     if (checknum(((l + r) >> 1)) >= p) n       l = ((l + r) >> 1); n     else n       r = ((l + r) >> 1); n   } n   ans1 = checksum(r, p); n   upans2(); n   if (ans2 > -8333583335000000000ll) n     ans = ans1 + ans2; n   else n     ans = -8333583335000000000ll; n } n void pre() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     ls[i] = sum[i] = sum[i - 1] + a[i]; n   } n   ls[n + 1] = 0; n   sort(ls + 1, ls + 2 + n); n   ls[0] = -8333583335000000000ll; n   for (int i = 1; i <= n + 1; i++) n     if (ls[i] != ls[i - 1]) ls[++lcnt] = ls[i]; n   for (int i = 0; i <= n; i++) lsum[i] = find(sum[i]); n   lmin[0] = sum[0]; n   for (int i = 1; i <= n; i++) lmin[i] = Min(lmin[i - 1], sum[i]); n   rmax[n] = sum[n]; n   for (int i = n - 1; i >= 0; i--) rmax[i] = Max(rmax[i + 1], sum[i]); n   s2.build(1, n - 1, 1); n   s3.build(); n   s4.build(); n   s.build(1, n - 1, s.root); n   if (m > n) n     p = m - n, q = n; n   else n     p = 0, q = m; n   work(); n } n void doit() { n   q--; n   p++; n   for (; q; q--, p++) { n     subs tmp = subsq.h[1], Tmp = Next(tmp); n     if (Tmp.w > -8333583335000000000ll) n       subsq.h[1] = Next(tmp), subsq.pushdown(1); n     else n       subsq.pop(); n     s1.cover(tmp.l + 1, tmp.r, 1, n, 1); n     ans1 += tmp.w; n     upans2(); n     if (ans2 > -8333583335000000000ll) ans = Max(ans, ans1 + ans2); n   } n   ans1 += subsq.h[1].w; n   s1.cover(subsq.h[1].l + 1, subsq.h[1].r, 1, n, 1); n   if (s1.b[1]) ans = ans1; n   printf( %I64d , ans); n } n int main() { n   pre(); n   doit(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200 * 1000 + 100; n int n, m, p; n int a[maxn]; n int b[maxn]; n map<int, int> numb; n map<int, int> numa; n vector<int> ans; n int sum = 0; n void add(int x) { n   int val = numa[x] + 1; n   numa[x] = val; n   if (numb[x] >= val) sum++; n } n void remove(int x) { n   int val = numa[x] - 1; n   numa[x] = val; n   if (numb[x] > val) sum--; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> p; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < m; ++i) { n     cin >> b[i]; n     numb[b[i]]++; n   } n   for (int i = 0; i < p; ++i) { n     numa.clear(); n     sum = 0; n     for (int j = i; j < i + (long long)p * (long long)m && j < n; j += p) n       add(a[j]); n     for (int q = i; (long long)q + (long long)p * (long long)(m - 1) < n; n          q += p) { n       if (sum == m) ans.push_back(q); n       if ((long long)q + (long long)p * (long long)m < n) { n         add(a[q + p * m]); n         remove(a[q]); n       } n     } n   } n   sort((ans).begin(), (ans).end()); n   cout << (int)(ans).size() << endl; n   for (int i = 0; i < (int)(ans).size(); ++i) cout << ans[i] + 1 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using dbl = double; n using ldb = long double; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using vi = vector<int>; n using mii = map<int, int>; n using msi = map<string, int>; n using umii = unordered_map<int, int>; n using umsi = unordered_map<string, int>; n using iii = tuple<int, int, int>; n using lll = tuple<ll, ll, ll>; n ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } n ll lcm(ll a, ll b) { n   if (!a || !b) return a + b; n   return a * (b / gcd(a, b)); n } n const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   int tc; n   cin >> tc; n   while (tc--) { n     int n; n     string s; n     cin >> n; n     cin >> s; n     int a = 0, b = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] !=  < ) { n         break; n       } n       a++; n     } n     for (int i = n - 1; i >= 0; i--) { n       if (s[i] !=  > ) { n         break; n       } n       b++; n     } n     cout << min(a, b) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x, y, a, b, n; n void init() { n   scanf( %d , &n); n   puts( YES ); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d%d , &x, &y, &a, &b); n     printf( %d n , 2 * abs(x % 2) + abs(y % 2) + 1); n   } n } n int main() { n   init(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, matrica[101][101], pas = 0; n   cin >> n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       cin >> a; n       matrica[i][j] = a; n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       if (i == j || i == ((n + 1) / 2) - 1 && j != ((n + 1) / 2) - 1 || n           j == ((n + 1) / 2) - 1 && i != ((n + 1) / 2) - 1 || n           i == n - j - 1 && j != ((n + 1) / 2) - 1) n         pas += matrica[i][j]; n     } n   cout << pas << endl; n   return 0; n } n


#include  bits/stdc++.h  n  n #define ll long long int n #define MOD 1000000007 n using namespace std; n  n  n ll randNum(ll a , ll b){ n  return a + rand() % (b-a+1); n } n  n template<class t> n void print(t *a , int n){ n  for(int i =0 ; i<n ; i++) cout<<a[i]<<   ; n  cout<<endl; n } n  n template<class t> n void print(vector<t> &v){ n  for(int i=0 ; i<v.size() ; i++) cout<<v[i]<<   ; n  cout<<endl; n } n  n  n ll gcd(ll a, ll b) n { n   if (b == 0) n     return a; n   return gcd(b, a % b); n } n  n  n ll lcm(ll a, ll b) n { n     return (a / gcd(a, b)) * b; n } n  n  n ll gcdExtended(ll a, ll b, ll *x, ll *y)  n {  n     n     if (a == 0)  n     {  n         *x = 0, *y = 1;  n         return b;  n     }  n    n     ll x1, y1; n     ll gcd = gcdExtended(b%a, a, &x1, &y1);  n    n     *x = y1 - (b/a) * x1;  n     *y = x1;  n    n     return gcd;  n }  n  n ll modInverse(ll b, ll m)  n {  n     ll x, y; n     ll g = gcdExtended(b, m, &x, &y);  n    n     if (g != 1)  n         return -1;  n    n     return (x%m + m) % m;  n }  n    n ll modDivide(ll a, ll b, ll m)  n {  n     a = a % m;  n     ll inv = modInverse(b, m);  n     if (inv == -1)  n        return -1;  n     else n        return (inv * a) % m;  n }  n  n //precompute factorial array if multiple queries are present n unsigned long long nCrModPFermat(unsigned long long n, int r, int p){ n   n     if (n < (unsigned long long)r) n         return 0; n     if (r == 0) n         return 1; n   n     unsigned long long fac[n + 1]; n     fac[0] = 1; n     for (unsigned long long i = 1; i <= n; i++) n         fac[i] = (fac[i - 1] * i) % p; n   n     return (fac[n] * modInverse(fac[r], p) % p n             * modInverse(fac[n - r], p) % p) n            % p; n } n    n    n //-------- n  n  n void testcase(){ n  int n; n  cin>>n; n  vector<int> v(n); n  for(int i=0 ; i<n ; i++) cin>>v[i]; n  vector<int> prevmax(n) , prevmaxindex(n); n  prevmax[0] = v[0]; n  prevmaxindex[0] = 0; n  for(int i=1 ; i<n ; i++) { n   if(v[i] > prevmax[i-1]){ n    prevmax[i] = v[i]; n    prevmaxindex[i] = i; n   } n   else{ n    prevmax[i] = prevmax[i-1]; n    prevmaxindex[i] = prevmaxindex[i-1]; n   } n   //prevmax[i] = max(v[i] , prevmax[i-1]); n  } n   n  int end = n-1; n  vector<int> ansindex; n  while(end >= 0){ n   int index = prevmaxindex[end]; n   ansindex.push_back(index); n   end = index-1; n  } n   n  for(int i=0 ; i<ansindex.size() ; i++){ n   if(i == 0){ n    for(int j=ansindex[i] ; j<n ; j++) cout<<v[j]<<   ; n   } n   else{ n    for(int j = ansindex[i] ; j<ansindex[i-1] ; j++) cout<<v[j]<<   ; n   } n  } n  cout<<endl; n } n  n int main(){ n  cin.sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n     int tt; n     cin>>tt; n     tt++; n     while(--tt){ n         testcase(); n     } n     return 0; n      n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXSIZE = 300010; n int rank1[MAXSIZE]; n int parent[MAXSIZE]; n int visited[MAXSIZE]; n int diameter[MAXSIZE]; n int N; n vector<int> V[MAXSIZE]; n int maxL = 0, last = 0; n int FindSet(int x) { n   if (x != parent[x]) parent[x] = FindSet(parent[x]); n   return parent[x]; n } n void Union(int root1, int root2) { n   int x = FindSet(root1), y = FindSet(root2); n   if (x == y) return; n   if (rank1[x] > rank1[y]) n     parent[y] = x; n   else { n     parent[x] = y; n     if (rank1[x] == rank1[y]) ++rank1[y]; n   } n } n void Union1(int root1, int root2) { n   int x = FindSet(root1), y = FindSet(root2); n   if (x == y) return; n   if (rank1[x] > rank1[y]) n     parent[y] = x; n   else { n     parent[x] = y; n     if (rank1[x] == rank1[y]) ++rank1[y]; n   } n   diameter[FindSet(x)] = n       max((double)max(diameter[x], diameter[y]), n           ceil(diameter[x] / 2.0) + ceil(diameter[y] / 2.0) + 1); n } n void Init() { n   memset(rank1, 0, sizeof(rank1)); n   for (int i = 0; i < MAXSIZE; ++i) parent[i] = i; n } n void dfs(int v, int path, int p) { n   if (path > maxL) { n     maxL = path; n     last = v; n   } n   for (int i = 0; i < V[v].size(); i++) { n     int to = V[v][i]; n     if (to != p) dfs(V[v][i], path + 1, v); n   } n } n int nextInt() { n   int x = 0, p = 1; n   char c; n   do { n     c = getchar(); n   } while (c <= 32); n   if (c ==  - ) { n     p = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * p; n } n void solve() { n   int m, q; n   N = nextInt(); n   m = nextInt(); n   q = nextInt(); n   Init(); n   for (int i = 0; i < m; i++) { n     int p, q; n     p = nextInt(); n     q = nextInt(); n     p--, q--; n     Union(p, q); n     V[p].push_back(q); n     V[q].push_back(p); n   } n   memset(diameter, -1, sizeof(diameter)); n   memset(visited, 0, sizeof(visited)); n   for (int i = 0; i < N; i++) { n     int setIbelongs = FindSet(i); n     if (!visited[setIbelongs]) { n       visited[setIbelongs] = 1; n       maxL = -1; n       dfs(i, 0, -1); n       maxL = -1; n       dfs(last, 0, -1); n       diameter[setIbelongs] = maxL; n     } n   } n   int x, y, p; n   while (q--) { n     p = nextInt(); n     x = nextInt(); n     x--; n     if (p == 1) { n       printf( %d n , diameter[FindSet(x)]); n     } else { n       y = nextInt(); n       y--; n       Union1(x, y); n     } n   } n } n int main() { n   Init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int NN = 200005; n vector<int> vv[NN]; n void _addedge(int x, int y) { vv[x].push_back(y); } n int dfn[NN], low[NN], stk[NN], blo[NN], block, Cnt, Now; n int ins[NN]; n void tarjan(int n) { n   int i, j; n   low[n] = dfn[n] = ++Cnt, ins[stk[++Now] = n] = 1; n   for (i = 0; i < vv[n].size(); i++) n     if (dfn[j = vv[n][i]] == 0) n       tarjan(j), low[n] = min(low[n], low[j]); n     else if (ins[j]) n       low[n] = min(low[n], low[j]); n   if (dfn[n] == low[n] && ++block) do n       blo[i = stk[Now--]] = block, ins[i] = 0; n     while (i != n); n } n int uu[NN]; n vector<int> ans[NN]; n bool vs[NN]; n void check(int n) { n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < vv[i].size(); j++) n       if (blo[i] != blo[vv[i][j]]) vs[blo[i]] = 1; n   } n   for (int i = 1; i <= n; i++) ans[blo[i]].push_back(i); n   int best = n + 1; n   int index = 0; n   for (int i = 1; i <= block; i++) n     if (!vs[i]) { n       if (best > ans[i].size()) { n         best = ans[i].size(); n         index = i; n       } n     } n   printf( %d n , ans[index].size()); n   for (int i = 0; i < ans[index].size(); i++) n     printf( %d%c , ans[index][i], i == ans[index].size() ?   n  :    ); n } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) scanf( %d , &uu[i]); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     int v1 = uu[x], v2 = uu[y]; n     if (v1 < v2) { n       if (v1 + 1 == v2) _addedge(x, y); n       if (v1 == 0 && v2 == k - 1) _addedge(y, x); n     } n     if (v2 < v1) { n       if (v2 + 1 == v1) _addedge(y, x); n       if (v2 == 0 && v1 == k - 1) _addedge(x, y); n     } n   } n   for (int i = 1; i <= n; i++) n     if (dfn[i] == 0) tarjan(i); n   check(n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1500 + 5, inf = 1e9 + 7; n int n, m; n pair<int, int> mark[M][M]; n bool A[M][M]; n inline int state(int i, int j) { return (i * M + j); } n void dfs(int i, int j, int i2, int j2) { n   i = (i % n + n) % n, j = (j % m + m) % m; n   if (!A[i][j]) return; n   if (mark[i][j] == make_pair(i2, j2)) return; n   if (mark[i][j].first < inf && mark[i][j] != make_pair(i2, j2)) { n     cout <<  Yes n ; n     exit(0); n   } n   mark[i][j] = make_pair(i2, j2); n   int dx[] = {1, 0, -1, 0}; n   int dy[] = {0, 1, 0, -1}; n   for (int k = 0; k < 4; k++) dfs(i + dx[k], j + dy[k], i2 + dx[k], j2 + dy[k]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   for (int i = 0; i < M; i++) n     for (int j = 0; j < M; j++) mark[i][j] = make_pair(inf + 1, inf + 1); n   int root; n   cin >> n >> m; n   string s; n   for (int i = 0; i < n; i++) { n     cin >> s; n     for (int j = 0; j < m; j++) n       if (s[j] !=  # ) { n         A[i][j] = 1; n         if (s[j] ==  S ) root = state(i, j); n       } n   } n   dfs(root / M, root % M, root / M, root % M); n   cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 211; n vector<pair<int, int> > con[maxn]; n vector<pair<int, int> > edge; n bool vis[maxn]; n int n, w, tmx, tv, ans; n void dfs(int x, int dis) { n   vis[x] = 1; n   if (dis > tmx) tmx = dis, tv = x; n   for (int i = 0; i < con[x].size(); i++) n     if (!vis[con[x][i].first] && w != con[x][i].second) n       dfs(con[x][i].first, dis + 1); n } n int go(int x) { n   memset(vis, 0, sizeof(vis)); n   tmx = tv = 0, dfs(x, 0); n   memset(vis, 0, sizeof(vis)); n   tmx = 0, dfs(tv, 0); n   return tmx; n } n int main() { n   scanf( %d , &n); n   int x, y; n   for (int i = 1; i < n; i++) { n     scanf( %d %d , &x, &y); n     con[x].push_back(make_pair(y, i)); n     con[y].push_back(make_pair(x, i)); n     edge.push_back(make_pair(x, y)); n   } n   for (w = 1; w < n; w++) n     ans = max(ans, go(edge[w - 1].first) * go(edge[w - 1].second)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n char g[1001][1001]; n int main() { n   int k, z, n = 2, i, j, b3[30], len = 0, from, prev, y; n   scanf( %d , &k); n   memset(g,  N , sizeof(g)); n   z = k; n   while (z) b3[len++] = z % 3, z /= 3; n   for (i = 0; i < len; i++) n     if (b3[i]) { n       from = 0; n       prev = 1; n       for (j = 0; j < i; j++) { n         for (z = 0; z < prev; z++) n           for (y = 0; y < 3; y++) g[from + z][n + y] =  Y ; n         from = n; n         prev = 3; n         n += 3; n       } n       for (z = 0; z < prev; z++) n         for (y = 0; y < b3[i]; y++) g[from + z][n + y] =  Y ; n       from = n; n       prev = b3[i]; n       n += b3[i]; n       for (; j < len - 1; j++) { n         for (z = 0; z < prev; z++) g[from + z][n] =  Y ; n         from = n; n         n++; n         prev = 1; n       } n       for (z = 0; z < prev; z++) g[from + z][1] =  Y ; n     } n   for (i = 0; i < n; i++) n     for (j = 0; j < n; j++) n       if (g[i][j] ==  Y ) g[j][i] =  Y ; n   printf( %d n , n); n   for (i = 0; i < n; i++) { n     g[i][n] = 0; n     puts(g[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k, sum = 0, i, j, ara[100000], mid, low, high; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &j); n     sum += j; n     ara[i] = sum; n   } n   scanf( %d , &m); n   for (i = 0; i < m; i++) { n     scanf( %d , &k); n     low = 0; n     high = n - 1; n     for (j = 0;; j++) { n       mid = (high + low) / 2; n       if (ara[mid] == k) { n         printf( %d n , mid + 1); n         break; n       } else if (ara[mid] > k) n         high = mid; n       else if (ara[mid] < k) n         low = mid + 1; n       if (high == low) { n         printf( %d n , low + 1); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pow(long long b, long long e, long long m) { n   long long t = 1; n   for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; n   return t; n } n template <class T> n inline bool chkmin(T &a, T b) { n   return a > b ? a = b, true : false; n } n template <class T> n inline bool chkmax(T &a, T b) { n   return a < b ? a = b, true : false; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <typename T> n T gcd(T x, T y) { n   for (T t; x; t = x, x = y % x, y = t) n     ; n   return y; n } n template <class edge> n struct Graph { n   vector<vector<edge> > adj; n   Graph(int n) { n     adj.clear(); n     adj.resize(n + 5); n   } n   Graph() { adj.clear(); } n   void resize(int n) { adj.resize(n + 5); } n   void add(int s, edge e) { adj[s].push_back(e); } n   void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); } n   vector<edge> &operator[](int t) { return adj[t]; } n }; n string S, T; n map<pair<char, char>, int> f; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> S >> T; n   int n = (int)((S).size()), m = (int)((T).size()), L = 0, R = 0; n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     for (; R < m - 1 && T[R] != S[i]; ++R) ++f[make_pair(T[R], T[R + 1])]; n     ans += R - L + 1; n     if (i < n - 1 && S[i] != S[i + 1]) ans -= f[make_pair(S[i + 1], S[i])]; n     if (T[L] == S[i]) --f[make_pair(T[L], T[L + 1])], ++L; n     if (R < m - 1) ++f[make_pair(T[R], T[R + 1])], ++R; n     if (L == m) break; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = (int)1e5 + 15; n bool isAns = 1; n int n, m; n vector<vector<int>> gr; n vector<int> a, b; n void in() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   gr.resize(n + 5); n   for (int i = 0; i < m; ++i) { n     int a, b; n     cin >> a >> b; n     gr[a].push_back(b); n     gr[b].push_back(a); n   } n } n void solution() { n   if (n == 1) { n     isAns = 0; n     return; n   } n   a.resize(n + 1); n   int last = 2; n   int ind = -1; n   for (int i = 1; i <= n; ++i) { n     if (gr[i].size() != n - 1) { n       ind = i; n       a[i] = 1; n       int cur = n; n       for (auto it : gr[i]) { n         a[it] = cur--; n       } n       break; n     } n   } n   if (ind == -1) { n     isAns = 0; n     return; n   } n   for (int i = 1; i <= n; ++i) { n     if (a[i] == 0) { n       a[i] = last++; n     } n   } n   b = a; n   pair<int, int> res = {0, 0}; n   int i = ind; n   int L = 1, R = n; n   for (auto it : gr[i]) { n     if (a[i] < a[it]) n       R = min(R, a[it] - 1); n     else n       L = max(L, a[it] + 1); n   } n   for (int j = L; j <= R; ++j) { n     if (j != a[i]) { n       res = {i, j}; n       break; n     } n   } n   if (res.first == 0) { n     isAns = 0; n   } else { n     b[res.first] = res.second; n   } n } n void out() { n   if (!isAns) { n     cout <<  NO n ; n   } else { n     cout <<  YES n ; n     for (int i = 1; i <= n; ++i) { n       cout << a[i] <<    ; n     } n     cout <<   n ; n     for (int i = 1; i <= n; ++i) { n       cout << b[i] <<    ; n     } n     cout <<   n ; n   } n } n int main() { n   in(); n   solution(); n   out(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n const long long mod = 1e9 + 7; n struct Matrix { n   int n, m; n   long long a[10][10]; n   Matrix() { memset(this->a, 0, sizeof(this->a)); } n   Matrix(int n, int m) { n     this->n = n; n     this->m = m; n     memset(this->a, 0, sizeof(this->a)); n   } n }; n Matrix operator*(Matrix a, Matrix b) { n   Matrix out = Matrix(a.n, b.m); n   for (int i = 0; i < a.n; i++) { n     for (int j = 0; j < b.m; j++) { n       for (int p = 0; p < a.m; p++) { n         out.a[i][j] = (out.a[i][j] + a.a[i][p] * b.a[p][j]) % mod; n       } n     } n   } n   return out; n } n Matrix fastPow(Matrix x, long long p) { n   Matrix answer = Matrix(x.n, x.m), current = x; n   for (int i = 0; i < x.n; i++) { n     answer.a[i][i] = 1; n   } n   while (p != 0) { n     if (p % 2 != 0) { n       answer = answer * current; n     } n     p /= 2; n     current = current * current; n   } n   return answer; n } n string fString[35]; n void precalcStrings() { n   fString[1] =  a ; n   fString[2] =  b ; n   for (int i = 3; i <= 30; i++) { n     fString[i] = fString[i - 1] + fString[i - 2]; n   } n } n int match(string s, string pattern) { n   vector<int> f; n   f.assign(pattern.size() + 1, 0); n   int len = 0; n   f[0] = 0; n   for (int i = 1; i < pattern.size(); i++) { n     if (pattern[len] == pattern[i]) { n       len++; n       f[i] = len; n     } else { n       while (pattern[len] != pattern[i] && len != 0) len = f[len - 1]; n       if (pattern[len] == pattern[i]) { n         len++; n         f[i] = len; n       } else { n         f[i] = 0; n       } n     } n   } n   len = 0; n   int answer = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] == pattern[len]) { n       len++; n       if (len == pattern.size()) { n         answer++; n         len = f[pattern.size() - 1]; n       } n     } else { n       while (s[i] != pattern[len] && len != 0) len = f[len - 1]; n       if (s[i] == pattern[len]) { n         len++; n       } else { n         len = 0; n       } n     } n   } n   return answer; n } n string getPrefix(string s, int len) { n   string out =   ; n   for (int i = 0; i < len; i++) out += s[i]; n   return out; n } n string getSuffix(string s, int len) { n   string out =   ; n   for (int i = s.size() - len; i < s.size(); i++) out += s[i]; n   return out; n } n long long k; n string s; n int val[2]; n string prefix; n string suffix[2]; n long long dp[MAXN]; n void solve() { n   if (k < 30) { n     cout << match(fString[k], s) <<   n ; n     return; n   } n   int n = s.size(); n   int ind; n   for (int i = 1; i < 30; i++) { n     if (fString[i].size() > s.size()) { n       ind = i; n       break; n     } n   } n   prefix = getPrefix(fString[ind], n - 1); n   suffix[0] = getSuffix(fString[ind], n - 1); n   suffix[1] = getSuffix(fString[ind + 1], n - 1); n   val[0] = match(suffix[0] + prefix, s); n   val[1] = match(suffix[1] + prefix, s); n   Matrix A = Matrix(1, 4); n   A.a[0][0] = match(fString[ind], s); n   A.a[0][1] = match(fString[ind + 1], s); n   A.a[0][2] = val[1]; n   A.a[0][3] = val[0]; n   Matrix B = Matrix(4, 4); n   B.a[0][0] = 0; n   B.a[0][1] = 1; n   B.a[0][2] = 0; n   B.a[0][3] = 0; n   B.a[1][0] = 1; n   B.a[1][1] = 1; n   B.a[1][2] = 0; n   B.a[1][3] = 0; n   B.a[2][0] = 0; n   B.a[2][1] = 1; n   B.a[2][2] = 0; n   B.a[2][3] = 1; n   B.a[3][0] = 0; n   B.a[3][1] = 0; n   B.a[3][2] = 1; n   B.a[3][3] = 0; n   cout << (A * fastPow(B, k - ind - 1)).a[0][1] <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   precalcStrings(); n   int T; n   cin >> k >> T; n   while (T--) { n     cin >> s; n     solve(); n   } n } n


#include <bits/stdc++.h> n bool isRight(int *c) { n   int sides[3] = {0}; n   sides[0] = (c[4] - c[2]) * (c[4] - c[2]) + (c[5] - c[3]) * (c[5] - c[3]); n   sides[1] = (c[4] - c[0]) * (c[4] - c[0]) + (c[5] - c[1]) * (c[5] - c[1]); n   sides[2] = (c[2] - c[0]) * (c[2] - c[0]) + (c[3] - c[1]) * (c[3] - c[1]); n   std::sort(sides, sides + 3); n   if (sides[0] > 0 && sides[2] == sides[0] + sides[1]) { n     return 1; n   } else { n     return 0; n   } n } n int main() { n   int points[6] = {0}; n   for (int k = 0; k < 6; k++) { n     scanf( %d , points + k); n   } n   std::string output =  NEITHER ; n   if (isRight(points)) { n     output =  RIGHT ; n   } else { n     for (int k = 0; k < 6; k++) { n       ++points[k]; n       if (isRight(points)) { n         output =  ALMOST ; n         break; n       } n       points[k] -= 2; n       if (isRight(points)) { n         output =  ALMOST ; n         break; n       } n       ++points[k]; n     } n   } n   std::cout << output << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, m, k, q, bst, onemove, pmove; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m >> k; n   bst = 1e9; n   for (int i = 1; i <= n; i++) { n     cin >> q; n     if (q < bst && i % 2 == 1) bst = q; n   } n   if (n % 2 == 0) { n     cout << 0 << endl; n     return 0; n   } else { n     onemove = n + 1; n     onemove /= 2; n     pmove = m / onemove; n     if (pmove == 0) n       bst = 0; n     else if (bst / pmove >= k) n       bst = pmove * k; n     cout << bst << endl; n   } n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); } n long long int read_int() { n   char r; n   bool start = false, neg = false; n   long long int ret = 0; n   while (true) { n     r = getchar(); n     if ((r -  0  < 0 || r -  0  > 9) && r !=  -  && !start) { n       continue; n     } n     if ((r -  0  < 0 || r -  0  > 9) && r !=  -  && start) { n       break; n     } n     if (start) ret *= 10; n     start = true; n     if (r ==  - ) n       neg = true; n     else n       ret += r -  0 ; n   } n   if (!neg) n     return ret; n   else n     return -ret; n } n int main() { n   map<long long, long long> x; n   map<long long, long long> y; n   map<pair<long long, long long>, long long> z; n   long long n, a, b, ans = 0; n   n = read_int(); n   while (n--) { n     a = read_int(); n     b = read_int(); n     ans += (x[a]++); n     ans += (y[b]++); n     ans -= z[make_pair(a, b)]++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int inf = 0x3f3f3f3f; n const long long Inf = 0x3f3f3f3f3f3f3f3f; n const int maxn = 1e3 + 4; n const int maxm = 1e5 + 4; n vector<int> v; n int n, k, b[maxn]; n int main() { n   ios::sync_with_stdio(false); n   int i, j, kase; n   cin >> n >> k; n   for (i = 1; i <= n; ++i) cin >> b[i]; n   v.push_back(n); n   auto it = v.begin(); n   for (i = n - 1; i >= 1; --i) { n     it = v.begin(); n     int num = 0; n     while (num < b[i]) { n       if (*it >= i + k) num++; n       it++; n     } n     v.insert(it, i); n   } n   for (i = 0; i < v.size(); ++i) { n     if (i) cout <<    ; n     cout << v[i]; n   } n   cout << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n vector<tuple<long long int, long long int, long long int> > v; n vector<long long int> v1, v3; n vector<pair<char, long long int> > v2; n long long int i, j, k, n, sum = 0, p, r, ct = 0, q, sum1 = 0, sum2, t; n set<long long int> s, s1; n priority_queue<long long int> g; n const int maxn = 1e3 + 100, inf = 1e9 + 100; n long long int dp[maxn][3]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> t; n   while (t--) { n     q = 0; n     cin >> n; n     long long int a[n]; n     map<long long int, long long int> m, m1; n     for (i = 1; i < n + 1; i++) { n       cin >> a[i]; n       m[a[i]] = i; n     } n     p = m[1]; n     for (i = 1; i < n + 1; i++) { n       if (m[i] != p) { n         q = 1; n         break; n       } else { n         a[m[i]] = -1; n       } n       if (m[i] == n && i < n) n         p = m[i + 1]; n       else if (m[i] < n && a[m[i] + 1] != -1) n         p++; n       else if (m[i] < n && a[m[i] + 1] == -1) n         p = m[i + 1]; n     } n     if (q != 1) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n   point(int a = 0, int b = 0) { n     x = a; n     y = b; n   } n }; n struct cmpx { n   bool operator()(point a, point b) { n     if (a.x == b.x) return a.y < b.y; n     return a.x < b.x; n   } n }; n struct cmpy { n   bool operator()(point a, point b) { n     if (a.y == b.y) return a.x < b.x; n     return a.y < b.y; n   } n }; n bool ltx(point a, point b) { n   if (a.x == b.x) return a.y < b.y; n   return a.x < b.x; n } n int conq(set<point, cmpx>& sx, set<point, cmpy>& sy) { n   if (sx.size() == 1) return 1; n   set<point, cmpx> sx1; n   set<point, cmpy> sy1; n   set<point, cmpx>::iterator itx1; n   set<point, cmpx>::reverse_iterator itx2; n   set<point, cmpy>::iterator ity1; n   set<point, cmpy>::reverse_iterator ity2; n   itx1 = sx.begin(); n   itx2 = sx.rbegin(); n   ity1 = sy.begin(); n   ity2 = sy.rbegin(); n   int t; n   do { n     t = itx1->x; n     itx1++; n     if (itx1->x - t > 1) { n       while (sx.begin()->x <= t) { n         sx1.insert(*sx.begin()); n         sy1.insert(*sx.begin()); n         sy.erase(*sx.begin()); n         sx.erase(*sx.begin()); n       } n       return conq(sx, sy) + conq(sx1, sy1); n     } n     t = itx2->x; n     itx2++; n     if (t - itx2->x > 1) { n       while (sx.rbegin()->x >= t) { n         sx1.insert(*sx.rbegin()); n         sy1.insert(*sx.rbegin()); n         sy.erase(*sx.rbegin()); n         sx.erase(*sx.rbegin()); n       } n       return conq(sx, sy) + conq(sx1, sy1); n     } n     t = ity1->y; n     ity1++; n     if (ity1->y - t > 1) { n       while (sy.begin()->y <= t) { n         sx1.insert(*sy.begin()); n         sy1.insert(*sy.begin()); n         sx.erase(*sy.begin()); n         sy.erase(*sy.begin()); n       } n       return conq(sx, sy) + conq(sx1, sy1); n     } n     t = ity2->y; n     ity2++; n     if (t - ity2->y > 1) { n       while (sy.rbegin()->y >= t) { n         sx1.insert(*sy.rbegin()); n         sy1.insert(*sy.rbegin()); n         sx.erase(*sy.rbegin()); n         sy.erase(*sy.rbegin()); n       } n       return conq(sx, sy) + conq(sx1, sy1); n     } n   } while (ltx(*itx1, *itx2)); n   return 1; n } n int main() { n   int n, i, x, y; n   set<point, cmpx> sx; n   set<point, cmpy> sy; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &x, &y); n     sx.insert(point(x, y)); n     sy.insert(point(x, y)); n   } n   printf( %d , conq(sx, sy)); n } n


#include <bits/stdc++.h> n const int MAXN = 200010; n const int mod = 1000000007; n void reduce(int& x) { x += x >> 31 & mod; } n int mul(int a, int b) { return (long long)a * b % mod; } n int remod(long long x) { n   x %= mod; n   return x + (x >> 63 & mod); n } n int n; n int A[MAXN], W[MAXN]; n long long rt[MAXN]; n void mdfc(int x, int v) { n   for (; x <= n; x += x & -x) rt[x] += v; n } n long long qry(int x) { n   long long res = 0; n   for (; x; x &= x - 1) res += rt[x]; n   return res; n } n long long qry(int l, int r) { return qry(r) - qry(l - 1); } n int tr[MAXN]; n void mdfv(int x, int v) { n   for (v -= mod; x <= n; x += x & -x) reduce(tr[x] += v); n } n int qryv(int x) { n   int res = 0; n   for (; x; x &= x - 1) reduce(res += tr[x] - mod); n   return res; n } n int qryv(int l, int r) { n   int x = qryv(r) - qryv(l - 1); n   return x + (x >> 31 & mod); n } n void add(int x, int v) { mdfc(x, v), mdfv(x, remod(mul(A[x], v))); } n int main() { n   std::ios_base::sync_with_stdio(false), std::cin.tie(0); n   int T; n   std::cin >> n >> T; n   for (int i = 1; i <= n; ++i) std::cin >> A[i], A[i] -= i; n   for (int i = 1; i <= n; ++i) std::cin >> W[i], add(i, W[i]); n   while (T-- > 0) { n     int x, y; n     std::cin >> x >> y; n     if (x < 0) n       x = -x, add(x, y - W[x]), W[x] = y; n     else { n       int l = x, r = y, at = -1; n       long long sm = qry(l, r); n       while (l <= r) { n         int mid = l + r >> 1; n         if (qry(x, mid) * 2 >= sm) n           at = mid, r = mid - 1; n         else n           l = mid + 1; n       } n       l = x, r = y; n       int X = A[at]; n       long long ans = qry(l, at) % mod * X - qryv(l, at); n       ans += qryv(at + 1, r) - qry(at + 1, r) % mod * X; n       std::cout << remod(ans) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long n; n   scanf( %ld n , &n); n   std::vector<long> blocks(n + 2); n   for (long p = 1; p <= n; p++) { n     scanf( %ld , &blocks[p]); n   } n   for (long p = 1; p <= n; p++) { n     blocks[p] = std::min(blocks[p], 1 + blocks[p - 1]); n   } n   for (long p = n; p >= 0; p--) { n     blocks[p] = std::min(blocks[p], 1 + blocks[p + 1]); n   } n   long final(0); n   for (long p = 1; p <= n; p++) { n     final = std::max(final, blocks[p]); n   } n   printf( %ld n , final); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000010; n int a[N], pos[N], cnt[N]; n long long ans[N], res; n struct node { n   int l, r, idx; n   bool operator<(const node& x) const { n     if (pos[l] == pos[x.l]) return r < x.r; n     return l < x.l; n   } n } q[N]; n int L, R, n, m, k; n void update(int x, int add) { n   if (add > 0) { n     res += cnt[a[x] ^ k]; n     cnt[a[x]]++; n   } else { n     cnt[a[x]]--; n     res -= cnt[a[x] ^ k]; n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   int sz = sqrt(n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     a[i] ^= a[i - 1]; n     pos[i] = i / sz; n   } n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &q[i].l, &q[i].r); n     q[i].idx = i; n   } n   sort(q + 1, q + m + 1); n   cnt[0] = 1; n   L = 1, R = 0, res = 0; n   for (int i = 1; i <= m; ++i) { n     while (q[i].l < L) update(--L - 1, 1); n     while (q[i].l > L) update(L++ - 1, -1); n     while (q[i].r < R) update(R--, -1); n     while (q[i].r > R) update(++R, 1); n     ans[q[i].idx] = res; n   } n   for (int i = 1; i <= m; ++i) printf( %I64d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p, k; n long long r[20005]; n int i, j; n int main() { n   scanf( %I64d%I64d , &p, &k); n   while (p) { n     r[i] = p % (-k); n     p = p / (-k); n     while (r[i] < 0) { n       r[i] = r[i] + k; n       p++; n     } n     i++; n   } n   if (i == 0) i++; n   for (j = 0; j < i; j++) n     if (r[j] >= k) { n       printf( -1 n ); n       return 0; n     } n   printf( %d n , i); n   for (j = 0; j < i; j++) printf(j == i - 1 ?  %I64d n  :  %I64d  , r[j]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200 + 5; n string s; n int dist[maxn * maxn]; n bool a[maxn][maxn], chek[maxn * maxn]; n vector<int> v[maxn * maxn]; n void bfs(int x) { n   chek[x] = true; n   queue<int> q; n   q.push(x); n   while (!q.empty()) { n     x = q.front(); n     q.pop(); n     for (int i = 0; i < (int)v[x].size(); i++) n       if (chek[v[x][i]] == false) { n         q.push(v[x][i]); n         chek[v[x][i]] = true; n         dist[v[x][i]] = dist[x] + 1; n       } n   } n } n int main() { n   int x = 102, y = 102, ii, jj, e[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; n   cin >> s; n   ii = x; n   jj = y; n   a[ii][jj] = true; n   for (int i = 0; i < (int)s.size(); i++) { n     if (s[i] ==  U ) n       ii--; n     else if (s[i] ==  D ) n       ii++; n     else if (s[i] ==  R ) n       jj++; n     else if (s[i] ==  L ) n       jj--; n     a[ii][jj] = true; n   } n   for (int i = 0; i < maxn; i++) n     for (int j = 0; j < maxn; j++) n       for (int d = 0; d < 4; d++) { n         if (a[i][j] == false) { n           chek[i * maxn + j] = true; n           continue; n         } n         if (i + e[d][0] >= 0 && i + e[d][0] < maxn && j + e[d][1] < maxn && n             j + e[d][1] >= 0) n           if (a[i + e[d][0]][j + e[d][1]] == true) n             v[i * maxn + j].push_back((i + e[d][0]) * maxn + j + e[d][1]); n       } n   bfs(x * maxn + y); n   if (dist[ii * maxn + jj] < (int)s.size()) { n     cout <<  BUG  << endl; n     return 0; n   } n   cout <<  OK  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const N = 2e5 + 70; n long long n, k, a[N], cnt[N], ans, fi = -1, la = -1; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (long long i = 0; i <= n; i++) { n     cin >> a[i]; n     cnt[i] = a[i]; n   } n   for (long long i = 0; i < n + 50; i++) { n     if (cnt[i] < 0) { n       cnt[i + 1] += cnt[i]; n       cnt[i] = -cnt[i]; n     } n     cnt[i + 1] += cnt[i] / 2; n     cnt[i] &= 1; n     if (cnt[i]) { n       if (fi == -1) fi = i; n       la = i; n     } n   } n   if (cnt[n + 50]) { n     fi = la = -1; n     memset(cnt, 0, sizeof cnt); n     for (long long i = 0; i <= n; i++) a[i] = -a[i], cnt[i] = a[i]; n     for (long long i = 0; i < n + 50; i++) { n       if (cnt[i] < 0) { n         cnt[i + 1] += cnt[i]; n         cnt[i] = -cnt[i]; n       } n       cnt[i + 1] += cnt[i] / 2; n       cnt[i] &= 1; n       if (cnt[i]) { n         if (fi == -1) fi = i; n         la = i; n       } n     } n   } n   if (la - fi >= 35) return cout <<  0 n , 0; n   long long res = 0; n   for (long long i = fi; i <= la; i++) res += (1 << (i - fi)) * cnt[i]; n   long long cur = res; n   for (long long i = fi; i >= 0 && cur <= 2e9; i--) { n     if (i <= n && a[i] - cur >= -k && (i != n || a[i] - cur != 0)) ans++; n     cur *= 2; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[310]; n long long fac[310], rev[310]; n bool mmp[310][310]; n long long mod = (long long)1e9 + 7; n long long qmod(long long a, long long p) { n   long long ret = 1; n   while (p) { n     if (p & 1) ret = (ret * a) % mod; n     a = (a * a) % mod; n     p /= 2; n   } n   return ret; n } n void Init() { n   fac[0] = 1; n   for (int i = 1; i < 310; ++i) fac[i] = fac[i - 1] * i; n   rev[0] = 1; n   for (int i = 1; i < 310; ++i) rev[i] = qmod(fac[i], mod - 2); n } n long long C(int n, int m) { return fac[n] * rev[m] % mod * rev[n - m] % mod; } n bool isSqure(long long q) { n   long long sq = (long long)sqrt(q); n   return sq * sq == q; n } n void input() { n   istream& in = cin; n   in >> n; n   for (int i = 1; i <= n; ++i) in >> a[i]; n } n bool vis[310]; n int dfs(int p) { n   if (vis[p]) return 0; n   vis[p] = 1; n   int ret = 1; n   for (int i = 1; i <= n; ++i) { n     if (mmp[p][i]) { n       ret += dfs(i); n     } n   } n   return ret; n } n int num[310]; n long long dp[310][310][310]; n bool END[310]; n void add(long long& a, long long b) { n   a %= mod; n   b %= mod; n   a = (a + b) % mod; n } n int main() { n   input(); n   Init(); n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) { n       if (isSqure(a[i] * a[j])) { n         mmp[i][j] = mmp[j][i] = 1; n       } n     } n   vector<int> block; n   for (int i = 1; i <= n; ++i) { n     int ret = dfs(i); n     if (ret) block.push_back(ret); n   } n   sort(block.begin(), block.end()); n   reverse(block.begin(), block.end()); n   int sum = 0; n   for (int p : block) { n     for (int i = 1; i <= p; ++i) num[sum + i] = i; n     sum += p; n     END[sum] = 1; n   } n   dp[0][0][0] = 1; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= n + 1; ++j) n       for (int k = 0; k <= n + 1; ++k) { n         long long& ref = dp[i - 1][j][k]; n         if (dp[i - 1][j][k] == 0) continue; n         int allslot = i; n         int prewa = j - k; n         int nowwa = k; n         int nookwaslot = (num[i] - 1) * 2 - 2 * nowwa; n         int okslot = allslot - j - nookwaslot; n         if (!END[i]) { n           if (j > 0) add(dp[i][j - 1][k], dp[i - 1][j][k] * prewa); n           add(dp[i][j + 1][k + 1], dp[i - 1][j][k] * nowwa); n           add(dp[i][j][k], dp[i - 1][j][k] * okslot); n           add(dp[i][j + 1][k + 1], dp[i - 1][j][k] * nookwaslot); n         } else { n           if (j > 0) add(dp[i][j - 1][0], dp[i - 1][j][k] * prewa); n           add(dp[i][j + 1][0], dp[i - 1][j][k] * nowwa); n           add(dp[i][j][0], dp[i - 1][j][k] * okslot); n           add(dp[i][j + 1][0], dp[i - 1][j][k] * nookwaslot); n         } n       } n   cout << dp[n][0][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int MOD = 998244353; n int add(int x, int y) { n   int res = x + y; n   return res < MOD ? res : res - MOD; n } n int mul(int x, int y) { return (long long)x * y % MOD; } n int pow(int base, int p) { n   int res = 1; n   while (p > 0) { n     if (p & 1) res = mul(res, base); n     base = mul(base, base); n     p /= 2; n   } n   return res; n } n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   std::vector<int> dp(std::min(k, n) + 1, 0); n   dp[1] = n; n   for (int i = 2; i <= k; ++i) n     for (int j = std::min(i, n); j >= 1; --j) n       dp[j] = add(mul(dp[j - 1], n - j + 1), mul(dp[j], j)); n   int res = 0; n   for (int i = 1; i < dp.size(); ++i) n     res = add(res, mul(dp[i], pow(pow(m, MOD - 2), i))); n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n int id(string s) { n   if (s ==  C ) n     return 1; n   else if (s ==  C# ) n     return 2; n   else if (s ==  D ) n     return 3; n   else if (s ==  D# ) n     return 4; n   else if (s ==  E ) n     return 5; n   else if (s ==  F ) n     return 6; n   else if (s ==  F# ) n     return 7; n   else if (s ==  G ) n     return 8; n   else if (s ==  G# ) n     return 9; n   else if (s ==  A ) n     return 10; n   else if (s ==  B ) n     return 11; n   else if (s ==  H ) n     return 12; n } n int Distance(string s, string t) { n   int a = id(s); n   int b = id(t); n   if (b < a) b += 12; n   return b - a; n } n int main() { n   string x, y, z; n   int i, j, k; n   vector<string> v; n   cin >> x >> y >> z; n   v.push_back(x); n   v.push_back(y); n   v.push_back(z); n   for (i = 0; i < 3; i++) { n     for (j = 0; j < 3; j++) { n       for (k = 0; k < 3; k++) { n         if (Distance(v[i], v[j]) == 4 and Distance(v[j], v[k]) == 3) { n           cout <<  major ; n           return 0; n         } else if (Distance(v[i], v[j]) == 3 and Distance(v[j], v[k]) == 4) { n           cout <<  minor ; n           return 0; n         } n       } n     } n   } n   cout <<  strange ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string n; n   cin >> n; n   vector<string> vec; n   int si = n.size(); n   for (int i = 0; i < si; i++) { n     if (n[i] ==  0 ) continue; n     string s =   ; n     s += n[i]; n     for (int j = i + 1; j < si; j++) { n       s +=  0 ; n     } n     vec.push_back(s); n   } n   cout << vec.size() <<   n ; n   for (int i = 0; i < int(vec.size()); i++) { n     cout << vec[i] <<    ; n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static int aQWEs = []() -> int { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   return 0; n }(); n long long int power(long long int a, long long int p, n                     long long int m = 1000000007) { n   a %= m; n   long long int val = 1; n   while (p > 0) { n     if (p & 1) { n       val = (val * a) % m; n     } n     a = (a * a) % m; n     p >>= 1; n   } n   return (val); n } n int dfs(const vector<int>& v, std::vector<bool>& vis, int cur, int depth) { n   vis[cur] = 1; n   if (!vis[v[cur]]) return dfs(v, vis, v[cur], depth + 1); n   return depth; n } n int main() { n   long long int i, j; n   int T = 1; n   while (T--) { n     int n; n     cin >> n; n     std::vector<int> v(n + 1); n     std::vector<bool> vis(n + 1); n     int ans = 0; n     for (i = 1; i <= n; ++i) { n       cin >> v[i]; n     } n     for (i = 1; i <= n; ++i) { n       if (!vis[i]) ans += dfs(v, vis, i, 0); n     } n     if (!((3 * n - ans) & 1)) { n       cout <<  Petr n ; n     } else n       cout <<  Um_nik n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const long long INFL = LLONG_MAX; n int N, w, W[100100], b, B[100100], C[100100], S[100100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> N; n   for (int(i) = 1; (i) <= (N); (i)++) { n     cin >> C[i] >> S[i]; n     if (C[i]) n       B[b++] = i; n     else n       W[w++] = i; n   } n   w--, b--; n   while (w >= 0 && b >= 0) { n     int s = min(S[W[w]], S[B[b]]); n     cout << W[w] <<     << B[b] <<     << s << endl; n     S[W[w]] -= s; n     S[B[b]] -= s; n     if (S[B[b]]) n       w--; n     else n       b--; n   } n   w--; n   b--; n   while (w >= 0) cout << W[w] <<     << B[0] <<     << 0 << endl, w--; n   while (b >= 0) cout << W[0] <<     << B[b] <<     << 0 << endl, b--; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 3e5 + 10; n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n int n, q; n int cnt[M]; n string ss; n void solve() { n   cin >> n >> q; n   cin >> ss; n   for (int i = 1; i <= n; i++) { n     if (i % 2) n       cnt[i] = ss[i - 1] ==  +  ? 1 : -1; n     else n       cnt[i] = ss[i - 1] ==  -  ? 1 : -1; n     cnt[i] += cnt[i - 1]; n   } n   int l, r; n   while (q--) { n     cin >> l >> r; n     int ans = cnt[r] - cnt[l - 1]; n     if (ans == 0) n       cout << 0 << endl; n     else if ((r - l + 1) % 2) n       cout << 1 << endl; n     else n       cout << 2 << endl; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   ; n   int T; n   cin >> T; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int findLengthOfLNDS(vector<pair<int, float> > &plants_data) { n   if (plants_data.size() == 0) return 0; n   vector<int> m; n   m.push_back(0); n   for (int i = 1; i < plants_data.size(); i++) { n     if (plants_data[m.back()].first <= plants_data[i].first) { n       m.push_back(i); n     } else { n       int b = 0, e = m.size() - 1, mid = 0, replace_at = 0; n       while (b <= e) { n         mid = (b + e) / 2; n         if (plants_data[i].first >= plants_data[m[mid]].first) { n           b = mid + 1; n         } else { n           e = mid - 1; n         } n       } n       if (plants_data[m[mid]].first <= plants_data[i].first) { n         replace_at = mid + 1; n       } else { n         replace_at = mid; n       } n       m[replace_at] = i; n     } n   } n   return m.size(); n } n int main() { n   int no_plants, no_species; n   vector<pair<int, float> > plants_data; n   cin >> no_plants >> no_species; n   for (int i = 0; i < no_plants; i++) { n     int speci; n     float location; n     cin >> speci >> location; n     plants_data.push_back(make_pair(speci, location)); n   } n   cout << no_plants - findLengthOfLNDS(plants_data); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n0, n1, n2; n     cin >> n0 >> n1 >> n2; n     if (n1 == 0 && n2 == 0) { n       cout << string(n0 + 1,  0 ) << endl; n       continue; n     } n     string result = string(n2 + 1,  1 ); n     char turn =  0 ; n     while (n1--) { n       result += turn; n       if (turn ==  0 ) { n         turn =  1 ; n       } else { n         turn =  0 ; n       } n     } n     if (result[result.size() - 1] ==  0 ) { n       result += string(n0,  0 ); n     } else { n       result = result.substr(0, result.size() - 1); n       result = result + string(n0,  0 ) +  1 ; n     } n     cout << result << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n string s; n bool flag; n void solve() { n   int n, k; n   cin >> n >> k; n   unordered_set<int> s; n   queue<int> q; n   vector<int> a(n); n   for (auto i = 0; i < n; i++) cin >> a[i]; n   ; n   for (auto i = 0; i < n; i++) { n     if (s.find(a[i]) == s.end()) { n       if (s.size() >= k) { n         int kk = q.front(); n         s.erase(kk); n         q.pop(); n       } n       s.insert(a[i]); n       q.push(a[i]); n     } n   } n   stack<int> st; n   while (!q.empty()) { n     int aa = q.front(); n     st.push(aa); n     q.pop(); n   } n   cout << st.size() << endl; n   while (!st.empty()) { n     int aa = st.top(); n     st.pop(); n     cout << aa <<    ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int modn = (int)1e9 + 7, N = (int)1e5 + 10; n int a[N], p[N][6], q[N], c[6][6][6], n, m; n inline void add(int &x, int y) { n   x += y; n   if (x >= modn) x %= modn; n } n struct zz { n   int power, x, s, t, tot; n   int v[N << 2], ls[N << 2], rs[N << 2], b[N << 2]; n   void build(int l, int r, int now) { n     b[now] = -1; n     if (l == r) { n       v[now] = (long long)a[l] * p[l][power] % modn; n       return; n     } n     int mid = (l + r) >> 1; n     ls[now] = ++tot; n     rs[now] = ++tot; n     build(l, mid, ls[now]); n     build(mid + 1, r, rs[now]); n     add(v[now], v[ls[now]]); n     add(v[now], v[rs[now]]); n   } n   void push(int l, int r, int now) { n     if (b[now] != -1 && l != r) { n       int mid = (l + r) >> 1; n       v[ls[now]] = n           (long long)b[now] * (p[mid][power] - p[l - 1][power] + modn) % modn; n       v[rs[now]] = n           (long long)b[now] * (p[r][power] - p[mid][power] + modn) % modn; n       b[rs[now]] = b[ls[now]] = b[now]; n       b[now] = -1; n     } n   } n   void make(int l, int r, int now) { n     if (l >= s && r <= t) { n       b[now] = x; n       v[now] = (long long)x * (p[r][power] - p[l - 1][power] + modn) % modn; n       return; n     } n     push(l, r, now); n     int mid = (l + r) >> 1; n     if (mid >= s) make(l, mid, ls[now]); n     if (mid < t) make(mid + 1, r, rs[now]); n     v[now] = 0; n     add(v[now], v[ls[now]]); n     add(v[now], v[rs[now]]); n   } n   int ask(int l, int r, int now) { n     if (l >= s && r <= t) return v[now]; n     push(l, r, now); n     int mid = (l + r) >> 1, ret = 0; n     if (mid >= s) add(ret, ask(l, mid, ls[now])); n     if (mid < t) add(ret, ask(mid + 1, r, rs[now])); n     v[now] = 0; n     add(v[now], v[ls[now]]); n     add(v[now], v[rs[now]]); n     return ret; n   } n } T[6]; n inline void read(int &ret) { n   ret = 0; n   char x = getchar(); n   while (x <  0  || x >  9 ) x = getchar(); n   while (x >=  0  && x <=  9 ) { n     ret = ret * 10 + x -  0 ; n     x = getchar(); n   } n } n inline int spj() { n   static int l, r, k, ch[6], pp[6]; n   int ret = 0; n   read(l); n   read(r); n   read(k); n   pp[0] = 1; n   for (int i = 0; i <= k; ++i) { n     if (i != 0) pp[i] = (long long)pp[i - 1] * (modn - l) % modn; n     T[i].s = l; n     T[i].t = r; n     ch[i] = T[i].ask(1, n, 0); n   } n   for (int i = 0; i <= k; ++i) n     for (int j = 0; i + j <= k; ++j) n       add(ret, (long long)ch[i] * c[i][j][k - i - j] % modn * pp[j] % modn); n   return ret; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) read(a[i]); n   for (int i = 1; i <= n; ++i) { n     p[i][0] = 1; n     for (int j = 1; j < 6; ++j) p[i][j] = (long long)p[i][j - 1] * i % modn; n   } n   for (int i = 0; i < 6; ++i) T[i].power = i; n   for (int i = 0; i < 6; ++i) T[i].build(1, n, 0); n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j < 6; ++j) add(p[i][j], p[i - 1][j]); n   q[0] = 1; n   for (int i = 1; i < 6; ++i) q[i] = q[i - 1] * i; n   for (int i = 0; i < 6; ++i) n     for (int j = 0; j < 6; ++j) n       for (int k = 0; k < 6; ++k) n         if (i + j + k < 6) c[i][j][k] = q[i + j + k] / q[i] / q[j] / q[k]; n   for (int i = 1; i <= m; ++i) { n     int l, r, x; n     char cc = getchar(); n     while (cc !=  ?  && cc !=  = ) cc = getchar(); n     if (cc ==  = ) { n       read(l); n       read(r); n       read(x); n       for (int i = 0; i < 6; ++i) { n         T[i].s = l; n         T[i].t = r; n         T[i].x = x; n         T[i].make(1, n, 0); n       } n     } else n       printf( %d n , spj()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(1e5) + 10; n const int K = 11; n const int MOD = int(1e9) + 7; n const int INF = int(2e9) + 5; n const long long INF64 = 1e18; n int dp[N]; n void solve() { n   string s; n   cin >> s; n   stack<int> st; n   int n = s.length(); n   for (int i = 0; i < n; i++) { n     if (s[i] ==  (  || s[i] ==  [  || s[i] ==  { ) { n       st.push(i); n     } else { n       if (st.empty()) continue; n       int t = st.top(); n       if (s[i] ==  )  && s[t] ==  ( ) { n         dp[i] = i - t + 1; n         if (t > 0) dp[i] += dp[t - 1]; n         st.pop(); n       } else if (s[i] ==  ]  && s[t] ==  [ ) { n         dp[i] = i - t + 1; n         if (t > 0) dp[i] += dp[t - 1]; n         st.pop(); n       } else if (s[i] ==  }  && s[t] ==  { ) { n         dp[i] = i - t + 1; n         if (t > 0) dp[i] += dp[t - 1]; n         st.pop(); n       } else { n         while (!st.empty()) st.pop(); n       } n     } n   } n   int i = n - 1; n   int mx = 0, index = -1; n   while (i >= 0) { n     if (dp[i] == 0) { n       i--; n       continue; n     } n     int cnt = 0, t = i; n     int x = dp[i]; n     for (int j = 0; j < x; j++) { n       if (s[i] ==  [ ) cnt++; n       i--; n     } n     if (cnt > mx) { n       mx = cnt; n       index = t; n     } n   } n   if (mx == 0) { n     cout << 0 <<   n ; n     return; n   } n   cout << mx <<   n ; n   cout << s.substr(index + 1 - dp[index], dp[index]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005]; n int main() { n   int n; n   int i; n   while (scanf( %d , &n) != EOF) { n     for (i = 0; i < n; i++) { n       scanf( %d , &a[i]); n     } n     sort(a, a + n); n     int p = (n - 2) / 2; n     int ans = 1e9; n     for (i = 0; i < n - p - 1; i++) { n       int dis = a[i + p + 1] - a[i]; n       if (ans > dis) ans = dis; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 10; n const int B = 1e3 + 10; n bool vis[N], inque[2 * N]; n int n, k; n vector<int> v; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < k; i++) { n     int t; n     scanf( %d , &t); n     if (!vis[t]) { n       vis[t] = true; n       v.push_back(t); n     } n   } n   for (int i = 0; i < v.size(); i++) { n     v[i] -= n; n   } n   queue<pair<int, int>> Q; n   Q.push(make_pair(0, 0)); n   inque[B] = true; n   while (!Q.empty()) { n     pair<int, int> t = Q.front(); n     Q.pop(); n     inque[t.first + B] = 0; n     for (int i = 0; i < v.size(); i++) { n       int u = v[i] + t.first; n       if (u < -1000 || u > 1000) continue; n       if (!inque[u + B]) { n         if (u == 0) { n           printf( %d n , t.second + 1); n           return 0; n         } n         inque[u + B] = true; n         Q.push(make_pair(u, t.second + 1)); n       } n     } n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005]; n long long req = 1e18; n long long steps(long long n) { n   long long c = 1; n   if (n == 1) return 1e6; n   while (req > 1) { n     req /= n; n     c++; n   } n   req = 1e18; n   return c; n } n long long pow_exp(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n % 2) res = (res * a); n     a = (a * a); n     n /= 2ll; n   } n   return res; n } n int main() { n   long long t = 1; n   while (t--) { n     long long n; n     cin >> n; n     long long ans = 0; n     for (long long i = 0; i < n; i++) cin >> a[i], ans += (a[i] - 1ll); n     sort(a, a + n); n     if (n == 1) { n       cout << a[0] - 1ll << endl; n       continue; n     } n     long long st = 2; n     while (steps(st) >= n - 1) { n       long long sol = 0; n       for (long long i = 0; i < n; i++) { n         long long u = pow_exp(st, i); n         sol += abs(u - a[i]); n       } n       ans = min(ans, sol); n       if (pow_exp(st, n - 1) > a[n - 1]) break; n       st++; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct matrix { n   long long a[100][100]; n   void init() { memset(a, 0, sizeof(a)); } n   friend matrix operator*(matrix x, matrix y) { n     matrix ans; n     ans.init(); n     for (int i = 0; i < 100; i++) n       for (int j = 0; j < 100; j++) { n         for (int k = 0; k < 100; k++) n           ans.a[i][j] += (x.a[i][k] * y.a[k][j]) % 1000000007; n         ans.a[i][j] %= 1000000007; n       } n     return ans; n   } n   friend matrix operator^(matrix x, long long y) { n     matrix ans; n     if (y == 0) { n       memset(ans.a, 0, sizeof(ans.a)); n       for (int i = 0; i < 100; i++) ans.a[i][i] = 1; n       return ans; n     } else n       while ((y & 1) == 0) y >>= 1, x = x * x; n     ans = x; n     y >>= 1; n     for (; y != 0; y >>= 1) { n       x = x * x; n       if ((y & 1) != 0) ans = ans * x; n     } n     return ans; n   } n } x; n int n, m, Mod, a[1000010]; n int main() { n   x.init(); n   matrix h, t; n   h.init(); n   t.init(); n   scanf( %d%d%d , &n, &m, &Mod); n   for (int i = 1; i <= n; i++) { n     int v; n     scanf( %d , &v); n     h.a[0][v % Mod]++; n   } n   for (int i = 1; i <= n; i++) { n     int v; n     scanf( %d , &v); n     a[i] = v; n     for (int j = 0; j < Mod; j++) x.a[j][(j + v) % Mod]++; n   } n   matrix ans = h * (x ^ (m - 2)); n   for (int i = 1; i <= n; i++) { n     int v; n     scanf( %d , &v); n     int d = (Mod - (v + a[i]) % Mod) % Mod; n     t.a[d][0]++; n   } n   ans = ans * t; n   printf( %I64d n , ans.a[0][0]); n } n


#include <bits/stdc++.h> n using namespace std; n int N; n bool indegree[200010]; n int a[200010]; n bool visited[200010]; n vector<pair<int, int> > ans; n int main() { n   int i, j, k; n   scanf( %d , &N); n   for (i = (0); i < (N); i++) scanf( %d , a + i); n   for (i = (0); i < (N); i++) a[i]--; n   memset((visited), 0, sizeof(visited)); n   ; n   memset((indegree), 0, sizeof(indegree)); n   ; n   for (i = (0); i < (N); i++) indegree[a[i]] = true; n   bool f = false; n   for (i = (0); i < (N); i++) { n     if (indegree[i]) continue; n     f = true; n     int x = i; n     while (!visited[x]) { n       visited[x] = true; n       indegree[x] = true; n       x = a[x]; n     } n     ans.push_back(make_pair(i, x)); n   } n   for (i = (0); i < (N); i++) { n     if (visited[i]) continue; n     int x = i; n     while (!visited[x]) { n       visited[x] = true; n       x = a[x]; n     } n     ans.push_back(make_pair(i, i)); n   } n   if (!f && ans.size() < 2) { n     printf( 0 n ); n     return 0; n   } n   ans.push_back(ans[0]); n   printf( %d n , ans.size() - 1); n   for (i = (0); i < (ans.size() - 1); i++) n     printf( %d %d n , ans[i].second + 1, ans[i + 1].first + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string base, s; n   cin >> base >> s; n   int n = base.size(), m = s.size(); n   base =  *  + base; n   s =  *  + s; n   int f[m + 2][n + 2]; n   memset(f, 0, sizeof(f)); n   for (int i = m; i >= 1; i--) n     for (int j = n; j >= 1; j--) n       if (s[i] == base[j]) f[i][j] = f[i + 1][j + 1] + 1; n   int g[m + 2][n + 2]; n   memset(g, 0, sizeof(g)); n   for (int i = m; i >= 1; i--) n     for (int j = 1; j <= n; j++) n       if (s[i] == base[j]) g[i][j] = g[i + 1][j - 1] + 1; n   vector<pair<int, int>> ans; n   for (int i = 1; i <= m;) { n     int l1 = max_element(f[i] + 1, f[i] + n + 1) - f[i]; n     int l2 = max_element(g[i] + 1, g[i] + n + 1) - g[i]; n     if (!f[i][l1] && !g[i][l2]) { n       cout << -1; n       return 0; n     } n     if (f[i][l1] >= g[i][l2]) n       ans.push_back({l1, l1 + f[i][l1] - 1}); n     else n       ans.push_back({l2, l2 - g[i][l2] + 1}); n     i += max(f[i][l1], g[i][l2]); n   } n   cout << ans.size() <<   n ; n   for (auto p : ans) cout << p.first <<     << p.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, q; n   scanf( %lld %lld , &n, &q); n   while (q--) { n     long long x; n     scanf( %lld , &x); n     while (x % 2 == 0) { n       x += (n - x / 2); n     } n     printf( %lld n , x / 2 + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, x; n   while (cin >> n >> k) { n     long long sum = 0; n     x = n * (k - 1) / k - 10; n     if (x <= 0) { n       x = 1; n     } n     long long c = x; n     sum = x; n     while (sum < n) { n       c /= k; n       sum += c; n       if (sum >= n) { n         break; n       } n       if (c / k == 0 && sum < n) { n         x++; n         c = x; n         sum = c; n       } n     } n     cout << x << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005, inf = 1e9 + 7; n int fa[maxn]; n long long n, m, dp[maxn]; n int sz[maxn], costs[maxn]; n vector<int> fas; n bool vis[maxn]; n int findfa(int x) { n   if (fa[x] == x) n     return x; n   else n     return fa[x] = findfa(fa[x]); n } n void bag(int wei, int val) { n   for (int i = n; i >= wei; i--) { n     dp[i] = min(dp[i], dp[i - wei] + val); n   } n } n bool check(int xx) { n   while (xx) { n     if ((xx % 10) != 4 && (xx % 10) != 7) { n       return 0; n     } n     if (xx < 10) break; n     xx /= 10; n   } n   return 1; n } n int main() { n   memset(dp, 1e9 + 7, sizeof(dp)); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     fa[findfa(x)] = findfa(y); n   } n   for (int i = 1; i <= n; i++) { n     sz[findfa(i)]++; n   } n   for (int i = 1; i <= n; i++) { n     costs[sz[i]]++; n   } n   dp[0] = 0; n   for (int i = 1; i <= n; i++) { n     if (costs[i]) { n       for (int j = 1; j <= costs[i]; j <<= 1) { n         bag(j * i, j); n         costs[i] -= j; n       } n       bag(costs[i] * i, costs[i]); n     } n   } n   long long ans = 1e9 + 7; n   for (int i = 1; i <= n; i++) { n     if (check(i)) ans = min(ans, dp[i]); n   } n   if (ans == 1e9 + 7) n     cout <<  -1  << endl; n   else n     cout << ans - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 10; n const int INF = 1e9 + 7; n int prime[MAX]; n int main() { n   int x2; n   scanf( %d , &x2); n   for (int i = 2; i <= x2; i++) { n     if (!prime[i]) { n       for (int j = i * 2; j <= x2; j += i) prime[j] = i; n     } n   } n   int ans = INF; n   for (int i = x2 - prime[x2] + 1; i <= x2; i++) { n     if (prime[i] == 0) continue; n     ans = min(ans, i - prime[i] + 1); n   } n   printf( %d 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> A(n); n   vector<int> B(n, 1); n   for (int i = 0; i < n; i++) { n     cin >> A[i]; n   } n   int max1 = B[0]; n   for (int i = 1; i < n; i++) { n     if (A[i] >= A[i - 1]) { n       B[i] = B[i - 1] + 1; n     } n     max1 = max(max1, B[i]); n   } n   cout << max1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000100], b[5010]; n string second[10]; n int main() { n   int n, sm = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n); n   for (;;) { n     if (a[0] <= a[n - 1]) { n       a[n - 1]--; n       a[0]++; n       sm++; n     } n     sort(a + 1, a + n); n     if (a[0] > a[n - 1]) { n       cout << sm; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename tp> n inline void read(tp &x) { n   x = 0; n   char c = getchar(); n   int f = 0; n   for (; c <  0  || c >  9 ; f |= c ==  - , c = getchar()) n     ; n   for (; c >=  0  && c <=  9 ; x = (x << 3) + (x << 1) + c -  0 , c = getchar()) n     ; n   if (f) x = -x; n } n namespace { n const int mo = 1e9 + 7; n inline int add(int x, int y) { n   x += y; n   return x >= mo ? x - mo : x; n } n inline int sub(int x, int y) { n   x -= y; n   return x < 0 ? x + mo : x; n } n inline int mul(int x, int y) { return (long long)x * y % mo; } n inline int power(int a, int k = mo - 2) { n   int ans = 1; n   for (; k; k >>= 1, a = mul(a, a)) n     if (k & 1) ans = mul(ans, a); n   return ans; n } n inline void U(int &x, int y) { x = add(x, y); } n }  // namespace n const int N = 1e5 + 233; n int n, m, coefa, coefb, P, T; n int fac[N], ifac[N], p[N], sump[N]; n inline int C(int n, int m) { n   return n < m ? 0 : mul(fac[n], mul(ifac[m], ifac[n - m])); n } n int main(void) { n   read(n); n   read(m); n   read(coefa); n   read(coefb); n   P = mul(coefa, power(coefb)); n   fac[0] = 1; n   for (register int i = 1; i <= (N - 1); i++) fac[i] = mul(fac[i - 1], i); n   for (register int i = 0; i < (int)N; i++) ifac[i] = power(fac[i]); n   read(T); n   for (register int i = 0; i < (int)m + 1; i++) { n     if (i <= T) n       p[i + 1] = mul(C(T, i), mul(power(P, i), power(1 + mo - P, T - i))); n   } n   for (register int i = 0; i < (int)m + 1; i++) n     sump[i] = add(i ? sump[i - 1] : 0, p[i]); n   vector<int> h(m + 2, 0); n   h[m] = 1; n   for (register int i = 1; i <= (n); i++) { n     vector<int> new_h(m + 2, 0); n     vector<int> f(m + 2, 0), g(m + 2, 0); n     for (int r = 1; r <= m; r++) { n       f[r] = add(f[r - 1], h[r]); n       g[r] = add(g[r - 1], mul(f[r - 1], p[r])); n     } n     for (int r = 1; r <= m; r++) { n       int rr = m - r + 1; n       new_h[r] = sub(mul(sump[r], mul(p[rr], f[m])), n                      add(mul(p[rr], g[r]), mul(mul(p[rr], f[m - r]), sump[r]))); n     } n     swap(h, new_h); n   } n   int ans = 0; n   for (auto i : h) U(ans, i); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int color[10010], c[10010], d[10010], f[10010], N, t, x; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     scanf( %d , color + i); n     if (!f[color[i]]) f[color[i]] = i; n   } n   int m, col; n   for (int i = 1; i <= N; i++) { n     m = 1; n     col = color[i]; n     d[col]++; n     for (int j = 1; j <= N; j++) { n       c[j] = 0; n     } n     c[color[i]]++; n     for (int j = i + 1; j <= N; j++) { n       c[color[j]]++; n       if ((c[color[j]] > m) || (c[color[j]] == m && color[j] < col)) { n         m = c[color[j]]; n         col = color[j]; n       } n       d[col]++; n     } n   } n   for (int i = 1; i < N; i++) printf( %d  , d[i]); n   printf( %d n , d[N]); n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 10, inf = 4e18, mod = 1e9 + 7; n const long double pi = 3.14159265359, eps = 1e-9; n long long n, m; n vector<long long> g[maxn], jj[3]; n long long mk[maxn]; n bool dfs(long long v, long long p = -1, long long dep = 1) { n   mk[v] = dep; n   vector<long long> be, sn; n   for (auto u : g[v]) { n     if (mk[u] != 0 && mk[u] < mk[v]) { n       if (u != p) be.push_back(u); n       continue; n     } n     if (mk[u] == 0 && dfs(u, v, dep + 1)) sn.push_back(u); n   } n   while (be.size() > 1) { n     jj[0].push_back(be.back()); n     be.pop_back(); n     jj[1].push_back(v); n     jj[2].push_back(be.back()); n     be.pop_back(); n   } n   while (sn.size() > 1) { n     jj[0].push_back(sn.back()); n     sn.pop_back(); n     jj[1].push_back(v); n     jj[2].push_back(sn.back()); n     sn.pop_back(); n   } n   long long add = 0; n   if (be.size()) { n     if (sn.size()) { n       jj[0].push_back(sn.back()); n       sn.pop_back(); n       jj[1].push_back(v); n       jj[2].push_back(be.back()); n       be.pop_back(); n       return 1; n     } n     add = be.back(); n   } else if (sn.size()) { n     add = sn.back(); n   } n   if (!add) return 1; n   if (p != -1) { n     jj[0].push_back(add); n     jj[1].push_back(v); n     jj[2].push_back(p); n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   long long v, u; n   while (m--) { n     cin >> v >> u; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   for (int i = 1; i <= n; i++) { n     if (!mk[i]) dfs(i); n   } n   cout << jj[0].size() << endl; n   for (int i = 0; i < jj[0].size(); i++) { n     cout << jj[0][i] <<     << jj[1][i] <<     << jj[2][i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> s[222222], ss[222222]; n int n, m, cnt, cnt2, h; n int up[222222][22], fa[222222], deep[211111], q[211111], dfn[211111], n     low[211111]; n void tarjan(int x) { n   int c, i, y; n   dfn[x] = low[x] = ++cnt; n   q[h] = x; n   h++; n   c = s[x].size(); n   for (i = 0; i < c; i++) { n     y = s[x][i]; n     if (dfn[y] == 0) { n       tarjan(y); n       low[x] = min(low[x], low[y]); n       if (low[y] >= dfn[x]) { n         cnt2++; n         while (h > 0) { n           h--; n           ss[cnt2].push_back(q[h]); n           ss[q[h]].push_back(cnt2); n           if (q[h] == y) break; n         } n         ss[cnt2].push_back(x); n         ss[x].push_back(cnt2); n       } n     } else n       low[x] = min(low[x], dfn[y]); n   } n } n void make_yfs() { n   int i; n   cnt = 0, h = 0, cnt2 = n; n   tarjan(1); n } n void dfs(int x) { n   int c, i, y; n   c = ss[x].size(); n   for (i = 0; i < c; i++) { n     y = ss[x][i]; n     if (fa[x] == y) continue; n     fa[y] = up[y][0] = x; n     deep[y] = deep[x] + 1; n     dfs(y); n   } n } n int lca(int x, int y) { n   int tmp = 20; n   if (deep[x] < deep[y]) swap(x, y); n   while (1) { n     if ((1 << tmp) <= deep[x] - deep[y]) x = up[x][tmp]; n     tmp--; n     if (tmp < 0) break; n   } n   if (x == y) return x; n   tmp = 20; n   while (1) { n     if (up[x][tmp] != up[y][tmp]) x = up[x][tmp], y = up[y][tmp]; n     tmp--; n     if (tmp < 0) break; n   } n   return fa[x]; n } n int getdis(int x, int y) { n   int L; n   L = lca(x, y); n   return deep[x] - deep[L] + deep[y] - deep[L]; n } n int main() { n   int i, m, x, y, q, ans; n   scanf( %d%d%d , &n, &m, &q); n   for (i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     s[x].push_back(y); n     s[y].push_back(x); n   } n   make_yfs(); n   dfs(1); n   for (int j = 1; j <= 20; j++) n     for (i = 1; i <= cnt2; i++) up[i][j] = up[up[i][j - 1]][j - 1]; n   while (q--) { n     scanf( %d%d , &x, &y); n     ans = getdis(x, y) / 2; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, x; n     cin >> n >> x; n     int arr[n + 5], brr[n + 5]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     for (int i = 0; i < n; i++) { n       cin >> brr[i]; n     } n     sort(arr, arr + n); n     sort(brr, brr + n, greater<int>()); n     int f = 1; n     for (int i = 0; i < n; i++) { n       if (arr[i] + brr[i] > x) { n         f = 0; n         break; n       } n     } n     if (f == 1) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n     if (t != 0) { n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> st; n long long n, q; n vector<int> swp(20, false); n vector<long long> arr; n void build(long long ind, long long l, long long r) { n   if (l == r) { n     st[ind] = arr[l]; n     return; n   } n   long long mid = (l + r) >> 1; n   build(2 * ind, l, mid); n   build(2 * ind + 1, mid + 1, r); n   st[ind] = st[2 * ind] + st[2 * ind + 1]; n } n void update(long long ind, long long l, long long r, long long req, n             long long level) { n   if (req < l || req > r) return; n   if (l == r) { n     st[ind] = arr[req]; n     return; n   } n   long long mid = (l + r) >> 1; n   if (swp[level - 1]) { n     update(2 * ind + 1, l, mid, req, level - 1); n     update(2 * ind, mid + 1, r, req, level - 1); n   } else { n     update(2 * ind, l, mid, req, level - 1); n     update(2 * ind + 1, mid + 1, r, req, level - 1); n   } n   st[ind] = st[2 * ind + 1] + st[2 * ind]; n } n long long query(long long ind, long long l, long long r, long long ql, n                 long long qr, long long level) { n   if (l > qr || r < ql) return 0; n   if (ql <= l && r <= qr) return st[ind]; n   long long mid = (l + r) / 2; n   if (swp[level - 1]) n     return query(2 * ind + 1, l, mid, ql, qr, level - 1) + n            query(2 * ind, mid + 1, r, ql, qr, level - 1); n   else n     return query(2 * ind, l, mid, ql, qr, level - 1) + n            query(2 * ind + 1, mid + 1, r, ql, qr, level - 1); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> q; n   long long len = 1 << n; n   arr.resize(len); n   st.assign(4 * len, 0); n   for (long long i = 0; i < len; i++) cin >> arr[i]; n   build(1, 0, len - 1); n   while (q--) { n     long long type; n     cin >> type; n     if (type == 1) { n       long long ind, val; n       cin >> ind >> val; n       ind--; n       arr[ind] = val; n       update(1, 0, len - 1, ind, n); n     } else if (type == 2) { n       long long k; n       cin >> k; n       for (long long i = 0; i < k; i++) swp[i] ^= true; n     } else if (type == 3) { n       long long k; n       cin >> k; n       swp[k] ^= true; n     } else { n       long long l, r; n       cin >> l >> r; n       l--; n       r--; n       cout << query(1, 0, len - 1, l, r, n) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   for (; c >  9  || c <  0 ; c = getchar()) { n     if (c ==  - ) f = -1; n   } n   for (; c >=  0  && c <=  9 ; c = getchar()) { n     x = x * 10 + c -  0 ; n   } n   return x * f; n } n int a[100005]; n std::vector<int> vec[200005]; n inline long long q_pow(long long x, int t) { n   long long s = 1; n   for (; t; t >>= 1) { n     if (t & 1) s *= x; n     x *= x; n   } n   return s; n } n inline void divide(int x) { n   for (int i = 2; i * i <= x; i++) { n     if (x % i != 0) { n       vec[i].emplace_back(0); n       continue; n     } n     int cnt = 0; n     for (; x % i == 0; x /= i) cnt++; n     vec[i].emplace_back(cnt); n   } n   if (x != 1) vec[x].emplace_back(1); n } n inline void work() { n   int n = read(); n   long long ans = 1; n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     divide(a[i]); n   } n   for (int i = 1; i <= 200000; i++) { n     std::sort(vec[i].begin(), vec[i].end()); n     if (vec[i].size() < n - 2) n       continue; n     else if (vec[i].size() == n - 1 && !vec[i].empty()) n       ans *= q_pow(i, vec[i][0]); n     else if (vec[i].size() == n && vec[i].size() >= 2) n       ans *= q_pow(i, vec[i][1]); n     else if (vec[i].size() == n && !vec[i].empty()) n       ans *= i; n   } n   printf( %lld n , ans); n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 100005; n const int mod = 1e9 + 9; n const int pp = 31; n const int ppp = 56; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int i, j, k, n; n   string s, t; n   cin >> s >> t >> k; n   n = s.length(); n   vector<int> a(n), p(n); n   for (i = 0; i < n; ++i) { n     a[i] = t[s[i] -  a ] -  0 ; n   } n   p[0] = a[0]; n   for (i = 1; i < n; i++) p[i] = p[i - 1] + a[i]; n   set<pair<long long, long long>> sz; n   vector<long long> p_pow(n + 1), pp_pow(n + 1); n   p_pow[0] = 1; n   pp_pow[0] = 1; n   for (i = 1; i <= n; i++) { n     p_pow[i] = (p_pow[i - 1] * pp) % mod; n     pp_pow[i] = (pp_pow[i - 1] * ppp) % mod; n   } n   vector<long long> hsh(n + 1, 0), hshh(n + 1, 0); n   for (i = 0; i < n; i++) { n     hsh[i + 1] = (hsh[i] + p_pow[i] * (s[i] -  a  + 1)) % mod; n     hshh[i + 1] = (hshh[i] + pp_pow[i] * (s[i] -  a  + 1)) % mod; n   } n   for (i = 1; i <= n; i++) { n     for (j = 0; j < n - i + 1; ++j) { n       long long cur_h_1 = (hsh[j + i] + mod - hsh[j]) % mod; n       long long cur_h_2 = (hshh[j + i] + mod - hshh[j]) % mod; n       if (j > 0 && (i - p[j + i - 1] + p[j - 1]) <= k) n         sz.insert({cur_h_1 * p_pow[n - j - 1] % mod, n                    cur_h_2 * pp_pow[n - j - 1] % mod}); n       else if (j == 0 && (i - p[j + i - 1]) <= k) { n         sz.insert({cur_h_1 * p_pow[n - j - 1] % mod, n                    cur_h_2 * pp_pow[n - j - 1] % mod}); n       } n     } n   } n   cout << sz.size(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)(1e5) + 123; n const int INF = (int)(1e9); n int n, a[maxn], t[4 * maxn], tt; n pair<int, int> b[maxn]; n int gcd(int a, int b) { n   while (a > 0 && b > 0) { n     if (a > b) n       a %= b; n     else n       b %= a; n   } n   return a + b; n } n void build(int v, int tl, int tr) { n   if (tl == tr) n     t[v] = a[tl]; n   else { n     int tm = (tl + tr) / 2; n     build(v + v, tl, tm); n     build(v + 1 + v, tm + 1, tr); n     t[v] = gcd(t[v + v], t[v + 1 + v]); n   } n } n int get(int v, int tl, int tr, int l, int r) { n   if (l > tr || r < tl) return 0; n   if (l <= tl && r >= tr) return t[v]; n   int tm = (tl + tr) / 2; n   return gcd(get(v + v, tl, tm, l, r), get(v + 1 + v, tm + 1, tr, l, r)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     b[i] = make_pair(a[i], i); n   } n   sort(b + 1, b + 1 + n); n   build(1, 1, n); n   cin >> tt; n   for (int i = 1; i <= tt; ++i) { n     int l, r; n     cin >> l >> r; n     int cur = get(1, 1, n, l, r); n     pair<int, int> now = make_pair(cur, l); n     int to = (lower_bound(b + 1, b + 1 + n, now) - (b + 1)); n     now = make_pair(cur, r + 1); n     int from = (lower_bound(b + 1, b + 1 + n, now) - (b + 1)); n     cout << (r - l + 1) - (from - to) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long f = 1, x = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const int maxn = 5e5 + 5; n char st1[maxn], st2[maxn]; n long long ans = 0, pre = 0; n int n, k; n int main() { n   n = read(), k = read(); n   scanf( %s , st1); n   scanf( %s , st2); n   for (int i = 0; i <= n - 1; i++) { n     pre = min(pre * 2 + (int)(st2[i] - st1[i]), 1ll << 36); n     ans += min(pre + 1, (long long)k); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double eps = 1e-6; n const int maxn = 3e5 + 10; n const int maxm = 1e6 + 10; n const long long mod = 998244353; n int a[maxn], b[maxn]; n bool judge(int mid, int h) { n   int temph = h; n   for (int i = 0; i < mid; i++) b[i] = a[i]; n   sort(b, b + mid); n   int ans = 0; n   for (int i = 0; i < mid; i += 2) { n     if (b[i] <= h) ans++; n     if (i + 1 < mid && b[i + 1] <= h) ans++; n     if (i == mid - 1) n       h -= b[i]; n     else n       h -= b[i + 1]; n     if (h <= 0) break; n   } n   if (ans >= mid) return true; n   ans = 0, h = temph; n   for (int i = mid - 1; i >= 0; i -= 2) { n     if (b[i] <= h) ans++; n     if (i - 1 >= 0 && b[i - 1] <= h) ans++; n     h -= b[i]; n     if (h <= 0) break; n   } n   return ans >= mid; n } n int main() { n   int n, h; n   cin >> n >> h; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ans = 1; n   int l = 1, r = n; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (judge(mid, h)) n       l = mid + 1, ans = mid; n     else n       r = mid - 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p_arr[200005]; n int main() { n   int n, in, cnt, x, arr[200005]; n   std::cin >> n; n   queue<int> q; n   for (int i = 1; i <= n; i++) { n     std::cin >> in; n     q.push(in); n   } n   for (int i = 1; i <= n; i++) { n     std::cin >> arr[i]; n   } n   cnt = 0; n   for (int i = 1; i <= n; i++) { n     x = q.front(); n     while (p_arr[x] == -1) { n       q.pop(); n       x = q.front(); n     } n     x = q.front(); n     if (x == arr[i]) { n       q.pop(); n     } else { n       cnt++; n       p_arr[arr[i]] = -1; n     } n   } n   std::cout << cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char g[21][21]; n int n, m, id[21][21], cv, cb, var[(1 << 8)], n     dr[] = {1, 0, -1, 0}, dc[] = {0, 1, 0, -1}, d[21][21][(1 << 8)]; n bool inq[21][21][(1 << 8)]; n struct node { n   int r, c, msk; n }; n queue<node> q; n vector<node> b; n void input() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (n); i++) scanf( %s , g[i]); n   memset(id, -1, sizeof(id)); n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (m); j++) n       if (isdigit(g[i][j])) id[i][j] = g[i][j] -  1 , cv++; n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (m); j++) n       if (g[i][j] ==  B ) id[i][j] = cv + (cb++); n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (m); j++) { n       if (g[i][j] ==  B ) b.push_back((node){i, j, (1 << id[i][j] >> cv)}); n       if (isdigit(g[i][j])) b.push_back((node){i, j, (1 << id[i][j] << cb)}); n     } n   for (int i = 0; i < (cv); i++) scanf( %d , &var[1 << i]); n } n int lowbit(int x) { return x & (-x); } n void pretreat() { n   for (int i = 0; i < ((1 << 8)); i++) n     if (i) var[i] = var[i - lowbit(i)] + var[lowbit(i)]; n } n bool invalid(node cur) { n   if (cur.r < 0 || cur.r >= n || cur.c < 0 || cur.c >= m) return true; n   if (id[cur.r][cur.c] != -1) return true; n   if (g[cur.r][cur.c] ==  # ) return true; n   return false; n } n void updata(node nxt, int x) { n   if (d[nxt.r][nxt.c][nxt.msk] == -1 || d[nxt.r][nxt.c][nxt.msk] > x) { n     d[nxt.r][nxt.c][nxt.msk] = x; n     if (!inq[nxt.r][nxt.c][nxt.msk]) { n       inq[nxt.r][nxt.c][nxt.msk] = 1; n       q.push(nxt); n     } n   } n } n int bfs() { n   memset(d, -1, sizeof(d)); n   memset(inq, 0, sizeof(inq)); n   node cur, nxt; n   int ans = 0; n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (m); j++) n       if (g[i][j] ==  S ) { n         q.push((node){i, j, 0}); n         d[i][j][0] = 0; n         inq[i][j][0] = 1; n         break; n       } n   while (!q.empty()) { n     cur = q.front(); n     q.pop(); n     inq[cur.r][cur.c][cur.msk] = 0; n     if (g[cur.r][cur.c] ==  S  && ((cur.msk & ((1 << cb) - 1)) == 0)) n       ans = max(ans, var[cur.msk >> cb] - d[cur.r][cur.c][cur.msk]); n     for (int i = 0; i < (4); i++) { n       nxt.r = cur.r + dr[i]; n       nxt.c = cur.c + dc[i]; n       if (invalid(nxt)) continue; n       if (i & 1) { n         nxt.msk = cur.msk; n         for (int j = 0; j < ((int)b.size()); j++) { n           if (i == 1) { n             if (b[j].r > nxt.r && b[j].c == nxt.c) nxt.msk ^= b[j].msk; n           } else { n             if (b[j].r > cur.r && b[j].c == cur.c) nxt.msk ^= b[j].msk; n           } n         } n       } else n         nxt.msk = cur.msk; n       updata(nxt, d[cur.r][cur.c][cur.msk] + 1); n     } n   } n   return ans; n } n int main() { n   input(); n   pretreat(); n   printf( %d n , bfs()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   ; n   vector<int> v(n); n   for (int i = 0; i < n; i++) scanf( %d , &v[i]); n   ; n   int diff = 0; n   int ans = v[0]; n   for (int i = 0; i < n - 1; i++) { n     diff += (v[i] - v[i + 1]); n     if (diff < 0) { n       ans = ans + abs(diff); n       diff = 0; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int x; n int a[1000]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < 2 * n + 1; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < 2 * n + 1; i++) { n     if (i % 2 == 1 && k > 0 && a[i] > a[i - 1] + 1 && a[i] > a[i + 1] + 1) { n       printf( %d  , a[i] - 1); n       k--; n     } else n       printf( %d  , a[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100005], mi, mid, ma; n long long m, sum; n bool b[100005], flag; n bool check() { n   long long sum = 0; n   for (int i = 0; i < n; i++) sum += min(a[i], mid); n   return sum <= m; n } n int main() { n   cin >> n >> m; n   sum = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   if (sum < m) { n     printf( -1 n ); n   } else if (sum == m) { n     printf(  n ); n   } else { n     mi = 0; n     ma = 1000000000; n     while (mi < ma) { n       mid = (mi + ma + 1) >> 1; n       if (check()) n         mi = mid; n       else n         ma = mid - 1; n     } n     for (int i = 0; i < n; i++) { n       m -= min(a[i], mi); n       a[i] -= min(a[i], mi); n     } n     memset(b, true, sizeof(b)); n     flag = false; n     for (int i = 0; b[i]; i = (i + 1) % n) { n       if (a[i]) { n         if (m) { n           a[i]--; n           m--; n         } else { n           if (flag) printf(   ); n           flag = true; n           b[i] = false; n           printf( %d , i + 1); n         } n       } n     } n     printf(  n ); n   } n   return 0; n } n