malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y, e, t, d;   };   vector<node> v[555][555][2];   long long sub[555][2], sum[555][555];   long long dp[2][555][555][2];   int n, m;   const long long inf = 1e18;   void init(int type) {     for (int i = 0; i < m; i++)       for (int j = 0; j < max(n, m) + 1; j++)         for (int k = 0; k < 2; k++) dp[type][i][j][k] = inf;   }   int main() {     int k, x, y, d, t, e;     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i < k + 1; i++) {       scanf( %d%d%d%d%d , &x, &y, &d, &t, &e);       if ((x + y) >= t && (x + y - t) % 4 == 0) sum[x][y] += e;       if ((x + y) >= t + 1 && (x + y - t) % 4 == 1) sum[x + d][y - d] += e;       if ((x + y + d) >= t + 2 && (x + y + d - t) % 4 == 2)         v[x + d][y][0].push_back({x, y, e, t, d}), sum[x + d][y] += e;       if ((x + y + d) >= t + 3 && (x + y + d - t) % 4 == 3)         v[x][y + d][1].push_back({x, y, e, t, d}), sum[x][y + d] += e;     }     bool type = 1;     init(0);     dp[0][0][0][0] = 0;     for (int i = 0; i < n; i++) {       init(type);       for (int j = 0; j < m; j++) {         long long nowadd[2] = {sum[i][j], sum[i][j]};         memset(sub, 0, sizeof(sub));         for (auto &xx : v[i][j][0]) {           int x = xx.x, y = xx.y, t = xx.t;           if ((x + y) >= t && (x + y - t) % 4 == 0) sub[xx.d][1] += xx.e;           if ((x + y) >= t + 1 && (x + y - t) % 4 == 1) sub[xx.d][0] += xx.e;         }         for (auto &xx : v[i][j][1]) {           int x = xx.x, y = xx.y, t = xx.t;           if ((x + y) >= t && (x + y - t) % 4 == 0) sub[xx.d][0] += xx.e;         }         for (int kx = 0; kx < max(n, m) + 1; kx++)           for (int k = 0; k < 2; k++) {             if (k && i - kx < 0) continue;             if (!k && j - kx < 0) continue;             nowadd[k] -= sub[kx][k];             if (dp[!type][j][kx][k] != inf) {               if (!k)                 dp[!type][j + 1][kx + 1][0] = min(                     dp[!type][j + 1][kx + 1][0], dp[!type][j][kx][k] + nowadd[0]),                                       dp[type][j][1][1] =                                           min(dp[type][j][1][1],                                               dp[!type][j][kx][k] + nowadd[0]);               else                 dp[!type][j + 1][1][0] =                     min(dp[!type][j + 1][1][0], dp[!type][j][kx][k] + nowadd[1]),                               dp[type][j][kx + 1][1] =                                   min(dp[type][j][kx + 1][1],                                       dp[!type][j][kx][k] + nowadd[1]);             }           }       }       type = !type;     }     long long res = inf;     for (int i = 0; i < max(n, m) + 1; i++)       for (int j = 0; j < 2; j++) res = min(res, dp[!type][m - 1][i][j]);     printf( %I64d n , res);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int i, j, n;     set<long long int> s;     cin >> n;     for (i = 0; i < n; i++) {       cin >> j;       if (j != 0) {         s.insert(j);       }     }     cout << s.size() << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[200000 + 1];   struct info {     int sum, prop;   } t[3 * 200000 + 3];   void build(int node, int b, int e) {     if (b >= e) {       if (b == e) t[node].sum = a[b];       return;     }     int left = node << 1;     int right = left | 1;     int mid = (b + e) >> 1;     build(left, b, mid);     build(right, mid + 1, e);     t[node].sum = t[left].sum + t[right].sum;   }   void update(int node, int b, int e, int i, int j) {     if (i > e || j < b) return;     if (b > e) return;     if (b >= i && e <= j) {       t[node].sum += ((e - b + 1) * 1);       t[node].prop += 1;       return;     }     int left = node << 1;     int right = left | 1;     int mid = (b + e) >> 1;     update(left, b, mid, i, j);     update(right, mid + 1, e, i, j);     t[node].sum = t[left].sum + t[right].sum + (e - b + 1) * t[node].prop;   }   int query(int node, int b, int e, int pos, int carry = 0) {     if (pos < b || e < pos) return 0;     if (b > e) return 0;     if (b == pos && pos == e) return t[node].sum + carry * (e - b + 1);     int left = node << 1;     int right = left | 1;     int mid = (b + e) >> 1;     int p1 = query(left, b, mid, pos, carry + t[node].prop);     int p2 = query(right, mid + 1, e, pos, carry + t[node].prop);     return p1 + p2;   }   int chk[200000 + 1];   int main() {     int n, k, q, i, l, r;     cin >> n >> k >> q;     for (i = 0; i < n; i++) {       scanf( %d %d , &l, &r);       update(1, 1, 200000, l, r);     }     for (i = 1; i <= 200000; i++) {       if (query(1, 1, 200000, i) >= k) chk[i] = 1;     }     for (i = 1; i <= 200000; i++) chk[i] += chk[i - 1];     for (i = 0; i < q; i++) {       scanf( %d %d , &l, &r);       cout << chk[r] - chk[l - 1] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct RTC {};   const int maxn = 123456 + 5;   int n;   int root;   bool humans[maxn];   vector<int> g_tmp[maxn], g[maxn];   ;   bool cut(int u, int p = -1) {     bool ret = humans[u];     for (int v : g_tmp[u]) {       if (v == p) continue;       if (cut(v, u)) {         ret = true;         g[u].push_back(v);       }     }     return ret;   }   int cnt[maxn];   int deep[maxn];   void count(int u) {     cnt[u] = 1;     deep[u] = 0;     for (int v : g[u]) {       count(v);       cnt[u] += cnt[v];       deep[u] = max(deep[u], 1 + deep[v]);     }   }   int maxlen = -1, id = -1;   void solve(int u, int up) {     if (humans[u]) {       int res = max(deep[u], up);       if (res > maxlen) {         maxlen = res;         id = u;       } else if (res == maxlen) {         id = min(id, u);       }     }     if (((int)(g[u]).size()) > 0) {       int down_v = 0;       for (int i = 1; i < ((int)(g[u]).size()); i++)         if (deep[g[u][i]] > deep[g[u][down_v]]) down_v = i;       if (up >= (deep[g[u][down_v]] + 1)) {         for (int v : g[u]) solve(v, up + 1);       } else {         for (int i = 0; i < (((int)(g[u]).size())); i++) {           if (i == down_v) continue;           solve(g[u][i], deep[g[u][down_v]] + 2);         }         int second_down_v = -1;         for (int i = 0; i < (((int)(g[u]).size())); i++) {           if (i == down_v) continue;           if (second_down_v == -1 || deep[g[u][i]] > deep[g[u][second_down_v]])             second_down_v = i;         }         if (second_down_v == -1 || up >= (deep[g[u][second_down_v]] + 1))           solve(g[u][down_v], up + 1);         else           solve(g[u][down_v], deep[g[u][second_down_v]] + 2);       }     }   }   int N;   int main() {     int m;     scanf( %d %d , &n, &m);     for (int i = 0; i < (n - 1); i++) {       int u, v;       scanf( %d %d , &u, &v);       g_tmp[u].push_back(v);       g_tmp[v].push_back(u);     }     while (m--) {       int u;       scanf( %d , &u);       humans[u] = true;       root = u;     }     cut(root);     count(root);     N = cnt[root];     solve(root, 0);     int arcos = N - 1;     printf( %d n%d n , id, 2 * arcos - maxlen);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k;   long long get(long long x, long long y) {     if (x <= y * 2) return 0;     long long s1 = 1, s2 = 0, p1 = x, p2 = x - 1, ans = 0;     while (p2 >= y * 2) {       if (p2 == y * 2) return s1 + ans;       ans += s1 + s2;       if (p1 & 1)         s1 = s1 * 2 + s2;       else         s2 = s2 * 2 + s1;       p1 /= 2;       p2 = p1 - 1;     }     return ans;   }   long long solve(long long l, long long r, long long x, long long k) {     long long mid = (l + r) / 2;     if (k == 1) return mid;     long long X = get(mid - l, x), Y = get(r - mid, x + 1);     if (k > X + Y + 1) return solve(mid + 1, r, x, k - X - 1);     return solve(l, mid - 1, x, k - Y - 1);   }   signed main() {     scanf( %lld%lld , &n, &k);     if (k == 1) {       puts( 1 );       return 0;     }     if (k == 2) {       printf( %lld n , n);       return 0;     }     n -= 2;     k -= 2;     long long l = 0, r = n;     while (l < r) {       long long mid = (l + r + 1) / 2;       if (get(n, mid) < k)         r = mid - 1;       else         l = mid;     }     printf( %lld n , 1 + solve(1, n, l, k));   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 2e5 + 5;   long long int a[maxN], b[maxN];   int n;   void solve() {     cin >> n;     set<pair<long long int, int>> s;     int count = n;     for (int i = 0; i < n; i++) {       b[i] = 1;       cin >> a[i];       s.insert({a[i], i});     }     while (!s.empty()) {       auto it1 = *(s.begin());       s.erase(s.begin());       if (s.empty()) break;       auto it2 = *(s.begin());       if (it1.first == it2.first) {         count--;         s.erase(s.begin());         s.insert({2 * it2.first, it2.second});         a[it2.second] = 2 * it2.first;         b[it1.second] = 0;       }     }     cout << count << endl;     for (int i = 0; i < n; i++) {       if (b[i]) cout << a[i] <<    ;     }   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     srand(chrono::high_resolution_clock::now().time_since_epoch().count());     int t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       vector<int> a(n);       for (int i = 0; (i) < (int)(n); (i)++) {         scanf( %d , &a[i]);       }       queue<int> que;       vector<int> ans;       for (int i = 0; (i) < (int)(n); (i)++) {         if (i == 0 || a[i - 1] < a[i]) {           ans.push_back(a[i]);           int prev = (i == 0 ? 0 : a[i - 1]);           prev++;           while (prev != a[i]) {             que.push(prev);             prev++;           }         } else {           if (que.empty()) {             printf( -1 n );             goto end;           }           ans.push_back(que.front());           que.pop();         }       }       assert(ans.size() == n);       for (int i = 0; (i) < (int)(n); (i)++) {         printf( %d%c , ans[i], i == n - 1 ?   n  :    );       }     end:;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[307][307];   inline bool check(const int& i, const int& j) {     return i >= 0 && i < n && j >= 0 && j < m;   }   int cnt_neighbors(const int& i, const int& j) {     int res = 0;     if (check(i - 1, j)) res++;     if (check(i, j + 1)) res++;     if (check(i + 1, j)) res++;     if (check(i, j - 1)) res++;     return res;   }   int main() {     int tc;     scanf( %d , &tc);     while (tc--) {       scanf( %d %d , &n, &m);       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++) scanf( %d , &a[i][j]);       bool flag = 1;       for (int i = 0; i < n && flag; i++)         for (int j = 0; j < m && flag; j++) {           int tmp = cnt_neighbors(i, j);           if (a[i][j] > tmp)             flag = 0;           else             a[i][j] = tmp;         }       if (flag) {         puts( YES );         for (int i = 0; i < n; i++) {           for (int j = 0; j < m; j++) printf( %d  , a[i][j]);           puts(  );         }       } else         puts( NO );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000 + 2;   double x[4], y[4], r[4];   double dx, dy;   double Fun(double dx, double dy) {     double ap[4];     for (int i = 0; i < 3; i++) {       ap[i] =           sqrt(((x[i] - dx) * (x[i] - dx)) + ((y[i] - dy) * (y[i] - dy))) / r[i];     }     double res = 0.0;     for (int i = 0; i < 3; i++) {       res += ((ap[i] - ap[(i + 1) % 3]) * (ap[i] - ap[(i + 1) % 3]));     }     return res;   }   int main() {     dx = dy = 0.0;     for (int i = 0; i < 3; i++) {       scanf( %lf%lf%lf , &x[i], &y[i], &r[i]);       dx += x[i] / 3;       dy += y[i] / 3;     }     double i = 1.0;     while (i > 1e-5) {       if (Fun(dx + i, dy) < Fun(dx, dy))         dx += i;       else if (Fun(dx - i, dy) < Fun(dx, dy))         dx -= i;       else if (Fun(dx, dy + i) < Fun(dx, dy))         dy += i;       else if (Fun(dx, dy - i) < Fun(dx, dy))         dy -= i;       else         i *= 0.75;     }     if (Fun(dx, dy) < 1e-5) printf( %.5lf %.5lf n , dx, dy);     return 0;   }
#include<bits/stdc++.h>   using namespace std;      typedef pair<int,int> pii;   typedef pair<string,int> psi;   typedef pair<int,string> pis;   typedef array<int,2> aii;   typedef array<int,3> aiii;   typedef array<int,4> aiiii;   typedef unsigned long long ull;   typedef long long int ll;   typedef array<ll,2> all;   typedef array<ll,3> alll;   typedef array<ll,4> allll;      #define pb push_back   #define ff first   #define ss second   #define MAX 200005   #define MOD 1000000007   #define INF 1e9   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   vector<aiii>adj[MAX];   int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 };   int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 };   string s;      int dp[MAX][2],n;      int solve(int i, int p)   {       if(i==n)           return 0;       if(dp[i][p]!=-1)           return dp[i][p];          int ans=0;       if(s[i]== 1 )       {           if(p==0)               ans=1+solve(i+1,1);       }       else if(s[i]== 0 )       {           if(p==1)               ans=1+solve(i+1,0);       }       else       {           if(p==0)               ans=1+solve(i+1,1);           else ans=1+solve(i+1,0);       }          return dp[i][p]=ans;   }   int main()   {       //freopen( input.in , r ,stdin);       //freopen( output.txt , w ,stdout);       cin.tie(0),cout.tie(0);       ios_base::sync_with_stdio(0);       cout.setf(ios::fixed);       cout.precision(10);       int TC=1;       int m,k,q;          cin>>TC;       for(int t1=1; t1<=TC; t1++)       {           cin>>s;           n=s.size();           for(int i=0;i<s.size();i++)               dp[i][0]=-1,dp[i][1]=-1;              ll ans=0;           for(int i=0;i<n;i++)           {               if(s[i]== 0 )                   ans+=1+solve(i+1,0);               else if(s[i]== 1 )                   ans+=1+solve(i+1,1);               else ans+=max(1+solve(i+1,0),1+solve(i+1,1));           }              cout<<ans<<  n ;       }          return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   const int diri[4] = {-1, 0, 1, 0};   const int dirj[4] = {0, 1, 0, -1};   int A, B;   int va[110000], vb[110000];   bool esquerra(int a, int b) {     if (b < 0) return false;     if (a == 0) return true;     return va[a - 1] < vb[b];   }   bool dreta(int a, int b) {     if (b + 1 >= B) return false;     if (a + 1 == A) return true;     return va[a + 1] > vb[b + 1];   }   int main() {     int spam;     cin >> A >> B >> spam >> spam;     for (int i = 0; i < int(A); ++i) cin >> va[i];     for (int i = 0; i < int(B); ++i) cin >> vb[i];     vector<int> dist(B, -1);     int res = A;     int b = -1;     for (int a = 0; a < A; ++a) {       while (b + 1 < B and vb[b + 1] <= va[a]) ++b;       int d = INF;       if (0 <= b and b < B) d = min(d, abs(va[a] - vb[b]));       if (b + 1 < B) d = min(d, abs(va[a] - vb[b + 1]));       if (esquerra(a, b) and d == abs(va[a] - vb[b])) {         if (dist[b] == -1 or dist[b] == d) {           dist[b] = d;           --res;           continue;         }       }       if (dreta(a, b) and d == abs(va[a] - vb[b + 1])) {         dist[b + 1] = d;         --res;         continue;       }     }     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int biceps = 0, back = 0, chest = 0;     for (int i = 0; i < n; i++) {       int a;       cin >> a;       if (i % 3 == 0) chest += a;       if (i % 3 == 1) biceps += a;       if (i % 3 == 2) back += a;     }     if (chest > back && chest > biceps) cout <<  chest ;     if (back > chest && back > biceps) cout <<  back ;     if (biceps > chest && biceps > back) cout <<  biceps ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n;     cin >> t;     while (t--) {       cin >> n;       int a[n], flag = 0, x;       for (int i = 0; i < n; i++) cin >> a[i];       for (int i = 0; i < n; i++) {         if (a[i] % 2 == 0) {           x = i + 1;           flag = 1;           break;         }       }       if (n == 1 && flag == 0)         cout <<  -1  << endl;       else if (flag == 1)         cout <<  1  << endl << x << endl;       else         cout <<  2  << endl              <<  1               <<                  <<  2  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 10;   int cnt[MAX];   vector<int> up, down;   int main() {     ios::sync_with_stdio(false);     memset(cnt, 0, sizeof(cnt));     int n, x, max_num = -1;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       cnt[x]++;       max_num = max(max_num, x);     }     for (int i = 0; i <= max_num; i++) {       if (cnt[i] > 2) {         cout <<  NO  << endl;         return 0;       }     }     for (int i = max_num; i >= 0; i--) {       if (cnt[i]) {         down.push_back(i);         cnt[i]--;       }     }     for (int i = 0; i <= max_num; i++) {       if (cnt[i]) {         up.push_back(i);         cnt[i]--;       }     }     cout <<  YES  << endl;     cout << up.size() << endl;     if (up.size() > 0) {       for (int i = 0; i < up.size() - 1; i++) cout << up[i] <<    ;       cout << up[up.size() - 1] << endl;     } else {       cout <<     << endl;     }     cout << down.size() << endl;     if (down.size() > 0) {       for (int i = 0; i < down.size() - 1; i++) cout << down[i] <<    ;       cout << down[down.size() - 1] << endl;     } else {       cout <<     << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 101000;   int N;   bool getv(int n) {     int a, b;     printf( ? %d n , 1 + n);     fflush(stdout);     scanf( %d , &a);     printf( ? %d n , 1 + n + (N / 2));     fflush(stdout);     scanf( %d , &b);     if (a == b) {       printf( ! %d n , 1 + n);       exit(0);     }     return a > b;   }   int main() {     scanf( %d , &N);     if (N % 4 > 0) {       printf( ! -1 n );       return 0;     }     bool bvt = getv(0);     int low = 0, high = N / 2;     while (high > low + 1) {       int mid = low + (high - low) / 2;       if (getv(mid) == bvt)         low = mid;       else         high = mid;     }     getv(high);     printf( ! -1 n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, len, i, j, a[300009];   int main() {     while (scanf( %I64d , &n) == 1) {       for (i = 0; i < n; i++) {         scanf( %I64d , &a[i]);       }       sort(a, a + n);       if (n % 2 != 0) {         printf( %I64d n , a[n / 2]);       } else {         printf( %I64d n , a[n / 2 - 1]);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double PI = 3.14159265358979323846;   const double eps = (1e-10);   int isGreater(long double x, long double y) {     if (abs(x - y) < eps) return 0;     if (x > y) return 1;     return -1;   }   const int MOD = 998244353;   int mult(int x, int y) { return (1LL * x * y) % MOD; }   int add(int x, int y) {     x += y;     while (x >= MOD) x -= MOD;     while (x < 0) x += MOD;     return x;   }   int add(vector<int> v) {     int ret = 0;     for (int i = 0; i < (int)((v).size()); ++i) ret = add(ret, v[i]);     return ret;   }   const int MAX_N = 3e5 + 5;   vector<int> adj[MAX_N];   int dp[MAX_N][3][2];   int n;   void calc(int u, int p) {     for (int v : adj[u]) {       if (v != p) calc(v, u);     }     dp[u][0][1] = 0;     dp[u][0][0] = 1;     for (int v : adj[u]) {       if (v != p)         dp[u][0][0] =             mult(dp[u][0][0], add({dp[v][0][0], dp[v][2][0], dp[v][2][1]}));     }     dp[u][2][0] = dp[u][2][1] = 1;     for (int v : adj[u]) {       if (v != p) {         dp[u][2][0] =             mult(dp[u][2][0], add({dp[v][1][0], dp[v][1][1], dp[v][2][0],                                    dp[v][2][1], dp[v][0][0]}));         dp[u][2][1] =             mult(dp[u][2][1],                  add({dp[v][1][0], dp[v][2][0], dp[v][2][1], dp[v][0][0]}));       }     }     dp[u][2][0] = add(dp[u][2][0], -dp[u][0][0]);     dp[u][2][1] = add(dp[u][2][1], -dp[u][0][0]);     if (u == 0) return;     dp[u][1][0] = dp[u][1][1] = 1;     for (int v : adj[u]) {       if (v != p) {         dp[u][1][0] =             mult(dp[u][1][0], add({dp[v][0][0], dp[v][2][0], dp[v][2][1],                                    dp[v][1][0], dp[v][1][1]}));         dp[u][1][1] =             mult(dp[u][1][1],                  add({dp[v][0][0], dp[v][2][0], dp[v][2][1], dp[v][1][0]}));       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 0; i < n - 1; ++i) {       int u, v;       cin >> u >> v;       --u;       --v;       adj[u].push_back(v);       adj[v].push_back(u);     }     calc(0, n);     cout << add({dp[0][0][0], dp[0][2][0], dp[0][2][1], -1});     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long>> calced;   long long x_m(long long m, long long mod) {     for (pair<long long, long long> a : calced) {       if (a.first == m) return a.second;     }     if (m == 1) return 1 % mod;     if (m == 2) return 3 % mod;     for (long long i = 1; i < m; i *= 2) {       if (i * 2 > m) {         long long pw = x_m(i, mod);         long long pw2 = x_m(i - 1, mod);         long long val = (pw + (m - i) * (pw - pw2)) % mod;         if (val < 0) val += mod;         calced.insert(calced.begin(), make_pair(m, val));         return val;       }       if (i * 2 == m) {         long long val = (2 * (x_m(m - 1, mod)) + 1) % mod;         if (val < 0) val += mod;         calced.insert(calced.begin(), make_pair(m, val));         return val;       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       calced.clear();       long long d, m;       cin >> d >> m;       cout << x_m(d, m) << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     while (n--) {       string s;       cin >> s;       int i, k, cnt = 0;       for (i = 0; i < s.length(); i++)         if (s[i] !=  0 ) break;       k = i;       for (i; i < s.length(); i++)         if (s[i] ==  0 ) cnt++;       for (int j = s.length(); j >= k; j--) {         if (s[j] ==  0 )           cnt--;         else if (s[j] ==  1 )           break;       }       cout << cnt << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     ;     long long t;     cin >> t;     while (t--) {       long long n, x, m;       cin >> n >> x >> m;       long long c, d;       long long l = x, r = x;       while (m--) {         cin >> c >> d;         if (c <= l && d >= r) {           l = c;           r = d;         } else if (c <= l && d <= r && d >= l)           l = c;         else if (l <= c && c <= r && d >= r)           r = d;       }       cout << (r - l + 1) <<   n ;     }     return 0;   }