malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   template <typename T, typename U>   inline void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   inline void amax(T &x, U y) {     if (x < y) x = y;   }   template <typename T>   inline void write(T x) {     int i = 20;     char buf[21];     buf[20] =   n ;     do {       buf[--i] = x % 10 +  0 ;       x /= 10;     } while (x);     do putchar(buf[i]);     while (buf[i++] !=   n );   }   template <typename T>   inline T readInt() {     T n = 0, s = 1;     char p = getchar();     if (p ==  - ) s = -1;     while ((p <  0  || p >  9 ) && p != EOF && p !=  - ) p = getchar();     if (p ==  - ) s = -1, p = getchar();     while (p >=  0  && p <=  9 ) {       n = (n << 3) + (n << 1) + (p -  0 );       p = getchar();     }     return n * s;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, k, *h, smin, c;     n = readInt<long long int>();     h = (long long int *)malloc(n * sizeof(long long int));     k = readInt<long long int>();     for (int i = 0; i < n; i += 1) h[i] = readInt<long long int>();     sort(h, h + n);     reverse(h, h + n);     if (h[0] == h[n - 1]) {       write(0);       return 0;     }     smin = 1;     c = 0;     for (int i = 0; i < n - 1; i += 1) {       for (int j = h[i]; j >= h[i + 1] + 1; j -= 1) {         if (c + i + 1 > k) {           smin++;           c = i + 1;         } else           c += i + 1;       }     }     write(smin);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int smallest, largest, n, count = 0, a;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a;       if (i == 0) {         smallest = a;         largest = a;       } else {         if (a > largest) {           count += 1;           largest = a;         } else if (a < smallest) {           count += 1;           smallest = a;         }       }     }     cout << count;   }
#include <bits/stdc++.h>   using namespace std;   int n, a, b;   string s, g;   map<string, pair<int, int> > m;   int main() {     m[ goat ] = make_pair(1, 1);     m[ asiago ] = make_pair(1, 2);     m[ camembert ] = make_pair(1, 3);     m[ gouda ] = make_pair(2, 1);     m[ feta ] = make_pair(2, 2);     m[ swiss ] = make_pair(2, 3);     m[ muenster ] = make_pair(3, 1);     m[ cheddar ] = make_pair(3, 2);     m[ brie ] = make_pair(3, 3);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> s >> g;       if (g ==  soft ) {         a++;       } else {         b++;       }     }     int res = 1;     while ((res * res) / 2 + (res * res) % 2 < max(a, b) || res * res < a + b) {       res++;     }     cout << res <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 5;   int n, m, p;   const double PI = acos(-1);   vector<int> a, b;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     cin >> n >> m >> p;     for (int i = (int)1; i <= (int)n; i++) {       int x;       cin >> x;       a.push_back(x);     }     for (int i = (int)1; i <= (int)m; i++) {       int x;       cin >> x;       b.push_back(x);     }     int i = 0, j = 0;     while (i < n) {       if (a[i] % p != 0) break;       i++;     }     while (j < m) {       if (b[j] % p != 0) break;       j++;     }     cout << i + j << endl;   }
#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:400000000 )   int n, a[200010], b[200010], f[200010];   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       int x;       cin >> x;       f[x - 1] = i;     }     for (int i = 0; i < n; i++) b[i] = f[a[i] - 1];     for (int i = 1; i < n; i++) {       if (b[i] < b[i - 1]) {         cout << n - i;         return 0;       }     }     cout << 0;   }
#include <bits/stdc++.h>   using namespace std;   int memo[77][107][107];   inline int index(char ch) { return (ch ==  @ ) ? 0 : ch -  a ; }   bool eval(char ch, int i, int j, vector<vector<pair<int, char>>> &adj) {     int val = memo[index(ch)][i][j];     if (val == 1) return true;     if (val == 2) return false;     for (auto nxt : adj[i]) {       if (ch <= nxt.second) {         if (!eval(nxt.second, j, nxt.first, adj)) {           memo[index(ch)][i][j] = 1;           return true;         }       }     }     memo[index(ch)][i][j] = 2;     return false;   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     vector<vector<pair<int, char>>> adj(n + 1);     for (int i = 0; i < m; i++) {       int u, v;       char c;       cin >> u >> v >> c;       adj[u].push_back({v, c});     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) cout << (eval( @ , i, j, adj) ?  A  :  B );       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 126, MOD = 12345;   int res[maxn][maxn], tmp[maxn][maxn], POW[maxn][maxn], flag[maxn], check[maxn];   int A[maxn][maxn], B[maxn][maxn], a[maxn << 3], b[maxn << 3];   int m, size = 1, temp, ans, vec[maxn << 3], judge;   char ch;   long long n;   void mul(int a[maxn][maxn], int b[maxn][maxn], int c[maxn][maxn]) {     for (int i = 1; i <= size; i++)       for (int j = 1; j <= size; j++) {         tmp[i][j] = 0;         for (int k = 1; k <= size; k++)           tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j]) % MOD;       }     for (int i = 1; i <= size; i++)       for (int j = 1; j <= size; j++) c[i][j] = tmp[i][j];   }   void power(int a[maxn][maxn], long long p, int b[maxn][maxn]) {     for (int i = 1; i <= size; i++)       for (int j = 1; j <= size; j++) POW[i][j] = a[i][j], res[i][j] = (i == j);     while (p) {       if (p & 1) mul(res, POW, res);       mul(POW, POW, POW);       p >>= 1;     }     for (int i = 1; i <= size; i++)       for (int j = 1; j <= size; j++) b[i][j] = res[i][j];   }   int main() {     scanf( %lld%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf(  n%c %d , &ch, &b[i]);       a[i] = ch -  A ;       flag[a[i]] = 1;       size *= b[i];     }     for (int i = 0; i <= size - 1; i++) {       temp = i;       for (int j = m; j >= 1; j--) vec[j] = temp % b[j], temp /= b[j];       for (int j = 0; j <= 25; j++)         if (flag[j]) {           temp = 0;           for (int k = 1; k <= m; k++)             temp = temp * b[k] + (vec[k] + (a[k] == j)) % b[k];           A[temp + 1][i + 1]++;         }     }     power(A, n, A);     B[1][1] = 1;     mul(A, B, B);     for (int i = 0; i <= size - 1; i++) {       temp = i;       for (int j = m; j >= 1; j--) check[a[j]] |= !(temp % b[j]), temp /= b[j];       judge = 1;       for (int j = 0; j <= 25; j++) {         judge &= !(flag[j] ^ check[j]);         check[j] = 0;       }       if (judge) ans = (ans + B[i + 1][1]) % MOD;     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char s[100005];   char b[100005];   int main() {     cin >> s;     int cnt = 0;     char x =  a ;     for (int i = strlen(s); i >= 0; --i) {       if (s[i] >= x) {         x = s[i];         b[cnt++] = x;       }     }     while (cnt--) {       printf( %c , b[cnt]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<string> nap(n);     for (int i = 0; i < n; ++i) cin >> nap[i];     vector<vector<int> > dp(26);     for (int i = 0; i < 26; ++i)       for (int j = 0; j < 26; ++j) dp[i].push_back(0);     for (int i = 0; i < n; ++i) {       int dl = nap[i].size();       for (char c =  a ; c <=  z ; ++c) {         if (dp[c -  a ][nap[i][0] -  a ] != 0) {           dp[c -  a ][nap[i][dl - 1] -  a ] =               max(dp[c -  a ][nap[i][dl - 1] -  a ],                   dp[c -  a ][nap[i][0] -  a ] + dl);         }       }       dp[nap[i][0] -  a ][nap[i][dl - 1] -  a ] =           max(dp[nap[i][0] -  a ][nap[i][dl - 1] -  a ], dl);     }     int odp = 0;     for (int i = 0; i < 26; ++i) odp = max(odp, dp[i][i]);     cout << odp;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, dd, hh, mm, t;     int sep[16];     char buf[80];     vector<string> todo;     map<string, int> cost;     vector<pair<int, int> > time;     vector<vector<pair<int, bool> > > dp;     vector<pair<pair<int, int>, pair<int, int> > > job;     scanf( %d%d%d , &n, &m, &dd);     for (int i = 0; i < n; ++i) {       scanf( %s , buf);       todo.push_back(buf);     }     for (int i = 0; i < n; ++i) {       scanf( %d , &cost[todo[i]]);     }     for (int i = 0; i < 8; ++i) {       scanf( %*[^0-9]%d:%d , &hh, &mm);       sep[i] = hh * 60 + mm;     }     for (int i = 1, k = 0; i <= dd; ++i) {       for (int j = 0; j < 24 * 60; ++j) {         if (k % 2 == 0 && j == sep[k]) {           k = (k + 1) % 8;         }         if (k % 2 == 0) {           time.push_back(make_pair(i, j));         }         if (k % 2 != 0 && j == sep[k]) {           k = (k + 1) % 8;         }       }     }     for (int i = 0; i < m; ++i) {       scanf( %s%d%d:%d%d , buf, &dd, &hh, &mm, &t);       if (cost.count(buf) == 0) {         continue;       }       job.push_back(make_pair(make_pair(lower_bound(time.begin(), time.end(),                                                     make_pair(dd, hh * 60 + mm)) -                                             time.begin(),                                         i),                               make_pair(cost[buf], t)));     }     sort(job.begin(), job.end());     reverse(job.begin(), job.end());     n = time.size();     m = job.size();     dp = vector<vector<pair<int, bool> > >(1, vector<pair<int, bool> >(n + 1));     for (int i = 0; i < m; ++i) {       t = job[i].second.first;       dp.push_back(dp.back());       for (int j = 0; j <= n; ++j) {         dp[i + 1][j].second = false;       }       for (int j = t; j <= job[i].first.first; ++j) {         if (dp[i + 1][j - t].first < dp[i][j].first + job[i].second.second) {           dp[i + 1][j - t].first = dp[i][j].first + job[i].second.second;           dp[i + 1][j - t].second = true;         }       }     }     printf( %d n , dp[m][0].first);     todo.clear();     for (int i = m - 1, j = 0; i >= 0; --i) {       if (dp[i + 1][j].second) {         t = j + job[i].second.first - 1;         sprintf(buf,  %d %d %02d:%02d %d %02d:%02d , job[i].first.second + 1,                 time[j].first, time[j].second / 60, time[j].second % 60,                 time[t].first, time[t].second / 60, time[t].second % 60);         todo.push_back(buf);         j = t + 1;       }     }     printf( %d n , todo.size());     for (int i = 0; i < (int)todo.size(); ++i) {       puts(todo[i].c_str());     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool r[100010], c[100010];   int main() {     ios_base::sync_with_stdio(false);     long long n, m, a, b, li, co;     cin >> n >> m;     li = co = 0;     long long ans;     for (int i = 0; i < m; i++) {       cin >> a >> b;       if (r[a] == false) {         r[a] = true;         li++;       }       if (c[b] == false) {         c[b] = true;         co++;       }       ans = n * n - co * n;       ans = ans - li * (n - co);       if (i > 0) cout <<    ;       cout << ans;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<string, int> Ts;   string size[101] = { S ,  M ,  L ,  XL ,  XXL };   inline int to_size(string s) {     for (int i = 0; i < 5; ++i) {       if (s == size[i]) return i;     }   }   int main() {     int a[11], k;     string pe[10010];     for (int i = 0; i < 5; ++i) cin >> a[i];     for (int i = 0; i < 5; ++i) {       Ts[size[i]] = a[i];     }     cin >> k;     for (int i = 0; i < k; ++i) cin >> pe[i];     for (int i = 0; i < k; ++i) {       int pe1 = to_size(pe[i]);       for (int j = 0; j <= 4; ++j) {         if (pe1 + j <= 4) {           if (Ts[size[pe1 + j]] > 0) {             Ts[size[pe1 + j]]--;             cout << size[pe1 + j] << endl;             break;           }         }         if (pe1 - j >= 0) {           if (Ts[size[pe1 - j]] > 0) {             Ts[size[pe1 - j]]--;             cout << size[pe1 - j] << endl;             break;           }         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int maxn = 5010;   vector<int> G[maxn];   int d[maxn][maxn][2];   void check(int &a, int b) {     if (a > b) a = b;   }   int dfs(int u, int fa) {     int num = G[u].size(), tot = 0, now = 0;     d[u][0][0] = 0;     d[u][0][1] = 0;     if (num == 1) {       d[u][0][1] = INF;       d[u][1][1] = 0;       return 1;     }     for (int i = 0; i < num; ++i) {       int v = G[u][i];       if (v == fa) continue;       now = dfs(v, u);       tot += now;       for (int j = tot; j >= 0; --j) {         int mn0, mn1;         mn0 = mn1 = INF;         for (int k = 0; k <= now; ++k) {           check(mn0, min(d[u][j - k][0] + d[v][k][0],                          d[u][j - k][0] + d[v][k][1] + 1));           check(mn1, min(d[u][j - k][1] + d[v][k][0] + 1,                          d[u][j - k][1] + d[v][k][1]));         }         d[u][j][0] = mn0;         d[u][j][1] = mn1;       }     }     return tot;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       int u, v;       scanf( %d %d , &u, &v);       G[u].push_back(v);       G[v].push_back(u);     }     if (n == 2) {       puts( 1 );       return 0;     }     int cnt = 0, p = 1;     memset(d, INF, sizeof d);     for (int i = 1; i <= n; ++i) {       if (G[i].size() == 1)         ++cnt;       else         p = i;     }     dfs(p, 0);     int ans = min(d[p][cnt / 2][0], d[p][cnt / 2][1]);     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   const int inf = (1ll << 30) - 1;   const int maxn = (int)1e5 + 10;   const int mod = (int)1e9 + 7;   using namespace std;   void solve() {     string second;     cin >> second;     sort(second.begin(), second.end());     int cnt = 0;     vector<string> v;     string x =   ;     x += second[0];     for (int i = 1; i < second.size(); i++) {       if (second[i] != second[i - 1]) {         v.push_back(x);         x =   ;       }       x += second[i];     }     v.push_back(x);     if (v.size() == 1) {       cout << second <<   n ;       return;     }     string A =   ;     if (v.size() > 3) {       A = v[2] + v[0] + v[3] + v[1];       for (int i = 4; i < v.size(); i++) {         if (i % 2 == 0)           A += v[i];         else           A = v[i] + A;       }       cout << A <<   n ;       return;     }     if (v.size() == 2) {       if (v[1][0] - v[0][0] == 1) {         cout <<  No answer n ;         return;       }       cout << second <<   n ;       return;     }     if (v[1][0] - v[0][0] == 1 && v[2][0] - v[1][0] == 1) {       cout <<  No answer n ;       return;     }     if (v[1][0] - v[0][0] != 1 && v[2][0] - v[1][0] != 1) {       cout << second <<   n ;       return;     }     if (v[1][0] - v[0][0] == 1)       cout << v[0] << v[2] << v[1] <<   n ;     else       cout << v[1] << v[0] << v[2] <<   n ;   }   int main() {     int t = 1;     scanf( %d , &t);     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, K;   vector<int> G[700005], W[700005], I[700005];   long long int ptr[700005];   long long int ans[700005];   int dis[700005], rem[700005], ulta[700005], vis[700005], tim = 0, lev[700005];   int mx = 0;   vector<int> jinish[700005];   void dfs(int u, int l) {     lev[u] = l;     mx = max(mx, l);     vis[u] = 1;     dis[u] = ++tim;     ulta[tim] = u;     jinish[l].push_back(tim);     for (int v : G[u]) {       if (!vis[v]) {         dfs(v, l + 1);       }     }     rem[u] = tim;   }   void dfs2(int u) {     vis[u] = 1;     for (auto v : G[u]) {       if (!vis[v]) {         ans[v] += ans[u];         dfs2(v);       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d %d , &u, &v);       G[u].push_back(v);       G[v].push_back(u);     }     dfs(1, 1);     scanf( %d , &m);     while (m--) {       int u, d, x;       scanf( %d %d %d , &u, &d, &x);       ans[u] += x;       int go = min(lev[u] + d + 1, mx + 1);       int in = lower_bound(jinish[go].begin(), jinish[go].end(), dis[u]) -                jinish[go].begin();       if (in < jinish[go].size()) ptr[jinish[go][in]] += x;       int inn = upper_bound(jinish[go].begin(), jinish[go].end(), rem[u]) -                 jinish[go].begin();       if (inn < jinish[go].size()) ptr[jinish[go][inn]] -= x;     }     for (int i = 1; i <= mx; i++) {       for (int j = 1; j < jinish[i].size(); j++) {         ptr[jinish[i][j]] += ptr[jinish[i][j - 1]];       }     }     for (int i = 1; i <= mx; i++) {       for (int j = 0; j < jinish[i].size(); j++) {         ans[ulta[jinish[i][j]]] -= ptr[jinish[i][j]];       }     }     memset(vis, 0, sizeof vis);     dfs2(1);     bool ff = false;     for (int u = 1; u <= n; u++) {       if (ff) printf(   );       ff = true;       printf( %lld , ans[u]);     }     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   class Timer {     clock_t start;       public:     Timer() : start(clock()) {}     double now() { return (double)(clock() - start) / CLOCKS_PER_SEC; }   } timer;   struct node {     int u, v, l;     friend bool operator<(const node& a, const node& b) { return a.l < b.l; }   } in[300005];   int dp[300005];   queue<pair<int, int> > qe;   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int u, v, l;       scanf( %d%d%d , &in[i].u, &in[i].v, &in[i].l);     }     sort(in + 1, in + 1 + m);     for (int i = 1; i <= m;) {       int l = in[i].l;       while (i <= m && in[i].l == l) {         qe.push(make_pair(in[i].v, dp[in[i].u] + 1));         i++;       }       while (!qe.empty()) {         dp[qe.front().first] = max(dp[qe.front().first], qe.front().second);         qe.pop();       }     }     int ans = 0;     for (int i = 1; i <= n; i++) {       ans = max(ans, dp[i]);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, K;   int s[1000005], p[1000005];   int main() {     scanf( %d%d%d , &n, &m, &K);     ++K;     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       if (x + K == y)         s[x] = 1;       else if (x + 1 != y)         return puts( 0 ), 0;       ;     }     for (int i = 1; i <= n; i++) s[i] += s[i - 1];     for (int i = K; i <= n; i++)       if (s[i] - s[i - 1] && s[i - K]) return puts( 0 ), 0;     ;     p[0] = 1;     for (int i = 1; i <= n; i++) p[i] = p[i - 1] * 2 % 1000000007;     int ans = (s[n] == 0);     for (int i = 1; i <= n - K; i++)       if (s[i] == s[n] && (i <= K || s[i - K] == 0))         ans = (ans + p[min(i, K) - s[i - 1] - 1]) % 1000000007;     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, b, c, co, i, j, k, l, m, n;     cin >> n;     co = 0;     set<long long int> myset;     for (i = 0; i < n; i++) {       cin >> a;       myset.insert(a);     }     if (myset.size() > 3)       cout <<  NO n ;     else if (myset.size() == 3) {       i = 0;       for (auto it = myset.begin(); it != myset.end(); it++) {         if (i == 0) a = *it;         if (i == 1) b = *it;         if (i == 2) c = *it;         i++;       }       if (abs(a - b) == abs(b - c))         cout <<  YES n ;       else         cout <<  NO n ;     } else       cout <<  YES n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int mod = 1e9 + 7;   const int inf = (1 << 30) - 1;   const ll infll = (1LL << 61) - 1;   double a[1 << 17];   int b[1 << 17], N;   ll pos_sum = 0, neg_sum = 0;   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> N;     for (int i = 0; i < N; i++) {       cin >> a[i];       if (a[i] > 0) {         b[i] = ceil(a[i]);         pos_sum += b[i];       } else if (a[i] < 0) {         b[i] = floor(a[i]);         neg_sum += b[i];       }     }     if (abs(pos_sum) < abs(neg_sum)) {       for (int i = 0; i < N && abs(pos_sum) < abs(neg_sum); i++) {         if (a[i] < 0 && ceil(a[i]) != b[i]) {           b[i] = ceil(a[i]);           neg_sum++;         }       }     } else if (abs(pos_sum) > abs(neg_sum)) {       for (int i = 0; i < N && abs(pos_sum) > abs(neg_sum); i++) {         if (a[i] > 0 && floor(a[i]) != b[i]) {           b[i] = floor(a[i]);           pos_sum--;         }       }     }     for (int i = 0; i < N; i++) {       cout << b[i] << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     double a[1001][1001], x, y, z1, z2, z3, z4;     long long t, n, i, j;     for (i = 0; i < 1001; i++) {       if (i == 0)         a[0][0] = 0.0;       else         a[i][0] = 1.0;       a[0][i] = 0.0;       z1 = (double)i;       z2 = z1 + 1.0;       a[i][1] = z1 / z2;       z2 = z1 + 2.0;       if (i == 0)         continue;       else if (i == 1)         a[i][2] = 1.0 / 3.0;       else         a[i][2] = z1 / z2 + (2.0 / (z2 * (z2 - 1.0)));     }     for (i = 1; i < 1001; i++) {       for (j = 3; j < 1001; j++) {         z1 = (double)i;         z2 = (double)(j);         z3 = z1 + z2;         a[i][j] = z1 / z3;         z4 = (z2 / z3) * ((z2 - 1.0) / (z3 - 1.0)) *              (((z2 - 2.0) / (z3 - 2.0)) * a[i][j - 3] +               ((z1) / (z3 - 2.0)) * a[i - 1][j - 2]);         a[i][j] += z4;       }     }     cin >> i >> j;     printf( %.10lf , a[i][j]);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<char> A(n * 2);     for (int i = 0; i < n * 2; i++) cin >> A[i];     vector<vector<pair<int, int> > > table(n * 2, vector<pair<int, int> >(20));     for (int i = 0; i < n * 2; i++) {       if (A[i] ==  X  || i == n - 1 || i == n * 2 - 1)         table[i][0] = make_pair(-1, -1);       else {         if (A[i + 1] ==  . )           table[i][0] = make_pair(i + 1, 1);         else {           int num = (i < n ? i + n : i - n);           if (A[num] ==  . )             table[i][0] = make_pair(num, 1);           else             table[i][0] = make_pair(-1, -1);         }       }     }     for (int k = 1; k < 20; k++) {       for (int i = 0; i < n * 2; i++) {         if (table[i][k - 1].first == -1)           table[i][k] = make_pair(-1, -1);         else {           pair<int, int> p = table[i][k - 1];           table[i][k] = make_pair(table[p.first][k - 1].first,                                   p.second + table[p.first][k - 1].second);         }       }     }     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       a--, b--;       if (a % n > b % n) swap(a, b);       int k = 19;       int res = 0;       while (k >= 0) {         if (table[a][k].first != -1 && table[a][k].first % n < b % n) {           res += table[a][k].second;           a = table[a][k].first;         }         k--;       }       if (a % n != b % n) {         if (A[a + 1] ==  . )           a = a + 1, res++;         else {           a = a < n ? a + n : a - n, res++;           if (A[a] ==  X ) {             cout <<  -1 n ;             continue;           }           if (A[a + 1] ==  . )             a = a + 1, res++;           else {             cout <<  -1 n ;             continue;           }         }       }       if (a == b)         cout << res <<   n ;       else {         if (A[a < n ? a + n : a - n] ==  . ) {           a = (a < n ? a + n : a - n), res++;           if (a == b)             cout << res <<   n ;           else             cout <<  -1 n ;         } else           cout <<  -1 n ;       }     }     return 0;   }