#include <bits/stdc++.h>   using namespace std;   void r(int b, int p, int f, int h, int c);   int main() {     int b, p, f, h, c, t;     cin >> t;     while (t--) {       cin >> b >> p >> f >> h >> c;       r(b, p, f, h, c);     }   }   void r(int b, int p, int f, int h, int c) {     int i, j, k, t = 0;     i = b / 2;     if (i == 0) {       cout << 0 << endl;     } else {       if (i >= (p + f)) {         t = (p * h) + (c * f);         cout << t << endl;       } else {         if (h >= c) {           if (p >= i) {             t = (i * h);             cout << t << endl;           } else {             t = (h * p);             i = i - p;             t = t + (i * c);             cout << t << endl;           }         } else {           if (f >= i) {             t = (i * c);             cout << t << endl;           } else {             t = (c * f);             i = i - f;             t = t + (i * h);             cout << t << endl;           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   ;   clock_t start;   mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());   const long long N = 501, lmx = 11;   long long mx[N][N][lmx][lmx], n, m, q, col[4][N][N], logg[N];   long long sum(long long k, long long r1, long long c1, long long r2,                 long long c2) {     return col[k][r2][c2] - col[k][r2][c1] - col[k][r1][c2] + col[k][r1][c1];   }   void sparse() {     for (long long i = 2; i < N; i++) logg[i] = logg[i / 2] + 1;     for (long long lgi = 1; lgi < lmx; lgi++) {       for (long long i = 1; i <= n; i++) {         for (long long j = 1; j <= m; j++) {           if (i + (1 << (lgi - 1)) <= n)             mx[i][j][lgi][0] = max(mx[i][j][lgi - 1][0],                                    mx[i + (1 << (lgi - 1))][j][lgi - 1][0]);         }       }     }     for (long long lgi = 0; lgi < lmx; lgi++) {       for (long long lgj = 1; lgj < lmx; lgj++) {         for (long long i = 1; i <= n; i++) {           for (long long j = 1; j <= m; j++) {             long long k = lgj - 1;             if (j + (1 << k) <= m)               mx[i][j][lgi][lgj] =                   max(mx[i][j][lgi][k], mx[i][j + (1 << k)][lgi][k]);           }         }       }     }   }   long long query(long long r1, long long c1, long long r2, long long c2) {     if (r1 > r2 || c1 > c2) return 0;     long long k1 = logg[r2 - r1 + 1], k2 = logg[c2 - c1 + 1];     long long m1 = max(mx[r1][c1][k1][k2], mx[r2 - (1 << k1) + 1][c1][k1][k2]);     long long m2 = max(mx[r1][c2 - (1 << k2) + 1][k1][k2],                        mx[r2 - (1 << k1) + 1][c2 - (1 << k2) + 1][k1][k2]);     return max(m1, m2);   }   void solve() {     string s, ss =  RGYB ;     cin >> n >> m >> q;     for (long long i = 0; i < n; i++) {       cin >> s;       for (long long j = 0; j < m; j++) {         for (long long k = 0; k < 4; k++)           if (ss[k] == s[j]) col[k][i + 1][j + 1] = 1;       }     }     for (long long k = 0; k < 4; k++) {       for (long long i = 1; i <= n; i++)         for (long long j = 1; j <= m; j++)           col[k][i][j] +=               col[k][i - 1][j] + col[k][i][j - 1] - col[k][i - 1][j - 1];     }     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= m; j++) {         long long lim = min(min(i, j), min(n - i, m - j));         for (long long len = 1; len <= lim; len++) {           if (sum(0, i - len, j - len, i, j) != len * len) break;           if (sum(1, i - len, j, i, j + len) != len * len) break;           if (sum(2, i, j - len, i + len, j) != len * len) break;           if (sum(3, i, j, i + len, j + len) != len * len) break;           mx[i][j][0][0] = len * len;         }       }     }     sparse();     while (q--) {       long long r1, c1, r2, c2, ans = 0;       cin >> r1 >> c1 >> r2 >> c2;       long long s = 1, e = min(r2 - r1 + 1, c2 - c1 + 1);       while (s <= e) {         long long mid = (s + e) / 2;         if (query(r1 + mid - 1, c1 + mid - 1, r2 - mid, c2 - mid) >= mid * mid) {           s = mid + 1, ans = mid;         } else           e = mid - 1;       }       cout << 4 * ans * ans <<   n ;     }   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     start = clock();     cout << fixed << setprecision(15);     while (t--) {       solve();     }     double time_taken = double(clock() - start) / double(CLOCKS_PER_SEC);     cerr << time_taken;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   bool is_simple(int a) {     for (int i = 2; i < a; ++i) {       if (a % i == 0) return false;     }     return true;   }   int elems[200009] = {0};   int addings[200009] = {0};   int removes[200009] = {0};   vector<string> orders;   int main() {     int n;     double current_mean = 0.;     long long cur_num = 1;     scanf( %d , &n);     char buf[100];     gets(buf);     orders.reserve(n);     for (int i = 1; i < n + 1; ++i) {       gets(buf);       orders.push_back(string(buf));       int t = orders[i - 1][0] -  0 ;       addings[i] = addings[i - 1];       removes[i] = removes[i - 1];       if (t == 2) addings[i]++;       if (t == 3) removes[i]++;     }     for (int i = 0; i < n; ++i) {       stringstream ss(orders[i]);       int t;       ss >> t;       if (t == 1) {         int x, a;         ss >> a >> x;         double ttt = current_mean * cur_num;         ttt += a * x;         current_mean = ttt / cur_num;         int start_change = cur_num - (removes[n] - removes[i]);         if (start_change < 0) start_change = 0;         for (int i = start_change; i < a && i < cur_num; ++i) elems[i] += x;       }       if (t == 2) {         int k;         ss >> k;         elems[cur_num] = k;         cur_num++;         double ttt = current_mean * (cur_num - 1);         ttt += k;         current_mean = ttt / cur_num;       }       if (t == 3) {         double ttt = current_mean * (cur_num);         ttt -= elems[cur_num - 1];         cur_num--;         current_mean = ttt / cur_num;       }       printf( %.6f n , current_mean);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void chkmin(T &a, T b) {     if (a > b) a = b;   }   template <class T>   inline void chkmax(T &a, T b) {     if (a < b) a = b;   }   long long sum[30];   char fir[210000], sec[210000];   int main() {     int len, i;     double ans, cnt;     scanf( %d , &len);     scanf( %s%s , fir, sec);     cnt = 1LL * len * (len + 1) * (2 * len + 1) / 6;     ans = 0;     memset(sum, 0, sizeof(sum));     for (i = len - 1; i >= 0; i--) {       sum[fir[i] -  A ] += len - i;       ans += sum[sec[i] -  A ] * (i + 1) / cnt;     }     memset(sum, 0, sizeof(sum));     for (i = len - 1; i >= 0; i--) {       ans += sum[fir[i] -  A ] * (i + 1) / cnt;       sum[sec[i] -  A ] += len - i;     }     printf( %.15lf n , 1.0 * ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, m, k, ans[5];   char s[N];   void solve(int l, int r) {     while (l != r) {       int mid = l + r >> 1;       printf( 1 %d %d n , mid, mid + 1);       fflush(stdout);       scanf( %s , s);       if (!strcmp(s,  NIE ))         l = mid + 1;       else         r = mid;     }     ans[++m] = l;   }   int main() {     scanf( %d%d , &n, &k);     solve(1, n);     if (ans[1] > 1) solve(1, ans[1] - 1);     if (ans[1] < n) solve(ans[1] + 1, n);     if (m == 2) {       printf( 2 %d %d n , ans[1], ans[2]);       exit(0);     }     printf( 1 %d %d n , ans[2], ans[3]);     fflush(stdout);     scanf( %s , s);     if (!strcmp(s,  NIE )) ans[2] = ans[3];     printf( 2 %d %d n , ans[1], ans[2]);     return 0;   }


#include <bits/stdc++.h>   const int N = 2e5 + 5;   const int highestboolN = 1e9;   const long long unsigned inf = 1234568901234567890;   const long double pi = 3.141592653589793;   const int md = 1e9 + 7;   using namespace std;   int here = 1;   void force_break() {     here++;     if (here >= (N * 100)) {       cout <<   n n t Infinite loop  n n ;       exit(1);     }     return;   }   int board[105][105];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     scanf( %d , &n);     memset(board, 0, sizeof(board));     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (board[i][j]) continue;         char temp;         if ((i + j) % 2) {           board[i][j] = 1;           temp = 2;         } else {           board[i][j] = 2;           temp = 1;         }         if (i + 2 < n and j + 1 < n) board[i + 2][j + 1] = temp;         if (i + 1 < n and j + 2 < n) board[i + 1][j + 2] = temp;         if (i + 2 < n and j - 1 >= 0) board[i + 2][j - 1] = temp;         if (i + 1 < n and j - 2 >= 0) board[i + 1][j - 2] = temp;       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (board[i][j] == 1)           cout <<  B ;         else if (board[i][j] == 2)           cout <<  W ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[1010], brr[1010];   int main() {     long long n, i, j, x, y, ans;     cin >> n;     for (i = 1; i <= n; i++) cin >> arr[i];     for (i = 1; i <= n; i++) cin >> brr[i];     ans = 0;     for (i = 1; i <= n; i++) {       x = arr[i];       y = brr[i];       ans = max(ans, x + y);       for (j = i + 1; j <= n; j++) {         x = (x | arr[j]);         y = (y | brr[j]);         ans = max(ans, x + y);       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long dp[26][2][2];   long long need;   bool hasone = 0;   int ans[55];   inline void upd(long long &a, long long b) { a += b; }   inline long long getval(int p, int v) {     memset(dp, 0, sizeof(dp));     dp[0][0][0] = 1;     for (int i = 0; i < (n + 1) / 2; i++) {       for (int x = 0; x < 2; x++) {         for (int y = 0; y < 2; y++) {           if (!dp[i][x][y]) continue;           for (int a = 0; a < 2; a++) {             if (!i && a) continue;             if (i <= p && a != ans[i]) continue;             for (int b = 0; b < 2; b++) {               if ((n - 1 - i) <= p && b != ans[n - 1 - i]) continue;               if (i == n - 1 - i && a != b) continue;               if (a > b && !x) continue;               if (a > !b && !y) continue;               upd(dp[i + 1][x | (a < b)][y | (a < !b)], dp[i][x][y]);             }           }         }       }     }     return dp[(n + 1) / 2][0][0] + dp[(n + 1) / 2][0][1] + dp[(n + 1) / 2][1][0] +            dp[(n + 1) / 2][1][1] - !hasone;   }   int main() {     cin >> n >> need;     dp[0][0][0] = 1;     for (int i = 0; i < (n + 1) / 2; i++) {       for (int x = 0; x < 2; x++) {         for (int y = 0; y < 2; y++) {           if (!dp[i][x][y]) continue;           for (int a = 0; a < 2; a++) {             if (!i && a) continue;             for (int b = 0; b < 2; b++) {               if (i == n - 1 - i && a != b) continue;               if (a > b && !x) continue;               if (a > !b && !y) continue;               upd(dp[i + 1][x | (a < b)][y | (a < !b)], dp[i][x][y]);             }           }         }       }     }     long long all = dp[(n + 1) / 2][0][0] + dp[(n + 1) / 2][0][1] +                     dp[(n + 1) / 2][1][0] + dp[(n + 1) / 2][1][1];     all--;     if (all < need) {       printf( -1 n );       return 0;     }     for (int i = 0; i < n; i++) {       ans[i] = 0;       long long num = getval(i, ans[i]);       if (num < need || (i == n - 1 && !hasone)) {         ans[i] = 1;         need -= num;         hasone = 1;       }     }     for (int i = 0; i < n; i++) printf( %d , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T& x) {     int f = 0, c = getchar();     x = 0;     while (!isdigit(c)) f |= c ==  - , c = getchar();     while (isdigit(c)) x = x * 10 + c - 48, c = getchar();     if (f) x = -x;   }   template <typename T, typename... Args>   inline void read(T& x, Args&... args) {     read(x);     read(args...);   }   template <typename T>   void write(T x) {     if (x < 0) x = -x, putchar( - );     if (x > 9) write(x / 10);     putchar(x % 10 + 48);   }   template <typename T>   void writes(T x) {     write(x);     putchar(   );   }   template <typename T>   void writeln(T x) {     write(x);     puts(  );   }   template <typename T>   inline bool chkmin(T& x, const T& y) {     return y < x ? (x = y, true) : false;   }   template <typename T>   inline bool chkmax(T& x, const T& y) {     return x < y ? (x = y, true) : false;   }   const int maxn = (int)3e5 + 10;   const long long inf = (long long)1e18;   const int mod = (int)1e9 + 7;   int n, a[maxn];   long long ans;   vector<int> v[maxn];   int main() {     read(n);     for (int i = (1); i < (2 * n + 1); i++) read(a[i]);     for (int i = (1); i < (2 * n + 1); i++) {       v[a[i]].push_back(i);     }     int now = 1;     for (int i = (1); i < (n + 1); i++) {       ans += abs(v[i][0] - now);       now = v[i][0];     }     now = 1;     for (int i = (1); i < (n + 1); i++) {       ans += abs(v[i][1] - now);       now = v[i][1];     }     write(ans);   }


