#include <bits/stdc++.h> n using namespace std; n const int maxn = 300100; n int head[maxn], nxt[maxn << 1], ver[maxn << 1], tot; n void addedge(int a, int b) { n   nxt[++tot] = head[a]; n   ver[tot] = b; n   head[a] = tot; n   nxt[++tot] = head[b]; n   ver[tot] = a; n   head[b] = tot; n } n int f[maxn], getlen; n void treedp(int x, int fat) { n   f[x] = 0; n   int mx = 0, sm = 0; n   for (int i = head[x]; i; i = nxt[i]) { n     int y = ver[i]; n     if (y == fat) continue; n     treedp(y, x); n     int F = f[y] + 1; n     f[x] = max(f[x], F); n     if (F >= mx) n       sm = mx, mx = F; n     else if (F > sm) n       sm = F; n   } n   getlen = max(getlen, mx + sm); n } n int getpath(int x) { n   getlen = 0; n   treedp(x, 0); n   return getlen; n } n int v[maxn], fa[maxn]; n int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n int main() { n   int n, m, q; n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     addedge(a, b); n     fa[find(a)] = find(b); n   } n   for (int i = 1; i <= n; i++) n     if (find(i) == i) v[i] = getpath(i); n   for (int i = 1; i <= q; i++) { n     int opt, a, b; n     scanf( %d , &opt); n     if (opt == 1) n       scanf( %d , &a), printf( %d n , v[find(a)]); n     else { n       scanf( %d%d , &a, &b); n       a = find(a); n       b = find(b); n       if (a == b) continue; n       v[b] = max(max(v[a], v[b]), ((v[a] + 1) >> 1) + ((v[b] + 1) >> 1) + 1); n       fa[a] = b; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[5002]; n int main() { n   int n, i; n   scanf( %d , &n); n   if (n <= 2) { n     printf( 1 n1 n ); n     return 0; n   } n   if (n == 3) { n     printf( 2 n1 3 n ); n     return 0; n   } n   int curr = (n & 1) ? n : n - 1; n   for (i = 0; i < n; i++) { n     if (curr < 0) curr = (n & 1) ? n - 1 : n; n     arr[i] = curr; n     curr -= 2; n   } n   printf( %d n , n); n   for (i = 0; i < n; i++) printf( %d  , arr[i]); n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, m = 1000000007, a, b; n int main() { n   cin >> n; n   a = 1, b = 1; n   for (i = 0; i < 3 * n; i++) a = (a * 3) % m; n   for (i = 0; i < n; i++) b = (b * 7) % m; n   cout << (a - b + m) % m; n   return 0; n } n


