#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, m, A[12][N], q; n struct node { n   int ans, lrt[12], rrt[12]; n   void trans(int u, int v) { n     ans--; n     for (int i = 1; i <= n; i++) { n       if (lrt[i] == u) lrt[i] = v; n       if (rrt[i] == u) rrt[i] = v; n     } n   } n } t[N << 2]; n node _merge(node a, node b, int l, int r) { n   node ret; n   ret.ans = a.ans + b.ans; n   for (int i = 1; i <= n; i++) ret.lrt[i] = a.lrt[i], ret.rrt[i] = b.rrt[i]; n   for (int i = 1; i <= n; i++) { n     if (A[i][l] == A[i][r] && a.rrt[i] != b.lrt[i]) { n       ret.trans(a.rrt[i], b.lrt[i]); n       a.trans(a.rrt[i], b.lrt[i]); n     } n   } n   return ret; n } n void build(int u, int l, int r) { n   if (l == r) { n     t[u].ans = n; n     for (int i = 1; i <= n; i++) t[u].lrt[i] = t[u].rrt[i] = (l - 1) * n + i; n     for (int i = 1; i <= n; i++) n       if (A[i][l] == A[i - 1][l]) t[u].trans(t[u].lrt[i], t[u].lrt[i - 1]); n     return; n   } n   int mid = (l + r) / 2; n   build(u << 1, l, mid); n   build(u << 1 | 1, mid + 1, r); n   t[u] = _merge(t[u << 1], t[u << 1 | 1], mid, mid + 1); n } n node query(int u, int l, int r, int L, int R) { n   if (l == L && r == R) return t[u]; n   int mid = (l + r) / 2; n   if (R <= mid) n     return query(u << 1, l, mid, L, R); n   else if (L > mid) n     return query(u << 1 | 1, mid + 1, r, L, R); n   else n     return _merge(query(u << 1, l, mid, L, mid), n                   query(u << 1 | 1, mid + 1, r, mid + 1, R), mid, mid + 1); n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &A[i][j]); n   build(1, 1, m); n   while (q--) { n     int l, r; n     scanf( %d%d , &l, &r); n     printf( %d n , query(1, 1, m, l, r).ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int n, l[N], r[N], cnt[4 * N], one[4 * N]; n vector<int> g; n map<int, int> mp; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> l[i] >> r[i]; n     g.push_back(l[i] - 1); n     g.push_back(l[i]); n     g.push_back(r[i]); n     g.push_back(r[i] + 1); n   } n   sort(g.begin(), g.end()); n   for (int i = 0, cnt = 0; i < g.size(); ++i) { n     if (!i || g[i] != g[i - 1]) cnt++; n     mp[g[i]] = cnt; n   } n   for (int i = 1; i <= n; ++i) { n     cnt[mp[l[i]]]++; n     cnt[mp[r[i] + 1]]--; n   } n   for (int i = 1; i <= 4 * n; ++i) { n     cnt[i] += cnt[i - 1]; n     if (cnt[i] == 1) { n       one[i] = 1; n     } n     one[i] += one[i - 1]; n   } n   for (int i = 1; i <= n; ++i) { n     if (one[mp[r[i]]] == one[mp[l[i] - 1]]) { n       cout << i <<   n ; n       return 0; n     } n   } n   cout <<  -1 n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class temp> n inline void Rd(temp &p) { n   p = 0; n   char c; n   while (c = getchar(), c < 48) n     ; n   do p = p * 10 + (c & 15); n   while (c = getchar(), c > 47); n } n int pow(int n, int m) { n   int ans = 1; n   while (m) { n     if (m & 1) ans = 1ll * ans * n % 1000000007; n     m >>= 1; n     n = 1ll * n * n % 1000000007; n   } n   return ans; n } n int head[100005], tot = 0; n struct eg { n   int v, col, nxt; n } E[100005 << 1]; n void add_edge(int u, int v, int w) { n   E[++tot] = (eg){v, w, head[u]}; n   head[u] = tot; n   E[++tot] = (eg){u, w, head[v]}; n   head[v] = tot; n } n int col[100005]; n bool dfs(int u) { n   for (int j = head[u]; j; j = E[j].nxt) { n     int v = E[j].v, c = E[j].col; n     if (!~col[v]) { n       col[v] = col[u] ^ c ^ 1; n       if (!dfs(v)) return false; n     } else { n       if ((col[u] ^ c ^ 1) != col[v]) return false; n     } n   } n   return true; n } n int main() { n   int n, m; n   Rd(n), Rd(m); n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     Rd(u), Rd(v), Rd(w); n     add_edge(u, v, w); n   } n   memset(col, -1, sizeof(col)); n   int sz = 0; n   for (int i = 1; i <= n; i++) n     if (!~col[i]) { n       col[i] = 0; n       if (!dfs(i)) { n         puts( 0 ); n         return 0; n       } n       ++sz; n     } n   printf( %d n , pow(2, sz - 1)); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6; n const long long LINF = 1e18; n long long int n, cnt = 0, ans = 0, a, b, c, d, cmp[ohara], cmpp, m, h, w, x, y, n                  sum[ohara], pos; n int dy[] = {1, 0, -1, 0}; n int dx[] = {0, 1, 0, -1}; n string alph( abcdefghijklmnopqrstuvwxyz ), s; n bool fl = true; n struct edge { n   int to, cost; n }; n int main(void) { n   cin.tie(0); n   ios::sync_with_stdio(false); n   cin >> n >> c; n   for (long long int(i) = 0; (i) < (int)(n); (i)++) { n     cin >> a >> b; n     if (a > b) { n       pos = b; n       b = a; n       a = pos; n     } n     sum[a]++; n     sum[b + 1]--; n   } n   for (long long int i = (1); i < (9999); i++) { n     sum[i] += sum[i - 1]; n   } n   for (long long int(i) = 0; (i) < (int)(9999); (i)++) { n     if (sum[i] == n) { n       cmp[cnt] = i; n       cnt++; n       fl = false; n     } n   } n   if (fl) { n     cout << (-1) << endl; n     return 0; n   } n   ans = INF; n   for (long long int(i) = 0; (i) < (int)(cnt); (i)++) { n     ans = min(ans, max(cmp[i], c) - min(cmp[i], c)); n   } n   cout << (ans) << endl; n   return 0; n } n


# include <bits/stdc++.h> n using namespace std; n  n  n  n int main(){ n  ios::sync_with_stdio(false); n  int t; n  cin>>t; n  while(t--){ n   int n,m; n   cin>>n>>m; n   int ans=0; n   int a[101]={0}; n   for(int i=0; i<n;i++){ n    int x; n    cin>>x; n    a[x]=1; n   } n   for(int i=0;i<m;i++){ n    int x; n    cin>>x; n    if(a[x]==1) n     ans++; n   } n   cout<<ans<<  n ; n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, ans, last, d, x, y, a, b; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n void exgcd(long long a, long long b, long long& d, long long& x, long long& y) { n   if (!b) { n     x = 1, y = 0, d = a; n   } else { n     exgcd(b, a % b, d, y, x); n     y -= (a / b) * x; n   } n } n void check(long long x0, long long y0) { n   exgcd(a, b, d, x, y); n   if ((y0 - x0) % d != 0) return; n   long long tmp = abs(b / d); n   x *= (y0 - x0) / d; n   x = (x % tmp + tmp) % tmp; n   long long xx = x0 + 2 * n * x; n   if (xx < 0 || xx >= last) return; n   ans = min(ans, xx); n } n int main() { n   cin >> n >> m >> k; n   a = 2 * n, b = -2 * m; n   while (k--) { n     long long xx, yy; n     cin >> xx >> yy; n     ans = 1e18; n     last = n / gcd(n, m) * m; n     check(xx, yy); n     check(-xx, yy); n     check(xx, -yy); n     check(-xx, -yy); n     if (ans == 1e18) n       cout << -1 << endl; n     else n       cout << ans << endl; n   } n } n


#include<bits/stdc++.h> n using namespace std; n using ll = long long; n const ll nax = 1e5 + 69; n int n; n vector<ll>possible; n void fun(ll *a,int l,int r,ll *pref){ n  if(l>r || l<0 || r>n) n   return; n  possible.push_back(pref[r+1]-pref[l]); n  if(a[r]==a[l]) n   return; n  int mid = (a[l] + a[r])/2; n  int ind = lower_bound(a,a+n,mid+1)-a; n  fun(a,l,ind-1,pref); n  fun(a,ind,r,pref); n } n int main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     int t; n     cin>>t; n     while(t--) n     {  n      int q; n         cin>>n>>q; n         ll a[n]; n         ll pref[n+1]; n         pref[0] = 0; n         for (int i = 0; i < n; ++i) n          cin>>a[i]; n         sort(a,a+n); n         for (int i = 0; i < n; ++i) n          pref[i+1] = pref[i] + a[i]; n         fun(a,0,n-1,pref); n         sort(possible.begin(),possible.end()); n         //for (auto i : possible) n         // cout<<i<<   ; n         //cout<<endl; n         while(q--){ n          ll s; n          cin>>s; n          auto it = lower_bound(possible.begin(),possible.end(),s); n          if(it!=possible.end() && *it==s) n           cout<< Yes n ; n          else n           cout<< No n ; n         } n         possible.clear(); n     }     n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 101010 * 2; n vector<int> vec[maxn]; n vector<int> mp[maxn]; n bool vis[maxn]; n int arr[maxn]; n int dfs(int x) { n   vis[x] = true; n   int ans = vec[x].size(); n   int siz = mp[x].size(); n   for (int i = 0; i < siz; i++) { n     int y = mp[x][i]; n     if (vis[y]) continue; n     ans = max(ans, dfs(y)); n   } n   return ans; n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     vec[arr[i]].push_back(i); n   } n   int ans = 0, las = 0, pos = 0; n   int maxv = 0; n   for (int i = 1; i <= n; i++) { n     if (las != arr[i] && las) { n       if (vec[arr[i]][0] < pos || maxv > i) n         mp[arr[i]].push_back(las), mp[las].push_back(arr[i]); n     } n     las = arr[i], pos = i; n     int siz = vec[arr[i]].size(); n     maxv = max(maxv, vec[arr[i]][siz - 1]); n   } n   for (int i = 1; i <= n; i++) { n     if (vis[i]) continue; n     ans += dfs(i); n   } n   printf( %d n , n - ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int n) { n   if (n == 1) { n     return (a); n   } n   if (n == 0) { n     return 1; n   } n   long long int c = power(a, n / 2); n   if (n % 2 == 0) { n     return (c * c); n   } else { n     return c * c * a; n   } n } n long long int func(int n, long long int k) { n   long long int x = power(2, n - 1); n   if (n == 1) return 1; n   if (k == x) { n     return n; n   } n   if (k < x) { n     return func(n - 1, k); n   } else { n     return func(n - 1, k - x); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   long long int k; n   cin >> n >> k; n   cout << func(n, k) << endl; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, p, q, a, b; n   scanf( %d %d %d %d , &n, &m, &p, &q); n   scanf( %d %d , &a, &b); n   int u = q - m, v = p - n; n   if (u < 0) u = -u; n   if (v < 0) v = -v; n   if ((u) % b == 0 && (v) % a == 0) { n     int c = u / b - v / a; n     if (c < 0) c = -c; n     if (c % 2 == 0) n       printf( YES n ); n     else n       printf( NO n ); n   } else n     printf( NO n ); n } n