#include <bits/stdc++.h>   using namespace std;   const bool debug = 1;   int xl = 1 << 30, xr = -(1 << 30), yl = 1 << 30, yr = -(1 << 30), zl = 1 << 30,       zr = -(1 << 30);   bool getpod(pair<int, int> p, int s) {     int pzl = p.first - p.second - s;     int pzr = p.first - p.second + s;     int pxl = p.first - s;     int pxr = p.first + s;     int pyl = p.second - s;     int pyr = p.second + s;     if (pzl <= zl && pzr >= zr && pyl <= yl && pyr >= yr && pxl <= xl &&         pxr >= xr)       return 1;     return 0;   }   int main() {     if (!debug) {       cin.tie(0);       cout.tie(0);       ios::sync_with_stdio(0);     }     int n;     cin >> n;     vector<pair<int, int>> pt(n, {0, 0});     for (int i = 0; i < n; i++) {       string s;       cin >> s;       for (int j = 0; j < s.size(); j++)         if (s[j] ==  B )           pt[i].first++;         else           pt[i].second++;     }     for (int i = 0; i < n; i++) {       xl = min(pt[i].first, xl);       xr = max(pt[i].first, xr);       yl = min(pt[i].second, yl);       yr = max(pt[i].second, yr);       zl = min(pt[i].first - pt[i].second, zl);       zr = max(pt[i].first - pt[i].second, zr);     }     vector<pair<int, int>> p(12);     vector<int> s(12);     {       int l = -1, r = 1 << 23;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {zr + yl, yl + c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[0] = r;       p[0] = {zr + yl, yl + s[0]};     }     {       int l = -1, r = 1 << 23;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {xl + c, xl - zl};         if (getpod(p, c))           r = c;         else           l = c;       }       s[1] = r;       p[1] = {xl + s[1], xl - zl};     }     {       int l = -1, r = 1 << 23;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {zl + yr, yr - c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[2] = r;       p[2] = {zl + yr, yr - s[2]};     }     {       int l = -1, r = 1 << 23;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {xr - c, xr - zr};         if (getpod(p, c))           r = c;         else           l = c;       }       s[3] = r;       p[3] = {xr - s[3], xr - zr};     }     {       int l = -1, r = 1 << 23;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {xr - c, yr - c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[4] = r;       p[4] = {xr - s[4], yr - s[4]};     }     {       int l = -1, r = 1 << 23;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {xl + c, yl + c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[5] = r;       p[5] = {xl + s[5], yl + s[5]};     }     {       int l = -1, r = zl - xl + yr;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {xl + c, yr - c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[6] = r;       p[6] = {xl + s[6], yr - s[6]};     }     {       int l = -1, r = xr - yl - zr;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {xr - c, yl + c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[7] = r;       p[7] = {xr - s[7], yl + s[7]};     }     {       int l = -1, r = xr - zl - yr;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {xr - c, xr - zl - 2 * c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[8] = r;       p[8] = {xr - s[8], xr - zl - 2 * s[8]};     }     {       int l = -1, r = zr + yr - xr;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {zr + yr - 2 * c, yr - c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[9] = r;       p[9] = {zr + yr - 2 * s[9], yr - s[9]};     }     {       int l = -1, r = yl + zr - xl;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {xl + c, xl - zr + 2 * c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[10] = r;       p[10] = {xl + s[10], xl - zr + 2 * s[10]};     }     {       int l = -1, r = xl - zl - yl;       while (l + 1 < r) {         int c = (l + r) / 2;         pair<int, int> p = {zl + yl + 2 * c, yl + c};         if (getpod(p, c))           r = c;         else           l = c;       }       s[11] = r;       p[11] = {zl + yl + 2 * s[11], yl + s[11]};     }     int iout = 0;     for (int i = 0; i < 12; i++)       if (s[i] < s[iout] && getpod(p[i], s[i])) iout = i;     cout << s[iout] <<   n ;     for (int i = 0; i < p[iout].first; i++) cout <<  B ;     for (int i = 0; i < p[iout].second; i++) cout <<  N ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, x, a1, a2;   int main() {     scanf( %lld , &n);     for (int i = 1; i <= n; ++i) {       scanf( %lld , &x);       if (i % 2 == 0) {         a1 += (x + 1) / 2;         a2 += x / 2;       } else {         a1 += x / 2;         a2 += (x + 1) / 2;       }     }     printf( %lld n , min(a1, a2));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);     int a, b, c;     cin >> a >> b >> c;     cout << ((a + c - 1) * (b + c - 1) - c * (c - 1)) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   long long dx[] = {-1, 0, 1, 0};   long long dy[] = {0, -1, 0, 1};   vector<long long> p(10000005, -1);   void f(long long n = 10000005) {     p[2] = 2;     for (long long i = 4; i < n; i += 2) {       p[i] = 2;     }     for (long long i = 3; i < n; i += 2) {       if (p[i] == -1) {         p[i] = i;         for (long long j = i * i; j < n; j += i) {           if (p[j] == -1) p[j] = i;         }       }     }   }   void find(long long x, vector<long long>& v) {     while (x != 1) {       v.push_back(p[x]);       x /= p[x];     }   }   void solve() {     long long n, m;     cin >> n >> m;     vector<vector<long long>> v(n, vector<long long>(m));     map<long long, vector<long long>> z;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         cin >> v[i][j];         z[i + j].push_back(v[i][j]);       }     }     long long f = 0, r = n + m - 2;     long long ans = 0;     while (f < r) {       map<long long, long long> p;       for (auto x : z[f]) p[x]++;       for (auto x : z[r]) p[x]++;       long long t = 0, mx = 0;       for (auto x : p) {         t += x.second;         mx = max(mx, x.second);       }       ans += t - mx;       f++;       r--;     }     cout << ans <<   n ;   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   char str[100005];   int cnt[250];   int main() {     scanf( %s , &str);     int len = strlen(str);     set<char> myset;     for (int i = 0; i < len; i++) {       myset.insert(str[i]);       cnt[str[i]]++;     }     if (myset.size() == 1 || myset.size() > 4) {       printf( NO );       return 0;     }     if (myset.size() == 4) {       printf( YES );       return 0;     }     if (myset.size() == 3) {       if (len == 3) {         printf( NO );         return 0;       } else {         printf( YES );         return 0;       }     }     if (myset.size() == 2) {       for (int i =  a ; i <=  z ; i++) {         if (cnt[i] == 1) {           printf( NO );           return 0;         }       }       printf( YES );     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100005;   int n, m;   int t[MAX_N], x[MAX_N];   int nxt;   int idx[MAX_N];   int res[MAX_N];   bool mycmp(const int a, const int b) {     if (x[a] != x[b]) return x[a] < x[b];     return a < b;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) scanf( %d%d , &t[i], &x[i]);     int time = 0;     for (int step = 0; step < n; step += m) {       nxt = 0;       for (int i = step; i < min(step + m, n); ++i) idx[nxt++] = i;       time += max(0, t[idx[nxt - 1]] - time);       sort(idx, idx + nxt, mycmp);       for (int i = 0; i < nxt;) {         int j = i;         for (; j < nxt && x[idx[i]] == x[idx[j]]; ++j)           ;         time += x[idx[i]];         if (i > 0) time -= x[idx[i - 1]];         for (int k = i; k < j; ++k) res[idx[k]] = time;         time += 1 + (j - i) / 2;         i = j;       }       time += x[idx[nxt - 1]];     }     printf( %d , res[0]);     for (int i = 1; i < n; ++i) printf(  %d , res[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 7;   const int mod = 1e9 + 7;   const long long INF = 1e18;   string a[30], b[30], ans[407];   void rua() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     int cnt = 0, s1 = -1, s2 = -1;     while (cnt < n * m) {       s1 = (s1 + 1) % n;       s2 = (s2 + 1) % m;       ans[cnt++] = a[s1] + b[s2];     }     int q;     cin >> q;     while (q--) {       int x;       cin >> x;       cout << ans[(x - 1) % (n * m)] << endl;     }   }   int main() {     rua();     return 0;   }


#include <bits/stdc++.h>   int main(int argc, char *argv[]) {     int n, i, a, b, c, d;     long int s;     scanf( %d , &n);     s = 0;     for (i = 1; i <= n; i++) {       scanf( %d %d %d %d , &a, &b, &c, &d);       s = s + (1 + abs(a - c)) * (1 + abs(b - d));     }     printf( %ld , s);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   int main() {     ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     vector<vector<int> > edges(n + 10, vector<int>());     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       edges[x].push_back(y);       edges[y].push_back(x);     }     map<int, int> not_first;     map<int, int> not_second;     map<int, int> not_third;     set<int> first;     set<int> second;     set<int> third;     for (int i = 1; i < n + 1; i++) {       if (not_first.find(i) == not_first.end()) {         first.insert(i);         for (int j = 0; j < edges[i].size(); j++) {           not_first[edges[i][j]]++;         }       } else if (not_second.find(i) == not_second.end()) {         second.insert(i);         for (int j = 0; j < edges[i].size(); j++) {           not_second[edges[i][j]]++;         }       } else if (not_third.find(i) == not_third.end()) {         third.insert(i);         for (int j = 0; j < edges[i].size(); j++) {           not_third[edges[i][j]]++;         }       } else {         cout << -1;         return 0;       }     }     if (second.size() == 0 or third.size() == 0) {       cout << -1;     } else {       for (auto i = not_first.begin(); i != not_first.end(); i++) {         if (i->second != first.size()) {           cout << -1;           return 0;         }       }       for (auto i = not_second.begin(); i != not_second.end(); i++) {         if (i->second != second.size()) {           cout << -1;           return 0;         }       }       for (auto i = not_third.begin(); i != not_third.end(); i++) {         if (i->second != third.size()) {           cout << -1;           return 0;         }       }       vector<int> cols(n + 1, 0);       for (auto i = first.begin(); i != first.end(); i++) {         cols[*i] = 1;       }       for (auto i = second.begin(); i != second.end(); i++) {         cols[*i] = 2;       }       for (auto i = third.begin(); i != third.end(); i++) {         cols[*i] = 3;       }       for (int i = 1; i < n + 1; i++) {         cout << cols[i] <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimization( unroll-loops )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4 )   using namespace std;   const int BASE = 1000000;   char what[1 << 20];   pair<int, int> nxt[10][1 << 20];   pair<long long, int> to[10][10];   inline long long Calc(long long x) {     long long suff = x / BASE, pref = x % BASE;     long long ans = suff;     while (suff > 0) {       ans += nxt[what[suff]][pref].first;       pref = nxt[what[suff]][pref].second;       pref += BASE - max(what[suff], what[pref]);       --suff;     }     return ans + nxt[0][pref].first;   }   inline long long Process(long long x) {     long long suff = x / (1LL * BASE * BASE), psuff = x / BASE % BASE,               pref = x % BASE;     if (suff == 0) {       return Calc(x);     }     long long ans = 0;     while (psuff >= 0) {       ans += nxt[max(what[psuff], what[suff])][pref].first + 1;       pref = nxt[max(what[psuff], what[suff])][pref].second;       pref += BASE - max(what[psuff], max(what[suff], what[pref]));       --psuff;     }     --suff;     while (suff > 0) {       ans += to[what[suff]][BASE - pref].first;       pref = to[what[suff]][BASE - pref].second;       --suff;     }     return ans + Calc(1LL * (BASE - 1) * BASE + pref);   }   int main() {     for (int i = 1; i <= BASE; ++i) {       what[i] = i % 10 > what[i / 10] ? i % 10 : what[i / 10];       for (int d = 0; d < 10; ++d) {         char r = what[i] < d ? d : what[i];         if (i >= r) {           nxt[d][i] = make_pair(nxt[d][i - r].first + 1, nxt[d][i - r].second);         } else {           nxt[d][i] = make_pair(0, i);         }       }     }     for (char d = 1; d < 10; ++d) {       for (int l = 0; l < 10; ++l) {         int suff = BASE - 1, pref = BASE - l;         to[d][l].first = BASE;         while (suff >= 0) {           char r = (d < what[suff] ? what[suff] : d);           to[d][l].first += nxt[r][pref].first;           pref = nxt[r][pref].second;           pref += BASE - (r < what[pref] ? what[pref] : r);           --suff;         }         to[d][l].second = pref;       }     }     long long n;     cin >> n;     cout << Process(n) <<   n ;     return 0;   }


