#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 239; n int n, m, k; n vector<set<int>> row; n vector<set<int>> column; n vector<set<int>> row_free; n vector<set<int>> column_free; n int get_row_left(int i, int to, int from) { n   auto it = row[i].lower_bound(from); n   if (it == row[i].begin()) { n     return to; n   } n   it--; n   if (to <= *(it) && *(it) <= from) { n     return *(it) + 1; n   } else { n     return to; n   } n } n int get_row_right(int i, int to, int from) { n   auto it = row[i].lower_bound(from); n   if (it == row[i].end()) { n     return to; n   } n   if ((*it) <= to) { n     return (*it) - 1; n   } else { n     return to; n   } n } n int get_column_up(int j, int to, int from) { n   auto it = column[j].lower_bound(from); n   if (it == column[j].begin()) { n     return to; n   } n   it--; n   if (to <= *(it) && *(it) <= from) { n     return *(it) + 1; n   } else { n     return to; n   } n } n int get_column_down(int j, int to, int from) { n   auto it = column[j].lower_bound(from); n   if (it == column[j].end()) { n     return to; n   } n   if ((*it) <= to) { n     return (*it) - 1; n   } else { n     return to; n   } n } n vector<pair<int, int>> all_next(int i, int j) { n   int ff = get_row_left(i, max(0, j - k), j); n   int ss = get_row_right(i, min(m - 1, j + k), j); n   int tt = get_column_up(j, max(0, i - k), i); n   int th = get_column_down(j, min(n - 1, i + k), i); n   vector<pair<int, int>> ans; n   if (j > 0) { n     auto it = row_free[i].lower_bound(ff); n     while (true) { n       if (it == row_free[i].end()) { n         break; n       } n       if (*(it) >= j) { n         break; n       } n       ans.push_back({i, *(it)}); n       it++; n     } n   } n   if (j + 1 < m) { n     auto it = row_free[i].upper_bound(j); n     while (true) { n       if (it == row_free[i].end()) { n         break; n       } n       if (*(it) > ss) { n         break; n       } n       ans.push_back({i, *(it)}); n       it++; n     } n   } n   if (i > 0) { n     auto it = column_free[j].lower_bound(tt); n     while (true) { n       if (it == column_free[j].end()) { n         break; n       } n       if (*(it) >= i) { n         break; n       } n       ans.push_back({*(it), j}); n       it++; n     } n   } n   if (i + 1 < n) { n     auto it = column_free[j].upper_bound(i); n     while (true) { n       if (it == column_free[j].end()) { n         break; n       } n       if (*(it) > th) { n         break; n       } n       ans.push_back({*(it), j}); n       it++; n     } n   } n   return ans; n } n void bfs(int si, int sj, int ei, int ej) { n   vector<vector<int>> d(n, vector<int>(m, inf)); n   d[si][sj] = 0; n   queue<pair<int, int>> q; n   q.push({si, sj}); n   row_free[si].erase(sj); n   column_free[sj].erase(si); n   while (!q.empty()) { n     auto tmp = q.front(); n     q.pop(); n     int ci = tmp.first; n     int cj = tmp.second; n     auto next = all_next(ci, cj); n     for (auto [ni, nj] : next) { n       if (d[ni][nj] > d[ci][cj] + 1) { n         d[ni][nj] = d[ci][cj] + 1; n         row_free[ni].erase(nj); n         column_free[nj].erase(ni); n         q.push({ni, nj}); n       } n     } n   } n   int res = d[ei][ej]; n   if (res == inf) { n     cout << -1; n   } else { n     cout << res; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> k; n   row.resize(n); n   column.resize(m); n   row_free.resize(n); n   column_free.resize(m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char c; n       cin >> c; n       if (c ==  # ) { n         row[i].insert(j); n         column[j].insert(i); n       } else { n         row_free[i].insert(j); n         column_free[j].insert(i); n       } n     } n   } n   int si, sj, ei, ej; n   cin >> si >> sj >> ei >> ej; n   si--; n   sj--; n   ei--; n   ej--; n   bfs(si, sj, ei, ej); n } n


#include <bits/stdc++.h> n using namespace std; n string s, t; n int dp[5005][5005]; n int get(int i, int j) { n   if (dp[i][j] != -1) return dp[i][j]; n   if (i >= s.size() or j >= t.size()) return 0; n   int ret = get(i, j + 1); n   if (s[i] == t[j]) ret = (ret + 1 + get(i + 1, j + 1)) % 1000000007; n   return dp[i][j] = ret; n   ; n } n int main() { n   for (int i = 0; i < 5005; ++i) n     for (int j = 0; j < 5005; ++j) dp[i][j] = -1; n   cin >> s >> t; n   long long ret = 0; n   for (int i = 0; i < s.size(); ++i) ret = (ret + get(i, 0)) % 1000000007; n   cout << ret << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, k; n long long f(long long x) { n   if (x == 0) return k * (k + 1) / 2; n   long long t = x % k, val = f(x / k), ofs = 0; n   if (val < k * t + 2) { n     ofs = k; n   } else if ((t + 1) * k < val) { n     ofs = 0; n   } else { n     ofs = (t + 1) * k - val + 1; n   } n   return k * (k + 1) / 2 - t + ofs; n } n long long rjesi(long long x) { n   long long buk = (x - 1) / (k * k + 1); n   long long pos = (k * k + 1) * buk + f(buk); n   if (x != pos) { n     long long lef = buk + (pos < x); n     return (x - 1 - lef) / k - lef + x; n   } else { n     long long t = buk % k, val = f(buk / k); n     if ((t + 1) * k < val) { n       return rjesi(buk / k * (k * k + 1) + (t + 1) * k) + 1; n     } else { n       return rjesi(buk / k * (k * k + 1) + (t + 1) * k + 1) + 1; n     } n   } n } n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> k; n     cout << rjesi(n) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 305; n long long dp[maxn][maxn][6]; n int n, x[maxn], y[maxn]; n vector<pair<double, pair<int, int>>> seg; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       for (int k = 0; k < 6; k++) dp[i][j][k] = 0; n       if (i == j) dp[i][j][0] = 1; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i != j) { n         int xx = x[j] - x[i], yy = y[j] - y[i]; n         seg.push_back({atan2(xx, yy), {i, j}}); n       } n     } n   } n   sort(seg.begin(), seg.end()); n   int m = seg.size(); n   for (int cc = 0; cc < m; cc++) { n     int i = seg[cc].second.first, j = seg[cc].second.second; n     for (int k = 0; k < n; k++) n       for (int f = 0; f < 5; f++) dp[k][i][f + 1] += dp[k][j][f]; n   } n   long long ret = 0; n   for (int i = 0; i < n; i++) ret += dp[i][i][5]; n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   if ((n & (n - 1)) == 0) { n     cout << 0 <<   n ; n   } else { n     int k = 0; n     while ((1 << k + 1) <= n) { n       k++; n     } n     n ^= 1 << k; n     int ans = 0; n     for (int i = 0; i < k; ++i) { n       int j = i + 1; n       if (j >= k) { n         j -= k; n       } n       if (n & (1 << j)) { n         ans |= 1 << i; n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime(int n) { n   if (n == 2) return 1; n   for (int i = 2; i < n; i++) { n     if (n % i == 0) return 0; n   } n   return 1; n } n int main() { n   int n, m; n   cin >> n >> m; n   int flag = 1; n   if (isprime(n) && isprime(m)) { n     for (int i = n + 1; i < m; i++) { n       if (isprime(i)) { n         cout <<  NO  << endl; n         flag = 0; n         break; n       } n     } n     if (flag) cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n const int N = 200000 + 10; n int a[N], n, K; n int main() { n   scanf( %d%d , &n, &K); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   long long pre = 0; n   int tot = n - 1, id = 0; n   for (int i = 0; i < n; ++i) { n     long long d = pre - (long long)a[i] * (tot - id) * id; n     if (d < K) { n       printf( %d n , i + 1); n       --tot; n     } else { n       pre = pre + (long long)a[i] * id; n       ++id; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105][105], t, m, n, ret, cnt, mn; n int main(void) { n   cin >> t; n   for (int i = 0; i < t; i++) { n     ret = 0, cnt = 0, mn = 101; n     cin >> n >> m; n     for (int j = 0; j < n; j++) { n       for (int k = 0; k < m; k++) { n         cin >> a[j][k]; n         if (a[j][k] <= 0) cnt++; n         if (abs(a[j][k]) < mn) mn = abs(a[j][k]); n       } n     } n     for (int j = 0; j < n; j++) { n       for (int k = 0; k < m; k++) { n         ret += abs(a[j][k]); n       } n     } n     if (cnt % 2 == 1) ret -= 2 * mn; n     cout << ret << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 510; n int n, m; n double xa, ya, x2, y2; n double v, vx1, vx2, vy1, vy2, t; n int check(double k) { n   double x, y; n   x = xa + vx1 * min(k, t) + vx2 * max(0.0, k - t); n   y = ya + vy1 * min(k, t) + vy2 * max(0.0, k - t); n   double dist = sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2)); n   if (v * k >= dist) return 1; n   return 0; n } n int main() { n   int i, j, k; n   while (~scanf( %lf%lf%lf%lf , &xa, &ya, &x2, &y2)) { n     scanf( %lf%lf , &v, &t); n     scanf( %lf%lf , &vx1, &vy1); n     scanf( %lf%lf , &vx2, &vy2); n     double l = 0.0, r = 1e9; n     while ((r - l) > 1e-6) { n       double mid = (l + r) / 2; n       if (check(mid)) n         r = mid; n       else n         l = mid; n     } n     printf( %.10lf n , l); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, sum = 0, q; n   vector<int> v; n   vector<int>::iterator it; n   scanf( %d , &n); n   while (n--) { n     scanf( %d , &x); n     sum += x; n     v.push_back(sum); n   } n   int sz = v.size(); n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &x); n     it = lower_bound(v.begin(), v.end(), x); n     printf( %d n , (it - v.begin()) + 1); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long r = n % 3; n   if (r != 2) n     cout << 1ll <<     << 1ll <<     << n - 2 << endl; n   else n     cout << 1ll <<     << 2ll <<     << n - 3 << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n template <typename T> n using vc = vector<T>; n template <typename T> n using uset = unordered_set<T>; n template <typename A, typename B> n using umap = unordered_map<A, B>; n template <typename T, typename Comp = less<T>> n using pq = priority_queue<T, vc<T>, Comp>; n template <typename T> n using minpq = pq<T, greater<T>>; n using ld = long double; n using ll = long long; n using ull = unsigned long long; n using pii = pair<int, int>; n using ppi = pair<pii, int>; n using pip = pair<int, pii>; n using vi = vc<int>; n using vii = vc<pii>; n constexpr char el =   n ; n constexpr char sp =    ; n constexpr int INF = 0x3f3f3f3f; n constexpr ll LLINF = 0x3f3f3f3f3f3f3f3fLL; n char g[8][8]; n int dr[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; n int dc[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; n vi a[8][8]; n bool good(int r, int c) { return 0 <= r && r < 8 && 0 <= c && c < 8; } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed; n   for (auto& x : g) n     for (auto& y : x) cin >> y; n   for (int i = 0; i < 8; ++i) n     for (int j = 0; j < 8; ++j) n       if (g[i][j] ==  S ) n         for (int k = i; k < 8; ++k) a[k][j].push_back(k - i); n   for (auto& x : a) n     for (auto& y : x) sort(y.begin(), y.end()); n   queue<tuple<int, int, int>> q; n   q.emplace(7, 0, 0); n   while (!q.empty()) { n     auto [r, c, d] = q.front(); n     q.pop(); n     for (int i = 0; i < 9; ++i) { n       int r1 = r + dr[i]; n       int c1 = c + dc[i]; n       if (r1 == 0 && c1 == 7) { n         cout <<  WIN ; n         exit(0); n       } n       if (!good(r1, c1) || binary_search(begin(a[r1][c1]), end(a[r1][c1]), d) || n           binary_search(begin(a[r1][c1]), end(a[r1][c1]), d + 1)) n         continue; n       q.emplace(r1, c1, d + 1); n     } n   } n   cout <<  LOSE ; n } n