#include <bits/stdc++.h> n int cal(int x) { n   int sum = 0; n   for (int i = 1; i < x; i++) { n     sum = sum + i; n   } n   return sum; n }; n char x[101][101]; n using namespace std; n int main() { n   int n, c1 = 0, c2 = 0, total1 = 0, total2 = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> x[i][j]; n       if (x[i][j] ==  C ) c1++; n     } n     total1 = total1 + cal(c1); n     c1 = 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (x[j][i] ==  C ) c2++; n     } n     total2 = total2 + cal(c2); n     c2 = 0; n   } n   cout << total1 + total2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void action() { n   string s; n   cin >> s; n   reverse(s.begin(), s.end()); n   long long c = 0, f = 0, j; n   for (long long i = 0; i < s.length(); i++) { n     if (s[i] ==  1 ) { n       f++; n     } n     if (i % 2 == 0) c++; n   } n   if (f > 1) n     cout << c; n   else { n     if ((s.length()) % 2 == 0) n       cout << c; n     else n       cout << c - 1; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   action(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   int n, k; n   std::cin >> n >> k; n   int s[n + 1]; n   for (int i = 1; i < n + 1; ++i) { n     std::cin >> s[i]; n   } n   if (k >= n) n     std::cout << s[n]; n   else { n     int max = s[n]; n     for (int i = 1; i <= (n - k); ++i) { n       int temp = s[i] + s[2 * (n - k) + 1 - i]; n       if (max < temp) { n         max = temp; n       } n     } n     std::cout << max; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long inf = 1e18; n const long long N = 2e2 + 5; n long long n; n long long a[N]; n void solve(long long id) { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long odd = 0, even = 0; n   long long oddind = 0, evenind = 0; n   for (long long i = 0; i < n; i++) { n     if (i % 2 != a[i] % 2) { n       if (i % 2) { n         oddind++; n         even++; n       } else { n         evenind++; n         odd++; n       } n     } n   } n   if (oddind == odd) { n     cout << odd << endl; n   } else n     cout <<  -1  << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t = 1; n   cin >> t; n   for (long long i = 1; i <= t; i++) solve(i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int tt = 0; tt < int(t); tt++) { n     int n; n     cin >> n; n     map<int, int> c; n     for (int i = 0; i < int(n); i++) { n       int pi; n       cin >> pi; n       c[-pi]++; n     } n     vector<int> pp; n     for (auto p : c) pp.push_back(p.second); n     bool ok = false; n     int g = pp[0]; n     int i = 1; n     int s = 0; n     while (s <= g && i < pp.size()) s += pp[i++]; n     if (g < s) { n       int b = 0; n       while (b <= g && i < pp.size()) b += pp[i++]; n       while (i < pp.size() && g + s + b + pp[i] <= n / 2) b += pp[i++]; n       if (g < b && g + s + b <= n / 2) { n         ok = true; n         cout << g <<     << s <<     << b << endl; n       } n     } n     if (!ok) cout << 0 <<     << 0 <<     << 0 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   for (int i = 0; i < n / 4; i++) { n     cout <<  bbaa ; n   } n   if (n % 4 == 1) { n     cout <<  b ; n   } n   if (n % 4 == 2) { n     cout <<  bb ; n   } n   if (n % 4 == 3) { n     cout <<  bba ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, kol = 0, sum = 0, a; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (sum + a > m) { n       kol++; n       sum = a; n     } else n       sum += a; n   } n   cout << kol + 1; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n vector<int> v[maxn]; n pair<long long, long long> a[maxn]; n long long q[maxn], siz[maxn]; n bool c[maxn]; n long long n, m, k; n set<pair<long long, long long> > s; n map<pair<long long, long long>, bool> mark; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (long long y = 0; y < m; y++) { n     cin >> a[y].first >> a[y].second; n     v[a[y].first].push_back(a[y].second), v[a[y].second].push_back(a[y].first); n     siz[a[y].second]++, siz[a[y].first]++; n   } n   for (long long y = 1; y <= n; y++) n     s.insert(pair<long long, long long>(siz[y], y)); n   for (long long y = m - 1; y >= 0; y--) { n     if (y != m - 1 && !mark[a[y + 1]]) { n       mark[pair<long long, long long>(a[y + 1].second, a[y + 1].first)] = n           mark[pair<long long, long long>(a[y + 1].first, a[y + 1].second)] = 1; n       long long i = a[y + 1].second; n       siz[i]--; n       if (c[i] == 0) { n         pair<long long, long long> x = n             pair<long long, long long>(siz[i] + 1, i); n         s.erase(x); n         x.first--; n         s.insert(x); n       } n       i = a[y + 1].first; n       siz[i]--; n       if (c[i] == 0) { n         pair<long long, long long> x = n             pair<long long, long long>(siz[i] + 1, i); n         s.erase(x); n         x.first--; n         s.insert(x); n       } n     } n     while (s.size() != 0) { n       pair<long long, long long> x = *s.begin(); n       if (x.first >= k) break; n       s.erase(x); n       c[x.second] = 1; n       for (long long ii = 0; ii < v[x.second].size(); ii++) { n         long long i = v[x.second][ii]; n         if (!mark[pair<long long, long long>(x.second, i)]) { n           mark[pair<long long, long long>(x.second, i)] = n               mark[pair<long long, long long>(i, x.second)] = 1; n           siz[i]--; n           siz[x.second]--; n           if (!c[i]) { n             pair<long long, long long> z = n                 pair<long long, long long>(siz[i] + 1, i); n             s.erase(z); n             z.first--; n             s.insert(z); n           } n         } n       } n     } n     q[y] = s.size(); n   } n   for (long long y = 0; y < m; y++) cout << q[y] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dep[100005], present[100005]; n int main() { n   int n, p; n   scanf( %d , &n); n   for (int i = 2; i <= n; ++i) { n     scanf( %d , &p); n     present[dep[i] = dep[p] + 1] ^= 1; n   } n   printf( %d n , accumulate(present, present + n + 1, 1)); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int cnt[maxn]; n int check(int k) { n   if (k == 0) return 1; n   long long d = 1, ans = maxn; n   while (d < maxn) d <<= 1; n   while (d >>= 1) n     if (ans - d >= 0) n       if ((ans - d) * (ans - d - 1) >= 2 * k) ans -= d; n   if (ans * (ans - 1) != 2 * k) return -1; n   return ans; n } n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int aa = check(a), dd = check(d); n   if (aa == -1 || dd == -1) return cout <<  Impossible  << endl, 0; n   bool safe = false; n   if (aa == 1 || dd == 1) { n     if (b + c == 0) safe = true; n   } n   if (!safe && (b + c) != aa * dd) return cout <<  Impossible  << endl, 0; n   if (safe) { n     if (aa == 1) aa = 0; n     if (dd == 1) dd = 0; n   } n   if (aa == 0) { n     if (dd == 0) dd = 1; n     for (int i = 0; i < dd; ++i) cout << 1; n     return 0; n   } n   if (dd == 0) { n     if (aa == 0) aa = 1; n     for (int i = 0; i < aa; ++i) cout << 0; n     return 0; n   } n   int f = dd - (b + aa - 1) / aa; n   for (int i = 0; i < f; ++i) cout << 1; n   int m = b % aa; n   for (int i = 0; i < aa; ++i) { n     if (m && i == m) cout << 1; n     cout << 0; n   } n   f = b / aa; n   for (int i = 0; i < f; ++i) cout << 1; n   return 0; n } n