#include <bits/stdc++.h>   const int MAXN = 200000 + 10;   const int INF = 0x7fffffff;   using namespace std;   struct EDGE {     int u, v, w;     bool operator<(const EDGE &b) const { return w > b.w; }   };   int fa[MAXN + MAXN];   int n, m, vis[MAXN];   int Min[MAXN];   vector<EDGE> edge;   int Find(int x) { return fa[x] < 0 ? x : fa[x] = Find(fa[x]); }   int main() {     edge.clear();     scanf( %d%d , &n, &m);     memset(fa, -1, sizeof(fa));     memset(vis, 0, sizeof(vis));     for (int i = 1; i <= m; i++) {       int a, b, w;       scanf( %d%d%d , &a, &b, &w);       edge.push_back((EDGE){a, b, w});     }     sort(edge.begin(), edge.end());     long long Ans = 0ll;     for (int i = 0; i < m; i++) {       int u = edge[i].u, v = edge[i].v;       u = Find(u);       v = Find(v);       if (u == v) {         if (fa[u] == -2) continue;         fa[u] = -2;         Ans += (long long)edge[i].w;         continue;       }       if (fa[u] == -2 && fa[v] == -2) continue;       fa[v] = min(fa[v], fa[u]);       Ans += (long long)edge[i].w;       fa[u] = v;     }     printf( %lld n , Ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int intervalo[200010];   int aparicoes[200010];   int aceito[200010];   int main() {     int n, k, q;     cin >> n >> k >> q;     int l, r;     for (int i = 0; i < n; i++) {       cin >> l >> r;       intervalo[l] += 1;       intervalo[r + 1] -= 1;     }     int x = 0;     for (int i = 1; i <= 200000; i++) {       x += intervalo[i];       aparicoes[i] = x;     }     for (int i = 1; i <= 200000; i++) {       if (aparicoes[i] >= k)         aceito[i] = 1;       else         aceito[i] = 0;       aceito[i] += aceito[i - 1];     }     for (int i = 0; i < q; i++) {       cin >> l >> r;       cout << aceito[r] - aceito[l - 1] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a;     b = a;     for (string::reverse_iterator itr = a.rbegin(); itr != a.rend(); itr++)       b += *itr;     cout << b << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using lint = long long;   const int inf = 1000000007;   const int MOD = 1000000007;   int f(int x) {     int res = 0;     while (x > 0) {       res += x % 10;       x /= 10;     }     return res;   }   bool calc(int n, int k) {     int sm = 0;     for (int i = 0; i < k; ++i) {       sm += f(i);     }     for (int i = k; i <= 100000; ++i) {       sm += f(i);       if (sm == n) {         cout << i - k <<   n ;         return true;       }       sm -= f(i - k);     }     if (n <= 45) {       cout << -1 <<   n ;       return true;     }     return false;   }   void solve() {     int n, k;     cin >> n >> k;     if (calc(n, k)) return;     n -= k * (k + 1) / 2;     if (n < 0) {       cout << -1 <<   n ;       return;     }     int top = 0;     string mid;     int last = 0;     int cnt = 0;     while (n % (k + 1) != 0) {       n += 9;       cnt++;       if (cnt > 9) {         cout << -1 <<   n ;         return;       }     }     if (n - cnt * 9 < (k - cnt) * 9) {       cout << -1 <<   n ;       return;     }     last = 9 - (k - cnt);     n /= (k + 1);     n -= last;     while (n > 9) {       n -= 9;       mid +=  9 ;     }     top = n;     if (cnt > 0) {       int l = mid.length();       if (l != 0) {         mid[l - 1] =  8 ;         top++;       }     }     string hoge;     while (top > 9) {       top -= 9;       hoge +=  9 ;     }     if (top > 0) cout << top;     cout << hoge << mid << last <<   n ;   }   signed main() {     int t;     cin >> t;     for (int i = 0; i < t; ++i) solve();     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3,no-stack-protector,unroll-loops,fast-math )   using namespace std;   struct coor {     long long x, y, x1, y1;   };   vector<struct coor> v;   struct coor ldp[200005], rdp[200005];   struct coor inter(struct coor l, struct coor r) {     struct coor res = {INT_MIN, INT_MIN, INT_MIN, INT_MIN};     if (min(l.x1, r.x1) < max(r.x, l.x) || min(l.y1, r.y1) < max(r.y, l.y))       return res;     res.x = max(l.x, r.x);     res.x1 = min(l.x1, r.x1);     res.y = max(l.y, r.y);     res.y1 = min(l.y1, r.y1);     return res;   }   bool check(struct coor l) { return (l.x != INT_MIN); }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long a = 0, b = 0, c, d, e, f = 0, l, g, m, n, k, i, j, t, p, q;     cin >> n;     for (i = 0; i < n; i++) {       cin >> a >> b >> c >> d;       v.push_back({a, b, c, d});     }     ldp[0] = v[0];     for (i = 1; i < n; i++) {       ldp[i] = inter(ldp[i - 1], v[i]);     }     rdp[n - 1] = v[n - 1];     for (i = n - 2; i >= 0; i--) {       rdp[i] = inter(rdp[i + 1], v[i]);     }     if (check(ldp[n - 2])) {       cout << ldp[n - 2].x <<     << ldp[n - 2].y <<   n ;     } else if (check(rdp[1])) {       cout << rdp[1].x <<     << rdp[1].y <<   n ;     } else {       for (i = 1; i < n - 1; i++) {         struct coor fin = inter(ldp[i - 1], rdp[i + 1]);         if (check(fin)) {           cout << fin.x <<     << fin.y <<   n ;           return 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, a, b, c, d;     cin >> t;     while (t--) {       cin >> n >> a >> b >> c >> d;       int mi = (a - b) * n, ma = (a + b) * n;       if ((mi > c + d) || (ma < c - d))         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   const long long mod = 1000000007;   void solve() {     int n, m, u, v;     cin >> n >> m;     vector<int> adj[n + 1];     for (int i = 0; i < m; i++) {       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     vector<pair<long long, long long> > hash, hash_orig;     hash.reserve(n);     long long p1 = 31, p2 = 53, h1, h2;     long long pow_p1[n + 1], pow_p2[n + 1];     pow_p1[0] = pow_p2[0] = 1;     for (int i = 1; i <= n; i++) {       pow_p1[i] = pow_p1[i - 1] * p1 % mod;       pow_p2[i] = pow_p2[i - 1] * p2 % mod;     }     for (int i = 1; i <= n; i++) {       h1 = 0;       h2 = 0;       for (int &j : adj[i]) {         h1 += pow_p1[j];         h2 += pow_p2[j];       }       hash.push_back({h1, h2});     }     hash_orig = hash;     sort((hash).begin(), (hash).end());     long long ans = 0;     int cnt = 1;     for (int i = 1; i < n; i++) {       if (hash[i] != hash[i - 1]) {         ans += 1ll * cnt * (cnt - 1) / 2;         cnt = 1;       } else         cnt++;     }     ans += 1ll * cnt * (cnt - 1) / 2;     for (int i = 1; i <= n; i++) {       hash_orig[i - 1].first += pow_p1[i];       hash_orig[i - 1].second += pow_p2[i];     }     for (int i = 1; i <= n; i++) {       for (int &j : adj[i]) {         if (j < i || adj[i].size() != adj[j].size()) continue;         if (hash_orig[i - 1] == hash_orig[j - 1]) ans++;       }     }     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   typedef long long int ll;   typedef unsigned long long int ull;   inline void smax(int &x, int y) { x = max(x, y); }   inline void smin(int &x, int y) { x = min(x, y); }   ll gcd(ll a, ll b) { return ((b == 0) ? a : gcd(b, a % b)); }   const double PI = acos(-1.0l);   vector<long long> rev;   vector<complex<double> > omega;   void calc_rev(long long n, long long log_n) {     rev.resize(n);     omega.resize(n);     for (long long i = 0; i < n; i++) {       rev[i] = 0;       for (long long j = 0; j < log_n; j++) {         if ((i >> j) & 1) rev[i] |= 1 << (log_n - j - 1);       }     }   }   void fft(vector<complex<double> > &A, long long n, bool invert) {     for (long long i = 0; i < n; i++) {       if (i < rev[i]) swap(A[i], A[rev[i]]);     }     for (long long len = 2; len <= n; len <<= 1) {       double ang = 2 * PI / len * (invert ? -1 : +1);       long long half = (len >> 1);       complex<double> curomega(cos(ang), sin(ang));       omega[0] = complex<double>(1, 0);       for (long long i = 1; i < half; i++) omega[i] = omega[i - 1] * curomega;       for (long long i = 0; i < n; i += len) {         complex<double> t;         long long pu = i, pv = i + half, pu_end = i + half, pw = 0;         for (; pu != pu_end; pu++, pv++, pw++) {           t = A[pv] * omega[pw];           A[pv] = A[pu] - t;           A[pu] += t;         }       }     }     if (invert)       for (long long i = 0; i < n; i++) A[i] /= n;   }   void multiply(long long n, vector<complex<double> > &A,                 vector<complex<double> > &B, vector<long long> &C) {     fft(A, n, false);     fft(B, n, false);     for (long long i = 0; i < n; i++) A[i] *= B[i];     fft(A, n, true);     for (long long i = 0; i < n; i++) {       C[i] = (long long)(A[i].real() + 0.5);       C[i] = min(C[i], 1LL);     }   }   void Solve(long long n, vector<long long> &coeffA, vector<long long> &coeffB,              vector<long long> &result) {     vector<complex<double> > A(n), B(n);     for (long long i = 0; i < n; i++) A[i] = coeffA[i];     for (long long i = 0; i < n; i++) B[i] = coeffB[i];     multiply(n, A, B, result);   }   void do_FFT(vector<long long> &A, vector<long long> &B,               vector<long long> &result) {     long long n = 1, bits = 0;     while (n < 2 * A.size() || n < 2 * B.size()) n <<= 1, bits++;     result.resize(n, 0);     calc_rev(n, bits);     vector<long long> tempA(A.begin(), A.end());     vector<long long> tempB(B.begin(), B.end());     tempA.resize(n);     tempB.resize(n);     Solve(n, tempA, tempB, result);   }   vector<ll> polpow(vector<ll> &arr, long long k) {     vector<ll> sol;     while (k > 0) {       if (k % 2) {         if (sol.size() == 0)           for (__typeof(arr.size()) i = (0) - ((0) > (arr.size()));                i != (arr.size()) - ((0) > (arr.size()));                i += 1 - 2 * ((0) > (arr.size())))             sol.push_back(arr[i]);         else           do_FFT(sol, arr, sol);       }       do_FFT(arr, arr, arr);       k = k / 2;     }     return sol;   }   int32_t main() {     long long n, k;     cin >> n >> k;     vector<long long> v(n);     for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));          i += 1 - 2 * ((0) > (n)))       cin >> v[i];     vector<ll> arr(1001);     for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));          i += 1 - 2 * ((0) > (n))) {       arr[v[i]] = 1;     }     while (arr.back() == 0) {       arr.pop_back();     }     vector<ll> res = polpow(arr, k);     for (__typeof(res.size()) i = (1) - ((1) > (res.size()));          i != (res.size()) - ((1) > (res.size()));          i += 1 - 2 * ((1) > (res.size()))) {       if (res[i] > 0) cout << i <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m = 0;     cin >> n;     int last;     for (int(i) = 0; (i) < (n); ++(i)) {       cin >> last;       m += last;     }     cout << ((n - m == 1 && n > 1) || (n == 1 && m == 1) ?  YES n  :  NO n );   }