#include <bits/stdc++.h> n const int mxn = 1e5; n int cost[mxn][3], go[mxn][2], deg[mxn], path[mxn]; n int color[mxn]; n long long cc[3][3]; n int main() { n   int n, u, a, b, c = 0; n   scanf( %d , &n); n   for (int j = 0; j < 3; ++j) n     for (int i = 1; i <= n; ++i) scanf( %d , &cost[i][j]); n   for (int i = 1; i < n; ++i) { n     scanf( %d%d , &a, &b); n     ++deg[a], ++deg[b]; n     go[a][0] ? go[a][1] = b : go[a][0] = b; n     go[b][0] ? go[b][1] = a : go[b][0] = a; n   } n   for (int i = 1; i <= n; ++i) { n     if (deg[i] == 1) ++c, u = i; n     if (deg[i] > 2) return 0 * printf( -1 ); n   } n   if (c != 2) return 0 * printf( -1 ); n   for (int i = 1, v = u, p = 0, pa; p != v; ++i, p = pa) { n     path[i] = pa = v; n     if (go[v][0] != p && go[v][0]) n       v = go[v][0]; n     else if (go[v][1] != p && go[v][1]) n       v = go[v][1]; n   } n   for (int i = 0; i < 3; ++i) { n     for (int j = 0; j < 3; ++j) n       if (i != j) { n         a = i, b = j; n         cc[i][j] = cost[path[1]][i] + cost[path[2]][j]; n         for (int k = 3; k <= n; ++k) { n           c = (3 - a - b) % 3; n           cc[i][j] += cost[path[k]][c]; n           a = b, b = c; n         } n       } n   } n   long long min_cost = (long long)1e18; n   for (int i = 0; i < 3; ++i) n     for (int j = 0; j < 3; ++j) n       if (i != j) n         if (min_cost > cc[i][j]) a = i, b = j, min_cost = cc[i][j]; n   color[path[1]] = a, color[path[2]] = b; n   for (int k = 3; k <= n; ++k) { n     c = (3 - a - b) % 3; n     color[path[k]] = c; n     a = b, b = c; n   } n   printf( %lld n , min_cost); n   for (int i = 1; i <= n; ++i) printf( %d  , color[i] + 1); n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> ma; n map<string, int>::iterator p; n long i, n, m; n double ex, k; n int main() { n   cin >> n >> m >> k; n   string st; n   for (i = 1; i <= n; i++) { n     cin >> st >> ex; n     ma[st] = (int)(ex * k + 1e-9); n     if (ma[st] < 100) ma.erase(st); n   } n   for (i = 1; i <= m; i++) { n     cin >> st; n     ma[st] += 0; n   } n   cout << ma.size() << endl; n   for (p = ma.begin(); p != ma.end(); p++) n     cout << p->first <<     << p->second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cc = 1e9 + 7; n map<int, int> A; n map<int, int>::iterator itr, it; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     int nn = s.size(); n     vector<int> p; n     for (int j = 0; j < nn; j++) { n       int nnn = p.size(); n       if (p.size() == 0) { n         if (s[j] ==  ( ) n           p.push_back(1); n         else n           p.push_back(-1); n       } else { n         if (s[j] ==  ) ) { n           if (p[nnn - 1] == 1) n             p.pop_back(); n           else n             p.push_back(-1); n         } else { n           p.push_back(1); n         } n       } n     } n     int nnn = p.size(); n     if (nnn == 0) n       A[0]++; n     else if (p[0] == p[nnn - 1]) { n       A[(p[0] * nnn)]++; n     } n   } n   int sum = 0; n   for (itr = A.begin(); itr != A.end(); itr++) { n     if (itr->first == 0) { n       sum += (itr->second) / 2; n     } else { n       int yy = itr->first; n       it = A.find(0 - yy); n       if (it != A.end()) { n         sum += (min(itr->second, it->second)); n         it->second = 0; n       } n     } n   } n   cout << sum << endl; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long int ll; n #define endl   n  n  n  n  n  n bool powerOf2(ll x){ n     return x && (!(x&(x-1))); n } n  n void solve(){ n      n     ll u, v; n      n     cin>>u>>v; n      n      n     if(v<u || (powerOf2(u) && !powerOf2(v))){ n         cout<< No n ; n     }else{ n         if(u==v || (powerOf2(v))){ n             cout<< Yes n ; n         }else{ n             ll num = u; n              n            n              n             bool flag = false; n  n              n             while(num<v){ n                 ll maxN = -1; n  n                 for(int i = 0;i<32; i++){ n                      n                     ll msk = 1<<i; n                      n                     if(num + msk > v){ n                         break; n                     } n                      n                     if(num & msk){ n                         if(num + msk == v){ n                             flag = true; n                             break; n                         } n                         maxN = max(maxN, msk); n                     } n                 } n                  n                  n                 if(flag || maxN == -1) break; n                  n                 num+=maxN; n             } n              n              n             if(flag || num==v){ n                 cout<< Yes n ; n             }else{ n                 cout<< No n ; n             } n              n         } n       n     } n      n      n      n  n      n } n  n int main(){ n     int t; n     cin>>t; n     while(t--) n         solve(); n     return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 7; n int clr[MAX] = {0}, vis[MAX] = {0}, res = 0; n vector<int> adj[MAX]; n void dfs(int u, int p, int clr_p) { n   int f = 0; n   if (!vis[clr[u]]) { n     f = 1; n     vis[clr[u]] = 1; n     res++; n   } else if (p != -1 && clr_p != clr[u]) n     res++; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (p == v) continue; n     dfs(v, u, clr[u]); n   } n   if (f) vis[clr[u]] = 0; n } n int main(int argc, char const *argv[]) { n   int n, x; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     cin >> x; n     adj[i].push_back(x); n     adj[x].push_back(i); n   } n   for (int i = 1; i <= n; i++) cin >> clr[i]; n   dfs(1, -1, -1); n   std::cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<pair<int, int> > e; n bool check(vector<pair<int, int> > res) { n   if (res.size() > (n + m + 1) / 2) return false; n   vector<int> d(n, 0), d2(n, 0); n   for (pair<int, int> i : res) { n     d[--i.first]++; n     d[--i.second]++; n   } n   for (pair<int, int> i : e) { n     d2[--i.first]++; n     d2[--i.second]++; n   } n   for (int i = 0; i < n; i++) { n     if ((d2[i] + 1) / 2 > d[i]) return false; n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   e.resize(m); n   vector<set<int> > g(n + 1); n   for (int i = 0; i < m; i++) { n     cin >> e[i].first >> e[i].second; n     g[e[i].first].insert(e[i].second); n     g[e[i].second].insert(e[i].first); n   } n   for (int i = 1; i <= n; i++) { n     if ((int)(g[i].size()) & 1) { n       g[i].insert(0); n       g[0].insert(i); n     } n   } n   vector<int> c(n + 1, 0); n   vector<pair<int, int> > res; n   for (int s = 1; s <= n; s++) { n     if (!c[s]) { n       vector<int> st, ep; n       st.push_back(s); n       while (!st.empty()) { n         int f = st.back(); n         c[f] = 1; n         if (g[f].empty()) { n           st.pop_back(); n           ep.push_back(f); n         } else { n           int v = *g[f].begin(); n           g[f].erase(v); n           g[v].erase(f); n           st.push_back(v); n         } n       } n       ep.pop_back(); n       for (int i = 0; i < (int)ep.size(); i++) { n         int v0 = ep[(ep.size() + i - 1) % ep.size()], v1 = ep[i], n             v2 = ep[(i + 1) % ep.size()], v3 = ep[(i + 2) % ep.size()]; n         if (!v1 || !v2) continue; n         if (!v0 || !v3 || !(i & 1)) { n           res.push_back({v1, v2}); n         } n       } n     } n   } n   cout << res.size() <<   n ; n   for (pair<int, int> i : res) { n     cout << i.first <<     << i.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long input() { n   long long x; n   scanf( %lld , &x); n   return x; n } n const int inf = 1e9; n int clamp(int a, int l, int r) { n   if (a < l) a = l; n   if (a > r) a = r; n   return a; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> pos(1010101, -1); n   vector<int> L(1010101, -1), R(1010101, -1); n   set<tuple<int, int, int, int>> st; n   const int MostLeft = 1010101 - 2; n   const int MostRight = 1010101 - 3; n   pos[MostLeft] = -inf; n   pos[MostRight] = inf; n   const auto calcPos = [&](int i, int j) { n     if (i == MostLeft && j == MostRight) return 1; n     return clamp((pos[i] + pos[j]) / 2, 1, n); n   }; n   const auto calcDist = [&](int i, int j) { n     if (i == MostLeft && j == MostRight) n       return n - 1; n     else if (i == MostLeft) n       return pos[j] - 1; n     else if (j == MostRight) n       return n - pos[i]; n     return (pos[j] - pos[i]) / 2; n   }; n   for (int ii = 0; ii < m; ii++) { n     int t = input(); n     int id = input(); n     if (t == 1) { n       int lid, rid; n       if (st.empty()) { n         lid = MostLeft; n         rid = MostRight; n       } else { n         tie(ignore, ignore, lid, rid) = *st.begin(); n         st.erase(st.begin()); n       } n       pos[id] = calcPos(lid, rid); n       R[lid] = L[rid] = id; n       L[id] = lid; n       R[id] = rid; n       st.emplace(-calcDist(lid, id), pos[lid], lid, id); n       st.emplace(-calcDist(id, rid), pos[id], id, rid); n       printf( %d n , pos[id]); n     } else { n       int lid = L[id]; n       int rid = R[id]; n       R[lid] = rid; n       L[rid] = lid; n       st.erase(make_tuple(-calcDist(lid, id), pos[lid], lid, id)); n       st.erase(make_tuple(-calcDist(id, rid), pos[id], id, rid)); n       st.emplace(-calcDist(lid, rid), pos[lid], lid, rid); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5; n int m; n int num[MAXN + 10]; n int tree[4 * MAXN + 10]; n int prop[4 * MAXN + 10]; n void upd(int cn, int b, int e, int l, int r, int x, int p = 0) { n   if (l <= b && e <= r) { n     tree[cn] += x + p; n     prop[cn] += x + p; n     return; n   } n   if (r < b || l > e) { n     tree[cn] += p; n     prop[cn] += p; n     return; n   } n   p += prop[cn]; n   prop[cn] = 0; n   int m = (b + e) / 2; n   upd(2 * cn, b, m, l, r, x, p); n   upd(2 * cn + 1, m + 1, e, l, r, x, p); n   tree[cn] = min(tree[2 * cn], tree[2 * cn + 1]); n } n void upd(int i, int x) { upd(1, 1, m, i, m, x); } n int get(int cn, int b, int e, int l, int r, int p = 0) { n   if (l <= b && e <= r) return tree[cn] + p; n   if (r < b || l > e) return 1e9; n   int m = (b + e) / 2; n   int L = get(2 * cn, b, m, l, r, p + prop[cn]); n   int R = get(2 * cn + 1, m + 1, e, l, r, p + prop[cn]); n   return min(L, R); n } n bool oka(int j) { n   if (j == 0) return 0 < get(1, 1, m, m, m); n   return get(1, 1, m, j, m) < get(1, 1, m, m, m); n } n int solve() { n   int lo = 0, hi = m - 1; n   while (hi - lo > 1) { n     int m = (lo + hi) / 2; n     if (oka(m)) n       lo = m; n     else n       hi = m - 1; n   } n   if (oka(hi)) return num[hi + 1]; n   if (oka(lo)) return num[lo + 1]; n   return -1; n } n int main() { n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     int ps, tp; n     scanf( %d%d , &ps, &tp); n     if (tp == 0) n       upd(ps, -1); n     else { n       int x; n       scanf( %d , &x); n       num[ps] = x; n       upd(ps, +1); n     } n     printf( %d n , solve()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)2e9; n const int mod = (int)1e9 + 123; n const int N = (int)1e6 + 10; n const long long LLINF = (long long)3e18 + 10; n const long double pi = acos(-1); n template <typename T1, typename T2> n bool umin(T1 &x, T2 y) { n   if (x > y) return x = y, true; n   return false; n } n template <typename T1, typename T2> n bool umax(T1 &x, T2 y) { n   if (x < y) return x = y, true; n   return false; n } n template <typename T> n T getint() { n   char c = getchar(); n   T p = 1, x = 0; n   while (c ==     || c ==   n ) c = getchar(); n   if (c ==  - ) p = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x * p; n } n long long bp[N], h[N]; n int n, m, q; n map<tuple<int, int, int, int>, long long> s; n struct tree { n   vector<tree *> t; n   vector<long long> a; n   tree(int n, int m) { n     t.resize(n + 1); n     for (int i = 1; i <= n; ++i) { n       t[i] = new tree(m); n     } n   } n   tree(int m) { a.resize(m + 1); } n   void add(int x, int y, long long val) { n     if (x >= t.size()) return; n     for (; x <= n; x |= (x + 1)) t[x]->add(y, val); n   } n   void add(int y, long long val) { n     if (y >= a.size()) return; n     for (; y <= m; y |= (y + 1)) a[y] += val; n   } n   long long get(int x, int y) { n     long long ans = 0; n     for (; x >= 1; x = (x & (x + 1)) - 1) { n       ans += t[x]->get(y); n     } n     return ans; n   } n   long long get(int y) { n     long long ans = 0; n     for (; y >= 1; y = (y & (y + 1)) - 1) { n       ans += a[y]; n     } n     return ans; n   } n   void couttree() { n     if (t.size()) { n       for (int i = 1; i < t.size(); ++i) { n         t[i]->couttree(); n         cout <<   n ; n       } n       cout << endl; n     } else { n       for (int i = 1; i < a.size(); ++i) { n         cout << a[i] <<    ; n       } n     } n   } n }; n int main() { n   srand(time(NULL)); n   ios_base::sync_with_stdio(0); n   n = getint<int>(); n   m = getint<int>(); n   q = getint<int>(); n   tree *t = new tree(n, m); n   for (int i = 0; i < q; ++i) { n     bp[i] = (i ? bp[i - 1] * 23123 % mod : 1); n     h[i] = ((i ? h[i - 1] : 0) + (i + 1) * bp[i]) % mod; n     int ty = getint<int>(); n     int x1 = getint<int>(), y1 = getint<int>(); n     int x2 = getint<int>(), y2 = getint<int>(); n     if (ty == 1) { n       s[make_tuple(x1, y1, x2, y2)] = h[i]; n       t->add(x1, y1, h[i]); n       t->add(x1, y2 + 1, -h[i]); n       t->add(x2 + 1, y1, -h[i]); n       t->add(x2 + 1, y2 + 1, h[i]); n     } n     if (ty == 2) { n       long long val = s[make_tuple(x1, y1, x2, y2)]; n       t->add(x1, y1, -val); n       t->add(x1, y2 + 1, val); n       t->add(x2 + 1, y1, val); n       t->add(x2 + 1, y2 + 1, -val); n     } n     if (ty == 3) { n       if (t->get(x1, y1) == t->get(x2, y2)) n         printf( Yes n ); n       else n         printf( No n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n int n, m, k, a[N][5]; n int sparse[5][17][N]; n vector<int> last(5, 0); n void build(int u) { n   for (int i = 0; i < n; i++) sparse[u][0][i] = a[i][u]; n   for (int j = 1; j <= 17; j++) n     for (int i = 0; i + (1 << (j - 1)) < n; i++) n       sparse[u][j][i] = n           max(sparse[u][j - 1][i], sparse[u][j - 1][i + (1 << (j - 1))]); n } n int q(int u, int x, int y) { n   int v = log2(y - x + 1); n   return max(sparse[u][v][x], sparse[u][v][y + 1 - (1 << v)]); n } n bool check(int mid) { n   vector<int> v(5, 0); n   int sum; n   for (int i = 0; i < n - mid; i++) { n     sum = 0; n     for (int j = 0; j < m; j++) { n       v[j] = q(j, i, i + mid); n       sum += v[j]; n     } n     if (sum <= k) { n       last = v; n       return 1; n     } n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   for (int i = 0; i < m; i++) build(i); n   int l = 0, r = n, mid; n   while (l <= r) { n     mid = (l + r) / 2; n     if (check(mid)) n       l = mid + 1; n     else n       r = mid - 1; n   } n   for (int i = 0; i < m; i++) cout << last[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n struct pi { n   int x, y, t; n   double p; n } pp[1005]; n double dp[1005]; n int cmp(pi a, pi b) { return a.t < b.t; } n double get(int a, int b) { n   return sqrt(pow(pp[a].x - pp[b].x, 2.0) + pow(pp[a].y - pp[b].y, 2.0)); n } n int main() { n   int i, j, n; n   cin >> n; n   for (i = 1; i <= n; i++) { n     scanf( %d%d%d%lf , &pp[i].x, &pp[i].y, &pp[i].t, &pp[i].p); n   } n   sort(pp + 1, pp + 1 + n, cmp); n   dp[1] = pp[1].p; n   for (i = 2; i <= n; i++) { n     dp[i] = pp[i].p; n     for (j = 1; j < i; j++) { n       if (get(j, i) <= pp[i].t - pp[j].t) { n         dp[i] = max(dp[i], dp[j] + pp[i].p); n       } n     } n   } n   double s = 0; n   for (i = 1; i <= n; i++) s = max(s, dp[i]); n   printf( %.9lf n , s); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7, mod2 = 998244353; n unsigned long long base = 131; n const int w = 1e7 + 5; n long long m, n; n int main() { n   long long i, j, k, t, qqq; n   cin >> n; n   string a, b; n   cin >> a >> b; n   int flag = 0; n   for (i = a.size() - 1; i >= 0; i--) { n     int x = (b[i] -  a ); n     int y = (int)a[i] + x; n     if (flag) y++, flag = 0; n     if (y > (int) z ) y -= 26, flag++; n     a[i] = (char)y; n   } n   if (flag) a.insert(a.begin(),  b ); n   flag = 0; n   for (i = 0; i < a.size(); i++) { n     int x = a[i] -  a ; n     if (flag) x += 26, flag = 0; n     int y = x % 2; n     x /= 2; n     a[i] = x +  a ; n     if (y) flag = 1; n   } n   if (a.size() == n) n     cout << a << endl; n   else { n     for (i = 1; i < a.size(); i++) printf( %c , a[i]); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n const int maxm = 1e5 + 10; n const int INF = 0x3f3f3f3f; n const long long LINF = 3e17 + 1; n const int mod = 998244353; n int n, m, eid, sz; n inline int read() { n   register int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = getchar(); n   } n   return (f == 1) ? x : -x; n } n int a[maxn]; n int main() { n   ; n   ios::sync_with_stdio(0); n   cin.tie(0); n   ; n   int r; n   cin >> n >> r; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int cur = 1, res = 0; n   while (cur <= n) { n     int idx = 0; n     for (int i = 1; i <= n; i++) { n       if (a[i]) { n         if (cur >= i - r + 1 && cur <= i + r - 1) { n           idx = i; n         } n       } n     } n     if (idx == 0) { n       cout << -1 << endl; n       return 0; n     } n     res++; n     cur = idx + r; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1LL << 28; n const long long mod = 1LL; n int arr[3 * 100010]; n int main() { n   int n, k, i, ans, flag, j; n   while (scanf( %d %d , &n, &k) == 2) { n     for (i = 0; i < n; i++) { n       scanf( %d , &arr[i]); n     } n     sort(arr, arr + n); n     ans = arr[0]; n     while (true) { n       flag = true; n       for (i = 0; i < n; i++) { n         if (arr[i] % ans <= k) continue; n         flag = false; n         j = arr[i] / ans; n         j++; n         ans = arr[i] / j; n       } n       if (flag) break; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int dx[4] = {1, 0, -1, 0}; n const int dy[4] = {0, 1, 0, -1}; n const char dz[4] = { R ,  U ,  L ,  D }; n int n, m, bs, i, j, xa, ya, xb, yb, xx, yy, dd, d[1010], a[1010][1010], g[1010], n     c[1010], x[1010], y[1010], u[1010], f[1010], it; n long long cnt, cur; n char s[5]; n int main() { n   scanf( %d%d , &n, &bs); n   for (i = 1; i <= n; i++) { n     scanf( %d%d%d%d , &xa, &ya, &xb, &yb); n     x[i] = xb; n     y[i] = yb; n     if (xa == xb) { n       if (ya > yb) { n         d[i] = 3; n         for (j = yb; j <= ya; j++) a[xa][j] = i; n       } else { n         d[i] = 1; n         for (j = ya; j <= yb; j++) a[xa][j] = i; n       } n     } else { n       if (xa > xb) { n         d[i] = 2; n         for (j = xb; j <= xa; j++) a[j][ya] = i; n       } else { n         d[i] = 0; n         for (j = xa; j <= xb; j++) a[j][ya] = i; n       } n     } n   } n   for (i = 1; i <= n; i++) { n     for (xx = x[i], yy = y[i], dd = d[i]; n          xx >= 0 && yy >= 0 && xx <= bs && yy <= bs; n          xx += dx[dd], yy += dy[dd], c[i]++) { n       j = a[xx][yy]; n       if (j > 0) { n         dd = d[j]; n         if (c[i] > 0 && x[j] == xx && y[j] == yy) break; n       } n     } n     if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) g[i] = j; n   } n   scanf( %d , &m); n   while (m--) { n     scanf( %d%d , &xx, &yy); n     scanf( %s , s); n     for (dd = 0; dd < 4; dd++) n       if (dz[dd] == s[0]) break; n     scanf( %I64d , &cnt); n     for (; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs; n          xx += dx[dd], yy += dy[dd], cnt--) { n       if (cnt == 0) break; n       j = a[xx][yy]; n       if (j > 0) { n         dd = d[j]; n         if (x[j] == xx && y[j] == yy) break; n       } n     } n     if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) { n       if (cnt == 0) { n         printf( %d %d n , xx, yy); n         continue; n       } n     } else { n       printf( %d %d n , xx - dx[dd], yy - dy[dd]); n       continue; n     } n     for (cur = 0, ++it;; j = g[j]) { n       if (u[j] == it) { n         cnt -= f[j]; n         cnt %= cur - f[j]; n         cur = 0; n         ++it; n       } n       xx = x[j]; n       yy = y[j]; n       dd = d[j]; n       if (g[j] == 0 || cur + c[j] > cnt) { n         cnt -= cur; n         break; n       } n       u[j] = it; n       f[j] = cur; n       cur += c[j]; n     } n     for (; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs; n          xx += dx[dd], yy += dy[dd], cnt--) { n       if (cnt == 0) break; n       j = a[xx][yy]; n       if (j > 0) dd = d[j]; n     } n     if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) n       printf( %d %d n , xx, yy); n     else n       printf( %d %d n , xx - dx[dd], yy - dy[dd]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 7; n const long long INF = 1e18; n const long long mod = 1e9 + 7; n int n; n int a; n int b; n int mxa; n int mxb; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> a >> b; n   mxb = b; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     if (x && b < mxb) { n       if (a) { n         a--; n         b += b < mxb; n       } else if (b) { n         b--; n       } n     } else { n       if (b) { n         b--; n       } else if (a) { n         a--; n       } n     } n     if (!a && !b) { n       cout << i << endl; n       return 0; n     } n   } n   cout << n << endl; n } n