#include  bits/stdc++.h  n using namespace std; n #define int  long long n #define yes  YES n  n #define no  NO n  n const int M = 1000000007; n  n signed main(){ n     ios_base::sync_with_stdio(0);cin.tie(0); n     int T; n     cin >> T; n     //T = 1; n     while(T--){ n             int n, k; n             cin >> n >> k; n             int r; n             cout << 0 << endl; n             cin >> r; n             if(r!=1){ n                 for(int i = 1;i <= n-1;i++){ n                     cout << (i^(i-1)) << endl; n                     cin >> r; n                     if(r==1) n                         break; n                 } n             } n     } n     return 0; n } n  n  n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   int arr[n]; n   for (int i = (int)(0); i <= (int)(n - 1); i++) { n     cin >> arr[i]; n   } n   int hash1[100005] = {0}; n   for (int i = (int)(0); i <= (int)(n / 2 - 1); i++) { n     if (arr[i] - i > 0) hash1[arr[i] - i]++; n   } n   for (int i = (int)(n / 2); i <= (int)(n - 1); i++) { n     if (arr[i] - (n - i - 1) > 0) hash1[arr[i] - (n - i - 1)]++; n   } n   int cnt = 0, val = 0; n   for (int i = (int)(1); i <= (int)(100000); i++) { n     if (cnt < hash1[i]) { n       cnt = hash1[i]; n       val = i; n     } n   } n   cout << n - cnt <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int MaxN = 3e5 + 5; n map<int, int> mp; n struct node { n   int x, y; n } arr[MaxN], brr[MaxN]; n struct qnode { n   int x, y; n   bool operator>(const qnode &p) const { return p.y > y; } n } st; n bool cmp(node b, node c) { n   if (b.x == c.x) return b.y < c.y; n   return b.x < c.x; n } n int main() { n   int n, m, u, v, pp; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &pp), mp[pp] = i; n   int x = -1, y = inf; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &u, &v); n     arr[i].x = min(mp[u], mp[v]); n     arr[i].y = max(mp[u], mp[v]); n   } n   sort(arr, arr + m, cmp); n   priority_queue<qnode, vector<qnode>, greater<qnode> > pq; n   for (int i = 0; i < m; i++) { n     if (pq.empty()) { n       st.x = arr[i].x, st.y = arr[i].y; n       pq.push(st); n       continue; n     } n     while (!pq.empty() && arr[i].y <= pq.top().y) { n       pq.pop(); n     } n     st.x = arr[i].x, st.y = arr[i].y; n     pq.push(st); n   } n   int num = 0; n   while (!pq.empty()) { n     st = pq.top(); n     pq.pop(); n     brr[num].x = st.x, brr[num].y = st.y, num++; n   } n   long long p = (long long)(brr[num - 1].x) * (n - brr[num - 1].y + 1); n   for (int i = num - 1; i >= 1; i--) n     p += (long long)(brr[i - 1].x - brr[i].x) * (n - brr[i - 1].y + 1); n   long long ans = (long long)n + (long long)n * (n - 1) / 2; n   printf( %I64d n , ans - p); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline void chkmin(T1 &x, T2 y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n inline void chkmax(T1 &x, T2 y) { n   if (x < y) x = y; n } n inline int readChar(); n template <class T = int> n inline T readInt(); n template <class T> n inline void writeInt(T x, char end = 0); n inline void writeChar(int x); n inline void writeWord(const char *s); n static const int buf_size = 4096; n inline int getChar() { n   static char buf[buf_size]; n   static int len = 0, pos = 0; n   if (pos == len) { n     pos = 0, len = fread(buf, 1, buf_size, stdin); n   } n   if (pos == len) { n     return -1; n   } n   return buf[pos++]; n } n inline int readChar() { n   int c = getChar(); n   while (c <= 32) { n     c = getChar(); n   } n   return c; n } n template <class T> n inline T readInt() { n   int s = 1, c = readChar(); n   T x = 0; n   if (c ==  - ) s = -1, c = getChar(); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   return s == 1 ? x : -x; n } n static int write_pos = 0; n static char write_buf[buf_size]; n inline void writeChar(int x) { n   if (write_pos == buf_size) n     fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; n   write_buf[write_pos++] = x; n } n template <class T> n inline void writeInt(T x, char end) { n   if (x < 0) writeChar( - ), x = -x; n   char s[24]; n   int n = 0; n   while (x || !n) s[n++] =  0  + x % 10, x /= 10; n   while (n--) writeChar(s[n]); n   if (end) writeChar(end); n } n inline void writeWord(const char *s) { n   while (*s) writeChar(*s++); n } n struct Flusher { n   ~Flusher() { n     if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; n   } n } flusher; n using namespace std; n const int MAXN = 100228; n int n, m; n int a[MAXN], ans[MAXN], lp[MAXN]; n vector<pair<int, int> > vq[MAXN]; n int main() { n   n = readInt(), m = readInt(); n   for (int i = 0; i < n; ++i) { n     lp[i] = -1; n   } n   for (int i = 0; i < m; ++i) { n     int l = readInt(), r = readInt(); n     l--, r--; n     lp[r] = l; n   } n   int q = readInt(); n   for (int i = 0; i < q; ++i) { n     int x = readInt(), y = readInt(); n     x--, y--; n     vq[y].push_back({x, i}); n   } n   for (int i = 0; i < n; ++i) { n     a[i] = i; n   } n   for (int i = 0; i < n; ++i) { n     int x = lp[i]; n     for (int j = 0; j <= x; ++j) { n       a[j] = a[j] >= x ? i : a[j]; n     } n     for (auto p : vq[i]) { n       ans[p.second] = a[p.first]; n     } n   } n   for (int i = 0; i < q; ++i) { n     writeInt(ans[i] + 1,   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n long long n; n int main() { n   cin >> n; n   if (n == 1) { n     cout << 0; n     return 0; n   } n   cout << n / 2 - 1 + (n % 2 != 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 1; n const int MOD = 1e9 + 7; n struct node { n   int n4, n7, n47, n74; n }; n bool tab[MAX], lazy[4 * MAX]; n int n, q; n node seg[4 * MAX]; n void build(int v = 1, int l = 0, int r = n - 1) { n   if (l == r) { n     seg[v].n7 = tab[l]; n     seg[v].n4 = !tab[l]; n     seg[v].n47 = seg[v].n74 = 1; n     return; n   } n   int mid = (l + r) / 2; n   build(2 * v, l, mid); n   build(2 * v + 1, mid + 1, r); n   seg[v].n7 = seg[2 * v].n7 + seg[2 * v + 1].n7; n   seg[v].n4 = seg[2 * v].n4 + seg[2 * v + 1].n4; n   seg[v].n47 = max(max(seg[2 * v].n4 + seg[2 * v + 1].n47, n                        seg[2 * v].n7 + seg[2 * v + 1].n7), n                    seg[2 * v].n47 + seg[2 * v + 1].n7); n   seg[v].n74 = max(max(seg[2 * v].n74 + seg[2 * v + 1].n4, n                        seg[2 * v].n7 + seg[2 * v + 1].n7), n                    seg[2 * v].n7 + seg[2 * v + 1].n74); n } n void up(int v, int l, int r, int a, int b) { n   if (lazy[v]) { n     swap(seg[v].n4, seg[v].n7); n     swap(seg[v].n47, seg[v].n74); n     if (l != r) { n       lazy[2 * v + 1] = !lazy[2 * v + 1]; n       lazy[2 * v] = !lazy[2 * v]; n     } n     lazy[v] = 0; n   } n   if (l > b || r < a) return; n   if (l >= a && r <= b) { n     swap(seg[v].n4, seg[v].n7); n     swap(seg[v].n47, seg[v].n74); n     if (l != r) { n       lazy[2 * v + 1] = !lazy[2 * v + 1]; n       lazy[2 * v] = !lazy[2 * v]; n     } n     return; n   } n   int mid = (l + r) / 2; n   up(2 * v, l, mid, a, b); n   up(2 * v + 1, mid + 1, r, a, b); n   seg[v].n7 = seg[2 * v].n7 + seg[2 * v + 1].n7; n   seg[v].n4 = seg[2 * v].n4 + seg[2 * v + 1].n4; n   seg[v].n47 = max(max(seg[2 * v].n4 + seg[2 * v + 1].n47, n                        seg[2 * v].n7 + seg[2 * v + 1].n7), n                    seg[2 * v].n47 + seg[2 * v + 1].n7); n   seg[v].n74 = max(max(seg[2 * v].n74 + seg[2 * v + 1].n4, n                        seg[2 * v].n7 + seg[2 * v + 1].n7), n                    seg[2 * v].n7 + seg[2 * v + 1].n74); n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> n >> q; n   for (int i = 0; i < n; ++i) { n     char x; n     cin >> x; n     tab[i] = ((x -  0 ) & 1); n   } n   build(); n   while (q--) { n     string s; n     cin >> s; n     if (s[0] ==  c ) n       cout << seg[1].n47 <<   n ; n     else { n       int a, b; n       cin >> a >> b; n       up(1, 0, n - 1, a - 1, b - 1); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m; n   cin >> n >> m; n   int arr[n], brr[m]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> brr[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (arr[i] == brr[j]) { n         cout <<  YES  <<   n ; n         cout << 1 <<     << arr[i] <<   n ; n         return; n       } n     } n   } n   cout <<  NO  <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t = 1; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n void i1d(vector<long long> &v) { n   for (auto &i : v) cin >> i; n } n void p1d(vector<long long> &v) { n   for (auto i : v) cout << i <<    ; n   cout << endl; n } n long long t; n void solve() { n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     string s; n     cin >> s; n     set<long long> o, z; n     map<long long, long long> ans; n     for (long long i = 0; i <= n - 1; i++) { n       if (s[i] ==  0 ) n         z.insert(i); n       else n         o.insert(i); n     } n     long long ctr = 0; n     long long ptr1 = 0, ptr2 = 0; n     for (long long i = 0; i <= n - 1; i++) { n       if (s[i] ==  0 ) { n         auto it = o.upper_bound(i); n         if (ans.count(i) == 0) ans[i] = ++ctr; n         if (it != o.end()) { n           ans[*it] = ans[i]; n           o.erase(it); n         } n       } else { n         auto it = z.upper_bound(i); n         if (ans.count(i) == 0) ans[i] = ++ctr; n         if (it != z.end()) { n           ans[*it] = ans[i]; n           z.erase(it); n         } n       } n     } n     cout << ctr <<   n ; n     for (long long i = 0; i <= n - 1; i++) { n       cout << ans[i] <<    ; n     } n     cout <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(false); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500000; n mt19937 Rand(time(0)); n int mod[3]; n struct hasher { n   int a[3]; n   hasher(int x = 0) { a[0] = a[1] = a[2] = x; } n   friend hasher operator+(const hasher &a, const hasher &b) { n     hasher res; n     for (int i = 0; i < 3; ++i) { n       res.a[i] = a.a[i] + b.a[i]; n       if (res.a[i] >= mod[i]) res.a[i] -= mod[i]; n     } n     return res; n   } n   friend hasher operator-(const hasher &a, const hasher &b) { n     hasher res; n     for (int i = 0; i < 3; ++i) { n       res.a[i] = a.a[i] - b.a[i]; n       if (res.a[i] < 0) res.a[i] += mod[i]; n     } n     return res; n   } n   friend hasher operator*(const hasher &a, const hasher &b) { n     hasher res; n     for (int i = 0; i < 3; ++i) res.a[i] = 1LL * a.a[i] * b.a[i] % mod[i]; n     return res; n   } n   friend bool operator==(const hasher &a, const hasher &b) { n     for (int i = 0; i < 3; ++i) n       if (a.a[i] != b.a[i]) return 0; n     return 1; n   } n   hasher inv() { n     hasher res = 1; n     for (int i = 0; i < 3; ++i) { n       int k = mod[i] - 2, x = a[i]; n       for (; k; k >>= 1, x = 1LL * x * x % mod[i]) n         if (k & 1) res.a[i] = 1LL * res.a[i] * x % mod[i]; n     } n     return res; n   } n } pw[N + 9]; n void Get_pw() { n   pw[0] = 1; n   pw[1] = 10; n   for (int i = 2; i <= N; ++i) pw[i] = pw[i - 1] * pw[1]; n } n hasher Get_hash(hasher *h, int l, int r) { n   return h[r] - h[l - 1] * pw[r - l + 1]; n } n hasher Get_hash(vector<hasher> &h, int l, int r) { n   return h[r] - h[l - 1] * pw[r - l + 1]; n } n char s[N + 9], t[N + 9]; n int n, m; n void into() { n   scanf( %s%s , s + 1, t + 1); n   n = strlen(s + 1); n   m = strlen(t + 1); n } n int Get_mod0() { return 19260817; } n bool Check_prime(int n) { n   for (int i = 2; i * i <= n; ++i) n     if (n % i == 0) return 0; n   return 1; n } n int Get_prime(int n) { n   for (; !Check_prime(n); ++n) n     ; n   return n; n } n int Get_mod1() { n   int sum = 0; n   for (int i = 1; i <= n; ++i) sum += s[i]; n   for (int i = 1; i <= m; ++i) sum += t[i]; n   int n = Rand() % 200000000 + 300000000 + sum; n   return Get_prime(n); n } n int Get_mod2() { n   int x = Rand() % 200000000 + 300000000; n   double y = 1.0 * (Rand() % 10), z = 1.0 * (Rand() & (1 << 28) - 1); n   return Get_prime((int)(x + exp(y) + cos(z) * z)); n } n void Get_mod() { n   mod[0] = Get_mod0(); n   mod[1] = Get_mod1(); n   mod[2] = Get_mod2(); n } n hasher hs[N + 9], ht[N + 9]; n void Get_hash() { n   for (int i = 1; i <= n; ++i) hs[i] = hs[i - 1] * pw[1] + (s[i] -  0 ); n   for (int i = 1; i <= m; ++i) ht[i] = ht[i - 1] * pw[1] + (t[i] -  0 ); n } n int Get_lcp(int p) { n   int l = 1, r = m, res = 0; n   for (int mid; l <= r;) { n     mid = l + r >> 1; n     Get_hash(hs, p, p + mid - 1) == Get_hash(ht, 1, mid) n         ? (res = mid, l = mid + 1) n         : r = mid - 1; n   } n   return res; n } n void Get_ans() { n   for (int i = 1; i <= n; ++i) { n     if (i - (m - 1) >= 0 || i + (m - 1) <= n) { n       if (Get_hash(hs, i - (m - 1) + 1, i) + Get_hash(hs, i + 1, i + (m - 1)) == n           ht[m]) { n         printf( %d %d n%d %d n , i - (m - 1) + 1, i, i + 1, i + (m - 1)); n         return; n       } n     } n     if (i + m <= n) { n       int t = m - Get_lcp(i + 1); n       if (t >= 1 && i >= t && n           Get_hash(hs, i + 1, i + m) + Get_hash(hs, i - t + 1, i) == ht[m]) { n         printf( %d %d n%d %d n , i - t + 1, i, i + 1, i + m); n         return; n       } n       if (t >= 2 && i >= t - 1 && n           Get_hash(hs, i + 1, i + m) + Get_hash(hs, i - t + 2, i) == ht[m]) { n         printf( %d %d n%d %d n , i - t + 2, i, i + 1, i + m); n         return; n       } n     } n     if (i - m >= 0) { n       int t = m - Get_lcp(i - m + 1); n       if (t >= 1 && i + t <= n && n           Get_hash(hs, i - m + 1, i) + Get_hash(hs, i + 1, i + t) == ht[m]) { n         printf( %d %d n%d %d n , i - m + 1, i, i + 1, i + t); n         return; n       } n       if (t >= 2 && i + t - 1 <= n && n           Get_hash(hs, i - m + 1, i) + Get_hash(hs, i + 1, i + t - 1) == n               ht[m]) { n         printf( %d %d n%d %d n , i - m + 1, i, i + 1, i + t - 1); n         return; n       } n     } n   } n } n void work() { n   Get_mod(); n   Get_pw(); n   Get_hash(); n   Get_ans(); n } n void outo() {} n int main() { n   int T = 1; n   for (; T--;) { n     into(); n     work(); n     outo(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using nagai = long long; n using ll = long long; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   int c = 0; n   int t = 1; n   while (t <= n) { n     n -= t, ++c, t *= 2; n   } n   if (n) ++c; n   cout << c <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 200000 + 5; n const int MMAX = 2000 + 5; n const int INF = 1000000000; n const int BS = 1000000000 + 7; n const long long INFLL = 1000000000ll * 1000000000ll; n const long double eps = 1e-6; n int a[NMAX]; n bool is_inverted[4 * NMAX]; n bool shouldSetTrue[4 * NMAX]; n bool shouldSetFalse[4 * NMAX]; n bool shouldInvert[4 * NMAX]; n void merge_flags(int parent, int child) { n   if (shouldSetFalse[parent]) { n     shouldSetFalse[child] = true; n     shouldSetTrue[child] = false; n     shouldInvert[child] = false; n     return; n   } n   if (shouldSetTrue[parent]) { n     shouldSetFalse[child] = false; n     shouldSetTrue[child] = true; n     shouldInvert[child] = false; n     return; n   } n   if (shouldInvert[parent]) { n     if (shouldSetFalse[child]) { n       shouldSetFalse[child] = false; n       shouldSetTrue[child] = true; n       shouldInvert[child] = false; n     } else if (shouldSetTrue[child]) { n       shouldSetFalse[child] = true; n       shouldSetTrue[child] = false; n       shouldInvert[child] = false; n     } else { n       shouldInvert[child] = !shouldInvert[child]; n     } n   } n } n void pass_down(int v, int tl, int tr) { n   if (tl == tr) { n     bool current = is_inverted[v]; n     if (shouldSetFalse[v]) { n       current = false; n     } n     if (shouldSetTrue[v]) { n       current = true; n     } n     if (shouldInvert[v]) { n       current = !current; n     } n     is_inverted[v] = current; n     shouldSetFalse[v] = false; n     shouldSetTrue[v] = false; n     shouldInvert[v] = false; n     return; n   } n   merge_flags(v, v + v); n   merge_flags(v, v + v + 1); n   shouldSetFalse[v] = false; n   shouldSetTrue[v] = false; n   shouldInvert[v] = false; n } n bool get(int v, int tl, int tr, int pos) { n   pass_down(v, tl, tr); n   if (tl == tr) { n     return is_inverted[v]; n   } n   int tm = (tl + tr) / 2; n   if (pos <= tm) { n     return get(v + v, tl, tm, pos); n   } else { n     return get(v + v + 1, tm + 1, tr, pos); n   } n } n void set_inverted(int v, int tl, int tr, int l, int r, bool shouldBeTrue, n                   bool shouldBeFalse, bool shouldBeInverted) { n   if (r < l) return; n   pass_down(v, tl, tr); n   if (l == tl && r == tr) { n     if (shouldBeFalse) { n       shouldSetFalse[v] = true; n     } n     if (shouldBeTrue) { n       shouldSetTrue[v] = true; n     } n     if (shouldBeInverted) { n       shouldInvert[v] = true; n     } n     return; n   } n   int tm = (tl + tr) / 2; n   if (r <= tm) { n     set_inverted(v + v, tl, tm, l, r, shouldBeTrue, shouldBeFalse, n                  shouldBeInverted); n   } else if (l >= tm + 1) { n     set_inverted(v + v + 1, tm + 1, tr, l, r, shouldBeTrue, shouldBeFalse, n                  shouldBeInverted); n   } else { n     set_inverted(v + v, tl, tm, l, tm, shouldBeTrue, shouldBeFalse, n                  shouldBeInverted); n     set_inverted(v + v + 1, tm + 1, tr, tm + 1, r, shouldBeTrue, shouldBeFalse, n                  shouldBeInverted); n   } n } n int main(int argc, char** argv) { n   if (argc > 1 && strcmp(argv[1],  seland ) == 0) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } n   int n, q; n   scanf( %d %d n , &n, &q); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   scanf(  n ); n   int bias = 100000; n   for (int i = 0; i < q; i++) { n     char c; n     int x; n     scanf( %c %d n , &c, &x); n     if (c ==  <  && x <= 0) { n       x--; n       set_inverted(1, 0, NMAX - 1, 0, bias + x, true, false, false); n       set_inverted(1, 0, NMAX - 1, bias - x, 2 * bias, false, true, false); n     } else if (c ==  >  && x >= 0) { n       x++; n       set_inverted(1, 0, NMAX - 1, bias + x, 2 * bias, true, false, false); n       set_inverted(1, 0, NMAX - 1, 0, bias - x, false, true, false); n     } else if (c ==  <  && x > 0) { n       x--; n       set_inverted(1, 0, NMAX - 1, 0, bias - x - 1, true, false, false); n       set_inverted(1, 0, NMAX - 1, bias + x + 1, 2 * bias, false, true, false); n       set_inverted(1, 0, NMAX - 1, bias - x, bias + x, false, false, true); n     } else if (c ==  >  && x < 0) { n       x++; n       set_inverted(1, 0, NMAX - 1, bias - x + 1, 2 * bias, true, false, false); n       set_inverted(1, 0, NMAX - 1, 0, bias + x - 1, false, true, false); n       set_inverted(1, 0, NMAX - 1, bias + x, bias - x, false, false, true); n     } n   } n   for (int i = 0; i < n; i++) { n     bool inverted = get(1, 0, NMAX - 1, a[i] + bias); n     if (inverted) { n       printf( %d  , -a[i]); n     } else { n       printf( %d  , a[i]); n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long i, j, k, n, m, ans = 0, sum = 0, a[100005] = {0}, mn = 2e9, mx = 0, n                            x, y, z, cnt = 0; n   string s; n   set<long long> zero, one; n   set<long long>::iterator it, it1; n   vector<long long> v[200005]; n   cin >> s; n   for (i = 0; i < s.size(); i++) { n     if (s[i] ==  0 ) { n       zero.insert(i); n     } else { n       one.insert(i); n     } n   } n   while (one.size()) { n     i = -1; n     if (zero.size() == 0) { n       cout << -1; n       return 0; n     } n     it = zero.lower_bound(i); n     i = *it; n     zero.erase(i); n     v[ans].push_back(i); n     while (one.size()) { n       it = one.lower_bound(i); n       if (it == one.end()) { n         break; n       } n       i = *it; n       one.erase(i); n       v[ans].push_back(i); n       it = zero.lower_bound(i); n       if (it == zero.end()) { n         cout << -1; n         return 0; n       } n       i = *it; n       zero.erase(i); n       v[ans].push_back(i); n     } n     ans++; n   } n   k = ans + zero.size(); n   cout << k << endl; n   for (i = 0; i < ans; i++) { n     for (j = 0; j < v[i].size(); j++) { n       if (j == 0) { n         cout << v[i].size() <<    ; n       } n       cout << v[i][j] + 1 <<    ; n     } n     cout << endl; n   } n   while (zero.size()) { n     it = zero.begin(); n     cout << 1 <<     << *it + 1 << endl; n     zero.erase(it); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n long long int a[101]; n long long int dp[101][101][101]; n long long int f(int start, int end, int l) { n   if (start > end) { n     return 0; n   } n   if (start == end) { n     return a[l]; n   } n   if (dp[start][end][l] != 0) { n     return dp[start][end][l]; n   } n   long long int ans = a[l] + f(start + 1, end, 1); n   for (int i = start + 1; i <= end; i++) { n     if (s[i] == s[start]) { n       ans = max(ans, f(start + 1, i - 1, 1) + f(i, end, l + 1)); n     } n   } n   dp[start][end][l] = ans; n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> s; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   a[0] = 0; n   cout << f(0, n - 1, 1); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 103, maxm = 1003; n int m, n, ar[maxn]; n double dp[maxn * maxm], pre[maxn * maxm]; n int main() { n   scanf( %d%d , &n, &m); n   if (m == 1) { n     printf( 1 ); n     return 0; n   } n   int sum = 0; n   for (int i = 1; i <= n; i++) scanf( %d , &ar[i]), sum += ar[i]; n   dp[0] = 1; n   for (int i = 1; i <= n; i++) { n     pre[0] = dp[0]; n     for (int j = 1; j <= i * m; j++) pre[j] = pre[j - 1] + dp[j]; n     for (int j = i * m; j; j--) { n       dp[j] = pre[j - 1]; n       if (j - m - 1 >= 0) dp[j] -= pre[j - m - 1]; n       if (j - ar[i] >= 0) dp[j] -= dp[j - ar[i]]; n       dp[j] /= m - 1; n     } n     dp[0] = 0; n   } n   double ans = 0; n   for (int i = 0; i < sum; i++) ans += dp[i]; n   printf( %.10lf n , 1 + ans * (m - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> a(n, 0); n     vector<long long> pos(32, 0); n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       long long ind = 0; n       long long num = a[i]; n       while (num != 0) { n         if (num % 2 == 1) pos[ind] += 1; n         ind++; n         num /= 2; n       } n     } n     long long f = 0; n     for (long long i = pos.size() - 1; i >= 0; i--) { n       if (pos[i] % 2 == 1) { n         f = 1; n         if (pos[i] == 1) n           cout <<  WIN n ; n         else { n           if ((pos[i] / 2) % 2 == 0) n             cout <<  WIN n ; n           else { n             if (n % 2 == 0) n               cout <<  WIN n ; n             else n               cout <<  LOSE n ; n           } n         } n         break; n       } n     } n     if (f == 0) cout <<  DRAW n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, t, piles = 1; n   bool flag = true; n   cin >> n; n   vector<int> s1, s2; n   vector<int>::iterator it; n   for (i = 0; i < n; i++) { n     cin >> t; n     s1.push_back(t); n   } n   sort(s1.begin(), s1.end()); n   while (flag) { n     flag = false; n     for (it = s1.begin(), i = 0; it != s1.end();) { n       if ((*it) < i) { n         flag = true; n         s2.push_back(*it); n         s1.erase(it); n       } else { n         it++; n         i++; n       } n     } n     if (flag) { n       piles += 1; n       s1.clear(); n       s1 = s2; n       s2.clear(); n     } n   } n   cout << piles; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n const int M = 1 << 16; n bitset<N> mp[M]; n int a[N], sum, rk[N], k, n; n priority_queue<pair<int, int> > que; n void dfs(int s, int t) { n   if (s == 0) return; n   int r = 0; n   for (; t * k < N && mp[s][t * k]; r++, t *= k) { n   } n   for (int i = 0; i < n; i++) { n     if (s & (1 << i)) rk[i] += r; n   } n   for (int i = 0; i < n; i++) { n     if (s & (1 << i) && t >= a[i] && mp[s - (1 << i)][t - a[i]]) { n       dfs(s - (1 << i), t - a[i]); n       return; n     } n   } n } n int main() { n   sum = 0; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n     sum += a[i]; n   } n   mp[0][0] = 1; n   for (int s = 1; s < (1 << n); s++) { n     for (int i = 0; i < n; i++) { n       if (s & (1 << i)) mp[s] |= mp[s - (1 << i)] << a[i]; n     } n     for (int i = sum / k; i >= 1; i--) { n       if (mp[s][i * k]) mp[s][i] = 1; n     } n   } n   if (!mp[(1 << n) - 1][1]) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   dfs((1 << n) - 1, 1); n   for (int i = 0; i < n; i++) { n     que.push({rk[i], a[i]}); n   } n   while (que.size() != 1) { n     pair<int, int> u = que.top(); n     que.pop(); n     pair<int, int> v = que.top(); n     que.pop(); n     printf( %d %d n , u.second, v.second); n     u.second += v.second; n     while (u.second % k == 0) { n       u.second /= k; n       u.first--; n     } n     que.push(u); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int u; n   int v; n   int c; n }; n bool compare(edge e, edge u) { return e.c < u.c; } n int findparent(int parent[], int u) { n   int p = parent[u]; n   if (p == u) { n     return p; n   } n   int fp = findparent(parent, p); n   if (p == fp) { n     return fp; n   } n   parent[u] = fp; n   return parent[u]; n } n int mc = -1; n void uni(int special[], int parent[], int u, int v, int c) { n   int up = findparent(parent, u); n   int vp = findparent(parent, v); n   if (vp == up) { n     return; n   } n   parent[up] = vp; n   if (special[up] != 0 && special[vp] != 0) { n     special[vp] = c; n     special[up] = c; n     mc = c; n   } else { n     int max = special[up] > special[vp] ? special[up] : special[vp]; n     special[up] = special[vp] = max; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   vector<edge> edges; n   int special[n]; n   for (int i = 0; i < n; i++) { n     special[i] = 0; n   } n   for (int i = 0; i < k; i++) { n     int tmp; n     cin >> tmp; n     tmp--; n     special[tmp] = 1; n   } n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     u--; n     v--; n     edges.push_back({u, v, w}); n   } n   sort(edges.begin(), edges.end(), compare); n   int parent[n]; n   for (int i = 0; i < n; i++) { n     parent[i] = i; n   } n   for (int i = 0; i < m; i++) { n     int u = edges[i].u; n     int v = edges[i].v; n     int c = edges[i].c; n     uni(special, parent, u, v, c); n   } n   for (int i = 0; i < k; i++) { n     cout << mc <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double f[400][3100], p[400][3100], ans; n int n, m, up[400]; n void dp(int i) { n   int j; n   long double pre, nw; n   pre = f[i][0]; n   f[i][0] = 0; n   for (j = 1; j <= n; j++) { n     nw = f[i][j]; n     f[i][j] = pre * p[i][j] + f[i][j - 1] * (1 - p[i][j]); n     pre = nw; n   } n   up[i]++; n } n int main() { n   int i, j, k, x, to; n   long double mx; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) { n       scanf( %d , &x); n       p[j][i] = (long double)x / (long double)1000; n     } n   for (i = 1; i <= m; i++) n     for (j = 0; j <= n; j++) f[i][j] = 1; n   for (i = 1; i <= m; i++) up[i] = 0; n   for (i = 1; i <= m; i++) dp(i); n   ans = 0; n   for (i = 1; i <= n; i++) { n     mx = -1; n     for (j = 1; j <= m; j++) n       if (f[j][n] > mx) mx = f[j][n], to = j; n     ans += mx; n     dp(to); n   } n   double out = ans; n   printf( %.11f n , out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1010], seq[1010], id; n int qtes, u[1000010], v[1000010]; n inline void add(int x, int y) { n   a[y] -= a[x], a[x] *= 2, u[++qtes] = x, v[qtes] = y; n } n inline void Solve(int x, int y, int z) { n   int q[3] = {x, y, z}; n   register int i, j; n   for (i = 0; i < 3; ++i) n     for (j = i + 1; j < 3; ++j) n       if (a[q[i]] > a[q[j]]) swap(q[i], q[j]); n   x = q[0], y = q[1], z = q[2]; n   if (x == y) { n     add(x, y); n     return; n   } n   if (y == z) { n     add(y, z); n     return; n   } n   int w = a[y] / a[x]; n   int tmp = w, up; n   for (i = 0; tmp; ++i) n     if ((tmp >> i) & 1) up = i, tmp ^= (1 << i); n   for (i = 0; i <= up; ++i) { n     if ((w >> i) & 1) n       add(x, y); n     else n       add(x, z); n   } n   if (a[x] == 0 || a[y] == 0 || a[z] == 0) n     return; n   else n     Solve(x, y, z); n } n int main() { n   int n; n   scanf( %d , &n); n   register int i, j; n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]); n   int last = 0; n   for (i = 1; i <= n; ++i) n     if (a[i]) ++last; n   if (last < 2) { n     puts( -1 ); n     return 0; n   } else if (last == 2) { n     puts( 0 ); n     return 0; n   } n   for (last -= 2; last--;) { n     for (id = 0, i = 1; i <= n; ++i) n       if (a[i]) seq[++id] = i; n     Solve(seq[1], seq[2], seq[3]); n   } n   printf( %d n , qtes); n   for (i = 1; i <= qtes; ++i) printf( %d %d n , u[i], v[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int MAXN = 1e5 + 10; n const int MAXK = 5 + 1; n long long sumv[MAXK][MAXN << 2], setv[MAXN << 2]; n long long prefix[MAXK][MAXN], C[MAXK][MAXK]; n void Pushdown(int o, int lft, int rht) { n   if (setv[o] != -1) { n     setv[(o << 1)] = setv[(o << 1 | 1)] = setv[o]; n     int mid = lft + rht >> 1; n     for (int k = 0; k < MAXK; ++k) { n       sumv[k][(o << 1)] = n           setv[(o << 1)] * (prefix[k][mid] - prefix[k][lft - 1] + mod) % mod; n       sumv[k][(o << 1 | 1)] = n           setv[(o << 1 | 1)] * (prefix[k][rht] - prefix[k][mid] + mod) % mod; n     } n     setv[o] = -1; n   } n } n void Pushup(int o) { n   for (int k = 0; k < MAXK; ++k) n     sumv[k][o] = sumv[k][(o << 1)] + sumv[k][(o << 1 | 1)]; n } n void Build(int o, int lft, int rht) { n   if (lft == rht) { n     scanf( %lld , setv + o); n     sumv[0][o] = setv[o] % mod; n     for (int k = 1; k < MAXK; ++k) sumv[k][o] = sumv[k - 1][o] * lft % mod; n   } else { n     int mid = lft + rht >> 1; n     Build((o << 1), lft, mid); n     Build((o << 1 | 1), mid + 1, rht); n     Pushup(o); n   } n } n int ul, ur, uv; n void Update(int o, int lft, int rht) { n   if (ul <= lft and rht <= ur) { n     setv[o] = uv; n     for (int k = 0; k < MAXK; ++k) n       sumv[k][o] = setv[o] * (prefix[k][rht] - prefix[k][lft - 1] + mod) % mod; n   } else { n     Pushdown(o, lft, rht); n     int mid = lft + rht >> 1; n     if (ul <= mid) Update((o << 1), lft, mid); n     if (ur > mid) Update((o << 1 | 1), mid + 1, rht); n     Pushup(o); n   } n } n int ql, qr, qk; n long long Query(int o, int lft, int rht) { n   if (ql <= lft and rht <= qr) return sumv[qk][o]; n   Pushdown(o, lft, rht); n   int mid = lft + rht >> 1; n   long long ans = 0; n   if (ql <= mid) ans += Query((o << 1), lft, mid); n   if (qr > mid) ans += Query((o << 1 | 1), mid + 1, rht); n   return ans % mod; n } n void work() { n   for (int i = 0; i < MAXK; ++i) { n     C[i][0] = C[i][i] = 1; n     for (int j = 1; j < i; ++j) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; n   } n   for (int i = 1; i < MAXN; ++i) { n     long long u = 1; n     for (int k = 0; k < MAXK; ++k) { n       prefix[k][i] = (prefix[k][i - 1] + u) % mod; n       u = u * i % mod; n     } n   } n   int N, Q, K; n   char cmd[20]; n   long long ans, ret; n   scanf( %d%d , &N, &Q); n   memset(setv, -1, sizeof setv); n   Build(1, 1, N); n   while (Q--) { n     scanf( %s , cmd); n     switch (cmd[0]) { n       case  ? : n         scanf( %d%d%d , &ql, &qr, &K); n         ans = 0; n         ret = 1; n         for (qk = K; qk >= 0; --qk, ret = ret * (1 - ql) % mod) n           ans += Query(1, 1, N) * (ret) % mod * C[K][qk] % mod; n         printf( %lld n , (ans % mod + mod) % mod); n         break; n       case  = : n         scanf( %d%d%d , &ul, &ur, &uv); n         Update(1, 1, N); n         break; n     } n   } n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using uint = unsigned int; n using ll = long long; n using ull = unsigned long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <typename T1, typename T2> n ostream &operator<<(ostream &out, const pair<T1, T2> &item) { n   out <<  (  << item.first <<  ,   << item.second <<  ) ; n   return out; n } n template <typename T> n ostream &operator<<(ostream &out, const vector<T> &v) { n   for (const auto &item : v) out << item <<    ; n   return out; n } n const int D = 510; n const int S = 5010; n bool dp[S][D]; n pair<pii, int> prv[S][D]; n queue<pii> q; n string reconstruct(int s, int r) { n   if (prv[s][r].second == -1) return   ; n   return reconstruct(prv[s][r].first.first, prv[s][r].first.second) + n          char(prv[s][r].second +  0 ); n } n int main() { n   ios_base::sync_with_stdio(false); n   int d, s; n   cin >> d >> s; n   memset(dp, 0, sizeof dp); n   memset(prv, -1, sizeof prv); n   q.push({0, 0}); n   dp[0][0] = true; n   while (!q.empty()) { n     int sd = q.front().first; n     int r = q.front().second; n     q.pop(); n     for (int dig = 0; dig < 10; ++dig) { n       int newr = (r * 10 + dig) % d; n       if (sd + dig > s) continue; n       if (!dp[sd + dig][newr]) { n         dp[sd + dig][newr] = true; n         prv[sd + dig][newr] = {{sd, r}, dig}; n         q.push({sd + dig, newr}); n       } n     } n   } n   if (!dp[s][0]) n     cout << -1 <<   n ; n   else n     cout << reconstruct(s, 0) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x, y, dst, cur; n int main() { n   cin >> m >> n >> k; n   dst = n + m + 34; n   for (int i = 0; i < k; i++) { n     cin >> x >> y; n     cur = min(min(x - 1, y - 1), min(m - x, n - y)); n     dst = min(dst, cur); n   } n   if (dst < 5) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ceil(long long a, long long b) { n   if (a % b == 0) return a / b; n   return a / b + 1; n } n int main() { n   long long n; n   int q; n   scanf( %lld %d , &n, &q); n   for (int j = 0; (j) < int(q); (j)++) { n     int x, y; n     scanf( %d %d , &y, &x); n     long long ans = 0; n     int l = y - 1; n     int m = l / 2; n     if ((x + y) % 2 == 0) { n       ans += ceil(n, 2) * (l - m) + (n / 2) * m; n       ans += (y % 2 == 0 ? x : x + 1) / 2; n     } else { n       ans += ceil(n, 2) * m + (n / 2) * (l - m); n       ans += (y % 2 == 0 ? x + 1 : x) / 2; n       ans += ceil(n * n, 2); n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100005, xian = (long long)1500000 * 1500000; n long long q[N], ans[N], n; n pair<long long, long long> a[N]; n inline long long cal(pair<long long, long long> x) { n   return x.first * x.first + x.second * x.second; n } n pair<long long, long long> solve(long long l, long long r) { n   if (l == r) return a[q[l]]; n   long long mid = (l + r) >> 1; n   while (1) { n     random_shuffle(&q[l], &q[r + 1]); n     pair<long long, long long> t1 = solve(l, mid), t2 = solve(mid + 1, r); n     pair<long long, long long> t3 = make_pair(t1.first + t2.first, n                                               t1.second + t2.second), n                                t4 = make_pair(t1.first - t2.first, n                                               t1.second - t2.second); n     pair<long long, long long> t = cal(t3) < cal(t4) ? t3 : t4; n     if (cal(t) <= xian) return t; n   } n } n pair<long long, long long> sol(long long l, long long r) { n   if (l == r) return a[q[l]]; n   long long mid = (l + r) >> 1; n   while (1) { n     pair<long long, long long> t1 = sol(l, mid), t2 = sol(mid + 1, r); n     pair<long long, long long> t3 = make_pair(t1.first + t2.first, n                                               t1.second + t2.second), n                                t4 = make_pair(t1.first - t2.first, n                                               t1.second - t2.second), n                                t; n     if (cal(t3) < cal(t4)) n       t = t3; n     else { n       t = t4; n       ans[mid + 1] *= -1; n       ans[r + 1] *= -1; n     } n     if (cal(t) <= xian) return t; n   } n } n long long an[N]; n signed main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld%lld , &a[i].first, &a[i].second); n     q[i] = i; n     ans[i] = 1; n   } n   solve(1, n); n   sol(1, n); n   ans[0] = 1; n   for (long long i = 1; i <= n; i++) ans[i] *= ans[i - 1]; n   for (long long i = 1; i <= n; i++) an[q[i]] = ans[i]; n   for (long long i = 1; i <= n; i++) n     if (an[i] == 1) { n       putchar( 1 ); n       putchar(   ); n     } else { n       putchar( - ); n       putchar( 1 ); n       putchar(   ); n     } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void gg(T &res) { n   res = 0; n   T fh = 1; n   char ch = getchar(); n   while ((ch >  9  || ch <  0 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) fh = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) res = res * 10 + ch -  0 , ch = getchar(); n   res *= fh; n } n inline int gi() { n   int x; n   gg(x); n   return x; n } n inline long long gl() { n   long long x; n   gg(x); n   return x; n } n const int MAXN = 610; n const int MAXM = 200010; n const int INF = 1e9; n const int BIG = 1e7; n int bt = 1, b[MAXN], NEXT[MAXM], to[MAXM], val[MAXM]; n inline void add(int x, int y, int z) { n   NEXT[++bt] = b[x]; n   b[x] = bt; n   to[bt] = y; n   val[bt] = z; n   NEXT[++bt] = b[y]; n   b[y] = bt; n   to[bt] = x; n   val[bt] = 0; n } n int T, dis[MAXN], q[MAXN]; n bool fc() { n   for (int i = 1; i <= T; i++) dis[i] = 0; n   dis[0] = 1; n   int l = 1, r = 0; n   q[++r] = 0; n   while (l <= r) { n     int x = q[l++]; n     for (int i = b[x]; i; i = NEXT[i]) { n       if (!val[i] || dis[to[i]]) continue; n       dis[to[i]] = dis[x] + 1; n       if (to[i] == T) return 1; n       q[++r] = to[i]; n     } n   } n   return 0; n } n int dfs(int x, int M) { n   if (x == T) return M; n   int ans = 0; n   for (int i = b[x]; i; i = NEXT[i]) { n     if (!val[i] || dis[x] + 1 != dis[to[i]]) continue; n     int r = dfs(to[i], min(val[i], M - ans)); n     val[i] -= r; n     val[i ^ 1] += r; n     if ((ans += r) == M) return ans; n   } n   dis[x] = -1; n   return ans; n } n int main() { n   int n = gi(); n   T = n + n + 1; n   for (int i = 1; i <= n; i++) n     for (int t = gi(); t--;) add(i, n + gi(), INF); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     int x = BIG - gi(); n     ans -= x; n     add(0, i, x); n     add(n + i, T, BIG); n   } n   while (fc()) ans += dfs(0, INF); n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long INF = 1e12; n vector<long long> ans[200010]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (long long i = 0; i < n; i++) ans[i].push_back(-1); n   ans[0][0] = a[0]; n   long long sz = 1; n   for (long long i = 1; i < n; i++) { n     long long l = -1, r = sz; n     while (r - l > 1) { n       long long mid = (l + r) / 2; n       long long tmp = ans[mid][ans[mid].size() - 1]; n       if (tmp > a[i]) { n         l = mid; n       } else { n         r = mid; n       } n     } n     if (r == sz) { n       ans[r][0] = a[i]; n       sz++; n     } else { n       ans[r].push_back(a[i]); n     } n   } n   for (long long i = 0; i < sz; i++) { n     for (long long ii = 0; ii < ans[i].size(); ii++) { n       if (ii) cout <<    ; n       cout << ans[i][ii]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000; n const int INF = 0x7ffffff; n const int MOD = 1e9 + 7; n int a[MAXN + 17], b[MAXN + 17], id[MAXN + 17]; n bool cmp(int c, int d) { return a[c - 1] > a[d - 1]; } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) cin >> b[i]; n   for (int i = 0; i < n; ++i) id[i] = i + 1; n   sort(id, id + n, cmp); n   cout << n / 2 + 1 << endl; n   cout << id[0] << endl; n   if (!(n & 1)) cout << id[1] << endl; n   int t = (n & 1) ? n / 2 : n / 2 - 1, j; n   for (j = n - 1; t > 0; t--, j -= 2) n     cout << (b[id[j] - 1] > b[id[j - 1] - 1] ? id[j] : id[j - 1]) <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n const int N = 5e5 + 5; n using namespace std; n int n, m, k; n vector<int> build_kmp(string s, string t, bool check = false) { n   vector<int> t_match(((int)t.size()) + 1, 0); n   for (int i = (2); i <= (((int)t.size())); ++i) { n     int c = t_match[i - 1]; n     while (c > 0 && t[c] != t[i - 1]) c = t_match[c]; n     if (t[c] == t[i - 1]) ++c; n     t_match[i] = c; n   } n   vector<int> s_match(((int)s.size()) + 1, 0); n   for (int i = (1); i <= (((int)s.size())); ++i) { n     int c = s_match[i - 1]; n     while (c > 0 && t[c] != s[i - 1]) c = t_match[c]; n     if (t[c] == s[i - 1]) ++c; n     if (c == m && check) { n       int t = max(i, k); n       if (n - t >= k && t - k + 1 <= i - m + 1) { n         cout <<  Yes  << endl; n         cout << t - k + 1 <<     << t + 1 << endl; n         exit(0); n       } n       t = min(n - k, i - m); n       if (t >= k && i <= t + k) { n         cout <<  Yes  << endl; n         cout << t - k + 1 <<     << t + 1 << endl; n         exit(0); n       } n     } n     s_match[i] = c; n   } n   vector<int> best(((int)t.size()) + 1, -1); n   for (int i = (k); i <= (((int)s.size())); ++i) { n     int c = s_match[i]; n     for (; best[c] == -1; c = t_match[c]) best[c] = i; n   } n   return best; n } n int main() { n   string s, t; n   cin >> n >> m >> k >> s >> t; n   auto L = build_kmp(s, t, true); n   reverse((s).begin(), (s).end()); n   reverse((t).begin(), (t).end()); n   auto R = build_kmp(s, t); n   for (int i = (0); i <= (m); ++i) n     if (R[i] != -1) R[i] = n - R[i] + 1; n   for (int i = (0); i <= (m); ++i) n     if (i <= k && m - i <= k && L[i] != -1 && R[m - i] != -1 && n         L[i] < R[m - i]) { n       cout <<  Yes  << endl; n       cout << L[i] - k + 1 <<     << R[m - i] << endl; n       return 0; n     } n   cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int MOD = 1e9 + 7; n long long int INF = 1e18; n long long int power(long long int base, long long int exp) { n   long long int res = 1; n   while (exp > 0) { n     if (exp % 2 == 1) res = (res * base); n     base = (base * base); n     exp /= 2; n   } n   return res; n } n long long int bitc(long long int n, long long int x) { return ((n >> x) & 1); } n long long int __gcd(long long int a, long long int b) { n   return b == 0 ? a : __gcd(b, a % b); n } n long long int fsub(long long int a, long long int b, long long int p = MOD) { n   return ((a % p) - (b % p) + p) % p; n } n long long int fmult(long long int a, long long int b, long long int p = MOD) { n   return ((a % p) * (b % p)) % p; n } n long long int fadd(long long int a, long long int b, long long int p = MOD) { n   return (a % p + b % p) % p; n } n long long int fpow(long long int n, long long int k, long long int p = MOD) { n   long long int r = 1; n   while (k > 0) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n     k = k >> 1; n   } n   return r; n } n long long int inv(long long int a, long long int p = MOD) { n   return fpow(a, p - 2, p); n } n long long int fdiv(long long int a, long long int b, long long int p = MOD) { n   long long int yinv = inv(b); n   long long int ans = (a * yinv) % p; n   return ans; n } n template <typename T> n istream &operator>>(istream &in, vector<T> &a) { n   for (auto &item : a) { n     in >> item; n   } n   return in; n } n template <typename T, typename U> n ostream &operator<<(ostream &out, pair<T, U> &a) { n   cout << a.first <<     << a.second; n   return out; n } n template <typename T, typename U> n istream &operator>>(istream &out, pair<T, U> &a) { n   cin >> a.first >> a.second; n   return out; n } n template <typename T, typename U> n ostream &operator<<(ostream &out, map<T, U> &a) { n   for (auto &item : a) { n     out << item <<   n ; n   } n   return out; n } n template <typename T> n ostream &operator<<(ostream &out, vector<T> &a) { n   for (auto &item : a) { n     out << item <<    ; n   } n   return out; n } n template <typename T> n ostream &operator<<(ostream &out, vector<vector<T>> &a) { n   for (auto &item : a) { n     out << item <<   n ; n   } n   return out; n } n template <int D, typename T> n struct Vec : public vector<Vec<D - 1, T>> { n   static_assert(D >= 1,  Vector dimension must be greater than zero! ); n   template <typename... Args> n   Vec(int n = 0, Args... args) n       : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {} n }; n template <typename T> n struct Vec<1, T> : public vector<T> { n   Vec(int n = 0, T val = T()) : vector<T>(n, val) {} n }; n std::vector<bool> is_prime; n std::vector<long long int> primes; n void sieve(long long int n) { n   is_prime.resize(n + 2, true); n   primes.clear(); n   long long int p; n   for (p = 2; p * p <= n; p++) { n     if (is_prime[p]) { n       long long int i; n       for (i = p * p; i <= n; i += p) { n         is_prime[i] = false; n       } n     } n   } n   is_prime[0] = is_prime[1] = false; n   long long int i; n   for (i = 2; i <= n; i++) { n     if (is_prime[i]) { n       primes.emplace_back(i); n     } n   } n } n map<long long int, long long int> prime_factors(long long int n) { n   map<long long int, long long int> s; n   long long int i; n   long long int tc = 0; n   while (n % 2 == 0) { n     tc++; n     n /= 2; n   } n   if (tc > 0) { n     s[2] = tc; n   } n   for (i = 3; i <= sqrt(n); i += 2) { n     tc = 0; n     while (n % i == 0) { n       tc++; n       n /= i; n     } n     if (tc > 0) { n       s[i] = tc; n     } n   } n   if (n > 2) { n     s[n] += 1; n   } n   return s; n } n std::vector<long long int> fact_vec; n void fact_fun(long long int n) { n   fact_vec.resize(n + 10); n   long long int i; n   fact_vec[0] = 1; n   for (i = 1; i <= n + 2; i++) { n     fact_vec[i] = (fact_vec[i - 1] * i) % MOD; n   } n } n std::vector<long long int> p2; n void power_2(long long int n, long long int m = MOD) { n   long long int i; n   MOD = m; n   p2.emplace_back(1); n   for (i = 0; i < n; i++) { n     p2.emplace_back(fmult(p2.back(), 2)); n   } n } n long long int ncr(long long int n, long long int r) { n   if (r > n) return 0; n   return fdiv(fact_vec[n], fmult(fact_vec[r], fact_vec[n - r])); n } n std::vector<long long int> spf; n void sieve2(long long int MAXN) { n   MAXN += 10; n   spf.resize(MAXN, 0); n   spf[1] = 1; n   for (long long int i = 2; i < MAXN; i++) spf[i] = i; n   for (long long int i = 4; i < MAXN; i += 2) spf[i] = 2; n   for (long long int i = 3; i * i < MAXN; i++) { n     if (spf[i] == i) { n       for (long long int j = i * i; j < MAXN; j += i) { n         if (spf[j] == j) spf[j] = i; n       } n     } n   } n } n std::vector<long long int> getFactorization(long long int x) { n   std::vector<long long int> ret; n   while (x != 1) { n     ret.emplace_back(spf[x]); n     x = x / spf[x]; n   } n   return ret; n } n int main() { n   long long int i, j; n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long int ti; n   ti = 1; n   while (ti--) { n     long long int n, m; n     cin >> n >> m; n     Vec<2, long long int> mat(n + 2, m + 2), left, right, up, down, uvis, lvis, n         vis; n     uvis = lvis = vis = left = right = up = down = mat; n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         char c; n         cin >> c; n         if (c ==  * ) { n           mat[i][j] = 1; n         } n       } n     } n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         if (mat[i][j] && mat[i][j - 1]) { n           left[i][j] = left[i][j - 1] + 1; n         } n       } n       for (j = m; j >= 1; j--) { n         if (mat[i][j] && mat[i][j + 1]) { n           right[i][j] = right[i][j + 1] + 1; n         } n       } n     } n     for (j = 1; j <= m; j++) { n       for (i = 1; i <= n; i++) { n         if (mat[i][j] && mat[i - 1][j]) { n           up[i][j] = up[i - 1][j] + 1; n         } n       } n       for (i = n; i >= 1; i--) { n         if (mat[i][j] && mat[i + 1][j]) { n           down[i][j] = down[i + 1][j] + 1; n         } n       } n     } n     vector<pair<pair<long long int, long long int>, long long int>> ans; n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         std::vector<long long int> tv = {up[i][j], down[i][j], left[i][j], n                                          right[i][j]}; n         long long int mn = *min_element((tv).begin(), (tv).end()); n         if (mn != 0) { n           ans.emplace_back(make_pair(make_pair(i, j), mn)); n           long long int lr = j - mn; n           long long int rr = j + mn + 1; n           lvis[i][lr]++; n           lvis[i][rr]--; n           uvis[i - mn][j]++; n           uvis[i + mn + 1][j]--; n         } n       } n     } n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         vis[i][j] = lvis[i][j] + vis[i][j - 1]; n         if (mat[i][j] && vis[i][j] > 0) { n           mat[i][j] = 0; n         } n       } n     } n     vis.clear(); n     vis.resize(n + 2, m + 2); n     for (j = 1; j <= m; j++) { n       for (i = 1; i <= n; i++) { n         vis[i][j] = uvis[i][j] + vis[i - 1][j]; n         if (mat[i][j] && vis[i][j] > 0) { n           mat[i][j] = 0; n         } n       } n     } n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         if (mat[i][j]) { n           cout << -1 <<   n ; n           return 0; n         } n       } n     } n     cout << ans.size() <<   n ; n     for (auto p : ans) { n       cout << p.first.first <<     << p.first.second <<     << p.second <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> a(m); n   int cnt = 0, ma = 0; n   for (int i = 0; i < m; ++i) { n     cin >> a[i]; n     cnt += a[i]; n     ma = max(ma, a[i]); n   } n   if (cnt < n || 2 * (cnt - ma) < n || (n % 2) && m <= 2) { n     cout << -1 << endl; n     return 0; n   } n   set<pair<int, int> > q; n   vector<int> res; n   for (int i = 0; i < n; ++i) { n     int ma = 0, best = 0; n     if (i && res[0] != res.back() && a[res[0]] && i + 1 < n) { n       best = res[0]; n     } else { n       for (int j = 0; j < m; ++j) n         if (res.empty() || j != res.back()) n           if (i + 1 < n || res[0] != j) { n             if (ma < a[j]) { n               ma = a[j]; n               best = j; n             } n           } n     } n     res.push_back(best); n     --a[best]; n   } n   for (int i = 0; i < res.size(); ++i) printf( %d  , res[i] + 1); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int TC; n   cin >> TC; n   while (TC--) { n     map<char, int> mp; n     string s; n     cin >> s; n     for (int i = 0; i < s.size(); i++) { n       if (mp[s[i]] <= 1) mp[s[i]]++; n     } n     int x1 = 0, x2 = 0; n     for (auto i : mp) { n       if (i.second == 2) x2++; n       if (i.second == 1) x1++; n     } n     long long ans = (x1 / 2) + x2; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   setlocale(LC_ALL,  RUS ); n   double r; n   cin >> r; n   for (double x = 1; x <= 9; x++) { n     double y = ((r - 1) / (2 * x)) - ((x + 1) / 2); n     if (y == (long long)y && y > 0) { n       cout << x <<     << fixed << setprecision(0) << y; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * f; n } n const long long MAXN = 600010; n const long long INF = 2147483600; n long long N, K; n struct data { n   long long l, r, id; n   bool operator<(const data &a) const { n     if (a.l != l) return l < a.l; n     return r < a.r; n   } n } q[MAXN + 1]; n vector<pair<long long, long long> > vec[MAXN + 1]; n set<data>::iterator Ers[MAXN + 1]; n set<data> S; n long long sum[MAXN + 1], kans, pans; n inline data mdata(long long l, long long r, long long id) { n   data n; n   n.l = l; n   n.r = r; n   n.id = id; n   return n; n } n inline void check(long long x) { n   long long nw = 1, sm = 0; n   kans = 0; n   pans = 0; n   long long sumv = 0; n   for (long long i = 1; i <= N; i++) sum[i] = 0; n   for (long long i = 1; i <= N; i++) { n     long long sumt = 0; n     for (long long j = 0, sz = vec[i].size(); j < sz; j++) { n       pair<long long, long long> k = vec[i][j]; n       sum[k.first] += k.second; n       sumt += k.second; n       if (k.first < nw) sm += k.second, sumv += k.second * (nw - k.first); n     } n     sum[i + 1] -= sumt; n     pans += sumv; n     while (nw <= i && sum[nw] + sm >= x) n       sm += sum[nw], pans += sm, sumv += sm, ++nw; n     kans += nw - 1; n   } n } n long long top; n int main() { n   N = read(); n   K = read(); n   for (long long i = 1; i <= N; i++) { n     q[i].l = read(), q[i].r = read() - 1; n     q[i].id = i; n   } n   for (long long i = 1; i <= N; i++) { n     set<data>::iterator it = S.upper_bound(q[i]); n     data l; n     top = 0; n     if (S.begin() != it) --it; n     if (it == S.end()) n       l.l = INF; n     else n       l = (*it); n     while (it != S.end()) { n       if ((*it).r < q[i].l) { n         ++it; n         continue; n       } n       if ((*it).l > q[i].r) break; n       Ers[++top] = it; n       if ((*it).r >= q[i].r) break; n       ++it; n     } n     data r; n     r.r = -1; n     if (it != S.end()) r = (*it); n     long long last = q[i].l - 1; n     for (long long j = 1; j <= top; j++) { n       data x = (*Ers[j]); n       long long lt = max(x.l, q[i].l), rt = min(x.r, q[i].r); n       if (lt > rt) continue; n       if (last + 1 != lt) vec[i].push_back(make_pair(1, lt - last - 1)); n       vec[i].push_back(make_pair(x.id + 1, rt - lt + 1)); n       last = rt; n     } n     if (last != q[i].r) vec[i].push_back(make_pair(1, q[i].r - last)); n     for (long long j = 1; j <= top; j++) S.erase(Ers[j]); n     if (l.l < q[i].l && l.r >= q[i].l) S.insert(mdata(l.l, q[i].l - 1, l.id)); n     if (r.r > q[i].r && r.l <= q[i].r) S.insert(mdata(q[i].r + 1, r.r, r.id)); n     S.insert(q[i]); n   } n   long long l = 0, r = 1000000000; n   long long ans = 0; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     kans = 0; n     check(mid); n     if (kans < K) n       ans = pans + (mid - 1) * (K - kans), r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int indexp = INT_MAX, indexr = INT_MAX; n     int arr[n], brr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       if (arr[i] == -1 and indexp == INT_MAX) indexp = i; n       if (arr[i] == 1 and indexr == INT_MAX) indexr = i; n     } n     for (int i = 0; i < n; i++) { n       cin >> brr[i]; n     } n     int flag = 0; n     for (int i = n - 1; i >= 0; i--) { n       if (brr[i] == arr[i]) n         continue; n       else { n         if (brr[i] > arr[i]) { n           if (i > indexr) n             continue; n           else { n             flag = 1; n             break; n           } n         } else { n           if (i > indexp) n             continue; n           else { n             flag = 1; n             break; n           } n         } n       } n     } n     if (flag == 1) n       cout <<  NO  n            <<   n ; n     else n       cout <<  YES  n            <<   n ; n   } n   return (0); n } n