#include <bits/stdc++.h>   using namespace std;   int n, k, trie[100100][27], sz = 1, ok[2][100100];   char s[100100];   void add() {     int v = 0;     for (int i = 0; s[i] !=   0 ; i++) {       if (trie[v][s[i] -  a ] == 0) trie[v][s[i] -  a ] = sz++;       v = trie[v][s[i] -  a ];     }   }   void calc(int v, bool bo, int temp[30]) {     int arr[5];     for (int i = 0; i < 5; i++) arr[i] = 0;     for (int i = 0; i < 26; i++) {       if (temp[i] == -1) continue;       if (temp[i] == 0) {         ok[bo][v] = 0;         break;       }       if (temp[i] == 1) {         if (arr[2]) {           ok[bo][v] = 0;           break;         }       }       if (temp[i] == 2) {         if (arr[1]) {           ok[bo][v] = 0;           break;         }       }       arr[temp[i]]++;     }     if (ok[bo][v] != 0)       ok[bo][v] = (arr[3] && !arr[1] && !arr[2] ? 3 : (arr[1] ? 1 : 2));   }   int dfs(int v, bool st, bool bo) {     int cnt = 0, temp[26];     for (int i = 0; i < 26; i++) temp[i] = -1;     for (int i = 0; i < 26; i++) {       if (!trie[v][i]) continue;       cnt++;       temp[i] = dfs(trie[v][i], !st, bo);       if (temp[i] == 1)         ok[bo][v] = (ok[bo][v] < 2 ? 1 : 3);       else if (temp[i] == 2)         ok[bo][v] = (ok[bo][v] == 2 || ok[bo][v] == 0 ? 2 : 3);       else if (temp[i] == 3)         ok[bo][v] = 3;     }     if (cnt > 1 && !st && !bo) calc(v, bo, temp);     if (cnt > 1 && st && bo) calc(v, bo, temp);     if (cnt == 0) ok[bo][v] = (st ? 2 : 1);     return ok[bo][v];   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %s , &s);       add();     }     dfs(0, 1, 0);     dfs(0, 1, 1);     if (ok[0][0] == 3)       printf( First n );     else if (ok[0][0] == 1) {       if (k % 2)         printf( First n );       else         printf( Second n );     } else if (ok[0][0] == 2)       printf( Second n );     else {       if (ok[1][0] == 3)         printf( Second n );       else if (ok[1][0] == 1) {         if (k % 2)           printf( First n );         else           printf( Second n );       } else if (ok[1][0] == 2)         printf( Second n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int t, id, n, m, x, y, k, c, p, dif, ans, sum, pre, rem, cur, tmp, tot, r, l, u,       d, xx, yy;   int a[N], vis[N], f[N], b[N], cu[N];   vector<int> v, adj[N];   bool fl, ok;   int dp[N];   int mem[N];   int sol(int i = n) {     int &ret = mem[i];     if (~ret) return ret;     if (!i) return ret = 0;     ret = sol(i - 1) + 20;     int B90 = upper_bound(a + 1, a + n + 1, a[i] - 90) - (a + 1);     ret = min(ret, sol(B90) + 50);     int Bday = upper_bound(a + 1, a + n + 1, a[i] - 1440) - (a + 1);     ret = min(ret, sol(Bday) + 120);     return ret;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= (int)n; ++i) scanf( %d , &a[i]);     memset(mem, -1, sizeof mem);     ;     sol();     for (int i = 1; i <= (int)n; ++i) printf( %d n , sol(i) - sol(i - 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void task();   int main() {     task();     return 0;   }   const int N = 2e5 + 10;   const long long int MOD = (long long int)1e9 + 7;   const int INF = 0x3f3f3f3f;   const long long int LINF = (long long int)4e18 + 100;   const int BUF_SIZE = (int)1e6 + 10;   const double PI = acos(-1);   pair<int, int> a[N];   double u[N], ad[N], bd[N];   long long int sq_dist(long long int x1, long long int y1, long long int x2,                         long long int y2) {     return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);   }   double dist(long long int x1, long long int y1, long long int x2,               long long int y2) {     return sqrt(sq_dist(x1, y1, x2, y2));   }   double dp[N][2][2];   void task() {     int ai, aj, bi, bj, ui, uj;     cin >> ai >> aj >> bi >> bj >> ui >> uj;     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> a[i].first >> a[i].second;     }     for (int i = 0; i < n; ++i) {       u[i] = dist(ui, uj, a[i].first, a[i].second);       (void(1));       ;     }     for (int i = 0; i < n; ++i) {       ad[i] = dist(ai, aj, a[i].first, a[i].second) + u[i];       (void(1));       ;     }     for (int i = 0; i < n; ++i) {       bd[i] = dist(bi, bj, a[i].first, a[i].second) + u[i];       (void(1));       ;     }     fill(dp[0][0], dp[0][0] + N * 2 * 2, LINF);     dp[0][1][1] = 0;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < 2; ++j) {         for (int k = 0; k < 2; ++k) {           (void(1));           ;           if (j == 1) {             dp[i + 1][j - 1][k] = min(dp[i][j][k] + ad[i], dp[i + 1][j - 1][k]);           }           if (k == 1) {             dp[i + 1][j][k - 1] = min(dp[i][j][k] + bd[i], dp[i + 1][j][k - 1]);           }           dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k] + 2 * u[i]);         }       }     }     double ans = LINF;     for (int i = 0; i < 2; ++i) {       for (int j = 0; j < 2; ++j)         if (!(i == 1 && j == 1)) {           (void(1));           ;           ans = min(ans, dp[n][i][j]);         }     }     cout << fixed << setprecision(12) << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int f = 1;       int s = 0;       vector<int> A;       for (int i = 0; i < n; i++) {         int a;         cin >> a;         A.push_back(a);       }       if (A.size() == 1) {         cout <<  First  << endl;         continue;       }       if (n == 2) {         if (A[0] != 1) {           cout <<  First  << endl;           continue;         } else {           cout <<  Second  << endl;           continue;         }       }       for (int i = 0; i < n - 2; i++) {         if (A[i] != 1 && A[i + 1] != 1) {           if (f == 1) {             f = 1;             s = 0;           } else {             s = 1;             f = 0;           }         } else if (A[i] != 1 && A[i + 1] == 1) {           int count = 0;           int j = i + 1;           while (A[j] == 1 && j <= n - 2) {             count++;             j++;           }           if (count % 2 == 1) {             if (f == 1) {               s = 1;               f = 0;             } else {               f = 1;               s = 0;             }           } else {             if (f == 1) {               f = 1;               s = 0;             } else {               s = 1;               f = 0;             }           }         } else if (A[i] == 1) {           if (f == 1) {             s = 1;             f = 0;           } else {             f = 1;             s = 0;           }         }       }       if (A[n - 2] != 1 && f == 1) {         cout <<  First  << endl;         continue;       } else if (A[n - 2] == 1 && f == 1) {         cout <<  Second  << endl;         continue;       } else if (A[n - 2] != 1 && s == 1) {         cout <<  Second  << endl;         continue;       } else if (A[n - 2] == 1 && s == 1) {         cout <<  First  << endl;         continue;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 500100;   multiset<pair<long long, long long> > intervals;   long long n, k;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     for (int i = 0; i < k; i++) intervals.insert(make_pair(0, i));     long long s, d;     for (int i = 0; i < n; i++) {       cin >> s >> d;       auto x = intervals.begin();       long long stx = (*x).first;       long long idx = (*x).second;       if (stx < s) stx = s;       stx += d;       cout << stx <<   n ;       intervals.erase(x);       intervals.insert(make_pair(stx, idx));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y;   };   vector<node> q;   int n, m, sum[2020][2020];   char g[2020][2020];   void init() {     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++)         if (g[i][j] ==  w ) q.push_back((node){i, j});     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++)         if (g[i][j] ==  w ) sum[i][j] = 1;     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++)         if (j - 1 >= 0) sum[i][j] = sum[i][j - 1] + sum[i][j];     for (int i = 0; i < (m); i++)       for (int j = 0; j < (n); j++)         if (j - 1 >= 0) sum[j][i] = sum[j - 1][i] + sum[j][i];   }   bool cmpx(node a, node b) { return a.x < b.x; }   bool cmpy(node a, node b) { return a.y < b.y; }   int getlen() {     int res = -1;     sort(q.begin(), q.end(), cmpx);     res = max(res, abs(q[0].x - q[q.size() - 1].x) + 1);     sort(q.begin(), q.end(), cmpy);     res = max(res, abs(q[0].y - q[q.size() - 1].y) + 1);     return res;   }   int getarea(int x1, int y1, int x2, int y2) {     if (x1 > x2 || y1 > y2) return 0;     int res = sum[x2][y2];     if (x1 - 1 >= 0) res -= sum[x1 - 1][y2];     if (y1 - 1 >= 0) res -= sum[x2][y1 - 1];     if (x1 - 1 >= 0 && y1 - 1 >= 0) res += sum[x1 - 1][y1 - 1];     return res;   }   bool check(int x, int y, int len) {     int x2 = x - len + 1;     int y2 = y - len + 1;     int in = getarea(x2 + 1, y2 + 1, x - 1, y - 1);     int out = getarea(0, 0, n - 1, m - 1) - getarea(x2, y2, x, y);     if (in || out)       return false;     else       return true;   }   void filling(int x, int y, int x2, int y2) {     for (int i = x; i <= x2; i++)       for (int j = y; j <= y2; j++)         if (i == x || i == x2 || j == y || j == y2)           g[i][j] = (g[i][j] ==  . ) ?  +  :  w ;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < (n); i++) scanf( %s , g[i]);     init();     int len = getlen();     bool flg = 0;     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++)         if (i + 1 >= len && j + 1 >= len) {           if (flg) break;           if (check(i, j, len)) {             flg = true;             filling(i - len + 1, j - len + 1, i, j);             break;           }         }     if (flg)       for (int i = 0; i < (n); i++) printf( %s n , g[i]);     else       printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T, class U>   void maximize(T &x, U y) {     if (x < y) x = y;   }   template <class T, class U>   void minimize(T &x, U y) {     if (x > y) x = y;   }   template <class T>   T Abs(T x) {     return (x < (T)0 ? -x : x);   }   template <class T>   T safe_sqrt(T x) {     return sqrt(max((T)0, x));   }   template <class T, class U, class V>   T addmod(T x, U k, V mod) {     return ((x + k) % mod + mod) % mod;   }   template <class T, class U, class V>   T submod(T x, U k, V mod) {     return ((x - k) % mod + mod) % mod;   }   template <class T, class U, class V>   T mulmod(T x, U k, V mod) {     return (int64_t)x * k % mod;   }   namespace task {   int64_t dp[11][100][1200];   int b;   int64_t l, r;   vector<int> digits;   vector<int> trans(int64_t x, int b) {     vector<int> vec;     while (x > 0) vec.push_back(x % b), x /= b;     return vec;   }   int64_t calc(int base, int idx, int mask, bool flag) {     if (idx == -1) return (mask == 0);     if (~dp[base][idx][mask] and flag) return dp[base][idx][mask];     int64_t ret = 0;     int k = flag ? b - 1 : digits[idx];     for (int i = 0; i <= k; ++i)       ret += calc(base, idx - 1, mask ^ ((1LL) << (i)), flag | (i < k));     if (flag) dp[base][idx][mask] = ret;     return ret;   }   int64_t f(int64_t x) {     if (!x) return 0ll;     digits.clear();     digits = trans(x, b);     int64_t ret = 0;     for (int i = 0; i < ((digits).size()); ++i) {       int up = (i == ((digits).size()) - 1 ? digits[i] : b - 1);       for (int j = 1; j <= up; ++j)         ret +=             calc(b, i - 1, ((1LL) << (j)), (i < ((digits).size()) - 1 or j < up));     }     return ret;   }   void solve() {     int q;     scanf( %d , &q);     memset(dp, -1ll, sizeof(dp));     while (q--) {       scanf( %d%I64d%I64d , &b, &l, &r);       int64_t x = f(r);       int64_t y = f(l - 1);       int64_t ans = f(r) - f(l - 1);       printf( %I64d n , ans);     }   }   }  // namespace task   int main(void) { task::solve(); }