#include <bits/stdc++.h> n   n using namespace std; n   n #define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) n #define fileio freopen( in.in ,  r , stdin),freopen( out.out ,  w , stdout); n #define ll long long n #define int long long n #define FF first n #define SS second n #define mp make_pair n #define pb push_back n #define pii pair<ll,ll> n #define pll pair<long long,long long> n #define sd(x) scanf( %d ,&x) n #define slld(x) scanf( %lld ,&x) n #define pd(x) prllf( %d n ,x) n #define plld(x) prllf( %lld n ,x) n #define pss prllf n #define MOD 1000000007 n #define eps 0.00001 n #define endl   n  n #define debug(n1) cout<<n1<<endl n ll n,k,s[1005],e[1005],c[1005]; n bool y[1005]; n const ll INF = 1e17+5; n int mm[100005]; n vector<int>temp[100005]; n  n struct MinimumCostMaximumFlow { n   typedef ll Index; typedef ll Flow; typedef ll Cost; n   static const Flow InfCapacity = INF; n   struct Edge { n     Index to; Index rev; n     Flow capacity; Cost cost; n   }; n   vector<vector<Edge> > g; n   void init(Index n) { g.assign(n, vector<Edge>()); } n   void addEdge(bool inv,Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost() ) { n     Edge e, f; e.to = j, f.to = i; e.capacity = capacity, f.capacity = 0; e.cost = cost, f.cost = -cost; n     if(inv) n     { n         temp[i].push_back(mm[i]); n     } n     mm[i]++; n     mm[j]++; n  n     g[i].push_back(e); g[j].push_back(f); n     g[i].back().rev = (Index)g[j].size() - 1; g[j].back().rev = (Index)g[i].size() - 1; n   } n   void addB(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) { n     addEdge(0,i, j, capacity, cost); n     addEdge(0,j, i, capacity, cost); n   } n  n   pair<Cost, Flow> minimumCostMaximumFlow(Index s, Index t, Flow f = InfCapacity, bool useSPFA = false) { n     ll n = g.size(); n     vector<Cost> dist(n); vector<Index> prev(n); vector<Index> prevEdge(n); n     pair<Cost, Flow> total = make_pair(0, 0); n     vector<Cost> potential(n); n     while(f > 0) { n       fill(dist.begin(), dist.end(), INF); n       if(useSPFA || total.second == 0) { n         deque<Index> q; n         q.push_back(s); dist[s] = 0; vector<bool> inqueue(n); n         while(!q.empty()) { n           Index i = q.front(); q.pop_front(); inqueue[i] = false; n           for(Index ei = 0; ei < (Index)g[i].size(); ei ++) { n             const Edge &e = g[i][ei]; Index j = e.to; Cost d = dist[i] + e.cost; n             if(e.capacity > 0 && d < dist[j]) { n               if(!inqueue[j]) { n                 inqueue[j] = true; n                 q.push_back(j); n               } n               dist[j] = d; prev[j] = i; prevEdge[j] = ei; n             } n           } n         } n       } else { n         vector<bool> vis(n); n         priority_queue<pair<Cost, Index> > q; n         q.push(make_pair(-0, s)); dist[s] = 0; n         while(!q.empty()) { n           Index i = q.top().second; q.pop(); n           if(vis[i]) continue; n           vis[i] = true; n           for(Index ei = 0; ei < (Index)g[i].size(); ei ++) { n             const Edge &e = g[i][ei]; n             if(e.capacity <= 0) continue; n             Index j = e.to; Cost d = dist[i] + e.cost + potential[i] - potential[j]; n             if(dist[j] > d) { n               dist[j] = d; prev[j] = i; prevEdge[j] = ei; n               q.push(make_pair(-d, j)); n             } n           } n         } n       } n       if(dist[t] == INF) break; n       if(!useSPFA) for(Index i = 0; i < n; i ++) potential[i] += dist[i]; n   n       Flow d = f; Cost distt = 0; n       for(Index v = t; v != s; ) { n         Index u = prev[v]; const Edge &e = g[u][prevEdge[v]]; n         d = min(d, e.capacity); distt += e.cost; v = u; n       } n       f -= d; total.first += d * distt; total.second += d; n       for(Index v = t; v != s; v = prev[v]) { n         Edge &e = g[prev[v]][prevEdge[v]]; n         e.capacity -= d; g[e.to][e.rev].capacity += d; n       } n     } n     return total; n   } n }; n   n  n int find_num(string ss) n { n     int ans=0; n     int tt=1; n     reverse(ss.begin(),ss.end()); n  n     for(auto i:ss) n     { n         ans+=(i- 0 )*tt; n         tt*=2; n     } n     return ans; n } n  n  n  n vector<vector<int> >adj; n int sz[1000005]; n int vis[1000005]; n int a[100005]; n vector<string>fi; n int d; n  n void print(int yy) n { n     for(int i=0;i<10;i++) n     { n         if(((1<<i)&yy)) n         { n             fi.push_back(to_string(d-i-1)); n         } n     } n } n  n void dfs(int ver,int x) n { n     vis[ver]=1; n     print(a[ver]^x); n     int cnt=0; n  n  n     for(auto i:adj[ver]) n     { n         if(vis[i]==0) n         { n             if(cnt) n             { n                 fi.push_back( R ); n                 print(a[ver]); n                 dfs(i,a[ver]); n             } n             else n             { n                 dfs(i,a[ver]); n             } n             cnt++; n         } n     } n } n  n signed main() n { n   n ios_base::sync_with_stdio(false);  n cin.tie(NULL);   n cout.tie(0);    n   n #ifndef ONLINE_JUDGE n     if(fopen( INPUT.txt , r )) n     { n         freopen( INPUT.txt , r ,stdin); n         freopen( OUTPUT.txt , w ,stdout); n     } n  n #endif           n  n     int n; n     cin>>d>>n; n     string se[n]; n     for(int i=0;i<n;i++) n     { n         cin>>se[i]; n         a[i+1]=find_num(se[i]); n     } n  n     adj.resize(n+1); n  n     MinimumCostMaximumFlow kad; n     int source=0; n     int sink=2*n+5; n  n     kad.init(2*n+10); n  n     for(int i=1;i<=n;i++) n     { n         for(int j=1;j<=n;j++) n         { n             if(i!=j) n             { n                 if((a[i]&a[j])==a[j]) n                 { n                     kad.addEdge(1,n+j,i,1,-(__builtin_popcount(a[j])+1)); n                 } n             } n         } n  n         kad.addEdge(0,i,n+i,1,0); n         kad.addEdge(0,source,i,1,0); n         kad.addEdge(0,n+i,sink,1,-(__builtin_popcount(a[i])+1)); n     } n  n     kad.addEdge(0,source,sink,n,0); n  n     pair<int,int> ans=kad.minimumCostMaximumFlow(source,sink,n,0); n  n     for(int i=n+1;i<=2*n;i++) n     { n         for(auto j:temp[i]) n         { n             if(kad.g[i][j].capacity==0) n             { n                 adj[i-n].push_back(kad.g[i][j].to); n             } n         } n     } n  n     set<pair<int,int> >ss; n     int cc=0; n     for(int i=1;i<=n;i++) n     { n         ss.insert({a[i],i}); n     } n  n     while(!ss.empty()) n     { n         auto j=ss.begin(); n         if(vis[j->second]) n         { n             ss.erase(j); n             continue; n         } n         if(cc) n             fi.push_back( R ); n         cc++; n         dfs(j->second,0); n     } n  n     cout<<fi.size()<<  n ; n     for(auto i:fi) n         cout<<i<<   ; n  n  n  n }