#include <bits/stdc++.h> n long long num_div(long long x, long long limit) { n   long long ans(0); n   for (long long i = 1; i <= sqrt(x); ++i) { n     if (!(x % i)) { n       if (i > limit && i * i == x) n         ans++; n       else { n         if (i > limit) ans++; n         if (x / i > limit) ans++; n       } n     } n   } n   return ans; n } n int main() { n   long long n, m; n   scanf( %lld %lld , &n, &m); n   if (n == m) n     printf( infinity ); n   else n     printf( %lld , n < m ? 0 : num_div(n - m, m)); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100100; n int N; n vector<pair<int, int> > ans; n vector<int> E[maxn]; n void go(int i, int t) { ans.push_back({i, t}); } n void dfs(int i, int p, int t) { n   go(i, t); n   int d = (t < E[i].size()); n   if (!d) t -= E[i].size(), go(i, t); n   for (auto &j : E[i]) n     if (j != p) { n       t++; n       dfs(j, i, t); n       go(i, t); n       if (d && t == E[i].size()) t -= E[i].size(), go(i, t); n     } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> N; n   int u, v; n   for (int i = 1; i < N; i++) { n     cin >> u >> v; n     E[u].push_back(v); n     E[v].push_back(u); n   } n   go(1, 0); n   int t = 0; n   for (auto &i : E[1]) t++, dfs(i, 1, t), go(1, t); n   cout << ans.size() <<   n ; n   for (auto &i : ans) cout << i.first <<     << i.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200000 + 5], b[200000 + 5], vis[200000 + 5]; n int main() { n   long long i, j, k; n   long long n, m; n   long long o = 0, minx = 10005, s = 0; n   scanf( %I64d , &n); n   for (i = 0; i < n; i++) { n     scanf( %I64d , &m); n     if (m % 2 == 0) n       s += m; n     else { n       minx = min(minx, m); n       s += m; n       o++; n     } n   } n   if (minx == 10005) { n     cout << 0 << endl; n     return 0; n   } n   if (o % 2) n     cout << s << endl; n   else n     cout << s - minx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, k, i; n     cin >> n >> k; n     long long a[n], s = INT_MAX, l = INT_MIN; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n       s = min(s, a[i]); n       l = max(l, a[i]); n     } n     if (s + k >= l - k) n       cout << s + k << endl; n     else n       cout <<  -1  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   list<unsigned long long int> blind; n   list<unsigned long long int>::iterator bite, tempite; n   unsigned long long int total, l, maxarea = 0, sum, i; n   cin >> total >> l; n   for (i = 0; i < total; ++i) { n     cin >> sum; n     if (sum >= l) blind.push_back(sum); n   } n   sum = 1; n   while (sum != 0) { n     sum = 0; n     for (bite = blind.begin(); bite != blind.end();) { n       if (*bite < l) { n         tempite = bite; n         ++bite; n         blind.erase(tempite); n       } else { n         sum += *bite / l; n         ++bite; n       } n     } n     if (maxarea < l * sum) maxarea = l * sum; n     ++l; n   } n   cout << maxarea; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100006]; n vector<int> located[100006]; n int parent[100006], level[100006], visited[100006]; n int ancestor[100006][18]; n vector<int> st[100006][18]; n void dfs(int u) { n   visited[u] = 1; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (visited[v] == 0) { n       parent[v] = u; n       level[v] = level[u] + 1; n       dfs(v); n     } n   } n } n void merge(vector<int>& a, vector<int>& b) { n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   vector<int> tmp; n   int cnt = 0; n   int l1 = a.size(), l2 = b.size(); n   int i = 0, j = 0; n   while (i < l1 && j < l2 && cnt < 10) { n     if (a[i] < b[j]) n       tmp.push_back(a[i++]); n     else n       tmp.push_back(b[j++]); n     cnt++; n   } n   while (i < l1 && cnt < 10) { n     tmp.push_back(a[i++]); n     cnt++; n   } n   while (j < l2 && cnt < 10) { n     tmp.push_back(b[j++]); n     cnt++; n   } n   a.clear(); n   for (i = 0; i < cnt; i++) a.push_back(tmp[i]); n } n int main() { n   int tc = 1; n   for (int t = 1; t <= tc; t++) { n     int n, m, q; n     scanf( %d , &(n)); n     scanf( %d , &(m)); n     scanf( %d , &(q)); n     for (int j = 1; j < 18; j++) { n       for (int i = 1; i <= n; i++) { n         ancestor[i][j] = -1; n       } n     } n     for (int i = 1; i < n; i++) { n       int u, v; n       scanf( %d , &(u)); n       scanf( %d , &(v)); n       g[u].push_back(v); n       g[v].push_back(u); n     } n     for (int i = 1; i <= m; i++) { n       int tmp; n       scanf( %d , &(tmp)); n       located[tmp].push_back(i); n     } n     parent[1] = -1; n     level[1] = 1; n     dfs(1); n     for (int i = 1; i <= n; i++) ancestor[i][0] = parent[i]; n     for (int j = 1; j < 18; j++) { n       for (int i = 1; i <= n; i++) { n         if (ancestor[i][j - 1] != -1) n           ancestor[i][j] = ancestor[ancestor[i][j - 1]][j - 1]; n       } n     } n     for (int i = 1; i <= n; i++) { n       st[i][0] = located[i]; n       vector<int> vi; n       merge(st[i][0], vi); n     } n     for (int j = 1; j < 18; j++) { n       for (int i = 1; i <= n; i++) { n         st[i][j] = st[i][j - 1]; n         if (ancestor[i][j - 1] == -1) continue; n         merge(st[i][j], st[ancestor[i][j - 1]][j - 1]); n       } n     } n     while (q--) { n       int u, v, a; n       scanf( %d , &(u)); n       scanf( %d , &(v)); n       scanf( %d , &(a)); n       if (level[u] < level[v]) { n         int tmp = u; n         u = v; n         v = tmp; n       } n       vector<int> tmp; n       for (int j = 17; j >= 0; j--) { n         if (ancestor[u][j] != -1 && level[ancestor[u][j]] >= level[v]) { n           merge(tmp, st[u][j]); n           u = ancestor[u][j]; n         } n       } n       if (u != v) { n         for (int j = 17; j >= 0; j--) { n           if (ancestor[u][j] != -1 && ancestor[u][j] != ancestor[v][j]) { n             merge(tmp, st[u][j]); n             merge(tmp, st[v][j]); n             u = ancestor[u][j]; n             v = ancestor[v][j]; n           } n         } n         merge(tmp, st[u][0]); n         merge(tmp, st[v][0]); n         u = parent[u]; n         v = parent[v]; n       } n       merge(tmp, st[u][0]); n       int cnt = 0; n       int len = tmp.size(); n       cout << min(a, len); n       for (int it = 0; it < tmp.size() && cnt < a; it++, cnt++) { n         printf(   ); n         printf( %d , (tmp[it])); n       } n       printf(  n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double i, j, h, l, num; n   cin >> h >> l; n   num = (l * l - h * h) / (2 * h); n   cout << (fixed) << setprecision(12) << num; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n long long b, q, l, m, a[N]; n map<long long, int> vis; n int main() { n   scanf( %lld%lld%lld%lld , &b, &q, &l, &m); n   while (m--) { n     long long x; n     scanf( %lld , &x); n     vis[x] = 1; n   } n   if (max(-b, b) > l) { n     printf( 0 n ); n   } else if (b == 0) { n     if (vis.count(b) == 0) n       printf( inf n ); n     else n       printf( 0 n ); n   } else { n     if (q == 0) { n       if (vis.count(0) == 0) { n         printf( inf n ); n       } else { n         printf( %d n , !vis.count(b)); n       } n     } else if (q == 1) { n       if (vis.count(b) == 0) n         printf( inf n ); n       else n         printf( 0 n ); n     } else if (q == -1) { n       if (vis.count(b) == 0 || vis.count(-b) == 0) n         printf( inf n ); n       else n         printf( 0 n ); n     } else { n       int ans = 0; n       while (max(-b, b) <= l) { n         if (vis.count(b) == 0) ans++; n         b *= q; n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[100001]; n int res[100001]; n vector<vector<pair<int, int> > > g(1e5 + 2); n int check(int v) { n   if (dp[v]) { n     return dp[v]; n   } n   dp[v] = 1; n   for (int i = 0; i < g[v].size(); i++) { n     int nxt = g[v][i].first; n     if (check(nxt) + 1 > dp[v]) { n       dp[v] = dp[nxt] + 1; n       res[v] = max(res[nxt], g[v][i].second); n     } n   } n   return dp[v]; n } n int main() { n   int n, m, v, v1; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> v >> v1; n     g[--v].push_back({--v1, i + 1}); n   } n   for (int i = 0; i < n; i++) { n     if (check(i) == n) { n       cout << res[i]; n       return 0; n     } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 10; n int a[N]; n int vis[N]; n int sum[4 * N]; n void up(int id, int l, int r, int pos, int val) { n   if (l == r) { n     sum[id] = val; n     return; n   } n   int mid = l + r >> 1; n   if (pos <= mid) n     up(id << 1, l, mid, pos, val); n   else n     up(id << 1 | 1, mid + 1, r, pos, val); n   sum[id] = max(sum[id << 1], sum[id << 1 | 1]); n } n int qu(int id, int l, int r, int ql, int qr) { n   if (ql <= l && r <= qr) return sum[id]; n   int mx = 0; n   int mid = l + r >> 1; n   if (ql <= mid) mx = max(mx, qu(id << 1, l, mid, ql, qr)); n   if (qr > mid) mx = max(mx, qu(id << 1 | 1, mid + 1, r, ql, qr)); n   return mx; n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= (n); ++i) cin >> a[i]; n   int q; n   cin >> q; n   int h; n   int le = 0; n   int pre = 0; n   for (int i = 1; i <= q; ++i) { n     int ty; n     cin >> ty; n     if (ty == 1) { n       int pos, val; n       cin >> pos >> val; n       vis[pos] = i; n       a[pos] = val; n     } else { n       cin >> h; n       up(1, 1, q, i, h); n     } n   } n   for (int i = 1; i <= (n); ++i) { n     int le = qu(1, 1, q, vis[i], q); n     if (le > a[i]) n       printf( %d  , le); n     else n       printf( %d  , a[i]); n   } n } n