#include <bits/stdc++.h>   using namespace std;   int n, a[1050];   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int tens = a[0];     set<int> answer;     for (int rem = 0; rem < 10000000; rem++) {       double fuel = tens * 10 + static_cast<double>(rem) / 1000000;       double addend = fuel;       bool isValid = true;       for (int i = 0; isValid and i < n; i++) {         fuel -= (a[i] - (i == 0 ? 0 : a[i - 1])) * 10;         if (fuel < 0 or fuel >= 10 - 1e-6) {           isValid = false;         }         fuel += addend;       }       if (isValid) {         answer.insert(static_cast<int>(fuel) / 10 + a[n - 1]);       }     }     if (answer.size() > 1)       cout <<  not unique  << endl;     else       cout <<  unique  << endl << *answer.begin() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = (int)2e5 + 10;   vector<pair<int, int> > a, b;   vector<int> L;   int n, m, q[MAXN], id[MAXN], Tohka[MAXN], l[MAXN], r[MAXN], t[MAXN], fa[MAXN],       T[MAXN];   bool cmp(int first, int second) { return q[first] < q[second]; }   bool cmp1(int first, int second) { return t[first] < t[second]; }   int Getp(int a) { return lower_bound(L.begin(), L.end(), a) - L.begin(); }   int getf(int first) {     if (fa[first] == first) return first;     fa[first] = getf(fa[first]);     return fa[first];   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; ++i) {       scanf( %d%d%d , &l[i], &r[i], &t[i]);       L.push_back(l[i]), L.push_back(r[i]);       id[i] = i;     }     sort(L.begin(), L.end());     L.erase(unique(L.begin(), L.end()), L.end());     for (int i = 0; i < ((int)(L).size()); ++i) fa[i] = i;     sort(id + 1, id + m + 1, cmp1);     memset(T, -1, sizeof(T));     for (int i = 1; i <= m; ++i) {       int tl = Getp(l[id[i]]), tr = Getp(r[id[i]]), tT = t[id[i]];       for (tl = getf(tl); tl < tr; T[tl] = tT, fa[tl] = tl + 1, tl = getf(tl))         ;     }     for (int i = 0; i + 1 < ((int)(L).size()); ++i) {       int l = L[i], r = L[i + 1], t = T[i];       if (T[i] == -1) continue;       a.push_back(make_pair(t - r, 1)), a.push_back(make_pair(t - l, -1));       b.push_back(make_pair(t - r, -(t - r))),           b.push_back(make_pair(t - l, (t - r)));       b.push_back(make_pair(t - l, (r - l)));     }     sort(a.begin(), a.end());     sort(b.begin(), b.end());     for (int i = 1; i <= n; ++i) scanf( %d , &q[i]), id[i] = i;     sort(id + 1, id + n + 1, cmp);     long long s1 = 0, s2 = 0;     for (int i = 1, p1 = 0, p2 = 0; i <= n; ++i) {       for (; p1 < ((int)(a).size()) && a[p1].first <= q[id[i]];            s1 += a[p1].second, ++p1)         ;       for (; p2 < ((int)(b).size()) && b[p2].first <= q[id[i]];            s2 += b[p2].second, ++p2)         ;       Tohka[id[i]] = s1 * q[id[i]] + s2;     }     for (int i = 1; i <= n; ++i) printf( %d n , Tohka[i]);     fclose(stdin);     fclose(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, q;     scanf( %d %d %d , &n, &m, &q);     int log_n = 0;     while (1 << log_n <= n) {       ++log_n;     }     int jump_small = pow(n, 1.0 / 3), jump_large = pow(n, 2.0 / 3);     vector<vector<int>> rmq_min(log_n, vector<int>(n)),         rmq_max(log_n, vector<int>(n));     vector<int> a(n), next(n), jump(n), times(n), now_min(n), now_max(n);     priority_queue<pair<int, int>> heap;     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);       rmq_min[0][i] = rmq_max[0][i] = a[i];       next[i] = i + 1;       jump[i] = min(i + jump_small, n);       times[i] = jump[i] - i;     }     for (int i = 0; i < n - 1; ++i) {       now_min[i] = min(a[i], a[i + 1]);       now_max[i] = max(a[i], a[i + 1]);       heap.push(make_pair(now_min[i] - now_max[i], i));     }     for (int i = 1; i < log_n; ++i) {       for (int j = 0; j + (1 << i) <= n; ++j) {         rmq_min[i][j] = min(rmq_min[i - 1][j], rmq_min[i - 1][j + (1 << i - 1)]);         rmq_max[i][j] = max(rmq_max[i - 1][j], rmq_max[i - 1][j + (1 << i - 1)]);       }     }     vector<int> last(n);     vector<bool> visit(n);     auto update = [&](int x, int limit) {       while (next[x] < n && next[x] - x <= jump_large &&              now_max[x] - now_min[x] <= limit) {         ++next[x];         if (next[x] < n) {           now_max[x] = max(now_max[x], a[next[x]]);           now_min[x] = min(now_min[x], a[next[x]]);         }       }       if (next[x] < n && next[x] - x <= jump_small) {         heap.push(make_pair(now_min[x] - now_max[x], x));       }       vector<int> stack;       jump[x] = x;       times[x] = 0;       stack.push_back(jump[x]);       while (jump[x] < n && next[jump[x]] - x <= jump_small) {         jump[x] = next[jump[x]];         ++times[x];         stack.push_back(jump[x]);       }       last[x] = stack.size();       visit[x] = true;       for (int i = x - 1; i >= x - jump_small && ~i; --i) {         if (next[i] < n && visit[next[i]]) {           while (stack.back() - i > jump_small) {             stack.pop_back();           }           jump[i] = stack.back();           times[i] = times[next[i]] + 1 + stack.size() - last[next[i]];           last[i] = stack.size();           visit[i] = true;         }       }       for (int i = x; i >= x - jump_small && ~i; --i) {         visit[i] = false;       }     };     auto query = [&](int limit) {       while (!heap.empty() && -heap.top().first <= limit) {         int x = heap.top().second;         heap.pop();         update(x, limit);       }       int answer = -1, x = 0;       while (x < n) {         int dist = next[x] - x;         if (dist <= jump_small) {           answer += times[x];           x = jump[x];         } else {           while (next[x] < n && next[x] - x <= jump_large &&                  now_max[x] - now_min[x] <= limit) {             ++next[x];             if (next[x] < n) {               now_max[x] = max(now_max[x], a[next[x]]);               now_min[x] = min(now_min[x], a[next[x]]);             }           }           dist = next[x] - x;           if (dist <= jump_large) {             ++answer;             x = next[x];           } else {             ++answer;             int current_min = a[x], current_max = a[x];             for (int i = log_n - 1; ~i; --i) {               if (x + (1 << i) <= n) {                 int next_min = min(current_min, rmq_min[i][x]);                 int next_max = max(current_max, rmq_max[i][x]);                 if (next_max - next_min <= limit) {                   current_min = next_min;                   current_max = next_max;                   x += 1 << i;                 }               }             }           }         }       }       return answer;     };     vector<pair<int, int>> queries(q);     vector<int> answer(q);     for (int i = 0; i < q; ++i) {       scanf( %d , &queries[i].first);       queries[i].first = m - queries[i].first;       queries[i].second = i;     }     sort(queries.begin(), queries.end());     for (auto p : queries) {       answer[p.second] = query(p.first);     }     for (int i = 0; i < q; ++i) {       printf( %d n , answer[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long int n, i;     cin >> n;     long long int a[n];     for (i = 0; i < n; i++) {       cin >> a[i];     }     if (n == 1) {       cout << a[0];       return 0;     }     sort(a, a + n);     long long int sum = 0;     if (a[0] >= 0) {       sum = -1 * a[0];       for (i = 1; i < n; i++) {         sum += a[i];       }     } else if (a[n - 1] < 0) {       sum = a[n - 1];       for (i = 0; i < n - 1; i++) {         sum -= a[i];       }     } else {       for (i = 0; i < n; i++) {         if (a[i] < 0) {           sum -= a[i];         } else {           sum += a[i];         }       }     }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 6;   const long long M = 1e9 + 7;   const double eps = 1e-7;   int a[N];   int Int() {     int x;     scanf( %d , &x);     return x;   }   long long Lnt() {     long long x;     scanf( %I64d , &x);     return x;   }   long long Bigmod(long long A, long long B, long long C) {     if (B == 0LL) return 1LL;     long long x = Bigmod(A, B / 2LL, C);     x = (x * x) % C;     if (B % 2 == 1) x = (x * A) % C;     return x;   }   struct mar {     int i, j, c, d;   } cr[N];   bool cmp(mar a, mar b) {     if (a.i == b.i) return a.j < b.j;     return a.i < b.i;   }   bool Cmp(int a, int b) { return a > b; }   void print(int n) { printf( %d n , n); }   void Print(long long n) { printf( %I64d n , n); }   void debug(int n) { printf( %d  , n); }   int main() {     int x1 = Int(), x2 = Int(), x3 = Int(), x4 = Int(), x5 = Int(), x6 = Int();     int c = x3 + x5;     int d = max(x4, x6);     if (x1 > x2) swap(x1, x2);     if ((x1 >= c && x2 >= d) or (x2 >= c && x1 >= d)) return puts( YES ), 0;     c = x3 + x6;     d = max(x4, x5);     if ((x1 >= c && x2 >= d) or (x1 >= d and x2 >= c)) return puts( YES ), 0;     c = x4 + x5;     d = max(x3, x6);     if ((x1 >= c && x2 >= d) or (x1 >= d and x2 >= c)) return puts( YES ), 0;     c = x4 + x6;     d = max(x3, x5);     if ((x1 >= c && x2 >= d) or (x1 >= d and x2 >= c)) return puts( YES ), 0;     return puts( NO );   }