#include <bits/stdc++.h> n using namespace std; n namespace INIT { n char buf[1 << 15], *fs, *ft; n inline char getc() { n   return (fs == ft && n           (ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), fs == ft)) n              ? 0 n              : *fs++; n } n inline int read() { n   int x = 0, f = 1; n   char ch = getc(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getc(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getc(); n   } n   return x * f; n } n }  // namespace INIT n using namespace INIT; n int n, p[1000010], vis[1000010], ans[1000010]; n vector<int> a[1000010], b[1000010]; n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) p[i] = read(); n   for (int i = 1; i <= n; ++i) { n     int num = 0; n     for (int j = i; !vis[j]; j = p[j]) vis[j] = 1, ++num, a[i].push_back(j); n     b[num].push_back(i); n   } n   for (int i = 1; i <= n; ++i) n     if (i % 2 == 0 && (b[i].size() & 1)) { n       puts( -1 ); n       return 0; n     } n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i <= (int)b[1].size() - 1; ++i) ans[b[1][i]] = b[1][i]; n   for (int i = 2; i <= n; ++i) n     for (int j = 0; j <= (int)b[i].size() - 1; ++j) { n       if (i & 1) { n         int x = a[b[i][j]][0], y = a[b[i][j]][i / 2 + 1]; n         while (!vis[x]) { n           vis[x] = vis[y] = 1; n           ans[x] = y; n           x = p[x]; n           ans[y] = x; n           y = p[y]; n         } n       } else { n         int x = a[b[i][j]][0], y = a[b[i][j + 1]][0]; n         while (!vis[x] && !vis[y]) { n           vis[x] = vis[y] = 1; n           ans[x] = y; n           x = p[x]; n           ans[y] = x; n           y = p[y]; n         } n         j++; n       } n     } n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n const int MAXM = 20010; n const int MAXNODE = 16 * MAXN; n const int LOGMAXN = 50; n const int INF = 0x3f3f3f3f; n const double eps = 1e-9; n int N, M; n int b[MAXM], z[MAXM], x[3 * (MAXN + MAXM)]; n struct Tree { n   int x, h; n   double l, r; n } a[MAXN]; n struct SegmentTree { n   double mul[MAXNODE]; n   void build(int o, int L, int R) { n     mul[o] = 1; n     if (L >= R) return; n     int mid = L + (R - L) / 2; n     build(o << 1, L, mid); n     build(o << 1 | 1, mid + 1, R); n   } n   void pushdown(int o) { n     mul[o << 1] *= mul[o]; n     mul[o << 1 | 1] *= mul[o]; n     mul[o] = 1; n   } n   void update(int o, int L, int R, int ql, int qr, double p) { n     if (ql <= L && qr >= R) { n       mul[o] *= p; n       return; n     } n     pushdown(o); n     int mid = L + (R - L) / 2; n     if (ql <= mid) update(o << 1, L, mid, ql, qr, p); n     if (qr > mid) update(o << 1 | 1, mid + 1, R, ql, qr, p); n   } n   double query(int o, int L, int R, int pos) { n     if (L >= R) return mul[o]; n     pushdown(o); n     int mid = L + (R - L) / 2; n     double ans; n     if (pos <= mid) n       ans = query(o << 1, L, mid, pos); n     else n       ans = query(o << 1 | 1, mid + 1, R, pos); n     return ans; n   } n } tree; n int main() { n   while (scanf( %d%d , &N, &M) != EOF) { n     int k = 0; n     for (int i = 0; i < N; i++) { n       scanf( %d%d%lf%lf , &a[i].x, &a[i].h, &a[i].l, &a[i].r); n       a[i].l /= 100; n       a[i].r /= 100; n       x[k++] = a[i].x; n       x[k++] = a[i].x - a[i].h; n       x[k++] = a[i].x + a[i].h; n     } n     for (int i = 0; i < M; i++) { n       scanf( %d%d , &b[i], &z[i]); n       x[k++] = b[i]; n     } n     sort(x, x + k); n     k = unique(x, x + k) - x; n     tree.build(1, 1, k); n     for (int i = 0; i < N; i++) { n       int qm = lower_bound(x, x + k, a[i].x) - x + 1; n       int ql = lower_bound(x, x + k, a[i].x - a[i].h) - x + 1, n           qr = lower_bound(x, x + k, a[i].x + a[i].h) - x + 1; n       tree.update(1, 1, k, ql, qm - 1, (1 - a[i].l)); n       tree.update(1, 1, k, qm + 1, qr, (1 - a[i].r)); n     } n     double ans = 0; n     for (int i = 0; i < M; i++) { n       int pos = lower_bound(x, x + k, b[i]) - x + 1; n       ans += z[i] * tree.query(1, 1, k, pos); n     } n     printf( %.10lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 502; n void sw(float* x) { n   float tmp = *x; n   *x = *(x + 2); n   *(x + 2) = *(x + 1); n   *(x + 1) = tmp; n } n int main() { n   int _t; n   scanf( %d , &_t); n   while (_t--) { n     static float arr[N]; n     static int anss[N * N]; n     static int tm[N]; n     memset(tm, 0, sizeof(tm)); n     int n; n     scanf( %d , &n); n     int f2 = -1; n     for (int i = 1; i <= n; i++) { n       int a; n       scanf( %d , &a); n       tm[a]++; n       if (tm[a] == 2) f2 = a; n       arr[i] = a + 0.0001 * tm[a]; n     } n     int nx = 0; n     for (int i = 1; i < n; i++) n       for (int j = i + 1; j <= n; j++) n         if (arr[i] > arr[j]) nx++; n     if (nx & 1) { n       if (f2 != -1) { n         int f21 = -1, f22 = -1; n         for (int i = 1; i <= n; i++) n           if (((int)arr[i]) == f2) { n             if (f21 == -1) n               f21 = i; n             else if (f22 == -1) n               f22 = i; n             else n               break; n           } n         std::swap(arr[f21], arr[f22]); n         nx++; n       } else { n         printf( -1 n ); n         continue; n       } n     } n     int ans = 0; n     while (nx) { n       for (int i = 1; i <= n - 2; i++) { n         if (arr[i] < arr[i + 2] && arr[i + 2] < arr[i + 1] || n             arr[i] > arr[i + 2] && arr[i + 2] > arr[i + 1]) { n           sw(arr + i); n           anss[++ans] = i; n         } n         if (arr[i] < arr[i + 1] && arr[i] > arr[i + 2] || n             arr[i] > arr[i + 1] && arr[i + 1] > arr[i + 2]) { n           sw(arr + i); n           nx -= 2; n           anss[++ans] = i; n         } n       } n     } n     printf( %d n , ans); n     for (int i = 1; i <= ans; i++) printf( %d  , anss[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string test; n   cin >> test; n   for (int i = 0; i < m; i++) { n     int l, r; n     char c1, c2; n     cin >> l >> r >> c1 >> c2; n     l -= 1; n     r -= 1; n     for (; l <= r; l++) { n       if (test[l] == c1) { n         test[l] = c2; n       } n     } n   } n   cout << test << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n const long long mod = 1e9 + 7; n char s[maxn]; n int num[55]; n long long dp[maxn]; n long long tmp[maxn]; n long long quesnum[55][55]; n long long ques[55][55]; n long long inv[maxn], fac[maxn], invf[maxn]; n void init(int hf) { n   inv[1] = 1; n   for (int i = 2; i < maxn; i++) n     inv[i] = ((mod - mod / i) * inv[mod % i]) % mod; n   fac[0] = 1; n   for (int i = 1; i < maxn; i++) fac[i] = (fac[i - 1] * (long long)i) % mod; n   invf[0] = 1; n   for (int i = 1; i < maxn; i++) invf[i] = (invf[i - 1] * inv[i]) % mod; n   memset(dp, 0, sizeof(dp)); n   memset(ques, -1, sizeof(ques)); n   dp[0] = 1; n   for (int i = 0; i < 55; i++) { n     if (!num[i]) continue; n     for (int j = hf; j >= num[i]; j--) { n       dp[j] = (dp[j] + dp[j - num[i]]) % mod; n     } n   } n   for (int i = 0; i < 52; i++) { n     for (int j = i; j < 52; j++) { n       for (int k = 0; k <= hf; k++) tmp[k] = dp[k]; n       for (int k = num[i]; k <= hf; k++) n         tmp[k] = (tmp[k] - tmp[k - num[i]] + mod) % mod; n       if (i != j) n         for (int k = num[j]; k <= hf; k++) n           tmp[k] = (tmp[k] - tmp[k - num[j]] + mod) % mod; n       quesnum[i][j] = quesnum[j][i] = tmp[hf]; n     } n   } n } n int main() { n   scanf( %s , s); n   memset(num, 0, sizeof(num)); n   int len = strlen(s); n   int hf = len / 2; n   for (int i = 0; i < len; i++) { n     if (s[i] >=  a  && s[i] <=  z ) n       num[s[i] -  a ]++; n     else n       num[s[i] -  A  + 26]++; n   } n   init(hf); n   int q; n   scanf( %d , &q); n   int x, y; n   while (q--) { n     scanf( %d%d , &x, &y); n     x--; n     y--; n     int idx, idy; n     if (s[x] >=  a  && s[x] <=  z ) n       idx = s[x] -  a ; n     else n       idx = s[x] -  A  + 26; n     if (s[y] >=  a  && s[y] <=  z ) n       idy = s[y] -  a ; n     else n       idy = s[y] -  A  + 26; n     if (ques[idx][idy] != -1) { n       printf( %I64d n , ques[idx][idy]); n       continue; n     } n     long long ans; n     if (idx == idy) { n       if (num[idx] > hf) { n         printf( 0 n ); n         ques[idx][idx] = 0; n         continue; n       } n       long long cc = n           ((fac[hf] * invf[num[idx]]) % mod * invf[hf - num[idx]]) % mod; n       ans = ((cc * fac[hf - num[idx]]) % mod * fac[hf]) % mod; n     } else { n       if (num[idx] + num[idy] > hf) { n         ques[idx][idy] = ques[idy][idx] = 0; n         printf( 0 n ); n         continue; n       } n       int numx = num[idx], numy = num[idy]; n       int num_hfx = hf - numx; n       long long cc1 = ((fac[hf] * invf[numx]) % mod * invf[hf - numx]) % mod; n       long long cc2 = n           ((fac[num_hfx] * invf[numy]) % mod * invf[num_hfx - numy]) % mod; n       ans = (((cc1 * cc2) % mod * fac[hf]) % mod * fac[hf - numx - numy]) % mod; n     } n     for (int i = 0; i < 55; i++) { n       if (i == idx || i == idy) continue; n       ans = (ans * invf[num[i]]) % mod; n     } n     ans = (ans * quesnum[idx][idy]) % mod; n     ans = (ans * 2) % mod; n     ques[idx][idy] = ques[idy][idx] = ans; n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500000 + 10; n int a[MAXN]; n int n, m, ans, tp, tot; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   ans = n; n   sort(a + 1, a + 1 + n); n   for (int i = n / 2; i >= 1; --i) n     if (a[ans] >= a[i] * 2) --ans; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n char a[500001 + 10], b[500001 + 10], s[(500001 << 1) + 10]; n int n, m; n struct BIT { n   long long c[500001 << 1]; n   long long query(int x) { n     if (x <= 0) return 0; n     long long ans = 0; n     for (int i = x; i; i -= (i & -i)) ans += c[i]; n     return ans; n   } n   void add(int x, long long p) { n     if (x <= 0) x = 1; n     for (int i = x; i <= 1000000; i += (i & -i)) c[i] += p; n   } n } b1, b2; n int za[500001 * 3], zb[500001 * 3]; n char mod[500001 * 3]; n void zfunc(char *s, int n, int *z) { n   int l = 0, r = 0; n   z[1] = n; n   int pt; n   for (int i = 2; i <= n; ++i) { n     if (r < i) { n       for (pt = i; pt <= n && s[pt] == s[pt - i + 1]; ++pt) n         ; n       --pt; n       z[i] = pt - i + 1; n       if (z[i]) l = i, r = pt; n     } else { n       int b = r - i + 1, i_ = i - l + 1; n       if (z[i_] < b) n         z[i] = z[i_]; n       else { n         for (pt = r + 1; pt <= n && s[pt] == s[pt - i + 1]; ++pt) n           ; n         --pt; n         z[i] = pt - i + 1; n         l = i, r = pt; n       } n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   scanf( %s%s%s , a + 1, b + 1, s + 1); n   memcpy(mod + 1, s + 1, m - 1); n   mod[m] =  $ ; n   memcpy(mod + m + 1, a + 1, n); n   zfunc(mod, n + m, za); n   for (int i = 1; i <= n; ++i) za[i] = za[i + m]; n   reverse(s + 1, s + 1 + m); n   reverse(b + 1, b + 1 + n); n   memcpy(mod + 1, s + 1, m - 1); n   mod[m] =  $ ; n   memcpy(mod + m + 1, b + 1, n); n   zfunc(mod, n + m, zb); n   for (int i = 1; i <= n; ++i) zb[i] = zb[i + m]; n   reverse(zb + 1, zb + 1 + n); n   int cur = 1; n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     while (cur <= min(i + m - 2, n)) n       b1.add(m - 1 - zb[cur], 1), b2.add(m - 1 - zb[cur], zb[cur]), ++cur; n     ans += 1ll * b1.query(za[i]) * (za[i] - m + 1) + b2.query(za[i]); n     b1.add(m - 1 - zb[i], -1), b2.add(m - 1 - zb[i], -zb[i]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dec(string s, int n) { n   long long int r = 0, p = 0, l = s.size() - 1, tmp; n   while (l >= 0) { n     if (isalpha(s[l])) n       tmp = s[l] - 55; n     else n       tmp = s[l] - 48; n     int prod = 1; n     for (int k = 0; k < p; k++) prod *= n; n     r += prod * tmp; n     p++; n     l--; n   } n   return r; n } n int main() { n   string s, a, b; n   int mx = 0; n   cin >> s; n   int i = 0; n   while (s[i] !=  : ) { n     a.push_back(s[i]); n     if (mx < s[i]) mx = s[i]; n     i++; n   } n   i++; n   while (s[i] !=   0 ) { n     b.push_back(s[i]); n     if (mx < s[i]) mx = s[i]; n     i++; n   } n   int bg; n   bg = mx < 65 ? mx - 48 : mx - 55; n   int beg = max(2, bg + 1); n   if (dec(a, beg) >= 24 || dec(b, beg) >= 60) { n     cout << 0 << endl; n     return 0; n   } n   if (dec(a, 100) < 24 && dec(b, 100) < 60) { n     cout << -1 << endl; n     return 0; n   } n   for (i = beg;; i++) { n     if (dec(a, i) < 24 && dec(b, i) < 60) { n       cout << i <<    ; n     } else n       break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)2e9; n const long long INFL = (long long)9e18; n const int MAXINT = ((~0) ^ (1 << 31)); n const long long MAXLL = ((~0) ^ ((long long)1 << 63)); n template <class T> n inline T pow2(T a) { n   return a * a; n } n template <class T> n inline bool mineq(T& a, T b) { n   return (a > b) ? (a = b, true) : false; n } n template <class T> n inline bool maxeq(T& a, T b) { n   return (a < b) ? (a = b, true) : false; n } n const int maxn = 2e5; n long long n, k, x, cnt; n pair<long long, long long> p[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) p[i] = {1e10, 0}; n   for (int i = 1; i <= k; i++) { n     cin >> x; n     p[x].first = min(p[x].first, (long long)i); n     p[x].second = max(p[x].second, (long long)i); n   } n   if (p[1].first > p[2].second && n != 1) { n     cnt++; n   } n   if (p[n].first > p[n - 1].second && n != 1) { n     cnt++; n   } n   if (p[1].second == 0) cnt++; n   if (p[n].second == 0 && n != 1) cnt++; n   for (int i = 2; i <= n - 1; i++) { n     if (p[i].second == 0) cnt++; n     if (p[i].first > p[i + 1].second) { n       cnt++; n     } n     if (p[i].first > p[i - 1].second) { n       cnt++; n     } n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ti[2005], co[2005], dp[2005][2005], i, j; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> ti[i] >> co[i]; n     ti[i]++; n   } n   for (i = 0; i <= n; i++) { n     for (j = 1; j <= n; j++) dp[i][j] = 1e16; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       dp[i][j] = min(dp[i - 1][j], dp[i - 1][max(0ll, j - ti[i])] + co[i]); n     } n   } n   cout << dp[n][n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pen { n   int color; n   int diameter; n }; n int n, m, u, v; n pen marker[100000], cap[100000]; n struct match { n   vector<pen> marker; n   vector<pen> cap; n }; n match singleMatch[1000 + 1]; n match doubleMatch[1000 + 1][1000 + 1]; n int main(int argc, char *argv[]) { n   u = 0, v = 0; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     int color, diameter; n     cin >> color >> diameter; n     marker[i].color = color; n     marker[i].diameter = diameter; n     singleMatch[diameter].marker.push_back(marker[i]); n     doubleMatch[diameter][color].marker.push_back(marker[i]); n   } n   for (int i = 0; i < m; i++) { n     int color, diameter; n     cin >> color >> diameter; n     cap[i].color = color; n     cap[i].diameter = diameter; n     singleMatch[diameter].cap.push_back(marker[i]); n     doubleMatch[diameter][color].cap.push_back(cap[i]); n   } n   for (int i = 1; i < 1000 + 1; i++) { n     vector<pen> tempM = singleMatch[i].marker; n     vector<pen> tempC = singleMatch[i].cap; n     int temp1 = (int)tempM.size(); n     int temp2 = (int)tempC.size(); n     if (temp1 > 0 && temp2 > 0) u += min(temp1, temp2); n   } n   for (int i = 1; i < 1000 + 1; i++) n     for (int j = 1; j < 1000 + 1; j++) { n       vector<pen> tempM = doubleMatch[i][j].marker; n       vector<pen> tempC = doubleMatch[i][j].cap; n       int temp1 = (int)tempM.size(); n       int temp2 = (int)tempC.size(); n       if (temp1 > 0 && temp2 > 0) v += min(temp1, temp2); n     } n   cout << u <<     << v << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 109; n const int mod = 1000000007; n long long cnt[inf]; n long long dp[inf]; n long long temp[inf][inf]; n long long sol[inf][inf]; n long long bs[inf][inf]; n int main() { n   int n, x, a; n   scanf( %d%d , &n, &x); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a); n     cnt[a]++; n   } n   dp[0] = 1; n   int all = 0; n   for (int i = 1; i <= 100; ++i) { n     for (int j = 1; j <= i; ++j) dp[i] = (dp[i] + dp[i - j] * cnt[j]) % mod; n     all = (all + dp[i]) % mod; n   } n   dp[0] = all; n   for (int i = 1; i <= 50; ++i) swap(dp[i], dp[101 - i]); n   for (int i = 1; i <= 100; ++i) sol[i][i] = 1, bs[0][i] = bs[1][i] = cnt[i]; n   sol[0][0] = bs[0][0] = 1; n   for (int i = 2; i <= 100; ++i) bs[i][i - 1] = 1; n   if (x <= 100) { n     int ad = 0; n     for (int i = 100; i > 0 && x > 0; --i, --x) ad = (ad + dp[i]) % mod; n     printf( %d n , ad + 1); n     return 0; n   } n   x -= 100; n   while (x) { n     if (x & 1) { n       for (int i = 0; i <= 100; ++i) n         for (int j = 0; j <= 100; ++j) { n           temp[i][j] = 0; n           for (int u = 0; u <= 100; ++u) n             temp[i][j] = (temp[i][j] + sol[i][u] * bs[u][j]) % mod; n         } n       for (int i = 0; i <= 100; ++i) n         for (int j = 0; j <= 100; ++j) sol[i][j] = temp[i][j]; n     } n     x = x >> 1; n     for (int i = 0; i <= 100; ++i) n       for (int j = 0; j <= 100; ++j) { n         temp[i][j] = 0; n         for (int u = 0; u <= 100; ++u) n           temp[i][j] = (temp[i][j] + bs[i][u] * bs[u][j]) % mod; n       } n     for (int i = 0; i <= 100; ++i) n       for (int j = 0; j <= 100; ++j) bs[i][j] = temp[i][j]; n   } n   int ans = 0; n   for (int i = 0; i <= 100; ++i) ans = (ans + sol[0][i] * dp[i]) % mod; n   printf( %d n , (ans + 1) % mod); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 4e5 + 10; n const int M = 22; n class SegmentTree { n  public: n   int ql, qr, qx; n   int xorv[N]; n   int sumv[N]; n   void init() { n     memset((xorv), (0), sizeof(xorv)); n     memset((sumv), (0), sizeof(sumv)); n   } n   void set(int ql, int qr, int qx) { n     this->ql = ql; n     this->qr = qr; n     this->qx = qx; n   } n   void pushdown(int o, int len) { n     if (xorv[o]) { n       xorv[o << 1] ^= 1; n       xorv[o << 1 | 1] ^= 1; n       sumv[o << 1] = (len - len / 2) - sumv[o << 1]; n       sumv[o << 1 | 1] = len / 2 - sumv[o << 1 | 1]; n       xorv[o] = 0; n     } n   } n   void maintain(int o) { sumv[o] = sumv[o << 1] + sumv[o << 1 | 1]; } n   int query(int l, int r, int o = 1) { n     if (ql <= l && qr >= r) { n       return sumv[o]; n     } n     int mid = (l + r) >> 1; n     pushdown(o, r - l + 1); n     int ans = 0; n     if (ql <= mid) ans += query(l, mid, o << 1); n     if (qr > mid) ans += query(mid + 1, r, o << 1 | 1); n     return ans; n   } n   void update(int l, int r, int o = 1) { n     if (ql <= l && qr >= r) { n       xorv[o] ^= 1; n       sumv[o] = r - l + 1 - sumv[o]; n       return; n     } n     int mid = (l + r) >> 1; n     pushdown(o, r - l + 1); n     if (ql <= mid) update(l, mid, o << 1); n     if (qr > mid) update(mid + 1, r, o << 1 | 1); n     maintain(o); n   } n } st[M]; n int main() { n   for (int i = 0; i < M; i++) st[i].init(); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     for (int j = 0; j < M; j++) { n       if (x >> j & 1) st[j].set(i, i, 1), st[j].update(1, n); n     } n   } n   int q; n   cin >> q; n   while (q--) { n     int op; n     cin >> op; n     if (op == 1) { n       int l, r; n       cin >> l >> r; n       long long ans = 0; n       for (int i = 0; i < M; i++) { n         st[i].set(l, r, 0); n         ans += (1LL << i) * st[i].query(1, n); n       } n       cout << ans <<   n ; n     } else { n       int l, r, x; n       cin >> l >> r >> x; n       for (int i = 0; i < M; i++) { n         if (x >> i & 1) st[i].set(l, r, 1), st[i].update(1, n); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct data { n   int s, m, r, t; n   int operator<(const data &b) const { return t < b.t; } n   void get() { n     scanf( %d , &s); n     scanf( %d , &m); n     scanf( %d , &r); n     t = r ? (m / r + (m % r > 0)) : 2e9; n   } n   int calc(int o) { return min(1ll * m, s + 1ll * r * o); } n } a[20][100005]; n int T[100005 << 2]; n long long R[20][100005], sum[20][100005], ans; n int n, Q, t, ql, qr; n void Build(int x, int l, int r, int d) { n   T[x] = -2; n   if (l == r) { n     a[d][l].get(); n     R[d][l] = R[d][l - 1] + a[d][l].r; n     sum[d][l] = sum[d][l - 1] + a[d][l].m; n     return; n   } n   int mid = (l + r) >> 1; n   Build(x << 1, l, mid, d + 1); n   Build(x << 1 | 1, mid + 1, r, d + 1); n   for (int i = l; i <= r; i++) a[d][i] = a[d + 1][i]; n   sort(a[d] + l, a[d] + r + 1); n   for (int i = l; i <= r; i++) R[d][i] = R[d][i - 1] + a[d][i].r; n   for (int i = l; i <= r; i++) sum[d][i] = sum[d][i - 1] + a[d][i].m; n } n void Find(int x, int l, int r, int d) { n   if (ql <= l && r <= qr && T[x] != -1) { n     if (T[x] == -2) { n       for (int i = l; i <= r; i++) ans += a[d][i].calc(t); n       T[x] = t; n       return; n     } n     data tmp = (data){0, 0, 0, t - T[x]}; n     int id = upper_bound(a[d] + l, a[d] + r + 1, tmp) - a[d]; n     ans += 1ll * (R[d][r] - R[d][id - 1]) * (t - T[x]) + sum[d][id - 1] - n            sum[d][l - 1]; n     T[x] = t; n     return; n   } n   if (T[x] >= 0) T[x << 1] = T[x << 1 | 1] = T[x]; n   int mid = (l + r) >> 1; n   if (ql <= mid) Find(x << 1, l, mid, d + 1); n   if (qr > mid) Find(x << 1 | 1, mid + 1, r, d + 1); n   T[x] = (T[x << 1] == T[x << 1 | 1]) ? T[x << 1] : -1; n } n int main() { n   scanf( %d , &n); n   Build(1, 1, n, 0); n   scanf( %d , &Q); n   while (Q--) { n     scanf( %d , &t); n     scanf( %d , &ql); n     scanf( %d , &qr); n     ans = 0; n     Find(1, 1, n, 0); n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int v; n   Edge *link; n } edge[400010 * 2], *adj[200010], *tree[200010 * 2]; n int totE, con, root, currT, top, top1; n int totCut, totBi; n int dep[200010], deps[200010], num[200010], dfn[200010], lowlink[200010], n     cut[200010], bi[200010], color[200010]; n int temp[200010], st1[200010], st2[200010], st3[200010], st4[200010], n     f[200010 * 2]; n int used[200010 * 2], odd[200010 * 2]; n int fa[200010][20]; n int LOG[200010]; n void addEdge(int u, int v) { n   Edge *p = &edge[totE++]; n   p->v = v; n   p->link = adj[u]; n   adj[u] = p; n } n void addTree(int u, int v) { n   Edge *p = &edge[totE++]; n   p->v = v; n   p->link = tree[u]; n   tree[u] = p; n } n void dfs(int u, int pre) { n   dep[u] = dep[pre] + 1; n   dfn[u] = lowlink[u] = ++currT; n   num[u] = con; n   Edge *p = adj[u]; n   int cnt = 0; n   bool flag = false; n   while (p) { n     if (!dfn[p->v]) { n       ++cnt; n       dfs(p->v, u); n       lowlink[u] = min(lowlink[u], lowlink[p->v]); n       if (lowlink[p->v] >= dfn[u]) flag = true; n     } else if (p->v != pre) n       lowlink[u] = min(lowlink[u], dfn[p->v]); n     p = p->link; n   } n   if (flag && (u != root || cnt > 1)) cut[u] = ++totCut; n } n bool dfs2(int u) { n   Edge *p = adj[u]; n   while (p) { n     if (bi[p->v] == totBi) { n       if (color[p->v] == -1) { n         color[p->v] = !color[u]; n         if (!dfs2(p->v)) return false; n       } else if (color[p->v] == color[u]) n         return false; n     } n     p = p->link; n   } n   return true; n } n void solve() { n   ++totBi; n   int cnt = 0; n   for (int i = 0; i < top1; ++i) { n     int u = st3[i], v = st4[i]; n     bi[u] = totBi; n     bi[v] = totBi; n     color[u] = color[v] = -1; n     if (cut[u]) temp[cnt++] = cut[u]; n     if (cut[v]) temp[cnt++] = cut[v]; n   } n   sort(temp, temp + cnt); n   for (int i = 0; i < cnt; ++i) n     if (i == 0 || temp[i] != temp[i - 1]) { n       addTree(temp[i], totCut + totBi); n       addTree(totCut + totBi, temp[i]); n     } n   color[st3[0]] = 0; n   odd[totBi] = !dfs2(st3[0]); n } n void dfs1(int u, int pre) { n   dfn[u] = lowlink[u] = ++currT; n   Edge *p = adj[u]; n   while (p) { n     if (!dfn[p->v]) { n       st1[top] = u; n       st2[top++] = p->v; n       dfs1(p->v, u); n       lowlink[u] = min(lowlink[u], lowlink[p->v]); n       if (lowlink[p->v] >= dfn[u]) { n         top1 = 0; n         while (true) { n           --top; n           st3[top1] = st1[top]; n           st4[top1++] = st2[top]; n           if (st1[top] == u && st2[top] == p->v) break; n         } n         solve(); n       } n     } else { n       if (dfn[u] > dfn[p->v] && p->v != pre) { n         st1[top] = u; n         st2[top++] = p->v; n       } n       if (p->v != pre) lowlink[u] = min(lowlink[u], dfn[p->v]); n     } n     p = p->link; n   } n } n void dfs3(int u, int pre) { n   deps[u] = deps[pre] + 1; n   used[u] = true; n   fa[u][0] = pre; n   if (u <= totCut) n     f[u] = 0; n   else n     f[u] = odd[u - totCut]; n   f[u] += f[pre]; n   Edge *p = tree[u]; n   while (p) { n     if (!used[p->v]) dfs3(p->v, u); n     p = p->link; n   } n } n int LCA(int u, int v) { n   if (deps[u] < deps[v]) swap(u, v); n   int diff = deps[u] - deps[v]; n   for (int i = 0; diff; ++i, diff >>= 1) n     if (diff & 1) u = fa[u][i]; n   if (u == v) return u; n   int t = 19; n   for (int i = t; i >= 0; --i) { n     if (fa[u][i] != fa[v][i]) { n       u = fa[u][i]; n       v = fa[v][i]; n     } n   } n   return fa[u][0]; n } n int main() { n   LOG[1] = 0; n   for (int i = 2; i < 200010; ++i) LOG[i] = (LOG[i] >> 1) + 1; n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     addEdge(u, v); n     addEdge(v, u); n   } n   dep[0] = -1; n   for (int i = 1; i <= n; ++i) n     if (!dfn[i]) { n       root = i; n       dfs(i, 0); n       ++con; n     } n   currT = 0; n   memset(dfn, 0, sizeof(dfn)); n   for (int i = 1; i <= n; ++i) n     if (!dfn[i]) dfs1(i, 0); n   for (int i = 1; i <= totCut + totBi; ++i) n     if (!used[i]) dfs3(i, 0); n   for (int l = 1; (1 << l) <= totCut + totBi; ++l) n     for (int i = 1; i <= totCut + totBi; ++i) n       if (fa[i][l - 1] != 0) fa[i][l] = fa[fa[i][l - 1]][l - 1]; n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (u == v || num[u] != num[v]) n       puts( No ); n     else { n       int x, y; n       if (cut[u]) n         x = cut[u]; n       else n         x = totCut + bi[u]; n       if (cut[v]) n         y = cut[v]; n       else n         y = totCut + bi[v]; n       int lca = LCA(x, y); n       int sum = f[x] + f[y] - 2 * f[fa[lca][0]]; n       if (sum > 0) n         puts( Yes ); n       else if ((dep[u] + dep[v]) % 2 == 0) n         puts( No ); n       else n         puts( Yes ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); n } n using namespace std; n int t; n int pri[4001000], pcnt; n bool depri[35001000]; n inline void init() { n   depri[1] = true; n   const int up = 3.5e7; n   for (int i = 2; i <= up; ++i) { n     if (!depri[i]) pri[++pcnt] = i; n     for (int j = 1; j <= pcnt && i * pri[j] <= up; ++j) { n       depri[i * pri[j]] = true; n       if (i % pri[j] == 0) break; n     } n   } n } n struct TC { n   int id; n   long long n, k; n   inline bool operator<(const TC &a) const { return k < a.k; } n } tc[10100]; n bool ans[10100]; n long long p[100]; n int ptot; n inline void Div(long long x) { n   ptot = 0; n   for (int i = 1; 1ll * pri[i] * pri[i] <= x; ++i) { n     if (x % pri[i] == 0) { n       p[++ptot] = pri[i]; n       while (x % pri[i] == 0) x /= pri[i]; n     } n   } n   if (x != 1) p[++ptot] = x; n } n struct node { n   int cur; n   long long val; n   inline bool operator<(const node &a) const { return val > a.val; } n }; n priority_queue<node> q; n long long dis[100100]; n bool vis[100100]; n inline void Update(long long k) { n   Div(k); n   if (ptot <= 2) return; n   memset(dis, 0x3f, sizeof(dis)); n   memset(vis, 0, sizeof(vis)); n   dis[0] = 0; n   q.push((node){0, 0}); n   while (!q.empty()) { n     int cur = (q.top()).cur; n     q.pop(); n     if (vis[cur]) continue; n     vis[cur] = true; n     for (int i = 2; i <= ptot; ++i) { n       int to = (cur + p[i]) % p[1]; n       long long val = (cur + p[i]) / p[1]; n       if (dis[to] <= dis[cur] + val) continue; n       dis[to] = dis[cur] + val; n       q.push((node){to, dis[to]}); n     } n   } n } n inline long long quickmul(long long x, long long k, long long P) { n   x = (x % P + P) % P; n   k = (k % P + P) % P; n   long long res = 0; n   while (k) { n     if (k & 1) res = (res + x) % P; n     x = (x + x) % P; n     k >>= 1; n   } n   return res; n } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     x = 1; n     y = 0; n     return a; n   } n   long long g = exgcd(b, a % b, x, y); n   long long tmp = x; n   x = y; n   y = tmp - a / b * y; n   return g; n } n inline bool get_ans(long long n) { n   if (!ptot) return false; n   if (ptot == 1) return n % p[1] == 0; n   if (ptot == 2) { n     long long x = 0, y = 1; n     exgcd(p[1], p[2], x, y); n     x = quickmul(x, n, p[2]); n     if (1.0 * x * p[1] > 2e18) return false; n     return n >= x * p[1]; n   } n   if (1.0 * dis[n % p[1]] * p[1] > 2e18) return false; n   return dis[n % p[1]] * p[1] + n % p[1] <= n; n } n signed main() { n   init(); n   read(t); n   for (int i = 1; i <= t; ++i) { n     read(tc[i].n), read(tc[i].k); n     tc[i].id = i; n   } n   sort(tc + 1, tc + 1 + t); n   for (int i = 1; i <= t; ++i) { n     if (tc[i].k != tc[i - 1].k) Update(tc[i].k); n     ans[tc[i].id] = get_ans(tc[i].n); n   } n   for (int i = 1; i <= t; ++i) puts(ans[i] ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n std::vector<long long> factorial; n void precompute_factorial(int ma) { n   factorial.push_back(1); n   for (int i = 1; i <= ma; i++) n     factorial.push_back((factorial.back() * i) % MOD); n } n long long power(long long base, int e) { n   long long result = 1; n   while (e) { n     if (e & 1) result = (result * base) % MOD; n     base = (base * base) % MOD; n     e >>= 1; n   } n   return result; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   precompute_factorial(n + 1); n   vector<long long> inv_factorial(n + 1, 1); n   for (int i = 2; i < n + 1; i++) n     inv_factorial[i] = power(factorial[i], MOD - 2); n   vector<long long> inv(n + 2); n   for (int i = 1; i < n + 2; i++) inv[i] = power(i, MOD - 2); n   if (k == 0) { n     cout << ((factorial[n] - factorial[n - 1] + MOD) % MOD) << endl; n     return 0; n   } n   vector<long long> dp2(n + 1, 1); n   vector<long long> xxx(n + 1, 1); n   long long sum = 0; n   for (int i = 1; i < n + 1; i++) { n     sum = (sum + xxx[i - 1]) % MOD; n     if (i - 1 - k >= 0) { n       sum = (sum + MOD - xxx[i - 1 - k]) % MOD; n     } n     dp2[i] = (sum * factorial[i - 1]) % MOD; n     xxx[i] = (dp2[i] * inv_factorial[i]) % MOD; n   } n   vector<long long> dp(n + 1, 0); n   long long last = 0; n   long long total = 0; n   for (int i = k + 2; i <= n; i++) { n     long long y = i - k - 2; n     long long tt = dp2[y]; n     tt = (tt + MOD) % MOD; n     tt = (tt * inv_factorial[y]) % MOD; n     tt = (tt * inv[i - 1]) % MOD; n     last += tt; n     last %= MOD; n     total += last; n     total %= MOD; n     dp[i] = (factorial[i - 1] * total) % MOD; n   } n   cout << dp.back() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n const int M = 505; n const int MOD = int(1e9) + 7; n const int INF = 0x3f3f3f3f; n const double EPS = 1e-9; n const double PI = acos(-1.0); n const int dx[] = {-1, 1, 0, 0}; n const int dy[] = {0, 0, -1, 1}; n template <class T> n inline T Min(T a, T b) { n   return a < b ? a : b; n } n template <class T> n inline T Max(T a, T b) { n   return a > b ? a : b; n } n template <class T> n inline T Min(T a, T b, T c) { n   return min(min(a, b), c); n } n template <class T> n inline T Max(T a, T b, T c) { n   return max(max(a, b), c); n } n template <class T> n inline T sqr(T a) { n   return a * a; n } n template <class T> n inline T cub(T a) { n   return a * a * a; n } n template <class T> n inline T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <class T> n inline T lcm(T a, T b) { n   return a * b / gcd(a, b); n } n int main() { n   int a, b; n   while (scanf( %d%d , &a, &b) != EOF) { n     int i, tot = (a * (a - 1)) / 2; n     if (tot <= b) { n       printf( no solution n ); n     } else { n       for (i = 0; i < a; i++) { n         printf( %d %d n , 0, i); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int A[600006]; n int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } n map<pair<int, int>, int> M; n int L[600006], R[600006], lst[600006], cn; n struct que { n   int l, r, idx; n } Q[600006]; n vector<int> bc[600006]; n int T[600006]; n void ad(int x, int c) { n   while (x <= cn) T[x] += c, x += (x & -x); n } n int sum(int x) { n   int ret = 0; n   while (x > 0) ret += T[x], x -= (x & -x); n   return ret; n } n int ans[600006]; n void solve() { n   cin >> n; n   for (int i = (1), iend = (n); i <= iend; ++i) { n     int k; n     scanf( %d , &k); n     L[i] = cn + 1; n     int px, py, tx, ty, x, y, sx, sy; n     scanf( %d%d , &px, &py); n     sx = px, sy = py; n     for (int j = (1), jend = (k); j <= jend; ++j) { n       if (j != k) n         scanf( %d%d , &x, &y); n       else n         x = sx, y = sy; n       tx = x, ty = y; n       x -= px, y -= py; n       int g = gcd(abs(x), abs(y)); n       x /= g, y /= g; n       ++cn; n       lst[cn] = M[make_pair(x, y)]; n       bc[lst[cn]].push_back(cn); n       M[make_pair(x, y)] = cn; n       px = tx, py = ty; n     } n     R[i] = cn; n   } n   cin >> q; n   for (int i = (1), iend = (q); i <= iend; ++i) { n     int l, r; n     scanf( %d%d , &l, &r); n     l = L[l], r = R[r]; n     Q[i] = (que){l, r, i}; n   } n   sort(Q + 1, Q + 1 + q, [](que a, que b) { return a.l < b.l; }); n   int cur = 1; n   for (int i = (0), iend = (cn); i <= iend; ++i) { n     while (cur <= q && Q[cur].l == i) { n       ans[Q[cur].idx] = sum(Q[cur].r) - sum(Q[cur].l - 1); n       ++cur; n     } n     for (int x : bc[i]) ad(x, 1); n   } n   for (int i = (1), iend = (q); i <= iend; ++i) printf( %d n , ans[i]); n } n signed main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n const long long remi = 1000000007; n const long long remi2 = 998244353; n const long long inf = 1e18 + 1e17 + 1e16 + 1e15 + 1e14; n long long Pow(long long x, long long y, long long m) { n   if (y == 0) return 1; n   long long p = Pow(x, y / 2, m) % m; n   p = (p * p) % m; n   return (y % 2 == 0) ? p : (x * p) % m; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long i, j, k; n   long long n, m; n   cin >> n >> m; n   vector<long long> v(m); n   for (i = 0; i < m; i++) { n     cin >> v[i]; n   } n   sort(v.begin(), v.end()); n   bool ans = true; n   if (m == 0) { n     cout <<  YES ; n     exit(0); n   } n   if (v[0] == 1 or v[m - 1] == n) ans = false; n   for (i = 2; i < m; i++) { n     long long s0 = v[i - 2]; n     long long s1 = v[i - 1]; n     long long s2 = v[i]; n     if ((s0 == s2 - 2) and (s1 == s2 - 1)) ans = false; n   } n   if (ans) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n  n int main() n { n  ios_base::sync_with_stdio(false), cin.tie(0); n  int t; n  cin >> t; n  while (t--) { n   map<int, int>L, R; n   int bal = 0; n   string s; n   cin >> s; n   for (char &c : s) { n    if (c ==  1 ) { n     R[bal]++; n     bal++; n    } else { n     L[bal]++; n     bal--; n    } n   } n  n   int cur = 0; n   while (L[cur] > 0 || R[cur] > 0) { n    if (L[cur] > 0 && R[cur - 1] > 0 || (!R[cur] && !L[cur + 1])) { n     L[cur]--; n     cur--; n     cout << 0; n    } else { n     R[cur]--; n     cur++; n     cout << 1; n    } n   } n   cout <<   n ; n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int tree[100010 << 2]; n bool flag; n struct node { n   int row, col, pos; n   friend bool operator<(node a, node b) { n     if (a.row == b.row) return a.col < b.col; n     return a.row < b.row; n   } n }; n node p[100010]; n inline void pushUp(int rt) { tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]); } n void update(int x, int y, int val, int L, int R, int rt) { n   if (flag) return; n   if (L == R) { n     if (tree[rt] == 0) { n       flag = true; n       tree[rt] = val; n     } n     return; n   } else if (x <= L && R <= y) { n     int mid = (L + R) >> 1; n     if (tree[rt << 1] == 0) n       update(x, y, val, L, mid, rt << 1); n     else if (tree[rt << 1 | 1] == 0) n       update(x, y, val, mid + 1, R, rt << 1 | 1); n   } else { n     int mid = (L + R) >> 1; n     if (x <= mid) update(x, y, val, L, mid, rt << 1); n     if (y > mid) update(x, y, val, mid + 1, R, rt << 1 | 1); n   } n   pushUp(rt); n } n void out(int L, int R, int rt) { n   if (L == R) { n     if (tree[rt]) printf( %d  , tree[rt]); n   } else { n     int mid = (L + R) >> 1; n     out(L, mid, rt << 1); n     out(mid + 1, R, rt << 1 | 1); n   } n } n int main() { n   int i; n   int n, m, cnt; n   while (~scanf( %d%d , &n, &m)) { n     memset(tree, 0, sizeof(tree)); n     for (i = 0; i < m; i++) { n       scanf( %d%d , &p[i].row, &p[i].col); n       p[i].pos = i + 1; n     } n     sort(p, p + m); n     for (cnt = i = 0; i < m; i++) { n       flag = false; n       update(n - p[i].col + 1, p[i].row, p[i].pos, 1, n, 1); n       if (flag) cnt++; n     } n     printf( %d n , cnt); n     out(1, n, 1); n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n int n, t, sz[200010], cen; n vector<int> g[200010], v; n bool mark[200010]; n int dfs(int pos, int par) { n   sz[pos] = (int)mark[pos]; n   bool ok = true; n   for (int i = 0; i < g[pos].size(); i++) n     if (g[pos][i] != par) { n       sz[pos] += dfs(g[pos][i], pos); n       if (sz[g[pos][i]] > t) ok = false; n     } n   if (ok && sz[pos] >= t) cen = pos; n   return sz[pos]; n } n void dfs2(int pos, int par) { n   if (mark[pos]) v.push_back(pos); n   for (int i = 0; i < g[pos].size(); i++) n     if (g[pos][i] != par) dfs2(g[pos][i], pos); n } n int main() { n   cin >> n >> t; n   int x, y; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 0; i < t + t; i++) { n     scanf( %d , &x); n     mark[x] = true; n   } n   dfs(1, 0); n   for (int i = 0; i < g[cen].size(); i++) dfs2(g[cen][i], cen); n   if (mark[cen]) v.push_back(cen); n   cout << 1 << endl << cen << endl; n   for (int i = 0; i < t; i++) printf( %d %d %d n , v[i], v[i + t], cen); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline int countbit(T n) { n   return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); n } n template <class T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n const double EPS = 1e-9; n const double PI = acos(-1.0); n int n, k, h; n int ind[100005]; n int vel[100005]; n int mass[100005]; n int ret[100005]; n bool can(double mid) { n   int cnt = 0; n   double step = k; n   for (int j = n - 1; j >= 0 && cnt <= k; j--) { n     double tempo = step * h / ((double)vel[ind[j]]); n     if (tempo < mid - EPS || fabs(tempo - mid) < EPS) cnt++, step--; n   } n   return cnt >= k; n } n bool cmp(int i, int j) { n   if (mass[i] != mass[j]) return mass[i] < mass[j]; n   return vel[i] < vel[j]; n } n int main(void) { n   scanf( %d %d %d , &n, &k, &h); n   for (int i = (0); i < int(n); i++) scanf( %d , &mass[i]); n   for (int i = (0); i < int(n); i++) scanf( %d , &vel[i]); n   for (int i = (0); i < int(n); i++) ind[i] = i; n   sort(ind, ind + n, cmp); n   double l = 0, r = 10E9 + 100; n   for (int i = (0); i < int(100); i++) { n     double mid = (l + r) / 2; n     if (can(mid)) n       r = mid; n     else n       l = mid; n   } n   double mid = r, step = k; n   int p = k - 1; n   for (int j = n - 1; j >= 0 && p >= 0; j--) { n     double tempo = step * h / ((double)vel[ind[j]]); n     if (tempo < mid - EPS || fabs(tempo - mid) < EPS) { n       step--; n       ret[p--] = ind[j] + 1; n     } n   } n   for (int i = (0); i < int(k); i++) { n     if (i) printf(   ); n     printf( %d , ret[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n map<int, int> mym; n void kaisa() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   queue<int> q; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     mym[x] = 0; n     q.push(x); n   } n   long long res = 0; n   vector<int> ans; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     if (mym[x] != 0) { n       res += mym[x]; n       ans.push_back(x); n       if (ans.size() == m) break; n     } n     if (!mym.count(x + 1)) { n       mym[x + 1] = mym[x] + 1; n       q.push(x + 1); n     } n     if (!mym.count(x - 1)) { n       mym[x - 1] = mym[x] + 1; n       q.push(x - 1); n     } n   } n   cout << res <<   n ; n   for (int i : ans) { n     cout << i <<    ; n   } n   cout <<   n ; n } n int main() { n   kaisa(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long q, x, y; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> q; n   while (q--) { n     cin >> x >> y; n     if (x - y > 1) n       cout <<  YES  n            <<   n ; n     else n       cout <<  NO  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, a, b, x; n   cin >> t; n   while (t--) { n     cin >> a >> b; n     if (a < b) { n       x = b - a; n       x % 2 == 0 ? cout << 2 : cout << 1; n     } else if (a > b) { n       x = a - b; n       x % 2 == 0 ? cout << 1 : cout << 2; n     } else n       cout << 0; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd_(int a, int b) { n   if (b == 0) n     return a; n   else n     return gcd_(b, a % b); n } n int main() { n   int n, i, x; n   vector<int> v; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     v.push_back(x); n   } n   int max1 = v[0]; n   x = v[0]; n   for (i = 1; i < n; i++) { n     x = gcd_(v[i], x); n     if (v[i] > max1) max1 = v[i]; n   } n   int moves = ((max1 / x) - n); n   if (moves % 2 != 0) n     cout <<  Alice  << endl; n   else n     cout <<  Bob  << endl; n   return 0; n } n