#include <bits/stdc++.h> n int main() { n   long long count, number; n   long long Totalnumber = 200001; n   std::vector<long long> Calresult(Totalnumber); n   Calresult[2] = 1; n   for (long long i = 3; i <= Totalnumber; i++) { n     Calresult[i] = Calresult[i - 1] % 1000000007 * i % 1000000007; n   } n   std::cin >> count; n   while (count--) { n     std::cin >> number; n     std::cout << Calresult[number * 2] << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2500 + 5; n int n, a[maxn][maxn]; n int tot; n struct Data { n   int x, y, z; n   bool operator<(const Data &a) const { return z < a.z; } n } b[maxn * maxn]; n bitset<2505> bt[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) n       scanf( %d , &a[i][j]), b[++tot] = (Data){i, j, a[i][j]}; n   for (int i = 1; i <= n; ++i) n     if (a[i][i]) puts( NOT MAGIC ), exit(0); n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) n       if (a[i][j] != a[j][i]) puts( NOT MAGIC ), exit(0); n   sort(b + 1, b + 1 + tot); n   for (int i = 1, p = 1; i <= tot; i = p) { n     while (b[p].z == b[i].z && p <= tot) { n       int x = b[p].x, y = b[p].y; n       if ((bt[x] & bt[y]).count()) puts( NOT MAGIC ), exit(0); n       p++; n     } n     for (int j = i; j < p; ++j) bt[b[j].x][b[j].y] = 1; n   } n   puts( MAGIC ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isValid(string& x, string& y) { n   int found = y.find(x); n   if (found == string::npos) return false; n   for (int i = 0; i < found; ++i) { n     if (y[i] ==  0 ) return false; n   } n   for (int i = found + x.length(); i < y.length(); ++i) { n     if (y[i] ==  0 ) return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   unsigned long long x, y; n   cin >> x >> y; n   bitset<64> b1(x); n   bitset<64> b2(y); n   string xString = b1.to_string(); n   string yString = b2.to_string(); n   int index = 0; n   while (xString[index] ==  0 ) { n     index++; n   } n   xString = xString.substr(index); n   index = 0; n   while (yString[index] ==  0 ) { n     index++; n   } n   yString = yString.substr(index); n   if (isValid(xString, yString)) { n     int temp = yString.find(xString); n     if (temp != 0 && xString.back() ==  0  && n         temp + xString.length() == yString.length()) { n     } else { n       cout <<  YES n ; n       return 0; n     } n   } n   index = -1; n   for (int i = 0; i < xString.length(); ++i) n     if (xString[i] ==  1 ) index = i; n   string xNoTrailing = xString.substr(0, index + 1); n   if (isValid(xNoTrailing, yString)) { n     cout <<  YES n ; n     return 0; n   } n   reverse(xNoTrailing.begin(), xNoTrailing.end()); n   if (isValid(xNoTrailing, yString)) { n     cout <<  YES n ; n     return 0; n   } n   xString.push_back( 1 ); n   reverse(xString.begin(), xString.end()); n   if (isValid(xString, yString)) { n     cout <<  YES n ; n     return 0; n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int y[3], m[3], p[3]; n int result; n bool solve(int); n bool if_win(int, int, int); n int main() { n   double lb = 0, ub = 0xffffff; n   result = 0xfffffff; n   scanf( %d%d%d , &y[0], &y[1], &y[2]); n   scanf( %d%d%d , &m[0], &m[1], &m[2]); n   scanf( %d%d%d , &p[0], &p[1], &p[2]); n   if (if_win(0, 0, 0)) { n     printf( %d n , 0); n     return 0; n   } n   for (int i = 0; i < 100; i++) { n     int mid = (int)((lb + ub) / 2 + 0.5); n     if (solve(mid)) n       ub = mid; n     else n       lb = mid; n     if (lb == ub || ub - lb == 1) break; n   } n   printf( %d n , result); n   return 0; n } n bool solve(int x) { n   int i; n   int j; n   int z; n   for (i = x / p[0]; i >= 0; i--) { n     for (j = (x - i * p[0]) / p[1]; j >= 0; j--) { n       if (y[1] + j < m[2]) break; n       for (z = (x - i * p[0] - j * p[1]) / p[2]; z >= 0; z--) { n         int prise = p[0] * i + p[1] * j + p[2] * z; n         if (if_win(i, j, z)) { n           result = min(result, prise); n           return true; n         } n       } n     } n   } n   return false; n } n bool if_win(int i, int j, int z) { n   if (y[1] + j <= m[2]) return false; n   int time = m[0] / (y[1] + j - m[2]); n   if (m[0] % (y[1] + j - m[2]) != 0) time++; n   if (y[0] + i - time * (m[1] - y[2] - z) > 0) n     return true; n   else n     return false; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s, l; n const int mo = 1e9 + 7; n const int inv3 = 333333336; n int a[202020]; n int b[202020]; n int c[202020]; n int sum[202020]; n int sum2[202020]; n long long gcd_ex(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return 1ll * a; n   } n   long long d = gcd_ex(b, a % b, y, x); n   y = y - a / b * x; n   return d; n } n long long solve(long long a, long long b, long long m) { n   long long x, y; n   long long d = gcd_ex(a, m, x, y); n   if (b % d == 0) { n     x %= m; n     while (x < 0) x += m; n     x %= m; n     return x * (b / d) % (m / d); n   } else { n     return m; n   } n } n bool cmp(int x, int y) { return a[x] < a[y]; } n int main() { n   cin >> n >> m; n   for (register int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     sum[i] = (sum[i - 1] + a[i]) % mo; n     sum2[i] = (sum2[i - 1] + (1ll * (a[i]) * (a[i]) % mo)) % mo; n     b[i] = a[i]; n     c[i] = i; n   } n   sort(b + 1, b + n + 1); n   sort(c + 1, c + n + 1, cmp); n   int r, d, x; n   long long aa, ab, ba, bb, mm, ssr, sp, sr; n   for (register int i = 1; i <= m; ++i) { n     scanf( %d %d %d , &l, &r, &d); n     s = r - l + 1; n     aa = 1ll * s; n     ab = (0ll + mo + sum[r] - sum[l - 1]) % mo; n     ba = (1ll * ((1ll * s * (s - 1) / 2) % mo) * d) % mo; n     bb = (ab + mo - ba) % mo; n     mm = 1ll * mo; n     x = solve(aa, bb, mm); n     if (b[lower_bound(b + 1, b + n + 1, x) - b] != x) { n       printf( No n ); n       continue; n     } n     x = c[lower_bound(b + 1, b + n + 1, x) - b]; n     if (x < l && r < x) { n       printf( No n ); n       continue; n     } n     x = a[x]; n     ssr = (0ll + mo + sum2[r] - sum2[l - 1]) % mo; n     sr = ((2ll * ba) % mo * x) % mo; n     sp = (1ll * ((1ll * (1ll * ba * (2ll * s - 1) % mo) * inv3) % mo) * d) % mo; n     sp = (0ll + 1ll * s * (1ll * (x) * (x) % mo) % mo + sp + sr) % mo; n     if (ssr != sp) { n       printf( No n ); n       continue; n     } n     printf( Yes n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n const int maxn = 10000; n bool is_prime(int x) { n   for (int i = 2; i <= sqrt(x); i++) { n     if (x % i == 0) return 0; n   } n   if (x < 2) return 0; n   return 1; n } n int main() { n   int cc, n; n   cin >> cc >> n; n   vector<int> a(n); n   for (int i = 0; i < cc; i++) { n     int x; n     cin >> x; n     x--; n     a[x]++; n   } n   vector<std::vector<int> > dp(3, std::vector<int>(3, 0)); n   for (int i = 0; i < n; i++) { n     int c = a[i]; n     vector<std::vector<int> > new_dp(3, std::vector<int>(3, 0)); n     for (int x = 0; x < 3; x++) { n       for (int y = 0; y < 3; y++) { n         for (int z = 0; z < 3; z++) { n           if (x + y + z <= c) { n             new_dp[y][z] = n                 max(new_dp[y][z], dp[x][y] + z + (c - x - y - z) / 3); n           } n         } n       } n     } n     swap(dp, new_dp); n   } n   cout << dp[0][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (auto &i : a) cin >> i; n   if (n == 1) { n     cout << 0 <<   n ; n     return 0; n   } n   int ans = INT_MAX, base; n   for (int i = 1; i <= 1000; i++) { n     int first = i, curr = 0; n     for (int j = 0; j < n; j++) n       if (a[j] != first + j * k) curr++; n     if (curr < ans) base = i, ans = curr; n   } n   cout << ans <<   n ; n   for (int j = 0; j < n; j++) { n     int now = base + j * k; n     if (a[j] < now) n       cout <<  +   << j + 1 <<     << now - a[j] <<   n ; n     else if (a[j] > now) n       cout <<  -   << j + 1 <<     << a[j] - now <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     vector<int> a(4 * n); n     for (int i = 0; i < 4 * n; i++) cin >> a[i]; n     sort(a.begin(), a.end()); n     int area = a[0] * a[4 * n - 1]; n     int temp = 0; n     for (int i = 1; i <= n; i++) { n       if ((a[2 * i - 2] != a[2 * i - 1]) || n           (a[4 * n - 2 * i] != a[4 * n - 2 * i + 1]) || n           (a[2 * i - 1 - 1] * a[4 * n - 2 * i + 1] != area)) { n         temp = 1; n         break; n       } n     } n     if (temp == 0) { n       cout <<  YES  << endl; n     } else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using vi = vector<int>; n using ll = long long; n void solve() { n   string s; n   cin >> s; n   int m = ((int)(s).size()); n   vi a(m); n   a[0] = ((s[0] ==  - ) ? -1 : 1); n   for (__typeof(m) i = (1) - ((1) > (m)); i != (m) - ((1) > (m)); n        i += 1 - 2 * ((1) > (m))) { n     if (s[i] ==  - ) { n       a[i] = a[i - 1] - 1; n     } else { n       a[i] = a[i - 1] + 1; n     } n   } n   ll res = m; n   int i = 0, t = -1; n   while (i < m) { n     if (a[i] != t) { n       i++; n     } else { n       t--; n       res += (i + 1); n     } n   } n   cout << res <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MAXN = 100010; n int N; n int A[MAXN]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> A[i]; n   } n   ll ans = 0; n   for (int i = 0; i < N; i++) { n     ans += ll(A[i]) * (N + 1 - A[i]); n   } n   for (int i = 0; i < N - 1; i++) { n     ans -= ll(min(A[i], A[i + 1])) * (N + 1 - max(A[i], A[i + 1])); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[101]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, cnt = 0; n     scanf( %d , &n); n     for (int i = 1; i <= n; i *= 2) { n       ans[++cnt] = i; n       n -= i; n     } n     if (n > 0) { n       ans[++cnt] = n; n       sort(ans + 1, ans + cnt + 1); n     } n     printf( %d n , cnt - 1); n     for (int i = 2; i <= cnt; i++) printf( %d  , ans[i] - ans[i - 1]); n     printf(  n ); n   } n   return 0; n } n