#include <bits/stdc++.h>   using namespace std;   long long int a[2005];   long long int cnt[2005];   long long int b[2005];   long long int vis[2005];   vector<long long int> v;   vector<long long int> vv;   vector<long long int> ans;   long long int prime[2005];   bool pr[2005];   set<char> st;   priority_queue<long long int> bob;   priority_queue<long long int> ali;   priority_queue<long long int> bth;   queue<long long int> q;   map<long long int, long long int> mp;   long long int taken[105];   map<long long int, long long int>::iterator it;   map<long long int, vector<long long int> > ck;   string ss, tt;   long long int pos[205];   long long int afs[205];   long long int pre[2005];   long long int aa[2005];   long long int cs[27];   long long int ct[27];   long long int power(long long int k, long long int i) {     long long int res = 1;     for (long long int j = 1; j <= i; j++) {       res = res * k;     }     return res;   }   int main() {     long long int n, i, j, k, t, m;     cin >> t;     long long int minis;     long long int res;     long long int x, y;     long long int pp;     string fg;     long long int l, r;     long long int h;     long long int p, q;     string tt;     while (t--) {       v.clear();       cin >> n;       cin >> ss >> tt;       string fg;       long long int kk = n;       res = 0;       for (i = n - 1; i >= 0; i--) {         if (ss[i] == tt[i]) {           kk--;           continue;         } else {           if (ss[0] == tt[i]) {             res = res + 1;             v.push_back(1);             if (ss[0] ==  0 )               ss[0] =  1 ;             else               ss[0] =  0 ;           }           fg.clear();           for (j = 0; j <= i; j++) {             if (ss[j] ==  0 )               fg.push_back( 1 );             else               fg.push_back( 0 );           }           reverse(fg.begin(), fg.end());           res = res + 1;           v.push_back(kk);           for (j = 0; j <= i; j++) {             ss[j] = fg[j];           }         }         kk--;       }       cout << res <<    ;       for (i = 0; i < v.size(); i++) cout << v[i] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int n = (int)int(s.size());     int num = (s[n - 1] -  0 ) + 10 * (s[n - 2] -  0 );     if (num % 4 == 0)       cout << 4;     else       cout << 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void dfs(long long a, long long b, char A, char B) {     if (a == 1 && b == 1) return;     if (a < b) swap(a, b), swap(A, B);     printf( %I64d%c , (a - 1) / b, A);     dfs(a - (a - 1) / b * b, b, A, B);   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     long long a, b;     cin >> a >> b;     if (gcd(a, b) != 1) {       puts( Impossible );       return 0;     }     dfs(a, b,  A ,  B );     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   struct node;   typedef node *Node;   const int inf = 1000000;   bool fl = 0;   struct node {     Node left, right, link, parent;     int size, prior, sum, number;     set<int> all;     pair<int, int> mmin;     node(int number) : number(number) {       left = right = link = parent = NULL;       prior = rand() + (rand() << 16);       update();     }     int key() {       int ans = 1;       if (left) ans += left->size;       return ans;     }     void update() {       size = 1;       sum = all.size();       if (sum == 0)         mmin = make_pair(inf, inf);       else         mmin = make_pair(*all.begin(), number);       if (left) {         size += left->size;         sum += left->sum;         mmin = min(mmin, left->mmin);         left->parent = this;       }       if (right) {         size += right->size;         sum += right->sum;         mmin = min(mmin, right->mmin);         right->parent = this;       }     }   };   void upd_min(Node t) {     while (t) {       t->update();       t = t->parent;     }   }   void add_ppl(Node &t, int ppl) {     t->all.insert(ppl);     t->sum++;     upd_min(t);   }   void del_ppl(Node &t, int ppl) {     t->all.erase(ppl);     t->sum--;     upd_min(t);   }   Node merge(Node l, Node r) {     if (!l) return r;     if (!r) return l;     if (l->prior > r->prior) {       l->right = merge(l->right, r);       l->update();       return l;     } else {       r->left = merge(l, r->left);       r->update();       return r;     }   }   void split(Node t, int key, Node &l, Node &r) {     if (!t) return void(l = r = NULL);     if (t->key() <= key) {       split(t->right, key - t->key(), t->right, r);       l = t;     } else {       split(t->left, key, l, t->left);       r = t;     }     t->update();     t->parent = NULL;   }   int get_pos(Node t) {     vector<char> vc;     Node v = t;     while (v->parent) {       if (v->parent->left == v)         vc.push_back(0);       else         vc.push_back(1);       v = v->parent;     }     reverse(vc.begin(), vc.end());     int pos = 0;     for (int i = 0; i < vc.size(); ++i) {       if (vc[i] == 0)         v = v->left;       else         pos += v->key(), v = v->right;     }     pos += v->key();     return pos;   }   Node get_root(Node t) {     Node ans = NULL;     while (t) {       ans = t;       t = t->parent;     }     return ans;   }   Node leftmost(Node t) {     Node ans = NULL;     while (t) {       ans = t;       t = t->left;     }     return ans;   }   Node rightmost(Node t) {     Node ans = NULL;     while (t) {       ans = t;       t = t->right;     }     return ans;   }   void set_link(Node t, Node v) {     if (!t) return;     t->link = v;   }   void cut_out(Node t) {     int pos = get_pos(t);     Node l, r;     split(get_root(t), pos, l, r);     set_link(leftmost(r), t);   }   void cut_out1(Node t) {     int pos = get_pos(t);     Node l, r;     split(get_root(t), pos - 1, l, r);     set_link(leftmost(r), rightmost(l));   }   void expose(Node v) {     cut_out(v);     v = leftmost(get_root(v));     while (v->link) {       cut_out(v->link);       merge(get_root(v->link), get_root(v));       set_link(v, NULL);       v = leftmost(get_root(v));     }   }   void link(Node u, Node v) {     set_link(u, v);     expose(u);   }   void cut(Node u, Node v) {     expose(v);     set_link(u, NULL);   }   Node lca(Node v, Node u, bool &one_tree) {     expose(u);     if (get_root(u) == get_root(v)) {       one_tree = 1;       return v;     }     expose(v);     expose(u);     return leftmost(get_root(v))->link;   }   vector<Node> nodes;   const int maxn = 100000;   vector<pair<int, int> > add;   vector<int> g[maxn + 1];   vector<int> deep;   void dfs(int v, int p = -1) {     for (int i = 0; i < g[v].size(); ++i) {       int tv = g[v][i];       if (tv == p) continue;       deep[tv] = deep[v] + 1;       dfs(tv, v);     }   }   void print_(Node v) {     if (!v) return;     print_(v->left);     cout <<   t  << v->number <<     << (v->link ? v->link->number : -1) << endl;     print_(v->right);   }   void print(Node v) { print_(get_root(v)); }   void print_all(int n) {     for (int i = 1; i <= n; ++i) {       cout << i <<  : n ;       print(nodes[i]);       cout << endl;     }   }   void do_the_thing(Node v, int x) {     Node t = get_root(v);     int k = min(x, t->sum);     cout << k <<    ;     vector<pair<int, int> > add_after;     for (int i = 0; i < k; ++i) {       pair<int, int> cmin = t->mmin;       cout << cmin.first <<    ;       del_ppl(nodes[cmin.second], cmin.first);       add_after.push_back(cmin);     }     cout << endl;     for (int i = 0; i < add_after.size(); ++i) {       pair<int, int> ct = add_after[i];       add_ppl(nodes[ct.second], ct.first);     }   }   int main() {     srand(time(NULL));     ios_base::sync_with_stdio(0);     int n, m, q;     cin >> n >> m >> q;     nodes.resize(n + 1);     deep.assign(n + 1, 0);     for (int i = 1; i <= n; ++i) nodes[i] = new node(i);     for (int i = 0; i < n - 1; ++i) {       int a, b;       cin >> a >> b;       add.push_back(make_pair(a, b));       g[a].push_back(b);       g[b].push_back(a);     }     dfs(1);     for (int i = 0; i < add.size(); ++i) {       int a = add[i].first;       int b = add[i].second;       if (deep[a] < deep[b]) swap(a, b);       link(nodes[a], nodes[b]);     }     for (int i = 1; i <= m; ++i) {       int city;       cin >> city;       add_ppl(nodes[city], i);     }     for (int i = 0; i < q; ++i) {       int v, u, x;       cin >> v >> u >> x;       bool one_tree = 0;       Node t = lca(nodes[v], nodes[u], one_tree);       if (one_tree) {         cut_out1(nodes[v]);         do_the_thing(nodes[v], x);       } else {         int split_key = get_root(nodes[v])->size;         cut_out1(t);         merge(get_root(nodes[v]), get_root(t));         do_the_thing(nodes[v], x);         Node l, r;         split(get_root(nodes[v]), split_key, l, r);       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:100000000000000 )   using namespace std;   const long long int INF = 2e18;   int nex[20000][35];   int at[35];   int main() {     string f, s;     cin >> f >> s;     memset(nex, -1, sizeof nex);     memset(at, -1, sizeof at);     for (int i = 0; i < f.size(); i++) {       int he = f[i] -  a ;       if (at[he] == -1) {         at[he] = i;       }     }     for (int i = f.size() - 1; i >= 0; i--) {       for (int j = 0; j < 35; j++) {         nex[i][j] = at[j];       }       at[f[i] -  a ] = i;     }     int now = (f[0] == s[0] ? 0 : nex[0][s[0] -  a ]);     int cur = 0;     int cnt = 1;     while (cur < s.size()) {       if (now == -1) {         cout << -1;         return 0;       }       cur++;       if (cur == s.size()) break;       int gogo = nex[now][s[cur] -  a ];       if (gogo == -1) {         cout << -1;         return 0;       }       if (gogo <= now) {         cnt++;       }       now = gogo;     }     cout << cnt;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<int> dis(n + 1, n), cnt(n + 1, -1);     while (m--) {       int a, b;       cin >> a >> b;       ++cnt[a];       dis[a] = min(dis[a], a > b ? b - a + n : b - a);     }     for (int i = 1; i <= n; ++i) {       int res = 0;       for (int j = 1; j <= n; ++j)         if (~cnt[j])           res = max(res, (i > j ? j - i + n : j - i) + cnt[j] * n + dis[j]);       cout << res <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     string s, mx;     cin >> s;     int len = s.length();     for (int i = 1; i < 1 << len; i++) {       string foo;       for (int j = 0; j < len; j++)         if (i & (1 << j)) foo += s[j];       bool flag = 1;       for (int k = 0; k <= foo.length() / 2; k++)         if (foo[k] != foo[foo.length() - k - 1]) flag = 0;       if (flag && foo > mx) mx = foo;     }     cout << mx;   }