#include <bits/stdc++.h> n int prm[20] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; n long long n, ans; n void find(int k, long long cnt, long long sum) { n   int i; n   if (sum > n) return; n   if (sum == n && cnt < ans) ans = cnt; n   for (i = 1; i <= 63; i++) { n     if (ans / prm[k] < cnt) break; n     cnt = cnt * prm[k]; n     find(k + 1, cnt, sum * (i + 1)); n   } n } n int main() { n   while (scanf( %I64d , &n) != EOF) { n     ans = 2e65; n     find(0, 1, 1); n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[4000 + 10], d[4000 + 10], p[4000 + 10]; n int result[4000 + 10], check[1000000 + 10]; n int main() { n   int n, a, b; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     cin >> v[i] >> d[i] >> p[i]; n   } n   int lost = 0; n   for (int i = 0; i < n; i++) { n     if (p[i] >= 0) { n       result[lost++] = i + 1; n       long long q = v[i]; n       long long k = 0; n       for (int j = i + 1; j < n; j++) { n         if (p[j] < 0) continue; n         p[j] -= q; n         p[j] -= k; n         if (q) q--; n         if (p[j] < 0 && k < 1e7) k += d[j]; n       } n     } n   } n   printf( %d n , lost); n   for (int i = 0; i < lost; i++) { n     printf( %d  , result[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 7; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int mod = 1e9 + 7; n const double eps = 1e-8; n const double PI = acos(-1); n int n; n long long h[N], dp[N][2]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &h[i]), h[i]--; n   h[n + 1] = inf; n   dp[1][0] = h[1]; n   dp[1][1] = min(h[1], h[2]); n   long long ans = dp[1][0]; n   for (int i = 2; i <= n; i++) { n     dp[i][0] = (dp[i - 1][1] * min(h[i - 1], h[i]) + h[i]) % mod; n     dp[i][1] = ((dp[i - 1][1] * min(h[i - 1], min(h[i], h[i + 1])) % mod) + n                 min(h[i], h[i + 1])) % n                mod; n     ans = (ans + dp[i][0]) % mod; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 4 * atan(1); n int T, N; n long long X[100013], Y[100013], Z[100013]; n long long xmi, xma, ymi, yma, zmi, zma, smi, sma; n long long ansx, ansy, ansz; n inline bool is(long long x, int p) { return (((x % 2) + 2) % 2) == p; } n bool go(long long d) { n   xmi = -8.1e18, xma = 8.1e18, ymi = -8.1e18, yma = 8.1e18, zmi = -8.1e18, n   zma = 8.1e18, smi = -8.1e18, sma = 8.1e18; n   for (int i = 0; i < N; i++) { n     xmi = max(xmi, Y[i] + Z[i] - X[i] - d); n     xma = min(xma, Y[i] + Z[i] - X[i] + d); n     ymi = max(ymi, -Y[i] + Z[i] + X[i] - d); n     yma = min(yma, -Y[i] + Z[i] + X[i] + d); n     zmi = max(zmi, Y[i] - Z[i] + X[i] - d); n     zma = min(zma, Y[i] - Z[i] + X[i] + d); n     smi = max(smi, Y[i] + Z[i] + X[i] - d); n     sma = min(sma, Y[i] + Z[i] + X[i] + d); n   } n   if (smi > sma || xmi > xma || ymi > yma || zmi > zma) return false; n   for (int p = 0; p < 2; p++) { n     if (smi == sma && is(smi, !p) || xmi == xma && is(xmi, !p) || n         ymi == yma && is(ymi, !p) || zmi == zma && is(zmi, !p)) n       continue; n     long long qmi = 0; n     qmi += (is(xmi, p) ? xmi : xmi + 1); n     qmi += (is(ymi, p) ? ymi : ymi + 1); n     qmi += (is(zmi, p) ? zmi : zmi + 1); n     long long qma = 0; n     qma += (is(xma, p) ? xma : xma - 1); n     qma += (is(yma, p) ? yma : yma - 1); n     qma += (is(zma, p) ? zma : zma - 1); n     if (qmi <= qma && qmi <= sma && qma >= smi) { n       if (qmi >= smi) { n         ansx = (is(xmi, p) ? xmi : xmi + 1); n         ansy = (is(ymi, p) ? ymi : ymi + 1); n         ansz = (is(zmi, p) ? zmi : zmi + 1); n       } else if (qma <= sma) { n         ansx = (is(xma, p) ? xma : xma - 1); n         ansy = (is(yma, p) ? yma : yma - 1); n         ansz = (is(zma, p) ? zma : zma - 1); n       } else { n         xmi += is(xmi, !p); n         xma -= is(xma, !p); n         ymi += is(ymi, !p); n         yma -= is(yma, !p); n         zmi += is(zmi, !p); n         zma -= is(zma, !p); n         smi += is(smi, !p); n         sma -= is(sma, !p); n         if (xma + ymi + zmi < smi) { n           if (xma + yma + zmi < smi) { n             ansx = xma; n             ansy = yma; n             ansz = smi - ansx - ansy; n           } else { n             ansx = xma; n             ansz = zmi; n             ansy = smi - ansx - ansz; n           } n         } else { n           ansy = ymi; n           ansz = zmi; n           ansx = smi - ymi - zmi; n         } n       } n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> T; n   while (T--) { n     cin >> N; n     for (int i = 0; i < N; i++) { n       cin >> X[i] >> Y[i] >> Z[i]; n     } n     long long lo = 0; n     long long hi = 3.1e18; n     while (lo != hi) { n       long long mid = (lo + hi) / 2; n       if (go(mid)) { n         hi = mid; n       } else { n         lo = mid + 1; n       } n     } n     go(lo); n     cout << (ansy + ansz) / 2 <<     << (ansx + ansz) / 2 <<     n          << (ansx + ansy) / 2 <<   n ; n   } n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[4]; n int aux[100010]; n int next(int x) { n   if (x == 0) { n     if (arr[1] > 0) n       return 1; n     else n       return -1; n   } n   if (x == 1) { n     if (arr[0] > 0) n       return 0; n     else if (arr[2] > 0) n       return 2; n     else n       return -1; n   } n   if (x == 2) { n     if (arr[1] > 0) n       return 1; n     else if (arr[3] > 0) n       return 3; n     else n       return -1; n   } n   if (x == 3) { n     if (arr[2] > 0) n       return 2; n     else n       return -1; n   } n } n int arrini[4]; n int n; n void ok() { n   cout <<  YES n ; n   for (int i = 0; i < n; i++) cout << aux[i] <<    ; n   cout <<   n ; n } n bool tenta(int ini) { n   for (int i = 0; i < 4; i++) arr[i] = arrini[i]; n   arr[ini]--; n   aux[0] = ini; n   for (int i = 1; i < n; i++) { n     int nex = next(aux[i - 1]); n     if (nex == -1) return false; n     aux[i] = nex; n     arr[nex]--; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> arrini[0] >> arrini[1] >> arrini[2] >> arrini[3]; n   for (int i = 0; i < 4; i++) arr[i] = arrini[i]; n   n = arr[0] + arr[1] + arr[2] + arr[3]; n   if (arrini[0] > 0) { n     if (tenta(0)) { n       ok(); n       return 0; n     } n   } n   if (arrini[1] > 0) { n     if (tenta(1)) { n       ok(); n       return 0; n     } n   } n   if (arrini[2] > 0) { n     if (tenta(2)) { n       ok(); n       return 0; n     } n   } n   if (arrini[3] > 0) { n     if (tenta(3)) { n       ok(); n       return 0; n     } n   } n   cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 25; n const long long LL_INF = 1ll << 61; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> count(3, 0); n   for (int i = 0; i < n; i++) { n     int num; n     scanf( %d , &num); n     count[num]++; n   } n   if (count[1] == 0) { n     for (int i = 0; i < count[2]; i++) printf( 2  ); n     printf(  n ); n   } else { n     if (count[2] == 0) { n       for (int i = 0; i < count[1]; i++) printf( 1  ); n       printf(  n ); n     } else { n       printf( 2 1  ); n       count[2]--; n       count[1]--; n       for (int i = 0; i < count[2]; i++) printf( 2  ); n       for (int i = 0; i < count[1]; i++) printf( 1  ); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define int               long long n #define pb                push_back n #define ppb               pop_back n #define pf                push_front n #define ppf               pop_front n #define all(x)            (x).begin(),(x).end() n #define uniq(v)           (v).erase(unique(all(v)),(v).end()) n #define sz(x)             (int)((x).size()) n #define fr                first n #define sc                second n #define pii               pair<int,int> n #define rep(i,a,b)        for(int i=a;i<b;i++) n #define mem1(a)           memset(a,-1,sizeof(a)) n #define mem0(a)           memset(a,0,sizeof(a)) n #define ppc               __builtin_popcount n #define ppcll             __builtin_popcountll n  n template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2> &a) {in >> a.fr >> a.sc; return in;} n template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) {out << a.fr <<     << a.sc; return out;} n template<typename T, typename T1>T amax(T &a, T1 b) {if (b > a)a = b; return a;} n template<typename T, typename T1>T amin(T &a, T1 b) {if (b < a)a = b; return a;} n  n const long long INF = 1e18; n const int32_t M = 1e9 + 7; n const int32_t MM = 998244353; n  n const int N = 0; n  n  n void solve() { n  n  int n, m, k; n  cin >> n >> m >> k; n  int fl = 0; n  int sum = 1; n  sum = 1 * (m - 1) + m * (n - 1); n  n  n  if (sum == k) n   cout <<  YES n ; n  else n   cout <<  NO n ; n  n } n  n  n signed main() { n  ios_base::sync_with_stdio(false); n  cin.tie(0); cout.tie(0); n #ifndef ONLINE_JUDGE n  freopen( input.txt ,  r , stdin); n  freopen( output.txt ,  w , stdout); n #endif n  int t = 1; n  cin >> t; n  while (t--) solve(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n template <typename... As> n struct tpl : public std::tuple<As...> { n   using std::tuple<As...>::tuple; n   tpl() {} n   tpl(std::tuple<As...> const& b) { std::tuple<As...>::operator=(b); } n   template <typename T = tuple<As...>> n   typename tuple_element<0, T>::type const& x() const { n     return get<0>(*this); n   } n   template <typename T = tuple<As...>> n   typename tuple_element<0, T>::type& x() { n     return get<0>(*this); n   } n   template <typename T = tuple<As...>> n   typename tuple_element<1, T>::type const& y() const { n     return get<1>(*this); n   } n   template <typename T = tuple<As...>> n   typename tuple_element<1, T>::type& y() { n     return get<1>(*this); n   } n   template <typename T = tuple<As...>> n   typename tuple_element<2, T>::type const& z() const { n     return get<2>(*this); n   } n   template <typename T = tuple<As...>> n   typename tuple_element<2, T>::type& z() { n     return get<2>(*this); n   } n   template <typename T = tuple<As...>> n   typename tuple_element<3, T>::type const& w() const { n     return get<3>(*this); n   } n   template <typename T = tuple<As...>> n   typename tuple_element<3, T>::type& w() { n     return get<3>(*this); n   } n }; n using lli = long long int; n using llu = long long unsigned; n using pii = tpl<int, int>; n using piii = tpl<int, int, int>; n using piiii = tpl<int, int, int, int>; n using vi = vector<int>; n using vii = vector<pii>; n using viii = vector<piii>; n using vvi = vector<vi>; n using vvii = vector<vii>; n using vviii = vector<viii>; n template <class T> n using min_queue = priority_queue<T, vector<T>, greater<T>>; n template <class T> n using max_queue = priority_queue<T>; n template <size_t... I> n struct my_index_sequence { n   using type = my_index_sequence; n   static constexpr array<size_t, sizeof...(I)> value = {{I...}}; n }; n namespace my_index_sequence_detail { n template <typename I, typename J> n struct concat; n template <size_t... I, size_t... J> n struct concat<my_index_sequence<I...>, my_index_sequence<J...>> n     : my_index_sequence<I..., (sizeof...(I) + J)...> {}; n template <size_t N> n struct make_index_sequence n     : concat<typename make_index_sequence<N / 2>::type, n              typename make_index_sequence<N - N / 2>::type>::type {}; n template <> n struct make_index_sequence<0> : my_index_sequence<> {}; n template <> n struct make_index_sequence<1> : my_index_sequence<0> {}; n }  // namespace my_index_sequence_detail n template <class... A> n using my_index_sequence_for = n     typename my_index_sequence_detail::make_index_sequence<sizeof...(A)>::type; n template <class T, size_t... I> n void print_tuple(ostream& s, T const& a, my_index_sequence<I...>) { n   using swallow = int[]; n   (void)swallow{0, (void(s << (I == 0 ?    :  ,  ) << get<I>(a)), 0)...}; n } n template <class T> n ostream& print_collection(ostream& s, T const& a); n template <class... A> n ostream& operator<<(ostream& s, tpl<A...> const& a); n template <class... A> n ostream& operator<<(ostream& s, tuple<A...> const& a); n template <class A, class B> n ostream& operator<<(ostream& s, pair<A, B> const& a); n template <class T, size_t I> n ostream& operator<<(ostream& s, array<T, I> const& a) { n   return print_collection(s, a); n } n template <class T> n ostream& operator<<(ostream& s, vector<T> const& a) { n   return print_collection(s, a); n } n template <class T, class U> n ostream& operator<<(ostream& s, multimap<T, U> const& a) { n   return print_collection(s, a); n } n template <class T> n ostream& operator<<(ostream& s, multiset<T> const& a) { n   return print_collection(s, a); n } n template <class T, class U> n ostream& operator<<(ostream& s, map<T, U> const& a) { n   return print_collection(s, a); n } n template <class T> n ostream& operator<<(ostream& s, set<T> const& a) { n   return print_collection(s, a); n } n template <class T> n ostream& print_collection(ostream& s, T const& a) { n   s <<  [ ; n   for (auto it = begin(a); it != end(a); ++it) { n     s << *it; n     if (it != prev(end(a))) s <<    ; n   } n   return s <<  ] ; n } n template <class... A> n ostream& operator<<(ostream& s, tpl<A...> const& a) { n   s <<  ( ; n   print_tuple(s, a, my_index_sequence_for<A...>{}); n   return s <<  ) ; n } n template <class... A> n ostream& operator<<(ostream& s, tuple<A...> const& a) { n   s <<  ( ; n   print_tuple(s, a, my_index_sequence_for<A...>{}); n   return s <<  ) ; n } n template <class A, class B> n ostream& operator<<(ostream& s, pair<A, B> const& a) { n   return s <<  (  << get<0>(a) <<  ,   << get<1>(a) <<  ) ; n } n namespace std { n namespace { n template <class T> n inline void hash_combine(size_t& seed, T const& v) { n   seed ^= hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); n } n template <class Tuple, size_t Index = tuple_size<Tuple>::value - 1> n struct HashValueImpl { n   static void apply(size_t& seed, Tuple const& tuple) { n     HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); n     hash_combine(seed, get<Index>(tuple)); n   } n }; n template <class Tuple> n struct HashValueImpl<Tuple, 0> { n   static void apply(size_t& seed, Tuple const& tuple) { n     hash_combine(seed, get<0>(tuple)); n   } n }; n }  // namespace n template <typename... TT> n struct hash<tuple<TT...>> { n   size_t operator()(tuple<TT...> const& tt) const { n     size_t seed = 0; n     HashValueImpl<tuple<TT...>>::apply(seed, tt); n     return seed; n   } n }; n template <typename... TT> n struct hash<tpl<TT...>> { n   size_t operator()(tpl<TT...> const& tt) const { n     size_t seed = 0; n     HashValueImpl<tuple<TT...>>::apply(seed, tt); n     return seed; n   } n }; n }  // namespace std n struct wavelet_tree { n   int lo = 0, hi = 0; n   wavelet_tree *l = 0, *r = 0; n   vector<int> b; n   wavelet_tree() = default; n   ~wavelet_tree() { n     if (l) delete l; n     if (r) delete r; n   } n   wavelet_tree(vector<int> A) n       : wavelet_tree(begin(A), end(A), *min_element(begin(A), end(A)), n                      *max_element(begin(A), end(A))) {} n   wavelet_tree(vector<lli> A) n       : wavelet_tree(begin(A), end(A), *min_element(begin(A), end(A)), n                      *max_element(begin(A), end(A))) {} n   template <typename IT> n   wavelet_tree(IT from, IT to, int lo_, int hi_) { n     lo = lo_; n     hi = hi_; n     int mi = lo + (hi - lo) / 2; n     auto f = [&](int x) { return x <= mi; }; n     b.reserve(distance(from, to) + 1); n     b.push_back(0); n     for (auto it = from; it != to; ++it) b.push_back(b.back() + f(*it)); n     auto mit = stable_partition(from, to, f); n     if (lo == hi || from == to) return; n     l = new wavelet_tree(from, mit, lo, mi); n     r = new wavelet_tree(mit, to, mi + 1, hi); n   } n   void swap(int i, int si, int sj) { n     if (si == sj) return; n     if (lo == hi) { n       return; n       assert(0); n     } else { n       int mi = lo + (hi - lo) / 2; n       if (si <= mi && sj <= mi) { n         l->swap(b[i], si, sj); n       } else if (si > mi && sj > mi) { n         r->swap(i - b[i], si, sj); n       } else if (si <= mi) { n         b[i + 1] -= 1; n       } else { n         b[i + 1] += 1; n       } n     } n   } n   int kth(int l, int r, int k) { n     assert(l <= r); n     assert(k < r - l + 1); n     if (lo == hi) return lo; n     int inleft = b[r + 1] - b[l]; n     if (k < inleft) n       return this->l->kth(b[l], b[r + 1] - 1, k); n     else n       return this->r->kth(l - b[l], r - b[r + 1], k - inleft); n   } n   int cnt(int l, int r, int y) { n     if (l > r) return 0; n     if (lo == hi) { n       if (lo == y) { n         return b[r + 1] - b[l]; n       } else { n         return 0; n       } n     } else { n       int mi = lo + (hi - lo) / 2; n       if (y <= mi) { n         return this->l->cnt(b[l], b[r + 1] - 1, y); n       } else { n         return this->r->cnt(l - b[l], r - b[r + 1], y); n       } n     } n   } n   int allR(int l, int r) { n     if (l > r) return 0; n     return r - l + 1; n   } n   int cntR(int l, int r, int y) { n     if (l > r) return 0; n     if (hi <= y) return r - l + 1; n     if (y < lo) return 0; n     int mi = lo + (hi - lo) / 2; n     if (y <= mi) { n       return this->l->cntR(b[l], b[r + 1] - 1, y); n     } else { n       return this->l->allR(b[l], b[r + 1] - 1) + n              this->r->cntR(l - b[l], r - b[r + 1], y); n     } n   } n }; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   map<int, map<int, int>> data; n   vi ps; n   for (lli i = 0; i < (lli)(n); ++i) { n     int p; n     cin >> p; n     ps.push_back(p - 1); n   } n   wavelet_tree wt(ps); n   auto q0 = [&](int x, int y) -> int { n     if (x < 0 || y < 0) return 0; n     return wt.cntR(0, x, y); n   }; n   auto query = [&](int x1, int y1, int x2, int y2) -> int { n     if (x2 < x1) return 0; n     if (y2 < y1) return 0; n     return wt.cntR(x1, x2, y2) - (y1 == 0 ? 0 : wt.cntR(x1, x2, y1 - 1)); n   }; n   set<tpl<int, int, int, int>> ss; n   for (lli i = 0; i < (lli)(3); ++i) n     for (lli j = 0; j < (lli)(3); ++j) n       for (lli i2 = 0; i2 < (lli)(3); ++i2) n         for (lli j2 = 0; j2 < (lli)(3); ++j2) n           if (make_tuple(i, j) != make_tuple(i2, j2)) { n             if ((i <= 1 && 1 <= i2) || (i2 <= 1 && 1 <= i)) { n               if ((j <= 1 && 1 <= j2)) { n                 if (!ss.count(make_tuple(i2, j2, i, j))) n                   ss.insert(make_tuple(i, j, i2, j2)); n               } n             } n           } n   for (lli q_ = 0; q_ < (lli)(q); ++q_) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     --x1; n     --y1; n     --x2; n     --y2; n     int T[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; n     for (lli i = 0; i < (lli)(3); ++i) { n       for (lli j = 0; j < (lli)(3); ++j) { n         int a, b, c, d; n         if (i == 0) { n           a = 0; n           b = x1 - 1; n         } n         if (i == 1) { n           a = x1; n           b = x2; n         } n         if (i == 2) { n           a = x2 + 1; n           b = n - 1; n         } n         if (j == 0) { n           c = 0; n           d = y1 - 1; n         } n         if (j == 1) { n           c = y1; n           d = y2; n         } n         if (j == 2) { n           c = y2 + 1; n           d = n - 1; n         } n         T[i][j] = query(a, c, b, d); n       } n     } n     lli inside = T[1][1]; n     lli ans = (inside * (inside - 1)) / 2; n     for (auto s : ss) { n       int i, j, i2, j2; n       tie(i, j, i2, j2) = s; n       ans += (lli)((lli)T[i][j] * (lli)T[i2][j2]); n     } n     cout << ans <<   n ; n   } n   cout << flush; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     int n, g[105], sum = 0, s = 0; n     cin >> n; n     for (register int i = 0; i < n; i++) n       cin >> g[i], g[i] == 0 ? sum++, g[i]++ : 0, s += g[i]; n     if (s == 0) sum++; n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int setb = 0; n   int t[1000][1000]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> t[i][j]; n     } n   } n   int q; n   cin >> q; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       setb += t[i][j] * t[j][i]; n     } n   } n   bool anss = setb % 2; n   while (q--) { n     char c; n     cin >> c; n     if (c ==  1 ) { n       int idx; n       cin >> idx; n       anss ^= 1; n     } else if (c ==  2 ) { n       int idx; n       cin >> idx; n       anss ^= 1; n     } else { n       cout << anss; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n stack<int> stk; n vector<pair<int, int> > edge[500010]; n vector<pair<int, int> > w[500010]; n int a[500010], b[500010], c[500010], fa[500010], rk[500010]; n bool ans[500010]; n int n, m, q; n int Find(int x) { n   while (x != fa[x]) x = fa[x]; n   return x; n } n void Union(int x, int y, bool flag) { n   x = Find(x); n   y = Find(y); n   if (rk[x] < rk[y]) swap(x, y); n   if (flag) stk.push(y); n   fa[y] = x; n   if (rk[x] == rk[y]) ++rk[x]; n } n void undo() { n   while (!stk.empty()) { n     fa[stk.top()] = stk.top(); n     stk.pop(); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) fa[i] = i; n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d , &a[i], &b[i], &c[i]); n     edge[c[i]].push_back(make_pair(a[i], b[i])); n   } n   scanf( %d , &q); n   for (int i = 1; i <= q; ++i) { n     int k; n     scanf( %d , &k); n     while (k--) { n       int x; n       scanf( %d , &x); n       w[c[x]].push_back(make_pair(i, x)); n     } n     ans[i] = true; n   } n   for (int i = 1; i < 500010; ++i) { n     int last = -1; n     for (auto &j : w[i]) n       if (ans[j.first]) { n         int o = j.first, k = j.second; n         if (o != last) undo(), last = o; n         int x = a[k], y = b[k]; n         if (Find(x) == Find(y)) n           ans[o] = false; n         else n           Union(x, y, true); n       } n     undo(); n     for (auto &j : edge[i]) n       if (Find(j.first) != Find(j.second)) Union(j.first, j.second, false); n   } n   for (int i = 1; i <= q; ++i) puts(ans[i] ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n char board[3][3]; n int cross, nought; n bool cross_win() { n   if (board[0][0] ==  X  && board[0][1] ==  X  && board[0][2] ==  X ) n     return true; n   if (board[1][0] ==  X  && board[1][1] ==  X  && board[1][2] ==  X ) n     return true; n   if (board[2][0] ==  X  && board[2][1] ==  X  && board[2][2] ==  X ) n     return true; n   if (board[0][0] ==  X  && board[1][0] ==  X  && board[2][0] ==  X ) n     return true; n   if (board[0][1] ==  X  && board[1][1] ==  X  && board[2][1] ==  X ) n     return true; n   if (board[0][2] ==  X  && board[1][2] ==  X  && board[2][2] ==  X ) n     return true; n   if (board[0][0] ==  X  && board[1][1] ==  X  && board[2][2] ==  X ) n     return true; n   if (board[0][2] ==  X  && board[1][1] ==  X  && board[2][0] ==  X ) n     return true; n   return false; n } n bool nought_win() { n   if (board[0][0] ==  0  && board[0][1] ==  0  && board[0][2] ==  0 ) n     return true; n   if (board[1][0] ==  0  && board[1][1] ==  0  && board[1][2] ==  0 ) n     return true; n   if (board[2][0] ==  0  && board[2][1] ==  0  && board[2][2] ==  0 ) n     return true; n   if (board[0][0] ==  0  && board[1][0] ==  0  && board[2][0] ==  0 ) n     return true; n   if (board[0][1] ==  0  && board[1][1] ==  0  && board[2][1] ==  0 ) n     return true; n   if (board[0][2] ==  0  && board[1][2] ==  0  && board[2][2] ==  0 ) n     return true; n   if (board[0][0] ==  0  && board[1][1] ==  0  && board[2][2] ==  0 ) n     return true; n   if (board[0][2] ==  0  && board[1][1] ==  0  && board[2][0] ==  0 ) n     return true; n   return false; n } n int main() { n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < 3; j++) { n       cin >> board[i][j]; n       if (board[i][j] ==  X ) cross++; n       if (board[i][j] ==  0 ) nought++; n     } n   if (cross - nought > 1 || nought > cross) { n     printf( illegal ); n     return 0; n   } n   if (cross_win()) { n     if (nought >= cross) n       printf( illegal ); n     else n       printf( the first player won ); n     return 0; n   } n   if (nought_win()) { n     if (cross > nought) n       printf( illegal ); n     else n       printf( the second player won ); n     return 0; n   } n   if (cross + nought == 9) { n     printf( draw ); n     return 0; n   } n   if (cross - nought == 1) { n     printf( second ); n     return 0; n   } n   if (cross == nought) { n     printf( first ); n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double a, d, l, e; n int n; n int main() { n   scanf( %lf %lf , &a, &d); n   scanf( %d , &n); n   l = n * d + .5; n   e = d; n   double pos = 0, fa = 4 * a; n   for (int i = 0; i < n; i++) { n     pos = fmod(pos + e, fa); n     if (pos <= a) n       printf( %.10lf %.10lf n , pos, 0.); n     else if (pos <= 2 * a) n       printf( %.10lf %.10lf n , a, pos - a); n     else if (pos <= 3 * a) n       printf( %.10lf %.10lf n , a - (pos - 2 * a), a); n     else n       printf( %.10lf %.10lf n , 0., a - (pos - 3 * a)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void FAST_IO(string filein =   , string fileout =   , string fileerr =   ) { n   if (fopen(filein.c_str(),  r )) { n     freopen(filein.c_str(),  r , stdin); n     freopen(fileout.c_str(),  w , stdout); n   } n   cin.tie(0), cout.tie(0)->sync_with_stdio(0); n } n void Hollwo_Pelw(); n signed main() { n   FAST_IO( hollwo_pelw.inp ,  hollwo_pelw.out ); n   int testcases = 1; n   for (int test = 1; test <= testcases; test++) { n     Hollwo_Pelw(); n   } n   return 0; n } n const int N = 14, inf = 2e9; n inline bool smin(int &a, int b) { return a > b ? a = b, 1 : 0; } n int n, m, adj[N][N]; n int path[N][N][1 << N], dp[1 << N]; n int tr[1 << N], ps[1 << N], pe[1 << N]; n void Hollwo_Pelw() { n   cin >> n >> m; n   for (int i = 0, u, v; i < m; i++) n     cin >> u >> v, --u, --v, adj[u][v] = adj[v][u] = 1; n   memset(path, -1, sizeof path); n   for (int mask = 0; mask < 1 << n; mask++) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (mask >> i & 1) continue; n         if (mask >> j & 1) continue; n         if (__builtin_popcount(mask) == 1 && i == j) continue; n         if ((i == j || adj[i][j]) && !mask) { n           path[i][j][mask] = i; n         } else { n           for (int k = 0; k < n; k++) { n             if ((mask >> k & 1) && path[i][k][mask ^ (1 << k)] >= 0 && n                 adj[k][j]) { n               path[i][j][mask] = k; n               break; n             } n           } n         } n       } n     } n   } n   for (int mask = 1; mask < (1 << n); mask++) { n     if (__builtin_popcount(mask) == 1) { n       dp[mask] = 0; n     } else { n       dp[mask] = inf; n       for (int sub = mask; sub; sub = (sub - 1) & mask) { n         int nmask = mask ^ sub; n         for (int i = 0; i < n; i++) { n           for (int j = 0; j < n; j++) { n             if ((nmask >> i & 1) && (nmask >> j & 1) && path[i][j][sub] >= 0) { n               if (smin(dp[mask], dp[nmask] + __builtin_popcount(sub) + 1)) n                 tr[mask] = sub, ps[mask] = i, pe[mask] = j; n             } n           } n         } n       } n     } n   } n   cout << dp[(1 << n) - 1] <<   n ; n   int cur = (1 << n) - 1; n   while (__builtin_popcount(cur) > 1) { n     int nxt = tr[cur], i = ps[cur], j = pe[cur]; n     cur ^= tr[cur]; n     do { n       int k = path[i][j][nxt]; n       cout << k + 1 <<     << j + 1 <<   n ; n       nxt ^= 1 << k, j = k; n     } while (i != j); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[1000005]; n char op[1000005]; n bool A[1000005][2], val[1000005]; n void dfs(int u) { n   if (op[u] ==  I ) return; n   dfs(adj[u][0]); n   if (op[u] ==  N ) { n     val[u] = !val[adj[u][0]]; n     return; n   } n   dfs(adj[u][1]); n   if (op[u] ==  A ) n     val[u] = val[adj[u][0]] and val[adj[u][1]]; n   else if (op[u] ==  O ) n     val[u] = val[adj[u][0]] or val[adj[u][1]]; n   else n     val[u] = val[adj[u][0]] xor val[adj[u][1]]; n } n void dfs2(int u, int p) { n   if (u == 1) n     A[1][1 - val[1]] = 1 - val[1]; n   else { n     if (op[p] ==  N ) n       A[u][1 - val[u]] = A[p][val[u]]; n     else { n       int v; n       if (adj[p][0] == u) n         v = adj[p][1]; n       else n         v = adj[p][0]; n       if (op[p] ==  A ) n         A[u][1 - val[u]] = A[p][val[v] and (1 - val[u])]; n       else if (op[p] ==  O ) n         A[u][1 - val[u]] = A[p][val[v] or (1 - val[u])]; n       else n         A[u][1 - val[u]] = A[p][val[v] xor (1 - val[u])]; n     } n   } n   for (auto x : adj[u]) dfs2(x, u); n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, i, u, v; n   string s; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> s; n     op[i] = s[0]; n     if (s ==  AND  or s ==  OR  or s ==  XOR ) { n       cin >> u >> v; n       adj[i].push_back(u); n       adj[i].push_back(v); n     } else if (s ==  NOT ) { n       cin >> u; n       adj[i].push_back(u); n     } else { n       cin >> u; n       val[i] = u; n     } n   } n   dfs(1); n   for (i = 1; i <= n; i++) A[i][val[i]] = val[1]; n   dfs2(1, 0); n   for (i = 1; i <= n; i++) n     if (op[i] ==  I ) cout << A[i][1 - val[i]]; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n int T,n,m,flag,k,a[110],b[110],ans[300]; n int main() n { n  scanf( %d ,&T); n  while (T--) { n   scanf( %d%d%d ,&k,&n,&m); n   for (int i=1;i<=n;i++) scanf( %d ,&a[i]); n   for (int i=1;i<=m;i++) scanf( %d ,&b[i]); n   flag=1; n   for (int i=1,j=1;i<=n||j<=m;) { n    if ((a[i]>k||i>n)&&(b[j]>k||j>m)) { n     flag=0; n     break; n    } n    while (a[i]<=k&&i<=n) { n     ans[i+j-1]=a[i]; n     if (a[i]==0) k++; n     i++; n    } n    while (b[j]<=k&&j<=m) { n     ans[i+j-1]=b[j]; n     if (b[j]==0) k++; n     j++; n    } n   } n   if (flag) { n    for (int i=1;i<=n+m;i++) printf( %d  ,ans[i]); n    printf(  n ); n   } else { n    printf( -1 n ); n   } n  } n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n bool X[1000001], vis[1000001]; n int main() { n   int n; n   while (scanf( %d , &n) == 1) { n     vector<int> V; n     int x; n     for (int i = 0; i < n; i++) { n       scanf( %d , &x); n       X[x] = true; n       V.push_back(x); n     } n     deque<int> Q; n     for (int i = 1; i < 1000001; i++) n       if (!X[i] && !X[1000001 - i]) Q.push_front(i), Q.push_back(1000001 - i); n     printf( %d n , n); n     for (int i = 0; i < V.size(); i++) { n       if (vis[V[i]]) continue; n       if (!X[1000001 - V[i]]) n         printf( %d n , 1000001 - V[i]); n       else { n         printf( %d %d n , Q.front(), Q.back()); n         vis[1000001 - V[i]] = true; n         Q.pop_front(); n         Q.pop_back(); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[100001], parent[100001], sz[100001], p[(100001 / 320 + 1)], n     mx[(100001 / 320 + 1)], h[(100001 / 320 + 1)][100001]; n bool isBlockDirty[(100001 / 320 + 1)]; n int Find(int i) { n   if (i == parent[i]) n     return i; n   else { n     parent[i] = Find(parent[i]); n     return parent[i]; n   } n } n void Move(int k, int s, int d) { n   int h1 = h[k][s], h2 = h[k][d]; n   h[k][s] = -1; n   if (h1 == -1) return; n   if (h2 == -1) { n     a[h1] += (d - s); n     h[k][d] = h1; n   } else { n     parent[h1] = h2; n     sz[h2] += sz[h1]; n   } n } n void Print(int n) { n   for (int i = 0; i < n; ++i) { n     int j = Find(i); n     int k = i / 320; n     printf( %d %d %d %d %d %d n , i, j, a[j], p[k], mx[k], a[j] + p[k]); n   } n   printf(  n n ); n } n void DestroyBlockDSU(int k) { n   if (isBlockDirty[k]) return; n   for (int l = k * 320, r = min(l + 320, 100001), i = l; i < r; ++i) { n     int j = Find(i); n     a[i] = a[j]; n     h[k][a[i]] = -1; n   } n   for (int l = k * 320, r = min(l + 320, 100001), i = l; i < r; ++i) { n     a[i] += p[k]; n     parent[i] = i; n     sz[i] = 1; n   } n   mx[k] = 0; n   p[k] = 0; n   isBlockDirty[k] = 1; n } n void BuildBlockDSU(int k) { n   mx[k] = 0; n   p[k] = 0; n   for (int l = k * 320, r = min(l + 320, 100001), i = l; i < r; ++i) { n     if (h[k][a[i]] == -1) n       h[k][a[i]] = i; n     else { n       int j = h[k][a[i]]; n       parent[i] = j; n       ++sz[j]; n     } n     mx[k] = max(mx[k], a[i]); n   } n   isBlockDirty[k] = 0; n } n void PreProcess(int n) { n   for (int i = 0; i < (100001 / 320 + 1); ++i) n     for (int j = 0; j < 100001; ++j) h[i][j] = -1; n   for (int k = 0; k * 320 < n; ++k) isBlockDirty[k] = 1; n   for (int i = 0; i < n; ++i) { n     parent[i] = i; n     sz[i] = 1; n   } n } n void UpdateInBlock(int k, int l, int r, int x) { n   DestroyBlockDSU(k); n   for (int i = l; i <= r; ++i) { n     if (a[i] > x) a[i] -= x; n   } n } n void UpdateBlock(int k, int x) { n   if (isBlockDirty[k]) BuildBlockDSU(k); n   if (x >= mx[k]) return; n   if (2 * x < mx[k]) { n     for (int l = 1 - p[k], r = x - p[k], i = l; i <= r; ++i) Move(k, i, i + x); n     p[k] -= x; n     mx[k] -= x; n   } else { n     for (int l = x + 1 - p[k], r = mx[k] - p[k], i = l; i <= r; ++i) n       Move(k, i, i - x); n     int _mx = x - p[k]; n     for (; h[k][_mx] == -1; --_mx) n       ; n     mx[k] = _mx + p[k]; n   } n } n void Update(int l, int r, int x) { n   int lk = l / 320, rk = r / 320; n   if (lk == rk) n     UpdateInBlock(lk, l, r, x); n   else { n     for (int k = lk; k <= rk; ++k) { n       int i = max(k * 320, l), j = min((k + 1) * 320 - 1, r); n       if (j - i + 1 < 320) n         UpdateInBlock(k, i, j, x); n       else n         UpdateBlock(k, x); n     } n   } n } n int QueryInBlock(int k, int l, int r, int x) { n   int ans = 0; n   for (int i = l; i <= r; ++i) { n     int j = Find(i); n     ans += (a[j] + p[k] == x); n   } n   return ans; n } n int QueryBlock(int k, int x) { n   if (x - p[k] >= 100001) return 0; n   if (isBlockDirty[k]) BuildBlockDSU(k); n   int i = h[k][x - p[k]]; n   if (i == -1) return 0; n   int ans = sz[i]; n   return ans; n } n int Query(int l, int r, int x) { n   int lk = l / 320, rk = r / 320; n   if (lk == rk) n     return QueryInBlock(lk, l, r, x); n   else { n     int ans = 0; n     for (int k = lk; k <= rk; ++k) { n       int i = max(k * 320, l), j = min((k + 1) * 320 - 1, r); n       if (j - i + 1 < 320) n         ans += QueryInBlock(k, i, j, x); n       else n         ans += QueryBlock(k, x); n     } n     return ans; n   } n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   PreProcess(n); n   while (q--) { n     int t, l, r, x; n     scanf( %d%d%d%d , &t, &l, &r, &x); n     --l, --r; n     if (t == 1) { n       Update(l, r, x); n     } else n       printf( %d n , Query(l, r, x)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, h, m, k; n vector<long long int> freight; n int workingTrains(long long int time) { n   if (time - k >= 0) { n     return n - (lower_bound(freight.begin(), freight.end(), time) - n                 upper_bound(freight.begin(), freight.end(), time - k)); n   } else { n     return upper_bound(freight.begin(), freight.end(), n                        (time - k + m / 2) % (m / 2)) - n            lower_bound(freight.begin(), freight.end(), time); n   } n } n void solve(int test_index) { n   cin >> n >> h >> m >> k; n   freight.resize(n); n   vector<long long int> raw(n); n   for (int i = 0; i < n; i++) { n     long long int hh, mm; n     cin >> hh >> mm; n     freight[i] = (hh * m + mm) % (m / 2); n     raw[i] = freight[i]; n   } n   sort(freight.begin(), freight.end()); n   long long int t = freight[0], works = workingTrains(freight[0]); n   for (int time : freight) { n     long long int w = workingTrains(time); n     if (works < w) { n       works = w; n       t = time; n     } n   } n   cout << n - works <<     << t <<   n ; n   for (int i = 0; i < n; i++) { n     if (t - k < 0) { n       if (raw[i] < t || raw[i] > (t - k + m / 2) % (m / 2)) { n         cout << i + 1 <<    ; n       } n     } else { n       if (raw[i] > t - k && raw[i] < t) { n         cout << i + 1 <<    ; n       } n     } n   } n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int num_tests = 1; n   for (int test_index = 1; test_index <= num_tests; test_index++) { n     solve(test_index); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void optimizeIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int main() { n   optimizeIO(); n   int n, t; n   cin >> n >> t; n   string s1, s2, s3; n   cin >> s1 >> s2; n   s3 = s1; n   int sa = 0; n   vector<int> v, v2; n   for (typeof(n) i = 0; i < n; i++) { n     if (s1[i] == s2[i]) { n       sa++; n       v.push_back(i); n     } else n       v2.push_back(i); n   } n   if (n - sa < 2 * (n - t - sa)) n     cout <<  -1 n ; n   else { n     if ((n - t) < sa) { n       int i1 = 0, i2 = 0; n       int dd = n - t; n       for (typeof(n) i = 0; i < n; i++) { n         if (v[i1] == i && dd) { n           s3[i] = s1[i]; n           dd--; n           i1++; n         } else { n           if (abs(s1[i] - s2[i]) > 1) { n             s3[i] = min(s1[i], s2[i]) + 1; n           } else { n             if (max(s1[i], s2[i]) ==  z ) n               s3[i] =  a ; n             else n               s3[i] =  z ; n           } n         } n       } n     } else { n       int i1 = 0; n       int dd = n - t, c = 0; n       int d1 = n - t - sa, d2 = n - t - sa; n       for (typeof(n) i = 0; i < n; i++) { n         if (i1 < int((v).size())) { n           if (v[i1] == i) { n             s3[i] = s1[i]; n             i1++; n             continue; n           } n         } n         if (d1) { n           s3[i] = s1[i]; n           d1--; n           continue; n         } n         if (d2) { n           s3[i] = s2[i]; n           d2--; n           continue; n         } n         if (d1 == 0 && d2 == 0) { n           if (abs(s1[i] - s2[i]) > 1) { n             s3[i] = min(s1[i], s2[i]) + 1; n           } else { n             if (max(s1[i], s2[i]) ==  z ) n               s3[i] =  a ; n             else n               s3[i] =  z ; n           } n         } n       } n     } n     cout << s3 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n const int BIT = 17; n int n, m; n int P[N]; n int pot2[33]; n set<int> S[18]; n set<int>::iterator it; n long long sol; n inline int find(int bit, int pos) { n   if (S[bit].empty()) return 0; n   it = S[bit].lower_bound(pos); n   if (it == S[bit].begin()) return 0; n   it--; n   return *it + 1; n } n inline int find2(int bit, int pos) { n   if (S[bit].empty()) return n - 1; n   it = S[bit].lower_bound(pos); n   if (it == S[bit].end()) return n - 1; n   return *it - 1; n } n inline void load() { n   pot2[0] = 1; n   for (int i = 1; i <= BIT; i++) pot2[i] = (pot2[i - 1] << 1); n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &P[i]); n     x = P[i]; n     for (int j = BIT; j >= 0; j--) { n       if (x & (1 << j)) { n         sol += (long long)pot2[j] * (i - find(j, i) + 1); n       } else n         S[j].insert(i); n     } n   } n } n inline void print() { n   printf( SADRZAJ NA MJESTU == %d n , 1); n   for (set<int>::iterator it = S[0].begin(); it != S[0].end(); it++) { n     printf( %d  , *it); n   } n   printf(  n ); n   system( pause ); n } n inline void solve() { n   for (; m; m--) { n     int pos, val; n     scanf( %d %d , &pos, &val); n     pos--; n     for (int j = BIT; j >= 0; j--) { n       if (val & (1 << j)) { n         if ((P[pos] & (1 << j)) == 0) { n           S[j].erase(pos); n           sol += (long long)pot2[j] * (pos - find(j, pos) + 1) * n                  (find2(j, pos) - pos + 1); n         } n       } else { n         if ((P[pos] & (1 << j))) { n           int gore = pos - find(j, pos) + 1; n           int dole = find2(j, pos) - pos; n           sol -= (long long)pot2[j] * dole * gore; n           sol -= (long long)pot2[j] * gore; n           S[j].insert(pos); n         } n       } n     } n     P[pos] = val; n     cout << sol << endl; n   } n } n int main(void) { n   load(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 65536 + 10; n int n, deg[Maxn], s[Maxn]; n vector<pair<int, int> > ans; n set<pair<int, int> > mys; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &deg[i], &s[i]); n     mys.insert(make_pair(deg[i], i)); n   } n   while (!mys.empty()) { n     pair<int, int> p = *mys.begin(); n     mys.erase(p); n     if (p.first > 0) { n       ans.push_back(make_pair(p.second, s[p.second])); n       mys.erase(make_pair(deg[s[p.second]], s[p.second])); n       deg[s[p.second]]--; n       s[s[p.second]] ^= p.second; n       mys.insert(make_pair(deg[s[p.second]], s[p.second])); n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) n     printf( %d %d  n , ans[i].first, ans[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const double eps = 1e-10; n int main() { n   int n; n   scanf( %d , &n); n   int feet, inches; n   inches = n / 3; n   if (n - inches * 3 > 1) inches++; n   feet = inches / 12; n   inches %= 12; n   cout << feet <<     << inches << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, mod = 1e9 + 7, M = 100; n int n, m, k; n int q[N][6], a[N][6], r[6], f[6], e[6]; n inline void inq(int id, int x) { n   while (f[id] <= e[id] && a[x][id] >= a[q[e[id]][id]][id]) e[id]--; n   q[++e[id]][id] = x; n } n inline void outq(int id, int x) { n   if (q[f[id]][id] <= x) f[id]++; n } n inline int jug() { n   int sum = 0; n   for (int i = 1; i <= m; i++) { n     sum += a[q[f[i]][i]][i]; n   } n   return sum > k; n } n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       a[i][j] = read(); n     } n   } n   for (int i = 1; i <= m; i++) f[i] = 0, e[i] = -1; n   int res = 0; n   int pre = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       inq(j, i); n     } n     while (pre <= i && jug()) { n       for (int j = 1; j <= m; j++) outq(j, pre); n       pre++; n     } n     if (res < i - pre + 1) { n       res = i - pre + 1; n       for (int j = 1; j <= m; j++) r[j] = a[q[f[j]][j]][j]; n     } n   } n   for (int i = 1; i <= m; i++) { n     cout << r[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1555; n const int dx[4] = {0, 0, 1, -1}; n const int dy[4] = {1, -1, 0, 0}; n int n, m; n template <typename T> n inline T mod(const T& lhs, const T& rhs) { n   T _lhs = lhs; n   while (_lhs < 0) { n     _lhs += rhs; n   } n   return _lhs % rhs; n } n char gra[MAXN][MAXN]; n bool used[MAXN][MAXN]; n pair<int, int> visited[MAXN][MAXN]; n queue<pair<int, int> > Q; n bool bfs(int x, int y) { n   pair<int, int> now(x, y); n   Q.push(now); n   visited[now.first][now.second] = now; n   used[x][y] = 1; n   while (!Q.empty()) { n     now = Q.front(); n     Q.pop(); n     for (int i = 0; i < 4; i++) { n       int tx = dx[i] + now.first, ty = dy[i] + now.second; n       int rx = mod(tx, n), ry = mod(ty, m); n       if (gra[rx][ry] ==  # ) { n         continue; n       } n       if (!used[rx][ry]) { n         used[rx][ry] = 1; n         Q.push(pair<int, int>(tx, ty)); n         visited[rx][ry] = pair<int, int>(tx, ty); n       } else { n         if (visited[rx][ry].first != tx || visited[rx][ry].second != ty) { n           return true; n         } n       } n     } n   } n   return false; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %s , gra[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (gra[i][j] ==  S ) { n         if (bfs(i, j)) { n           puts( Yes ); n         } else { n           puts( No ); n         } n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T MAX(T a, T b) { n   return a > b ? a : b; n } n template <class T> n T MIN(T a, T b) { n   return a < b ? a : b; n } n template <class T1> n void deb(T1 p) { n   cout <<  Debugging:   << p << endl; n } n template <class T1, class T2> n void deb(T1 p, T2 q) { n   cout <<  Debugging:   << p <<   t  << q << endl; n } n template <class T1, class T2, class T3> n void deb(T1 p, T2 q, T3 r) { n   cout <<  Debugging:   << p <<   t   << q <<   t   << r << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 p, T2 q, T3 r, T4 s) { n   cout <<  Debugging:   << p <<   t   << q <<   t   << r <<   t   << s << endl; n } n long long int POOW(int b, int p) { n   if (p == 0) return 1; n   return b * POOW(b, p - 1); n } n const int xx[] = {0, 0, 1, -1, -1, 1, -1, 1}; n const int yy[] = {1, -1, 0, 0, 1, 1, -1, -1}; n const int kx[] = {-2, -1, 1, 2, 2, 1, -1, -2}; n const int ky[] = {1, 2, 2, 1, -1, -2, -2, -1}; n int main() { n   char mat[105][105]; n   int i, j, m, sum, res = 0, ar[105], n, k; n   scanf( %d , &n); n   getchar(); n   for (i = 0; i < n; i++) gets(mat[i]); n   res = sum = 0; n   for (i = 0; i < n; i++) { n     sum = 0; n     for (j = 0; j < n; j++) { n       for (k = 0; k < n; k++) { n         if (mat[i][k] == mat[j][k]) n           ; n         else { n           break; n         } n       } n       if (k == n) sum++; n     } n     res = max(res, sum); n   } n   sum = 0; n   for (i = 0; i < n; i++) { n     for (j = k = 0; j < n; j++) { n       if (mat[i][j] ==  1 ) ++k; n     } n     if (k == n) sum++; n   } n   if (res == 0) res = max(res, sum); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( inline , 2) n using namespace std; n const long long N = 5005; n const long long inf = 1e18; n long long n, hd[N], nxt[N << 1], to[N << 1], w[N << 1], tot = 1; n long long sz[N << 1]; n long long aa[N << 1], eg[N << 1]; n void add(long long a, long long b, long long c) { n   nxt[++tot] = hd[a], to[tot] = b, w[tot] = c; n   hd[a] = tot; n } n long long dfs1(long long u, long long fe, long long d) { n   long long re = d; n   for (long long i = hd[u]; i; i = nxt[i]) n     if (i != fe) re += dfs1(to[i], i ^ 1, d + w[i]); n   return eg[fe] = re; n } n long long dfs2(long long u, long long fe) { n   long long re = 1; n   for (long long i = hd[u]; i; i = nxt[i]) n     if (i != fe) re += dfs2(to[i], i ^ 1); n   sz[fe ^ 1] = n - re; n   return sz[fe] = re; n } n void solve(long long rt) { n   memset(eg, 0, sizeof eg); n   long long sum = dfs1(rt, 0, 0); n   for (long long i = 2; i <= tot; ++i) aa[i] = min(aa[i], sum - eg[i]); n } n long long zz[N]; n long long wi[N], wo[N], bb[N << 1]; n void dfs3(long long u, long long fe) { n   zz[u] = 1; n   wi[u] = 0, wo[u] = 0; n   for (long long i = hd[u]; i; i = nxt[i]) n     if (i != fe) { n       dfs3(to[i], i ^ 1); n       zz[u] += zz[to[i]]; n       wi[u] += wi[to[i]]; n       wo[u] += wo[to[i]] + zz[to[i]] * w[i]; n       wi[u] -= (wo[to[i]] + zz[to[i]] * w[i]) * zz[to[i]]; n     } n   wi[u] += wo[u] * zz[u]; n   bb[fe] = wi[u]; n } n signed main() { n   memset(aa, 0x3f, sizeof aa); n   cin >> n; n   for (long long i = 1; i <= n - 1; ++i) { n     long long x, y, z; n     cin >> x >> y >> z; n     add(x, y, z); n     add(y, x, z); n   } n   for (long long i = 1; i <= n; ++i) dfs3(i, 0); n   for (long long i = 1; i <= n; ++i) solve(i); n   dfs2(1, 0); n   long long ans = 1e18; n   for (long long i = 1; i <= n - 1; ++i) { n     long long r = i << 1, s = i << 1 | 1; n     ans = min(ans, 1LL * w[r] * sz[r] * sz[s] + aa[r] * sz[r] + aa[s] * sz[s] + n                        bb[r] + bb[s]); n   } n   cout << ans << endl; n } n