#include <bits/stdc++.h> n std::vector<int> ReadVector(size_t size) { n   std::vector<int> vector(size); n   for (auto& element : vector) { n     std::cin >> element; n   } n   return vector; n } n bool is_pal(const std::string& a) { n   for (size_t i = 0; i < a.size() / 2; ++i) { n     if (a[i] != a[a.size() - i - 1]) { n       return false; n     } n   } n   return true; n } n class Solver { n  public: n   std::vector<std::vector<int>> g; n   Solver() {} n   void read() {} n   void solve() { n     size_t n; n     std::cin >> n; n     g.resize(n); n     for (size_t i = 0; i < n - 1; ++i) { n       int a, b; n       std::cin >> a >> b; n       a--; n       b--; n       g[a].push_back(b); n       g[b].push_back(a); n     } n     int root = -1; n     int begin = -1; n     int end = -1; n     for (int v = 0; v < n; ++v) { n       if (g[v].size() > 2) { n         if (root != -1) { n           std::cout <<  No  << std::endl; n           return; n         } else { n           root = v; n         } n       } n       if (g[v].size() == 1) { n         if (begin == -1) { n           begin = v; n         } else { n           end = v; n         } n       } n     } n     std::cout <<  Yes n ; n     if (root == -1) { n       std::cout << 1 << std::endl; n       std::cout << begin + 1 <<     << end + 1 << std::endl; n       return; n     } n     std::cout << g[root].size() << std::endl; n     for (auto ch : g[root]) { n       std::cout << root + 1 <<    ; n       int tmp = ch; n       int parent = root; n       while (g[tmp].size() == 2) { n         if (g[tmp][0] == parent) { n           parent = tmp; n           tmp = g[tmp][1]; n         } else { n           parent = tmp; n           tmp = g[tmp][0]; n         } n       } n       std::cout << tmp + 1 << std::endl; n     } n   } n }; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   Solver solver; n   solver.solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long INF = 1e9; n const int maxn = 1e6 + 5; n int a[maxn]; n int tpr = 0; n vector<int> adj[maxn]; n void dfs(int root) { n   if (adj[root].size() > 0) { n     if (adj[root][0] == (root + 1)) { n       dfs(root + 1); n       tpr++; n     } else n       return; n   } n } n int main() { n   int n, m, t, i, j, k; n   int flag = 0, flag2; n   int ans = 0; n   cin >> n >> k; n   for (int i = 0; i < k; i++) { n     cin >> m; n     ans += m - 1; n     int y; n     int prev = -1; n     for (int j = 0; j < m; j++) { n       cin >> y; n       if (y == 1) { n         flag2 = m - 1; n       } n       if ((j == 0) && (y == 1)) { n         flag = 1; n       } n       if (prev != -1) adj[prev].push_back(y); n       prev = y; n     } n   } n   if (flag == 0) { n     ans += n - 1; n     cout << ans << endl; n     return 0; n   } n   dfs(1); n   ans -= tpr; n   ans += (n - tpr - 1); n   cout << ans << endl; n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n using ii = pair<int, int>; n using ll = long long; n const int N = 3000 + 5; n const int mod = 1e9 + 7; n string source[N], target[N]; n int n; n ii ranges[N]; n vector<int> FAIL(string pat) { n   int m = pat.size(); n   vector<int> F(m + 1); n   int i = 0, j = -1; n   F[0] = -1; n   while (i < m) { n     while (j >= 0 && pat[i] != pat[j]) j = F[j]; n     i++, j++; n     F[i] = j; n   } n   return F; n } n bool check(string a, string b, string s, string t) { n   vector<int> F = FAIL(s); n   int i = 0, j = 0; n   int n = a.size(); n   int m = s.size(); n   while (i < n) { n     while (j >= 0 && a[i] != s[j]) j = F[j]; n     i++, j++; n     if (j == m) { n       int idx = i - j; n       for (int k = 0; k < m; k++) { n         a[k + idx] = t[k]; n       } n       break; n     } n   } n   return a == b; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> source[i]; n   for (int i = 0; i < n; i++) cin >> target[i]; n   string s, t; n   string prefix, suffix; n   for (int i = 0; i < n; i++) { n     if (source[i] != target[i]) { n       int l = -1, r = -1; n       for (int j = 0; j < source[i].size(); j++) { n         if (source[i][j] != target[i][j]) r = j; n         if (source[i][j] != target[i][j] && l == -1) l = j; n       } n       ranges[i] = {l, r}; n       if (s.empty()) { n         s = source[i].substr(l, r - l + 1); n         t = target[i].substr(l, r - l + 1); n         prefix = source[i].substr(0, l); n         suffix = source[i].substr(r + 1); n         reverse(prefix.begin(), prefix.end()); n       } else { n         if (s != source[i].substr(l, r - l + 1) || n             t != target[i].substr(l, r - l + 1)) { n           cout <<  NO n ; n           return 0; n         } n         string cprefix = source[i].substr(0, l); n         string csuffix = source[i].substr(r + 1); n         reverse(cprefix.begin(), cprefix.end()); n         string p, ss; n         for (int j = 0; n              j < min(cprefix.size(), prefix.size()) && cprefix[j] == prefix[j]; n              j++) { n           p.push_back(prefix[j]); n         } n         for (int j = 0; n              j < min(csuffix.size(), suffix.size()) && csuffix[j] == suffix[j]; n              j++) { n           ss.push_back(suffix[j]); n         } n         prefix = p; n         suffix = ss; n       } n     } n   } n   reverse(prefix.begin(), prefix.end()); n   s = prefix + s + suffix; n   t = prefix + t + suffix; n   for (int i = 0; i < n; i++) { n     if (!check(source[i], target[i], s, t)) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   cout << s <<   n ; n   cout << t <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool inrange(T a, T l, T r) { n   if (l > r) swap(l, r); n   return (a >= l and a <= r); n } n template <typename T, typename U> n inline bool exist(T val, U &any) { n   return (any.find(val) != any.end()); n } n template <typename T> n inline void inp(T &any) { n   cin >> any; n } n template <typename T, typename... U> n inline void inp(T &a, U &...b) { n   cin >> a; n   inp(b...); n } n template <typename T> n inline istream &operator>>(istream &in, vector<T> &a) { n   for (auto &x : a) in >> x; n   return in; n } n template <typename T, typename U> n inline istream &operator>>(istream &in, pair<T, U> &a) { n   in >> a.first >> a.second; n   return in; n } n void solve(long long int &T) { n   long long int n; n   std::cin >> n; n   vector<long long int> a(n), b(n); n   std::cin >> a >> b; n   vector<long long int> sol(n); n   for (long long int i = (0); i < (n); i++) sol[a[i] - 1] = b[i]; n   for (auto &X : sol) std::cout << X <<    ; n   std::cout <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   for (long long int i = (1); i < (t + 1); i++) { n     cerr <<  Case #  << i <<   n ; n     solve(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<vector<int>> v; n   for (int i = 0; i < (n); ++i) { n     int l; n     cin >> l; n     vector<int> temp; n     for (int j = 0; j < (l); ++j) { n       int ele; n       cin >> ele; n       temp.push_back(ele); n     } n     v.push_back(temp); n   } n   vector<int> maxi; n   int cnt_no_ascent = 0; n   bool ascent[n]; n   int mini_arr[n]; n   int maxi_arr[n]; n   for (int i = 0; i < (n); ++i) ascent[i] = false; n   for (int i = 0; i < (n); ++i) { n     int flag = 0; n     int maxi_el = INT_MIN; n     int mini_el = INT_MAX; n     for (int j = 0; j < (v[i].size()); ++j) { n       if (v[i][j] > maxi_el) { n         maxi_el = v[i][j]; n       } n       if (v[i][j] < mini_el) { n         mini_el = v[i][j]; n       } n       if (j != 0 && v[i][j - 1] < v[i][j]) { n         ++cnt_no_ascent; n         flag = 1; n         ascent[i] = true; n         break; n       } n     } n     if (flag == 0) { n       mini_arr[i] = mini_el; n       maxi.push_back(maxi_el); n       maxi_arr[i] = maxi_el; n     } n   } n   long long ans = ((long long)cnt_no_ascent * (long long)n); n   sort(maxi.begin(), maxi.end()); n   for (int i = 0; i < (n); ++i) { n     if (ascent[i] == true) continue; n     auto it = upper_bound(maxi.begin(), maxi.end(), mini_arr[i]); n     ans = ans + (long long)(maxi.end() - it); n     ans = ans + cnt_no_ascent; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 6; n int main() { n   int n; n   cin >> n; n   string str =   ; n   while (n) { n     str += ((n % 2) +  0 ); n     n /= 2; n   } n   reverse(str.begin(), str.end()); n   while (str.size() < 6) str =  0  + str; n   string ss =   ; n   ss += str[0]; n   ss += str[5]; n   ss += str[3]; n   ss += str[2]; n   ss += str[4]; n   ss += str[1]; n   int ret = 0; n   for (auto x : ss) ret = ret * 2 + (x -  0 ); n   cout << ret <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int getint() { n   unsigned int c; n   int x = 0; n   while (((c = getchar()) -  0 ) >= 10) { n     if (c ==  - ) return -getint(); n     if (!~c) exit(0); n   } n   do { n     x = (x << 3) + (x << 1) + (c -  0 ); n   } while (((c = getchar()) -  0 ) < 10); n   return x; n } n int n, nn, sum; n int as[20]; n int c[10][10]; n bool used[20]; n int num[20], have[20], ns; n int perm[20][20][20]; n void dfs(int row, int col, int rows, int cols, int dir, int cnt) { n   int i, j, k; n   if (cnt == nn) { n     int tmp1 = 0, tmp2 = 0; n     for (i = 0; i < n; i++) { n       tmp1 += c[i][i]; n       tmp2 += c[i][n - 1 - i]; n     } n     if (tmp1 != sum) return; n     if (tmp2 != sum) return; n     tmp1 = tmp2 = 0; n     for (i = 0; i < n; i++) { n       tmp1 += c[n - 1][i]; n       tmp2 += c[i][n - 1]; n     } n     if (tmp1 != sum) return; n     if (tmp2 != sum) return; n     for (i = 0; i < n; i++) { n       for (j = 0; j < n; j++) { n         if (j) putchar(   ); n         printf( %d , c[i][j]); n       } n       puts(  ); n     } n     exit(0); n   } n   if (row == n or col == n) { n     int tmp = 0; n     if (dir == 0) { n       for (i = 0; i < n; i++) tmp += c[row][i]; n       if (tmp != sum) return; n     } else { n       for (i = 0; i < n; i++) tmp += c[i][col]; n       if (tmp != sum) return; n     } n     if (dir == 0) { n       dfs(rows + 1, cols, rows + 1, cols, 1, cnt); n     } else { n       dfs(rows, cols + 1, rows, cols + 1, 0, cnt); n     } n   } n   for (i = 0; i < ns; i++) { n     int d = perm[row][col][i]; n     if (have[d]) { n       have[d]--; n       c[row][col] = num[d]; n       if (dir == 0) dfs(row, col + 1, rows, cols, dir, cnt + 1); n       if (dir == 1) dfs(row + 1, col, rows, cols, dir, cnt + 1); n       have[d]++; n     } n   } n } n int main() { n   int i, j, tcc, tc = 1 << 28; n   for (tcc = 0; tcc < tc; tcc++) { n     n = getint(), nn = n * n, sum = 0; n     for (i = 0; i < nn; i++) as[i] = getint(), sum += as[i]; n     sum /= n; n     for (i = 0; i < nn; i++) num[i] = as[i]; n     sort(num, num + nn); n     ns = unique(num, num + nn) - num; n     for (i = 0; i < ns; i++) { n       for (j = 0; j < nn; j++) n         if (num[i] == as[j]) have[i]++; n     } n     for (i = 0; i < n; i++) n       for (j = 0; j < n; j++) { n         for (int k = 0; k < ns; k++) perm[i][j][k] = k; n         random_shuffle(perm[i][j], perm[i][j] + ns); n       } n     printf( %d n , sum); n     dfs(0, 0, 0, 0, 0, 0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MXX = 3e5 + 5; n int n, m; n int a[MXX], ans[MXX]; n bool ok(int x) { n   int mx = m - 1; n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] <= mx) { n       if (a[i] + x >= mx) { n         ans[i] = mx; n       } else { n         ans[i] = a[i] + x; n       } n     } else { n       if (a[i] + x >= mx + m) n         ans[i] = mx; n       else n         ans[i] = (a[i] + x) % m; n     } n     mx = ans[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     if (ans[i] > ans[i + 1]) { n       return false; n     } n   } n   return true; n } n bool ok2() { n   for (int i = 0; i < n - 1; i++) { n     if (a[i] > a[i + 1]) { n       return false; n     } n   } n   return true; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (ok2()) { n     return cout << 0 << endl, 0; n   } n   int lo = 0, hi = m - 1, mid, ans = -1; n   while (lo <= hi) { n     mid = (lo + hi) / 2; n     if (ok(mid)) { n       ans = mid; n       hi = mid - 1; n     } else { n       lo = mid + 1; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b); n void print(long long arr[], long long n); n void printv(vector<long long> arr); n signed main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long k; n     cin >> k; n     vector<long long> arr(n); n     unordered_map<long long, long long> m; n     for (long long i = 0; i < n; i++) { n       cin >> arr[i]; n       m[arr[i]]++; n     } n     vector<long long> tem = arr; n     sort(tem.begin(), tem.end()); n     set<long long> a, b; n     if (arr[0] * 2 != k) a.insert(arr[0]); n     for (long long i = 1; i < n; i++) { n       if (arr[i] * 2 == k) continue; n       if (a.find(k - arr[i]) == a.end()) n         a.insert(arr[i]); n       else n         b.insert(arr[i]); n     } n     long long ii = 0; n     for (long long i = 0; i < n; i++) { n       if (a.find(arr[i]) != a.end()) n         cout << 1 <<    ; n       else if (b.find(arr[i]) != b.end()) n         cout << 0 <<    ; n       else { n         cout << ii <<    ; n         ii = (ii + 1) % 2; n       } n     } n     cout <<   n ; n   } n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n void print(long long arr[], long long n) { n   for (long long i = 0; i < n; i++) cout << arr[i] <<    ; n   cout <<   n ; n } n void printv(vector<long long> arr) { n   for (long long i = 0; i < arr.size(); i++) cout << arr[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int par[300005]; n int vis[300005]; n unordered_map<int, int> edge[300005]; n int d[300005]; n vector<int> ans; n int find(int u) { return par[u] == u ? u : par[u] = find(par[u]); } n void join(int u, int v) { n   u = find(u); n   v = find(v); n   if (u > v) swap(u, v); n   par[v] = u; n } n void dfs(int u, int pr) { n   if (vis[u]) return; n   vis[u] = 1; n   for (auto i : edge[u]) { n     dfs(i.first, u); n   } n   if (d[u] == 1) { n     ans.push_back(edge[u][pr]); n     d[pr]++; n     d[pr] %= 2; n     return; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   memset(vis, 0, sizeof vis); n   int n, k, u, v; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     edge[i].clear(); n     par[i] = i; n     cin >> d[i]; n   } n   for (int i = 1; i <= k; i++) { n     cin >> u >> v; n     if (find(u) != find(v)) { n       edge[u][v] = i; n       edge[v][u] = i; n       join(u, v); n     } n   } n   int sum = 0; n   int avail = 0; n   for (int i = 1; i <= n; i++) { n     if (d[i] == -1) { n       avail++; n     } else n       sum += d[i]; n   } n   if (sum % 2 && !avail) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     if (d[i] == -1) { n       if (sum % 2 == 1) { n         d[i] = 1; n         sum++; n       } else n         d[i] = 0; n     } n   } n   dfs(1, 0); n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 1e6 + 100; n const long long mod = 1e9 + 7; n const long long inf = 1e18 + 12; n vector<long long> arr(MAX); n map<long long, long long> mm; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(NULL); n   cout.tie(NULL); n   ; n   long long tt = 1; n   while (tt--) { n     long long n; n     cin >> n; n     long long a[n]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     long long w = 0, b = 0; n     long long i = 0; n     while (i < n) { n       if (i % 2 == 0) { n         w += (a[i] + 1) / 2; n         b += (a[i]) / 2; n       } else { n         w += a[i] / 2; n         b += (a[i] + 1) / 2; n       } n       i++; n     } n     cout << min(w, b); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n const int N = 200005; n const double PI = 4 * atan(1); n const long long MAX = 9223372036854775807; n int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; n int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; } n bool isprime(long long u) { n   for (long long i = 2; i <= (int)sqrt(u); i++) { n     if (u % i == 0) return 0; n   } n   return 1; n } n long long mod(long long to_mod, long long MOD) { n   to_mod %= MOD; n   while (to_mod < 0) to_mod += MOD; n   return to_mod % MOD; n } n long long moduloMultiplication(long long a, long long b, long long mod) { n   long long res = 0; n   a %= mod; n   while (b) { n     if (b & 1) res = (res + a) % mod; n     a = (2 * a) % mod; n     b >>= 1; n   } n   return res; n } n long long power(long long x, long long y) { n   long long res = 1; n   x = x; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n long long n, m, x, y, t, k, z, w, l, r, ans; n bool vis[N]; n long long a[N]; n long long lvl[N]; n map<long long, vector<long long> > adj; n map<long long, long long> maa; n void bfs(long long src) { n   queue<long long> q; n   q.push(src); n   lvl[src] = 1; n   while (!q.empty()) { n     long long ras = q.front(); n     q.pop(); n     if (lvl[ras] % 2 == 0) { n       vis[ras] = 1; n       continue; n     } n     for (auto t : adj[ras]) { n       if (vis[t]) continue; n       lvl[t] = lvl[ras] + 1; n       q.push(t); n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     maa[a[i]] = i; n   } n   for (int j = 0; j < n; j++) { n     k = a[j]; n     for (int i = j; i < n; i += k) { n       if (a[i] > k) adj[i].push_back(j); n     } n     for (int i = j; i >= 0; i -= k) { n       if (a[i] > k) adj[i].push_back(j); n     } n   } n   for (int i = n; i >= 1; i--) { n     if (vis[maa[i]]) continue; n     bfs(maa[i]); n   } n   for (int i = 0; i < n; i++) { n     if (vis[i]) n       cout <<  A ; n     else n       cout <<  B ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 7; n int n, lim, a[N], mn[N], mx[N], suf[N]; n long long ans; n int main() { n   scanf( %d%d , &n, &lim); n   for (int i = 1; i <= lim; i++) mn[i] = n + 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     mn[a[i]] = min(mn[a[i]], i); n     mx[a[i]] = max(mx[a[i]], i); n   } n   int L = lim, R = 1, pre = mx[1]; n   for (int i = 1; i < lim; i++) { n     pre = max(pre, mx[i]); n     if (pre > mn[i + 1]) { n       L = i; n       break; n     } n   } n   if (L == lim) { n     cout << 1ll * lim * (lim + 1) / 2; n     return 0; n   } n   pre = mn[lim]; n   for (int i = lim; i > 1; i--) { n     pre = min(pre, mn[i]); n     if (mx[i - 1] > pre) { n       R = i; n       break; n     } n   } n   ans = lim - R + 2; n   suf[lim] = mn[lim]; n   for (int i = lim - 1; i >= R; i--) suf[i] = min(suf[i + 1], mn[i]); n   pre = mx[1]; n   for (int i = 1, j = R; i <= L; i++) { n     pre = max(pre, mx[i]); n     while (j <= lim && pre > suf[j]) j++; n     ans += lim - j + 2; n     if (j == i + 1) ans--; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n int n, m; n int a[300000]; n int b[300000]; n vector<int> edge[300000]; n int ngood; n int child[300000]; n int good[300000]; n int d[300000]; n int ans; n void back(int now, int parent) { n   child[now] = 1; n   good[now] = b[now]; n   for (auto e : edge[now]) n     if (e != parent) { n       back(e, now); n       child[now] += child[e], good[now] += good[e]; n     } n } n vector<pair<int, int> > v; n void back2(int now, int parent) { n   for (auto e : edge[now]) n     if (e != parent) { n       back2(e, now); n     } n   if (b[now] == 0) { n     d[now] = 0; n     return; n   } n   d[now] = 1; n   int xgood = 0; n   for (auto e : edge[now]) n     if (e != parent) { n       if (child[e] == good[e]) xgood += child[e]; n     } n   d[now] = 1 + xgood; n   for (auto e : edge[now]) n     if (e != parent) { n       d[now] = n           max(d[now], 1 + d[e] + xgood - (child[e] == good[e] ? child[e] : 0)); n     } n   ans = max(ans, d[now]); n   int u = 1 + (n - child[now] == ngood - good[now] ? n - child[now] : 0); n   v.clear(); n   for (auto e : edge[now]) n     if (e != parent) { n       v.emplace_back(d[e], e); n     } n   sort((v).begin(), (v).end(), greater<pair<int, int> >()); n   for (int i = 0; i < 8 && i < v.size(); i++) { n     for (int j = i + 1; j < 8 && j < v.size(); j++) { n       ans = max( n           ans, u + v[i].first + v[j].first + xgood - n                    (child[v[i].second] == good[v[i].second] ? child[v[i].second] n                                                             : 0) - n                    (child[v[j].second] == good[v[j].second] ? child[v[j].second] n                                                             : 0)); n     } n   } n } n int param(int x) { n   int i, j, k; n   for (i = 0; i < n; i++) b[i] = a[i] >= x; n   ngood = count(b, b + n, 1); n   back(0, -1); n   ans = 0; n   back2(0, -1); n   return ans >= m; n } n int main() { n   int i, j, k; n   cin >> n >> m; n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < n - 1; i++) { n     int x, y; n     scanf( %d%d , &x, &y), x--, y--; n     edge[x].push_back(y), edge[y].push_back(x); n   } n   int low = 0, high = 1000001, mid; n   while (low <= high) { n     mid = (low + high) / 2; n     if (param(mid)) n       low = mid + 1; n     else n       high = mid - 1; n   } n   cout << high; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e4 + 10, max_node = N * 200; n int n, m, q, C, c[N], a[N], size[N], heavy[N], dfn[N], dfn_t, top[N], father[N], n     tt, lo[max_node], ro[max_node], addv[max_node]; n long long sum[max_node], sum2[max_node]; n vector<int> graph[N], nodes[N]; n void dfs1(int u) { n   size[u] = 1; n   for (auto v : graph[u]) { n     dfs1(v); n     size[u] += size[v]; n     if (size[v] > size[heavy[u]]) { n       heavy[u] = v; n     } n   } n } n void dfs2(int u, int t) { n   top[u] = t; n   dfn[u] = ++dfn_t; n   if (heavy[u]) { n     dfs2(heavy[u], t); n     for (auto v : graph[u]) { n       if (v != heavy[u]) { n         dfs2(v, v); n       } n     } n   } n } n void add(int l, int r, int o, int v) { n   addv[o] += v; n   sum2[o] += ((sum[o] << 1) + (r - l + 1) * v) * v; n   sum[o] += (r - l + 1) * v; n } n void push_down(int l, int r, int o) { n   if (addv[o]) { n     int mid = l + r >> 1; n     if (!lo[o]) { n       lo[o] = ++tt; n     } n     if (!ro[o]) { n       ro[o] = ++tt; n     } n     add(l, mid, lo[o], addv[o]); n     add(mid + 1, r, ro[o], addv[o]); n     addv[o] = 0; n   } n } n void push_up(int o) { n   sum[o] = sum[lo[o]] + sum[ro[o]]; n   sum2[o] = sum2[lo[o]] + sum2[ro[o]]; n } n void modify(int l, int r, int& o, int ql, int qr, int v) { n   if (!o) { n     o = ++tt; n   } n   if (ql <= l && r <= qr) { n     return add(l, r, o, v); n   } else { n     int mid = l + r >> 1; n     push_down(l, r, o); n     if (ql <= mid) { n       modify(l, mid, lo[o], ql, qr, v); n     } n     if (qr > mid) { n       modify(mid + 1, r, ro[o], ql, qr, v); n     } n     push_up(o); n   } n } n struct info_t { n   int root; n   void init(vector<int>& nodes) { n     for (auto v : nodes) { n       for (int x = v; x; x = father[top[x]]) { n         modify(1, n, root, dfn[top[x]], dfn[x], 1); n       } n     } n   } n   void update(int u, int type) { n     for (int x = u; x; x = father[top[x]]) { n       modify(1, n, root, dfn[top[x]], dfn[x], type); n     } n   } n   double get_answer(int c) { n     double result = (double)C * C; n     result -= 2.0 * C * c * sum[root] / n; n     result += 1.0 * c * c * sum2[root] / n; n     return result; n   } n } info[N]; n int main() { n   scanf( %d%d%d%d , &n, &m, &q, &C); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     nodes[a[i]].push_back(i); n   } n   for (int i = 2; i <= n; ++i) { n     scanf( %d , &father[i]); n     graph[father[i]].push_back(i); n   } n   for (int i = 1; i <= m; ++i) { n     scanf( %d , &c[i]); n   } n   dfs1(1); n   dfs2(1, 1); n   for (int i = 1; i <= m; ++i) { n     info[i].init(nodes[i]); n   } n   while (q--) { n     int type, x, w; n     scanf( %d , &type); n     if (type == 1) { n       scanf( %d%d , &x, &w); n       info[a[x]].update(x, -1); n       a[x] = w; n       info[a[x]].update(x, 1); n     } else { n       scanf( %d , &x); n       printf( %.12lf n , info[x].get_answer(c[x])); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long int mod = 1000000007; n const long long int AM = 2e6 + 5; n using namespace std; n long long int powm(long long int a, long long int b, long long int n) { n   long long int x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y) % n; n     } n     y = (y * y) % n; n     b /= 2; n   } n   return x % n; n } n long long int pow(long long int x, long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) res = ((res) * (x)); n     y = y >> 1; n     x = ((x) * (x)); n   } n   return res; n } n long long int fact(long long int n) { n   long long int ans = 1; n   for (long long int i = 1; i <= n; i++) ans *= i; n   return ans; n } n long long int nCr(long long int n, long long int r) { n   if (r > n - r) r = n - r; n   long long int ans = 1, i; n   for (i = 1; i <= r; i++) { n     ans *= n - r + i; n     ans /= i; n   } n   return ans; n } n bool isPrime(long long int n) { n   if (n <= 1) return false; n   for (long long int i = 2; i < n; i++) n     if (n % i == 0) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int n; n   cin >> n; n   long long int s = sqrt(n), f1, f2; n   if (s * s >= n) { n     f1 = s; n     f2 = s; n   } else if (s * (s + 1) >= n) { n     f1 = s; n     f2 = s + 1; n   } else { n     f1 = s + 1; n     f2 = s + 1; n   } n   cout << f1 + f2; n } n