#include <bits/stdc++.h>   using namespace std;   const int size = 300000001;   bitset<size> gut;   int main() {     int l, r;     cin >> l >> r;     gut.set();     for (int i = 3; i * i <= r; i += 2) {       if (gut[i])         for (int j = i * i; j <= r; j += (i << 1)) {           gut[j] = false;         }     }     int rez = 0;     rez = (l <= 2 && r >= 2);     for (int i = 5; i <= r; i += 4) {       if (i >= l && gut[i]) rez++;     }     cout << rez << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3010;   struct edge {     int v, next, flag;   } e[N << 2];   int head[N], cnt;   void addedge(int u, int v, int flag) {     e[cnt].v = v;     e[cnt].flag = flag;     e[cnt].next = head[u];     head[u] = cnt++;   }   int sum[N], dp[N];   void dfs(int u, int pre) {     sum[u] = 0;     for (int i = head[u]; i != -1; i = e[i].next) {       if (e[i].v == pre) continue;       dfs(e[i].v, u);       int v = e[i].v;       sum[u] += sum[v] + e[i].flag;     }     dp[u] = sum[u];     for (int i = head[u]; i != -1; i = e[i].next) {       if (e[i].v == pre) continue;       int v = e[i].v;       dp[u] = min(dp[u], sum[u] - sum[v] + 1 - e[i].flag + dp[v] - e[i].flag);     }   }   int main() {     memset(head, -1, sizeof(head));     int n;     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int x, y, z;       scanf( %d%d , &x, &y);       addedge(x, y, 0);       addedge(y, x, 1);     }     int ans = 0x7fffffff;     for (int i = 1; i <= n; i++) {       dfs(i, -1);       for (int j = 1; j <= n; j++) {         ans = min(ans, sum[i] - sum[j] + dp[j]);       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e3 + 100;   int N;   int p[MAXN], rp[MAXN];   char ans[MAXN][MAXN];   bool v[MAXN];   int lvl, C;   bool pd;   bool id(int* a, int as) {     for (int i = 1; i <= as; i++)       if (a[i] != i) return false;     return true;   }   char get_dir(bool d) { return d ?  /  :     ; }   char get_dir(int a, int b) {     assert(a != b);     return get_dir(a < b);   }   bool addE(int a, int b) {     assert(a != b);     if (a < b) {       ans[lvl][a] = ans[lvl][b] =  / ;       return true;     }     if (a > b) {       ans[lvl][a] = ans[lvl][b] =     ;       return false;     }     assert(false);     return false;   }   void init(int n) {     assert(p[n] != n);     v[n] = true;     C = n;     for (int i = p[n]; i != n; i = p[i]) {       v[i] = true;       pd = addE(p[i], i);       lvl--;     }   }   int addFE(int n, int lf) {     if ((pd) xor (n > C)) {       ans[lvl][n] = get_dir(p[n], C);       ans[lf][C] = get_dir(C, n);       pd = C < n;       ans[lf][n] = get_dir(C, n);       return -1;     }     if ((p[n] < n) xor (n < C)) {       ans[lvl - 1][C] = get_dir(p[n], C);       ans[lf][C] = get_dir(C, n);       ans[lf][n] = get_dir(C, n);       return -1;     }     assert(false);     return -1;   }   void fill(int n) {     v[n] = true;     int l = 1, s = -1;     for (int i = p[n]; i != n; i = p[i]) {       v[i] = true, l++;       if (((p[n] < n) xor (C < n)) or (pd xor (n > C))) s = i;     }     lvl += addFE(s, lvl - l);     for (int i = p[s]; i != s; i = p[i]) {       addE(p[i], i), lvl--;     }   }   int main() {     scanf( %d , &N);     lvl = N;     pd = false;     for (int i = 0; i <= N; i++) v[i] = false, p[i] = rp[i] = -1;     for (int i = 1; i <= N; i++) scanf( %d , rp + i);     for (int i = 1; i <= N; i++) p[rp[i]] = i;     for (int i = 1; i <= N; i++)       for (int j = 1; j <= N; j++) ans[i][j] =  . ;     if (id(p, N)) {       printf( %d n , N);       for (int i = 1; i <= N; i++, printf(  n ))         for (int j = 1; j <= N; j++) printf( %c , ans[i][j]);       return 0;     }     for (int i = 1; i <= N; i++)       if (p[i] == i) v[i] = true;     for (int i = 1, c = 1; c and i <= N; i++)       if (!v[i]) init(i), c = 0;     lvl++;     for (int i = 1; i <= N; i++)       if (!v[i]) fill(i);     printf( %d n , N - 1);     for (int i = 1; i <= N; i++, printf(  n ))       for (int j = 1; j <= N; j++) printf( %c , ans[i][j]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t[35], mp[400][400], n;   void dfs(int step, int x, int y, int dir) {     if (step > n) return;     if (dir == 0) {       dfs(step + 1, x, y + t[step], 1 - dir);       for (int i = 0; i < 400; i++)         for (int j = 0; j < 400; j++)           if (mp[i][j]) {             mp[i][j] = mp[x * 2 - i][j] = 1;           }       for (int i = 1; i <= t[step]; i++) mp[x][y + i] = 1;     } else {       dfs(step + 1, x + t[step], y + t[step], 1 - dir);       int b = y - x;       for (int i = 0; i < 400; i++)         for (int j = 0; j < 400; j++)           if (mp[i][j]) {             mp[i][j] = mp[j - b][i + b] = 1;           }       for (int i = 1; i <= t[step]; i++) {         mp[x + i][y + i] = 1;       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , t + i);     memset(mp, 0, sizeof(mp));     dfs(1, 200, 199, 0);     int ans = 0;     for (int i = 0; i < 400; i++)       for (int j = 0; j < 400; j++)         if (mp[i][j]) ans++;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, X, Y, Z, p;     int x, y;     while (scanf( %d%d%d%d%d%d , &n, &m, &X, &Y, &Z, &p) != EOF) {       X %= 4;       Y %= 2;       Z %= 4;       int tn = n;       int tm = m;       for (int i = 1; i <= p; i++) {         n = tn;         m = tm;         scanf( %d%d , &x, &y);         if (X == 1) {           int xx = x;           x = y;           y = n + 1 - xx;           int nn = n;           n = m;           m = nn;         } else if (X == 2) {           x = n + 1 - x;           y = m + 1 - y;         } else if (X == 3) {           int xx = x;           x = m + 1 - y;           y = xx;           int nn = n;           n = m;           m = nn;         }         if (Y == 1) {           y = m + 1 - y;         }         if (Z == 1) {           int xx = x;           x = m + 1 - y;           y = xx;           int nn = n;           n = m;           m = nn;         } else if (Z == 2) {           x = n + 1 - x;           y = m + 1 - y;         } else if (Z == 3) {           int xx = x;           x = y;           y = n + 1 - xx;           int nn = n;           n = m;           m = nn;         }         printf( %d %d n , x, y);       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   const int INF = 1e9 + 7;   const int N = 4e5 + 7;   const int M = 5e6 + 7;   int vis[M];   int p[N];   int qt[N];   signed main() {     ios::sync_with_stdio(false);     int q, x, y;     cin >> q >> x;     set<pair<int, int>> vals;     for (int i = 0; i < x; i++) vals.insert({qt[i], i});     while (q--) {       int y;       cin >> y;       y %= x;       vals.erase({qt[y], y});       vals.insert({++qt[y], y});       auto k = *vals.begin();       int nVal = k.first * x + k.second;       cout << nVal << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double n, m;     cin >> n >> m;     printf( %.10lf n , n * pow(1.000000011, m));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, x, l[60], r[60], ans;   int main() {     scanf( %d %d , &n, &x);     for (int i = 1; i <= n; ++i) {       scanf( %d %d , &l[i], &r[i]);     }     for (int i = 1; i <= n; ++i) {       ans += (l[i] - r[i - 1] - 1) % x;       ans += r[i] - l[i] + 1;     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2000 + 7, Mod = 1000000000 + 7;   inline int Inc(int A, int B) {     A += B;     return A >= Mod ? A - Mod : A;   }   inline int Dec(int A, int B) {     A -= B;     return A < 0 ? A + Mod : A;   }   int n, m, A, B, Dpd[N][N], Dpr[N][N], Sumd[N][N], Sumr[N][N], d[N][N], r[N][N];   char Map[N][N];   int main() {     scanf( %d%d , &n, &m);     for (int i = (1), iLimit = (n); i <= iLimit; (++(i)))       for (int j = (1), jLimit = (m); j <= jLimit; (++(j))) cin >> Map[i][j];     if (n == 1 && m == 1) {       if (Map[1][1] ==  R )         puts( 0 );       else         puts( 1 );       return 0;     }     for (int i = (n), iLimit = (1); i >= iLimit; (--(i)))       for (int j = (m), jLimit = (1); j >= jLimit; (--(j))) {         d[i][j] = d[i + 1][j] + (Map[i + 1][j] ==  R );         r[i][j] = r[i][j + 1] + (Map[i][j + 1] ==  R );       }     Dpd[n][m] = Dpr[n][m] = Sumd[n][m] = Sumr[n][m] = (Map[n][m] !=  R );     for (int i = (n), iLimit = (1); i >= iLimit; (--(i)))       for (int j = (m), jLimit = (1); j >= jLimit; (--(j)))         if (i != n || j != m) {           Dpr[i][j] = Dec(Sumd[i][j + 1], Sumd[i][m - r[i][j] + 1]);           Dpd[i][j] = Dec(Sumr[i + 1][j], Sumr[n - d[i][j] + 1][j]);           Sumr[i][j] = Inc(Sumr[i + 1][j], Dpr[i][j]);           Sumd[i][j] = Inc(Sumd[i][j + 1], Dpd[i][j]);         }     printf( %d , Inc(Dpr[1][1], Dpd[1][1]));     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   const long long INF = 1e9;   const long long mod = 1e9 + 7;   const long long LINF = 1e14;   using namespace std;   long long n, l;   vector<long long> c;   vector<int> b;   int main() {     std::ios::sync_with_stdio(false);     cin >> n >> l;     c.resize(n);     cin >> c[0];     for (long long i = (1); i < (n); i++) {       cin >> c[i];       c[i] = min(c[i - 1] * 2, c[i]);     }     while (l > 0) b.push_back(l % 2), l /= 2;     while (c.size() < b.size()) c.push_back(c.back() * 2);     while (b.size() < c.size()) b.push_back(0);     c.push_back(c.back() * 2);     n = b.size();     vector<long long> d(n + 1, LINF);     d[0] = 0;     for (long long i = (1); i < (n + 1); i++) {       d[i] = min(d[i - 1], c[i - 1]);       if (b[i - 1] == 1) d[i] += c[i - 1];     }     cout << min(d[n], c.back()) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x;     cin >> x;     int k = x % 4;     if (k == 1) {       cout << 0 <<   A  << endl;     } else if (k == 0) {       cout << 1 <<   A  << endl;     } else if (k == 2) {       cout << 1 <<   B  << endl;     } else {       cout << 2 <<   A  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   char arr[50][50];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         if ((i % 2 == 0 ||              ((i % 4 == 1 && j == m - 1) || (i % 4 == 3 && j == 0))))           arr[i][j] =  # ;         else           arr[i][j] =  . ;       }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) cout << arr[i][j];       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   inline void chmin(A &a, B b) {     if (a > b) a = b;   }   template <typename A, typename B>   inline void chmax(A &a, B b) {     if (a < b) a = b;   }   struct UnionFindTree {     vector<long long> par, sz;     UnionFindTree(long long n) {       par.resize(n);       sz.resize(n);       for (long long i = 0; i < n; i++) {         par[i] = i;         sz[i] = 1;       }     }     long long find(long long x) {       return x == par[x] ? x : par[x] = find(par[x]);     }     void unite(long long x, long long y) {       x = find(x);       y = find(y);       if (x == y) return;       if (sz[x] < sz[y]) swap(x, y);       sz[x] += sz[y];       par[y] = x;     }     bool areSame(long long x, long long y) { return find(x) == find(y); }     long long size(long long x) { return sz[find(x)]; }   };   const long long SIZE = 1000000;   long long N, M;   long long A[SIZE], B[SIZE];   long long cnt[SIZE];   signed main() {     scanf( %lld%lld , &N, &M);     for (long long i = 0; i < (M); i++) scanf( %lld%lld , &A[i], &B[i]);     vector<long long> vs;     for (long long i = 0; i < (M); i++) {       vs.push_back(A[i]);       vs.push_back(B[i]);     }     sort((vs).begin(), (vs).end());     vs.erase(unique((vs).begin(), (vs).end()), vs.end());     for (long long i = 0; i < (M); i++) {       A[i] = lower_bound((vs).begin(), (vs).end(), A[i]) - vs.begin();       B[i] = lower_bound((vs).begin(), (vs).end(), B[i]) - vs.begin();     }     N = vs.size();     UnionFindTree uf(N);     for (long long i = 0; i < (M); i++) uf.unite(A[i], B[i]);     if (uf.size(0) != N) {       puts( 0 );       return 0;     }     long long luz = 0;     for (long long i = 0; i < (M); i++) {       if (A[i] == B[i])         luz++;       else {         cnt[A[i]]++;         cnt[B[i]]++;       }     }     long long ans = 0;     ans += luz * (M - luz);     ans += luz * (luz - 1) / 2;     for (long long i = 0; i < N; i++) ans += cnt[i] * (cnt[i] - 1) / 2;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int N = 100000, M = 100000;   long long int MOD = 1000000007;   void solve() {     long long int n;     cin >> n;     long long int a[n + 1], b[n + 1];     for (long long int i = 0; i < n; i++) cin >> a[i];     for (long long int i = 0; i < n; i++) {       b[i] = a[i] - i;       if (b[i] < 0)         b[i] = 0;       else if (b[i] % n == 0)         b[i] = b[i] / n;       else         b[i] = (b[i] / n) + 1;     }     for (long long int i = 0; i < n; i++) b[i] = i + (n * b[i]);     long long int min1 = b[0];     long long int ans = 0;     for (long long int i = 0; i < n; i++) {       if (min1 > b[i]) {         min1 = b[i];         ans = i;       }     }     cout << ans + 1;   }   int main() {     long long int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int k, m;   set<int> S;   int pw[10];   void rec(int ind, int number, int value) {     if (ind == 4) {       if (value > k && value - k < 10000) {         S.insert(number * 10000 + (value - k));       }       if (k >= value && k - value < 10000) {         S.insert(number * 10000 + (k - value));       }       return;     }     for (int i = 1; i + ind < 5; i++) {       for (int j = 0; j < pw[i]; j++) {         if (ind == 0) {           rec(ind + i, j, j);         } else {           rec(ind + i, number * pw[i] + j, value + j);           rec(ind + i, number * pw[i] + j, value - j);           rec(ind + i, number * pw[i] + j, value * j);         }       }     }   }   int main() {     scanf( %d , &k);     scanf( %d , &m);     pw[0] = 1;     for (int i = 1; i <= 6; i++) pw[i] = pw[i - 1] * 10;     rec(0, 0, 0);     for (set<int>::iterator it = S.begin(); (it != S.end()) && m; it++, m--) {       cout << setw(8) << setfill( 0 ) << (*it) << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   int main() {     int cases;     scanf( %d , &cases);     while (cases--) {       char str[1009];       int ret[9];       scanf( %s , str);       int num = 0;       int i;       int cnt = 0;       int flag = -1;       for (i = 0; str[i]; i++) {         if (str[i] >=  A  && str[i] <=  Z ) {           str[i] = str[i] -  A  +  a ;         }         if (str[i] ==  : ) {           if (str[i + 1] ==  : ) {             flag = cnt;             i++;           }           ret[cnt] = num;           cnt++;           num = 0;         } else if (str[i] >=  0  && str[i] <=  9 ) {           num = num * 16 + str[i] -  0 ;         } else {           num = num * 16 + str[i] -  a  + 10;         }       }       ret[cnt++] = num;       int k = 0;       for (int j = 0; j < cnt; j++) {         printf( %04x , ret[j]);         k++;         if (k != 8) {           putchar( : );         }         if (flag == j) {           for (i = 0; i < 8 - cnt; i++) {             printf( 0000 );             k++;             if (k != 8) {               putchar( : );             }           }         }       }       putchar(10);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, j, r, mn, mx, x, cnt = 0, a = 1, b = 1;     string s;     cin >> n;     for (i = 0; i < n; i++) {       cin >> s;       if (s ==  UR  || s ==  DL )         a++;       else if (s ==  UL  || s ==  DR )         b++;       else {         a++, b++;       }     }     cout << a * b << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2e9;   int n;   pair<int, int> p[25];   int dis[25][25];   int dp[1 << 24 | 1];   int pre[1 << 24 | 1];   int main() {     scanf( %d%d%d , &p[0].first, &p[0].second, &n);     for (int i = 1; i <= n; ++i) {       scanf( %d%d , &p[i].first, &p[i].second);     }     for (int i = 0; i <= n; ++i)       for (int j = i; j <= n; ++j) {         dis[i][j] = dis[j][i] =             pow(p[i].first - p[j].first, 2) + pow(p[i].second - p[j].second, 2);       }     for (int m = 1; m < (1 << n); ++m) {       dp[m] = INF;     }     dp[0] = 0;     for (int m = 0; m < (1 << n); ++m) {       if (dp[m] == INF) continue;       for (int i = 1; i <= n; ++i) {         if (m & 1 << (i - 1)) continue;         for (int j = 1; j <= n; ++j) {           if (m & 1 << (j - 1)) continue;           if (dp[m | (1 << i - 1) | (1 << j - 1)] >               dp[m] + dis[0][i] + dis[i][j] + dis[j][0]) {             dp[m | (1 << i - 1) | (1 << j - 1)] =                 dp[m] + dis[0][i] + dis[i][j] + dis[j][0];             pre[m | (1 << i - 1) | (1 << j - 1)] = m;           }         }         break;       }     }     printf( %d n , dp[(1 << n) - 1]);     int num = (1 << n) - 1;     while (num) {       printf( 0  );       int t = pre[num] ^ num;       for (int i = 0; i < n; ++i) {         if (t & (1 << i)) {           printf( %d  , i + 1);         }       }       num = pre[num];     }     printf( 0 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 1000001;   long long n, l[MAXN], r[MAXN], id[MAXN], K;   bool cmp(long long i, long long j) {     return (l[i] / K == l[j] / K)                ? ((l[i] / K & 1) ? (r[i] < r[j]) : (r[i] > r[j]))                : (l[i] < l[j]);   }   int main() {     scanf( %lld , &n);     K = 1000;     for (long long i = 1; i <= n; i++) scanf( %lld%lld , &l[i], &r[i]), id[i] = i;     sort(id + 1, id + 1 + n, cmp);     for (int i = 1; i <= n; i++) printf( %lld  , id[i]);   }