#include <iostream> n #include <algorithm> n using namespace std; n typedef long long ll; n const ll mod = 1e9+7; n const int SZ = 1e5+10; n  n ll fact[SZ]; n void buildFact(){ n     fact[0] = 1; n     for(int i = 1; i < SZ; i++){ n         fact[i] = fact[i-1] * i; n         fact[i] %= mod; n     } n } n  n ll powe(ll x, ll p){ n     ll ans = 1; n     for(; p > 0; x = (x*x)%mod, p >>= 1){ n         if (p&1){ ans *= x; ans %= mod; } n     } n     return ans; n } n  n ll modinv(ll n){ n     return powe(n, mod-2); n } n  n ll choose(ll n, ll k){ n     if (k > n){ return 0; } n      n     ll ans = fact[n]; n     ll den = fact[n-k] * fact[k]; den %= mod; n     ans *= modinv(den); ans %= mod; n      n     return ans; n } n  n int main(){ n     buildFact(); n      n     int t; cin >> t; n     while (t--){ n         int n, k; cin >> n >> k; n         ll ans = 1; n          n         for(ll p = 1; n-(k-1)*(p-1) >= p-1; p++){ n             ans += choose(n-(k-1)*(p-1), p) * modinv(choose(n, p)); n             ans %= mod; n         } n          n         cout << ans <<   n ; 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   long long N, P; n   cin >> N >> P; n   vector<long long> l(N), r(N); n   for (long long i = 0; i < N; ++i) cin >> l[i] >> r[i]; n   double exp_val = 0; n   for (long long i = 0; i < N; ++i) { n     long long j = (i + 1) % N; n     double tot = (r[i] - l[i] + 1) * (r[j] - l[j] + 1); n     double a = floor(((double)r[i]) / P); n     double b = ceil(((double)l[i]) / P); n     double c = a - b + 1; n     double e = floor(((double)r[j]) / P); n     double f = ceil(((double)l[j]) / P); n     double g = e - f + 1; n     double good = (c * (r[j] - l[j] + 1)) + (g * (r[i] - l[i] + 1)) - c * g; n     exp_val += good * 2000 / tot; n   } n   cout << fixed << setprecision(9) << exp_val << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int a[100010], b[100010]; n int main() { n   int n, k, m; n   scanf( %d%d , &n, &k); n   int val = 1; n   for (int i = 0; i < k; i++) val *= 10; n   m = n / k; n   for (int i = 0; i < m; i++) scanf( %d , &a[i]); n   for (int i = 0; i < m; i++) scanf( %d , &b[i]); n   long long ans = 1; n   for (int i = 0; i < m; i++) { n     int t = (val - (val % a[i])) / a[i]; n     if (val % a[i]) t++; n     int num = (a[i] - (val / 10 * b[i]) % a[i]) % a[i]; n     if ((val / 10) <= num) { n       ans = ans * t % mod; n       continue; n     } n     int tt = ((val / 10 - num) - ((val / 10 - num) % a[i])) / a[i]; n     if ((val / 10 - num) % a[i]) tt++; n     ans = ans * (t - tt) % mod; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, s, i, a, b, t, j, v, f, c; n   int candy = -1, d; n   scanf( %d%d , &n, &s); n   s = s * 100; n   while (n--) { n     scanf( %d%d , &a, &b); n     d = (a * 100) + b; n     if (d <= s) { n       f = s - d; n       c = f % 100; n       if (c > candy) candy = c; n     } n   } n   if (c != -1) n     printf( %d , candy); n   else n     printf( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 3; n const int M = 26; n char s1[N], s2[N]; n int z1[N][M], z2[N][M]; n int n; n bool check(int l1, int r1, int l2, int r2) { n   int l, i, t1, t2; n   for (i = 0; i < M; ++i) { n     t1 = z1[r1 - 1][i]; n     if (l1) t1 -= z1[l1 - 1][i]; n     t2 = z2[r2 - 1][i]; n     if (l2) t2 -= z2[l2 - 1][i]; n     if (t1 != t2) return false; n   } n   l = r1 - l1; n   if (l & 1) { n     for (i = 0; i < l; ++i) n       if (s1[l1 + i] != s2[l2 + i]) return false; n     return true; n   } n   l >>= 1; n   if (check(l1, l1 + l, l2, l2 + l) && check(l1 + l, r1, l2 + l, r2)) n     return true; n   if (check(l1, l1 + l, l2 + l, r2) && check(l1 + l, r1, l2, l2 + l)) n     return true; n   return false; n } n int main() { n   if (0) { n     freopen( input.txt ,  w , stdout); n     int n, i; n     n = 8192; n     for (i = 0; i < n; ++i) printf( a ); n     printf(  n ); n     for (i = 0; i < n; ++i) printf( a ); n     printf(  n ); n     return 0; n   } n   int i, j; n   scanf( %s , s1); n   scanf( %s , s2); n   n = strlen(s1); n   ++z1[0][s1[0] -  a ]; n   ++z2[0][s2[0] -  a ]; n   for (i = 1; i < n; ++i) { n     for (j = 0; j < M; ++j) z1[i][j] += z1[i - 1][j], z2[i][j] += z2[i - 1][j]; n     ++z1[i][s1[i] -  a ]; n     ++z2[i][s2[i] -  a ]; n   } n   printf( %s n , (check(0, n, 0, n)) ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e6 + 5; n int dp[N][2]; n int main() { n   int n, x; n   scanf( %d , &n); n   dp[0][0] = dp[0][1] = 1; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &x); n     for (int j = 1; j * j <= x; ++j) { n       if (x % j) continue; n       dp[j][1] = (dp[j][1] + dp[j - 1][0]) % mod; n       if (j * j != x) dp[x / j][1] = (dp[x / j][1] + dp[x / j - 1][0]) % mod; n     } n     for (int j = 1; j * j <= x; ++j) { n       if (x % j) continue; n       dp[j][0] = dp[j][1] % mod; n       if (j * j != x) dp[x / j][0] = dp[x / j][1] % mod; n     } n   } n   int sum = 0; n   for (int i = 1; i <= n; ++i) { n     sum = sum + dp[i][0]; n     sum %= mod; n   } n   printf( %d n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans[110]; n int main() { n   scanf( %d , &n); n   for (int i = n; i > 0; i--) { n     printf( ?  ); n     for (int j = 1; j < n; j++) printf( 1  ); n     printf( %d n , i); n     fflush(stdout); n     int k; n     scanf( %d , &k); n     if (k) { n       ans[n] = n - i + 1; n       ans[k] = n; n       break; n     } n   } n   if (!ans[n]) ans[n] = n; n   for (int i = ans[n] + 1; i <= n; i++) { n     printf( ?  ); n     for (int j = 1; j < n; j++) printf( 1  ); n     printf( %d n , i - ans[n] + 1); n     fflush(stdout); n     int k; n     scanf( %d , &k); n     ans[k] = i; n   } n   for (int i = 1; i < ans[n]; i++) { n     printf( ?  ); n     for (int j = 1; j < n; j++) printf( %d  , ans[n] - i + 1); n     printf( 1 n ); n     fflush(stdout); n     int k; n     scanf( %d , &k); n     ans[k] = i; n   } n   printf( !  ); n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   bool o[100]; n   int n, k1, k2; n   int a[100]; n   int b[100]; n   for (int j = 0; j < t; j++) { n     cin >> n >> k1 >> k2; n     for (int i = 0; i < k1; i++) cin >> a[i]; n     for (int i = 0; i < k2; i++) cin >> b[i]; n     o[j] = 0; n     for (int i = 0; i < k1; i++) n       if (a[i] == n) o[j] = 1; n   } n   for (int j = 0; j < t; j++) { n     if (o[j]) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7, N = 3e5 + 7, M = 998244353; n vector<int> G[N], add[N], del[N]; n long long f[N], fi[N], inv[N]; n int cans[41], dp[1 << 20][21]; n int C(int n, int k) { return f[n] * fi[k] % M * fi[n - k] % M; } n void ADD(int& a, int b) { n   a += b; n   if (a >= M) a -= M; n } n signed main() { n   std::ios::sync_with_stdio(0); n   std::cin.tie(0); n   ; n   f[0] = 1; n   for (int i = 1; i < N; i++) f[i] = f[i - 1] * i % M; n   inv[1] = 1; n   for (int i = 2; i < N; i++) inv[i] = M - M / i * inv[M % i] % M; n   fi[0] = 1; n   for (int i = 1; i < N; i++) fi[i] = fi[i - 1] * inv[i] % M; n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     int l, r; n     cin >> l >> r; n     add[l].push_back(i); n     del[r + 1].push_back(i); n   } n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     G[u].push_back(i); n     G[v].push_back(i); n   } n   long long ans = 0, cnt = 0, cnt1 = 0; n   dp[0][0] = cans[0] = 1; n   for (int i = 1; i <= n; i++) { n     bool change = false; n     for (int j : add[i]) { n       if (G[j].size()) { n         int mask = 0; n         for (int k : G[j]) mask |= 1 << k; n         for (int k = 0; k < (1 << m); k++) n           for (int l = 1; l <= 20; l++) n             if ((k & mask) == mask) ADD(dp[k][l], dp[k ^ mask][l - 1]); n         change = true; n         cnt1++; n       } else n         cnt++; n     } n     for (int j : del[i]) { n       if (G[j].size()) { n         int mask = 0; n         for (int k : G[j]) mask |= 1 << k; n         for (int k = 0; k < (1 << m); k++) n           for (int l = 1; l <= 20; l++) n             if ((k & mask) == mask) n               ADD(dp[k][l], n                   M - (dp[k ^ mask][l - 1] ? dp[k ^ mask][l - 1] : M)); n         change = true; n         cnt1--; n       } else n         cnt--; n     } n     if (change) { n       memset(cans, 0, sizeof cans); n       for (int mask = 0; mask < (1 << m); mask++) n         for (int j = 0; j <= 20; j++) ADD(cans[j], dp[mask][j]); n     } n     for (int j = 0; j <= cnt1; j++) { n       if (i - j < 0 || i - j > cnt) continue; n       ans = (ans + (long long)C(cnt, i - j) * cans[j]) % M; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int e[1010][1010]; n int main(void) { n   int n, a, b; n   scanf( %d%d%d , &n, &a, &b); n   if (a > 1 && b > 1) { n     printf( NO n ); n     return 0; n   } n   int x = n - max(a, b) + 1; n   if (x <= 0 || ((n == 2 || n == 3) && x == n)) { n     printf( NO n ); n     return 0; n   } n   printf( YES n ); n   for (int i = 0; i < x - 1; i++) e[i][i + 1] = e[i + 1][i] = 1; n   if (b != 1) n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) n         if (i != j) e[i][j] = 1 - e[i][j]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) printf( %d , e[i][j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const long long INF = 1000000000; n template <class T> n inline void read(T &x) { n   char c; n   int flag = 1; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) flag *= -1; n   x = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= flag; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, q; n   cin >> q; n   map<long long, long long> dp; n   while (q--) { n     long long op, u, v; n     cin >> op >> u >> v; n     if (op == 1) { n       long long w; n       cin >> w; n       while (u ^ v) { n         if (u < v) swap(u, v); n         dp[u] += w, u /= 2; n       } n     } else { n       long long ans = 0; n       while (u ^ v) { n         if (u < v) swap(u, v); n         ans += dp[u], u /= 2; n       } n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n int main(int argc, char const *argv[]) { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n, m; n     scanf( %d%d , &n, &m); n     std::vector<short> vertices(3 * n, 0); n     std::vector<int> edges; n     for (int i = 0; i < m; i++) { n       int u, v; n       scanf( %d%d , &u, &v); n       if (!vertices[u - 1] && !vertices[v - 1]) { n         vertices[u - 1] = 1; n         vertices[v - 1] = 1; n         edges.push_back(i + 1); n       } n     } n     if (edges.size() >= n) { n       printf( Matching n ); n       for (int i = 0; i < n; i++) printf( %d n , edges[i]); n     } else { n       printf( IndSet n ); n       for (int i = 0, cnt = 0; i < 3 * n && cnt < n; i++) { n         if (!vertices[i]) { n           cnt++; n           printf( %d n , i + 1); n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int L, R, i; n } q[100001]; n bool cmp(node x, node y) { n   if (x.L / 555 != y.L / 555) { n     return x.L / 555 < y.L / 555; n   } n   return x.R < y.R; n } n map<int, int> mpp; n int ans = 0, I; n int arr[100000 + 1], ct[100001], arr2[100001]; n void add(int p) { n   ct[arr2[p]]++; n   if (ct[arr2[p]] == arr[p]) { n     ans++; n   } n   if (ct[arr2[p]] == arr[p] + 1) ans--; n } n void rem(int p) { n   if (ct[arr2[p]] == arr[p]) ans--; n   if (ct[arr2[p]] == arr[p] + 1) ans++; n   ct[arr2[p]]--; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > V; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n     V.push_back(make_pair(arr[i], i)); n   } n   sort(V.begin(), V.end()); n   int r = 0; n   for (int i = 0; i < V.size(); i++) { n     if (i && V[i].first != V[i - 1].first) r++; n     arr2[V[i].second] = r; n   } n   int x, y; n   for (int i = 0; i < m; i++) { n     cin >> q[i].L >> q[i].R; n     q[i].i = i; n   } n   sort(q, q + m, cmp); n   x = q[0].L, y = q[0].R; n   for (int i = x; i <= y; i++) { n     ct[arr2[i]]++; n     if (ct[arr2[i]] == arr[i]) ans++; n     if (ct[arr2[i]] == arr[i] + 1) ans--; n   } n   int out[m]; n   out[q[0].i] = ans; n   for (I = 1; I < m; I++) { n     int l = q[I].L, r = q[I].R; n     while (x < l) { n       rem(x); n       x++; n     } n     while (x > l) { n       x--; n       add(x); n     } n     while (y < r) { n       y++; n       add(y); n     } n     while (y > r) { n       rem(y); n       y--; n     } n     out[q[I].i] = ans; n   } n   for (int i = 0; i < m; i++) { n     cout << out[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const int inf = 1 << 28; n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n long long In() { n   long long x; n   cin >> x; n   return x; n } n double inreal() { n   double x; n   scanf( %lf , &x); n   return x; n } n string instr() { n   char buf[1001]; n   scanf( %s , buf); n   return (string)buf; n } n struct UF { n   vector<int> p, r; n   UF(int n) { n     n++; n     p.resize(n, ~0), r.resize(n, 0); n   } n   int root(int x) { return ~p[x] ? p[x] = root(p[x]) : x; } n   bool conn(int x, int y) { n     x = root(x), y = root(y); n     if (x == y) return 0; n     if (r[x] < r[y]) n       p[x] = y; n     else { n       p[y] = x; n       if (r[x] == r[y]) r[x]++; n     } n     return 1; n   } n   bool same(int x, int y) { return root(x) == root(y); } n }; n const long long M = 1000000009LL; n long long resget(long long x) { n   x = x - 1; n   if (x < 0) return M - 1; n   return x; n } n int main() { n   int i, j, k, n = in(), m = in(), u, v; n   UF uf(n); n   long long res = 1; n   for ((i) = (0); (i) < (int)(m); (i)++) { n     u = in() - 1, v = in() - 1; n     if (uf.conn(u, v)) { n       cout << resget(res) << endl; n     } else { n       res = res * 2 % M; n       cout << resget(res) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m; n long long a, k, ak; n char c; n unordered_map<long long, bool> ma; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> t; n   for (int ti = 1; ti <= t; ++ti) { n     ma.clear(); n     cin >> n >> m; n     k = (1ll << m) - n; n     for (int i = 1; i <= n; ++i) { n       a = 0; n       for (int j = m - 1; j >= 0; --j) { n         cin >> c; n         a |= (c ==  1  ? (1ll << j) : 0ll); n       } n       ma[a] = 1; n     } n     a = (k - 1) / 2; n     ak = k = a + 1; n     for (pair<long long, bool> tw : ma) { n       if (tw.first <= a) --ak; n     } n     while (ak < k) { n       if (!ma[++a]) ++ak; n     } n     for (int i = m - 1; i >= 0; --i) { n       cout << (a & (1ll << i) ? 1 : 0); n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool flag; n long long Max = -1e18, tpMax = -1e18, dep[600005], a[600005]; n int n, m, sz[600005], In[600005], TOT, LA[600005], NE[600005 << 1], n     EN[600005 << 1]; n long long LE[600005 << 1]; n void Link(int x, int y, int z) { n   EN[++TOT] = y; n   LE[TOT] = z; n   NE[TOT] = LA[x]; n   LA[x] = TOT; n   EN[++TOT] = x; n   LE[TOT] = z; n   NE[TOT] = LA[y]; n   LA[y] = TOT; n } n void pre_dfs(int x, int fa) { n   if (x == n) In[x] = x; n   int i, y; n   sz[x] = 1; n   long long tp = -1e18, ttp = -1e18; n   for (i = LA[x]; i; i = NE[i]) { n     y = EN[i]; n     if (y == fa) continue; n     dep[y] = dep[x] + LE[i]; n     pre_dfs(y, x); n     sz[x] += sz[y]; n     if (In[y]) n       In[x] = y; n     else n       a[x] = LE[i]; n     if (a[y] && In[y]) tp = max(tp, a[y] - dep[y]); n     if (In[y]) ttp = max(ttp, a[y] - dep[y]); n   } n   if (sz[x] - sz[In[x]] > 2) flag = 1; n   if (x == n && sz[x] > 2) flag = 1; n   if (In[x]) { n     Max = max(Max, a[x] + dep[x] + max(tpMax, tp)); n     if (a[x]) Max = max(Max, a[x] + dep[x] + ttp); n     for (i = LA[x]; i; i = NE[i]) { n       y = EN[i]; n       if (y == fa) continue; n       if (In[y]) tpMax = max(tpMax, a[y] - dep[y]); n     } n   } n } n int main() { n   int i, j, k, x, y, z; n   long long t; n   scanf( %d%d , &n, &m); n   for (i = 1; i < n; i++) { n     scanf( %d%d%d , &x, &y, &z); n     Link(x, y, z); n   } n   pre_dfs(1, 0); n   for (i = 1; i <= m; i++) { n     scanf( %I64d , &t); n     if (flag || t + Max >= 0) n       printf( %I64d n , dep[n]); n     else n       printf( %I64d n , dep[n] + t + Max); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void pfill(T* pst, const T* ped, T val) { n   for (; pst != ped; *(pst++) = val) n     ; n } n const double pi = acos(-1); n const double eps = 0; n typedef class Point { n  public: n   int x, y, deg, id; n   Point() : x(0), y(0), deg(0), id(0) {} n   Point(Point p, int ndeg) : x(p.x), y(p.y), deg(ndeg), id(p.id) {} n   Point(int x, int y, int deg) : x(x), y(y), deg(deg), id(0) {} n } Point; n typedef class Event { n  public: n   int val, id; n   bool add; n   double theta; n   Event() {} n   Event(int val, int id, bool add, double theta) n       : val(val), id(id), add(add), theta(theta) {} n   bool operator<(Event e) const { return theta < e.theta; } n } Event; n const int N = 6e3 + 6; n int T; n int n, m; n int rs[N]; n int tp = 0; n bool onleft[N]; n Event events[N << 1]; n vector<Point> ps; n inline void init() { n   scanf( %d%d , &n, &m); n   ps.clear(); n   ps = vector<Point>(n + m); n   for (int i = 0; i < m; i++) scanf( %d , rs + i); n   for (int i = 0; i < n; i++) n     scanf( %d%d , &ps[i].x, &ps[i].y), ps[i].deg = 0, ps[i].id = i + 1; n   for (int i = 0; i < m; i++) n     scanf( %d%d , &ps[i + n].x, &ps[i + n].y), ps[i + n].deg = rs[i], n                                                       ps[i + n].id = i + 1; n } n void solve(vector<Point>& ps) { n   int r = 0, mxid = -1; n   for (int i = 0; i < (signed)ps.size(); i++) n     if (ps[i].deg > r) r = ps[i].deg, mxid = i; n   if (r == 1) { n     int cnt = 0; n     for (int i = 0; i < (signed)ps.size(); i++) n       if (!ps[i].deg) mxid = i, cnt++; n     assert(cnt == 1 || ps.size() == 1); n     for (int i = 0; i < (signed)ps.size(); i++) n       if (i ^ mxid) printf( %d %d n , ps[i].id, ps[mxid].id); n     return; n   } n   int dx, dy; n   double theta; n   tp = 0; n   int sum = 0; n   for (int i = 0; i < (signed)ps.size(); i++) { n     if (i == mxid) continue; n     dx = ps[i].x - ps[mxid].x, dy = ps[i].y - ps[mxid].y; n     theta = atan2(dy, dx); n     if (theta > 0 || fabs(theta) < eps) { n       sum += ps[i].deg - 1, onleft[i] = true; n       events[tp++] = Event(1 - ps[i].deg, i, false, theta); n     } else n       events[tp++] = Event(ps[i].deg - 1, i, true, theta + pi); n   } n   sort(events, events + tp); n   for (int i = 0; i < tp && (sum < 1 - r || sum > -1); i++) { n     sum += events[i].val; n     onleft[events[i].id] = events[i].add; n   } n   assert(sum >= 1 - r && sum <= -1); n   vector<Point> pl, pr; n   for (int i = 0; i < (signed)ps.size(); i++) { n     if (i == mxid) continue; n     if (onleft[i]) n       pl.push_back(ps[i]); n     else n       pr.push_back(ps[i]); n   } n   pfill(onleft, onleft + ps.size(), false); n   pl.push_back(Point(ps[mxid], -sum)); n   pr.push_back(Point(ps[mxid], r + sum)); n   solve(pl); n   solve(pr); n } n inline void solve() { n   puts( YES ); n   solve(ps); n } n int main() { n   scanf( %d , &T); n   while (T--) { n     init(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int N = 1050; n const long long mod = 1e9 + 7; n int l, r, n, id = 1; n int a[N]; n bool vis[N]; n char ans[N][N]; n void work(int now, int i) { n   bool flag = true; n   while (!vis[i]) { n     vis[i] = 1; n     if (flag) { n       if (a[i] > i) n         ans[id][i] = ans[id][a[i]] =  / ; n       else { n         ans[id][i] = ans[id][r] =  / ; n         ++id; n         ans[id][r] = ans[id][a[i]] =     ; n         flag = 0; n       } n     } else { n       if (a[i] < i) n         ans[id][i] = ans[id][a[i]] =     ; n       else { n         ans[id][i] = ans[id][now] =     ; n         ++id; n         ans[id][now] = ans[id][a[i]] =  / ; n         flag = 1; n       } n     } n     i = a[i]; n   } n } n int main() { n   cin >> n; n   l = inf, r = 0; n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) { n     if (a[i] != i) l = min(l, i), r = max(r, i); n   } n   if (l == inf) { n     printf( %d n , n); n     for (int i = 1; i <= n; ++i, puts(  )) n       for (int j = 1; j <= n; ++j) printf( . ); n     return 0; n   } n   int pre = 1, nxt = a[r]; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) ans[i][j] =  . ; n   for (int i = l; i <= r; ++i) n     if (a[i] == r) pre = i; n   a[pre] = a[r]; n   a[r] = r; n   for (int i = l; i < r; ++i) { n     if (a[i] == i || vis[i]) continue; n     work(i, i); n     id++; n   } n   ans[id][nxt] = ans[id][r] =  / ; n   cout << n - 1 << endl; n   for (int i = n; i >= 1; --i) { n     printf( %s n , ans[i] + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n void solve() { n   long long w, h; n   cin >> w >> h; n   int l1; n   cin >> l1; n   vector<long long> row1(l1); n   for (int i = 0; i < l1; i++) { n     cin >> row1[i]; n   } n   int l2; n   cin >> l2; n   vector<long long> row2(l2); n   for (int i = 0; i < l2; i++) { n     cin >> row2[i]; n   } n   sort(row1.begin(), row1.end()); n   sort(row2.begin(), row2.end()); n   long long gap = max(row1[l1 - 1] - row1[0], row2[l2 - 1] - row2[0]); n   int h1; n   cin >> h1; n   vector<long long> col1(h1); n   for (int i = 0; i < h1; i++) { n     cin >> col1[i]; n   } n   int h2; n   cin >> h2; n   vector<long long> col2(h2); n   for (int i = 0; i < h2; i++) { n     cin >> col2[i]; n   } n   sort(col1.begin(), col1.end()); n   sort(col2.begin(), col2.end()); n   long long gap2 = max(col1[h1 - 1] - col1[0], col2[h2 - 1] - col2[0]); n   long long area = max(h * gap, w * gap2); n   cout << area <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 3; n bool dp[30][(1 << 14)][30]; n int cnt[(1 << 14)], deg[(1 << 14)]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> a; n   int c = 0; n   bool fail = 0; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     if (x == 2) fail = 1; n     if (x == 1) n       c++; n     else n       a.push_back(x); n   } n   if (n == 1) { n     if (c == 1) return cout <<  YES n , 0; n     return cout <<  NO n , 0; n   } n   sort(a.begin(), a.end()); n   if (c < (n + 1) / 2 or fail) return cout <<  NO n , 0; n   int m = n - c; n   for (int mask = 0; mask < (1 << m); mask++) { n     for (int i = 0; i < m; i++) { n       if (mask & (1 << i)) { n         deg[mask] = deg[mask ^ (1 << i)] + a[i]; n         cnt[mask] = cnt[mask ^ (1 << i)] + 1; n       } n     } n   } n   dp[0][0][c] = 1; n   for (int i = 0; i < m; i++) { n     for (int mask = 0; mask < (1 << i); mask++) { n       for (int j = 0; j <= c; j++) { n         if (dp[i][mask][j] == 0) continue; n         for (int submask = mask; submask; submask = (submask - 1) & mask) { n           if (deg[submask] > a[i] - 1 or deg[submask] + j < a[i] - 1) continue; n           int t = a[i] - 1 - deg[submask]; n           if (cnt[submask] + t > 1) n             dp[i + 1][(mask ^ submask) | (1 << i)][j - t] = 1; n         } n         if (j >= a[i] - 1) dp[i + 1][mask | (1 << i)][j - a[i] + 1] = 1; n       } n     } n   } n   cout << (dp[m][(1 << (m - 1))][0] == 1 ?  YES n  :  NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n bool maxl(long long a, long long b) { return a > b; } n bool minl(long long a, long long b) { return a < b; } n bool cmp(int a, int b) { return a > b; } n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n int main() { n   int a[105], ji = 0, ou = 0, i, j, n, ans = 0; n   n = read(); n   for (i = 0; i < n; i++) { n     a[i] = read(); n     if (a[i] % 2 == 0) { n       ou++; n     } else { n       ji++; n     } n     ans += a[i]; n   } n   sort(a, a + n); n   if (ans % 2 == 1) { n     cout << ans << endl; n   } else { n     if (ji >= 1) { n       for (i = 0; i < n; i++) n         if (a[i] % 2 == 1) { n           ans -= a[i]; n           break; n         } n       cout << ans << endl; n     } else { n       cout << 0 << endl; n     } n   } n } n