#include <bits/stdc++.h> n using namespace std; n int f[305][305], rf[305][305], g[305][305], rg[305][305]; n int a[305][305]; n int n, m, s, t1, t2, t3; n int ans1, ans2, ans3, ans4, Min = 1999999999; n set<pair<int, int> > S; n int C(int first, int second) { n   if (first < second) return t2; n   if (first == second) return t1; n   return t3; n } n int G(int i, int j, int k, int l) { n   int ans = (f[i][l] - f[i][k]) + (rf[j][l] - rf[j][k]); n   ans += (g[l][j] - g[l][i]); n   return ans; n } n int main() { n   scanf( %d %d %d , &n, &m, &s); n   scanf( %d %d %d , &t1, &t2, &t3); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]); n   for (int i = 1; i <= n; i++) { n     f[i][1] = 0; n     for (int j = 2; j <= m; j++) n       f[i][j] = f[i][j - 1] + C(a[i][j - 1], a[i][j]); n     rf[i][1] = 0; n     for (int j = 2; j <= m; j++) n       rf[i][j] = rf[i][j - 1] + C(a[i][j], a[i][j - 1]); n   } n   for (int i = 1; i <= m; i++) { n     g[i][1] = 0; n     for (int j = 2; j <= n; j++) n       g[i][j] = g[i][j - 1] + C(a[j - 1][i], a[j][i]); n     rg[i][1] = 0; n     for (int j = 2; j <= n; j++) n       rg[i][j] = rg[i][j - 1] + C(a[j][i], a[j - 1][i]); n   } n   for (int i = 1; i <= n; i++) n     for (int j = i + 2; j <= n; j++) { n       S.clear(); n       for (int k = m; k >= 1; k--) { n         if (k <= m - 2) S.insert(make_pair(G(i, j, 1, k + 2), k + 2)); n         if (S.empty()) continue; n         int tmp = s + f[i][k] + rf[j][k] - rg[k][j] + rg[k][i]; n         set<pair<int, int> >::iterator it = S.upper_bound(make_pair(tmp, 0)); n         if (it != S.end()) { n           pair<int, int> tmpg = *it; n           if (abs(tmpg.first - tmp) < Min) { n             Min = abs(tmpg.first - tmp); n             ans1 = i, ans2 = j, ans3 = k, ans4 = tmpg.second; n           } n         } n         if (it == S.begin()) continue; n         it--; n         pair<int, int> tmpg = *it; n         if (abs(tmpg.first - tmp) < Min) { n           Min = abs(tmpg.first - tmp); n           ans1 = i, ans2 = j, ans3 = k, ans4 = tmpg.second; n         } n       } n     } n   printf( %d %d %d %d n , ans1, ans3, ans2, ans4); n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n T abs(T x) { n   return x > 0 ? x : -x; n } n const int MAXINT = 1111 * 1111 * 1111; n const long long MAXLINT = MAXINT * 1ll * MAXINT; n const long double EPS = 1e-10; n using namespace std; n int n, m, x, y, z, p; n int a, b; n void reverse_left() { n   int aa = a, bb = b; n   a = m - b + 1; n   b = aa; n   swap(n, m); n } n void reverse_middle() { b = m - b + 1; } n void reverse_right() { n   int aa = a, bb = b; n   b = n - a + 1; n   a = bb; n   swap(n, m); n } n void solve() { n   cin >> n >> m >> x >> y >> z >> p; n   int N = n, M = m; n   x %= 4; n   z %= 4; n   y %= 2; n   while (p--) { n     n = N; n     m = M; n     cin >> a >> b; n     for (int i = 0; i < x; i++) reverse_right(); n     for (int i = 0; i < y; i++) reverse_middle(); n     for (int i = 0; i < z; i++) reverse_left(); n     cout << a <<     << b << endl; n   } n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n set<long long> s; n map<long long, long long> m; n vector<long long> v; n long long n, a[100010], b[100010], dp[100010], mx = 0; n bool mark[100010]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     if (i == 1) n       dp[i] = a[i]; n     else n       dp[i] = dp[i - 1] + a[i]; n   } n   for (long long i = 1; i <= n; i++) cin >> b[i]; n   for (long long i = n; i > 0; i--) { n     v.push_back(mx); n     long long r, l; n     mark[b[i]] = 1; n     if (b[i] < n) { n       if (mark[b[i] + 1] == 1) { n         set<long long>::iterator it = s.lower_bound(b[i]); n         r = *it; n       } else n         r = b[i]; n     } else n       r = b[i]; n     s.insert(r); n     if (b[i] > 1) { n       if (mark[b[i] - 1] == 1) { n         s.erase(b[i] - 1); n       } n     } n     m[r] = m[b[i] - 1] + dp[r] - dp[b[i] - 1]; n     if (m[r] > mx) mx = m[r]; n   } n   for (long long i = v.size() - 1; i >= 0; i--) cout << v[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _print(long long t) { cerr << t; } n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(vector<vector<T>> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(vector<vector<T>> v) { n   cerr <<  ==>  << endl; n   for (vector<T> vec : v) { n     for (T i : vec) { n       _print(i); n       cerr <<    ; n     } n     cerr << endl; n   } n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n mt19937_64 rang( n     chrono::high_resolution_clock::now().time_since_epoch().count()); n int rng(int lim) { n   uniform_int_distribution<int> uid(0, lim - 1); n   return uid(rang); n } n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n long long mod_exp(long long a, long long b) { n   a %= mod; n   if (a == 0) return 0LL; n   long long res = 1LL; n   while (b > 0) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n long long mod_inv(long long a) { return mod_exp(a, mod - 2); } n long long GCD(long long a, long long b) { return (b == 0) ? a : GCD(b, a % b); } n void solve() { n   long long a1, a2, a3, a4, a5, a6; n   cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6; n   cout << ((a1 + a2 + a3) * (a1 + a2 + a3)) - (a1 * a1) - (a3 * a3) - (a5 * a5); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   srand(chrono::high_resolution_clock::now().time_since_epoch().count()); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1005]; n char t[1000005]; n int main() { n   scanf( %s , s); n   scanf( %s , t); n   int len1 = strlen(s); n   int len2 = strlen(t); n   int le = 0x3f3f3f3f; n   int ri = 0x3f3f3f3f; n   for (int i = 0, j = 0; j < len2; j++) { n     if (s[i] == t[j]) i++; n     if (i == len1) { n       le = j; n       break; n     } n   } n   for (int i = len1 - 1, j = len2 - 1; j >= 0; j--) { n     if (s[i] == t[j]) i--; n     if (i == -1) { n       ri = j; n       break; n     } n   } n   if ((le - ri) == (len1 - 1)) n     cout <<  0  << endl; n   else { n     int ans = ri - le; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n int N, K; n std::string S; n int main() { n   std::cin >> N >> K; n   std::cin >> S; n   std::string t = S; n   std::string s =   ; n   for (int l = 1; l < N; ++l) { n     if (S.substr(0, l) == S.substr(N - l, l)) { n       t = S.substr(0, N - l); n       s = S.substr(N - l, l); n     } n   } n   while (K--) std::cout << t; n   std::cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> candies(n); n   for (int i = 0; i < n; i++) cin >> candies[i]; n   int i = 0, j = n - 1; n   int store_a = 0, store_b = 0; n   int turn = 0, sum_a = 0, sum_b = 0; n   while (i <= j) { n     if (turn % 2 == 0) { n       int str_temp = 0; n       while ((str_temp <= store_b) && (i <= j)) { n         str_temp += candies[i]; n         i++; n       } n       store_a = str_temp; n       sum_a += store_a; n     } else { n       int str_tem = 0; n       while ((str_tem <= store_a) && (i <= j)) { n         str_tem += candies[j]; n         j--; n       } n       store_b = str_tem; n       sum_b += store_b; n     } n     turn++; n   } n   cout << turn <<     << sum_a <<     << sum_b <<   n ; n } n int main() { n   int t; n   cin >> t; n   while (t) { n     solve(); n     t--; n   } n   return 0; n } n