#include <bits/stdc++.h>   using namespace std;   const long long neutral = 0;   class SegmentTree {     vector<long long> a;     int n;       public:     SegmentTree(long long* st, long long* en) {       int sz = int(en - st);       for (n = 1; n < sz; n <<= 1)         ;       a.assign(n << 1, neutral);       for (int i = 0; i < sz; i++) a[i + n] = st[i];       for (int i = n + sz - 1; i > 1; i--) a[i >> 1] = ((a[i >> 1]) | (a[i]));     }     void update(int i, long long x) {       a[i += n] = x;       for (i >>= 1; i; i >>= 1) a[i] = ((a[i << 1]) | (a[1 + (i << 1)]));     }     long long query(int l, int r) {       long long ans = neutral;       for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {         if (l & 1) ans = ((ans) | (a[l++]));         if (r & 1) ans = ((ans) | (a[--r]));       }       return ans;     }   };   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     cin.exceptions(cin.failbit);     int n, q, m;     string s;     cin >> s;     n = (int)(s).size();     long long v[n];     for (int i = 0; i < (n); ++i) {       v[i] = 1LL << (s[i] -  a );     }     cin >> q;     SegmentTree st(v, v + n);     while (q--) {       int t;       cin >> t;       if (t == 2) {         int a, b;         cin >> a >> b;         long long res = st.query(a - 1, b - 1), c = 0;         while (res > 0) c++, res -= (res & -res);         cout << c << endl;       } else {         int a;         char c;         cin >> a >> c;         int b = c -  a ;         st.update(a - 1, 1LL << b);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9;   const long long INF = 1e15;   template <class o>   void qr(o& x) {     char c = getchar();     x = 0;     int f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) x = x * 10 + c -  0 , c = getchar();     x *= f;   }   template <class o>   void qw(o x) {     if (x / 10) qw(x / 10);     putchar(x % 10 +  0 );   }   template <class o>   void pr1(o x) {     if (x < 0) x = -x, putchar( - );     qw(x);     putchar(   );   }   template <class o>   void pr2(o x) {     if (x < 0) x = -x, putchar( - );     qw(x);     putchar(10);   }   template <class o>   void cmax(o& x, o y) {     if (x < y) x = y;   }   void cmax(int& x, int y) { x = x - y >> 31 ? y : x; }   template <class o>   void cmin(o& x, o y) {     if (x > y) x = y;   }   void cmin(int& x, int y) { x = x - y >> 31 ? x : y; }   int n, m, a[N], p[N], b[N];   struct BIT {     int c[N];     void clear() { memset(c + 1, 0, n * sizeof(int)); }     void clear(int x) {       for (; x <= n; x += x & -x) c[x] = 0;     }     int ask(int x) {       int y = 0;       for (; x; x -= x & -x) y += c[x];       return y;     }     int ask(int l, int r) { return ask(r) - ask(l - 1); }     void add(int x, int y) {       for (; x <= n; x += x & -x) c[x] += y;     }   } tr;   struct Q {     int l, r, y, v, id;     Q(int _l, int _r, int _y, int _v, int _id = 0)         : l(_l), r(_r), y(_y), v(_v), id(_id){};     Q(){};     bool operator<(Q b) const { return y < b.y; }   } q[N * 5], t[N * 5];   int tot, ans[N], id;   void cdq(int l, int r) {     if (r - l < 100) {       int j = 0;       for (auto i = l; i <= r; i++) {         if (!q[i].id)           t[++j] = q[i];         else           for (auto k = 1; k <= j; k++) {             if (q[i].l <= t[k].l && t[k].l <= q[i].r && t[k].y <= q[i].y)               ans[q[i].id] += q[i].v * t[k].v;           }       }       sort(q + l, q + r + 1);       return;     }     int mid = (l + r) / 2;     cdq(l, mid);     cdq(mid + 1, r);     Q* p = t;     for (int i = l, j = mid + 1; i <= mid || j <= r;) {       if (j > r || (i <= mid && q[i].y <= q[j].y)) {         if (!q[i].id) tr.add(q[i].l, q[i].v);         *p++ = q[i++];       } else {         if (q[j].id) ans[q[j].id] += q[j].v * tr.ask(q[j].l, q[j].r);         *p++ = q[j++];       }     }     if (r - l > 2333)       tr.clear();     else       for (auto i = l; i <= mid; i++)         if (!q[i].id) tr.clear(q[i].l);     for (auto i = r; i >= l; i--) q[i] = *--p;   }   void solve() {     qr(n);     qr(m);     for (auto i = 1; i <= n; i++) qr(a[i]), p[a[i]] = i;     for (auto i = 1; i <= n; i++)       qr(b[i]), b[i] = p[b[i]], q[++tot] = Q(i, i, b[i], 1);     int op, x, y, l, r;     while (m--) {       qr(op);       qr(x);       qr(y);       if (op == 1) {         qr(l);         qr(r);         ++id;         q[++tot] = Q(l, r, y, 1, id);         q[++tot] = Q(l, r, x - 1, -1, id);       } else {         q[++tot] = Q(x, x, b[x], -1);         q[++tot] = Q(y, y, b[y], -1);         swap(b[x], b[y]);         q[++tot] = Q(x, x, b[x], 1);         q[++tot] = Q(y, y, b[y], 1);       }     }     cdq(1, tot);     for (auto i = 1; i <= id; i++) pr2(ans[i]);   }   int main() {     int T = 1;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   template <class T>   class _Fenwick {    private:     std::vector<T> sum;     int _size;       public:     _Fenwick() { _size = 0; }     void reset(int n) {       _size = n;       sum.clear();       sum.resize(n + 1);     }     void add(int x, T y) {       while (x <= _size) {         sum[x] += y;         x += x & -x;       }     }     T query(int x, T y) {       T ans = y;       while (x) {         ans += sum[x];         x -= x & -x;       };       return ans;     }     void build(T *a, int n) {       reset(n);       for (int i = 1; i <= n; ++i) add(i, a[i]);     }   };   template <typename T>   void read(T &x);   template <typename T>   void read(T &x, T &y);   template <typename T>   void read(T &x, T &y, T &z);   template <typename T>   void write(T x);   template <typename T>   void writeC(T x);   template <typename T>   void writeN(T x);   template <typename T>   void _binary(T x, int len);   long long _qpow(long long a, long long b, long long M);   long long _inv(long long x, long long M);   long long ans;   int n, a[200005], b[200005][31], R[200005][31], L[200005][31], pre[35];   signed main() {     read(n);     for (int i = 1; i <= (n); ++i) read(a[i]);     ;     long long ans = (long long)n * (n + 1) / 2;     for (int i = 1; i <= n; ++i)       for (int j = 0; j <= 30; ++j) b[i][j] = (a[i] >> j) & 1;     for (int i = 1; i <= n; ++i) {       for (int j = 0; j <= 30; ++j) {         L[i][j] = pre[j];         if (b[i][j]) pre[j] = i;       }     }     for (int i = 0; i <= 30; ++i) pre[i] = n + 1;     for (int i = n; i >= 1; i--) {       for (int j = 0; j <= 30; ++j) {         R[i][j] = pre[j];         if (b[i][j]) pre[j] = i;       }     }     std::map<int, int> last;     for (int i = 1; i <= n; ++i) {       int l = last[a[i]] + 1, r = n;       for (int j = 0; j <= 30; ++j)         if (!b[i][j]) {           l = std::max(l, L[i][j] + 1);           r = std::min(r, R[i][j] - 1);         }       ans -= (long long)(i - l + 1) * (r - i + 1);       last[a[i]] = i;     }     printf( %lld , ans);   }   template <typename T>   void read(T &x) {     x = 0;     char c = getchar();     T flag = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) flag = -flag;       c = getchar();     }     while ( 0  <= c && c <=  9 ) {       x = x * 10 + c -  0 ;       c = getchar();     }     x *= flag;   }   template <typename T>   void read(T &x, T &y) {     read(x);     read(y);   }   template <typename T>   void read(T &x, T &y, T &z) {     read(x);     read(y);     read(z);   }   template <typename T>   void writeC(T x) {     if (x < 0) x = ~x + 1, putchar( - );     if (x > 9) writeC(x / 10);     putchar(x % 10 +  0 );   }   template <typename T>   void write(T x) {     writeC(x);     putchar(   );   }   template <typename T>   void writeN(T x) {     writeC(x);     puts(  );   }   template <typename T>   void _binary(T x, int len) {     for (int i = len - 1; i >= 0; --i) writeC((x >> i) & 1);   }   long long _inv(long long x, long long M) {     assert(x != 0);     return x == 1 ? 1 : (M - M / x) * _inv(M % x, M) % M;   }   long long _qpow(long long a, long long b, long long M) {     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a % M;       a = a * a % M;       b >>= 1;     }     return res;   }


#include <bits/stdc++.h>   using namespace std;   int num[400005];   int tot = 0;   set<int> st;   int mp[20];   int freq[20];   long long push[20][20];   long long addp[20][1048576];   vector<int> pos[20];   long long sum[20];   int cnt[20];   long long memo[1048576];   long long cost(int mask, int ofst, int id) {     return sum[id] + addp[id][mask] -            1LL * (ofst + 1 + ofst + cnt[id]) * cnt[id] / 2;     ;   }   long long dp(int i, int mask, int ofst) {     if (i == tot) return 0;     if (memo[mask] != -1) return memo[mask];     long long mn = 1000000000000000000;     int id;     for (int j = (0); j <= (tot - 1); j++) {       if ((mask >> j) & 1) continue;       long long v =           dp(i + 1, mask | (1 << j), ofst + cnt[j]) + cost(mask, ofst, j);       if (v < mn) {         mn = v;         id = j;       }     }     return memo[mask] = mn;   }   int main() {     int n;     scanf( %d , &n);     for (int i = (1); i <= (n); i++) {       scanf( %d , &num[i]);       --num[i];       st.insert(num[i]);     }     for (auto u : st) {       mp[u] = tot++;     }     for (int i = (0); i <= (tot - 1); i++) pos[i].push_back(0);     for (int i = (1); i <= (n); i++) {       num[i] = mp[num[i]];       int t = num[i];       pos[t].push_back(i);       cnt[t]++;       sum[t] += i;     }     memset(push, 0, sizeof(push));     for (int i = (1); i <= (n); i++) {       freq[num[i]]++;       for (int j = (0); j <= (tot - 1); j++) {         push[num[i]][j] += cnt[j] - freq[j];       }     }     memset(addp, 0, sizeof(addp));     for (int i = (0); i <= (tot - 1); i++) {       for (int j = (0); j <= ((1 << tot) - 1); j++) {         if ((j >> i) & 1) continue;         for (int k = (0); k <= (tot - 1); k++) {           if (~(j >> k) & 1) continue;           addp[i][j] += push[i][k];         }       }     }     memset(memo, -1, sizeof(memo));     printf( %lld n , dp(0, 0, 0));     return 0;   }