malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   #define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);   #define F first   #define S second   #define V vector   #define PB push_back   #define MP make_pair   #define EB emplace_back   #define ALL(v) (v).begin(), (v).end()   #define debug(x) cerr <<  Line(  << __LINE__ <<  ) ->   << #x <<   is   << x << endl      using namespace std;      typedef long long ll;   typedef pair<int, int> pi;   typedef V<int> vi;      const int INF = 1e9 + 7, N = 4e5 + 7;      int p[N], cnt[N], vis[N];   vi G[N];      int find(int u) {    return p[u] == u ? u : p[u] = find(p[u]);   }      void unite(int x, int y) {    int rx = find(x), ry = find(y);    if(rx == ry) return;    cnt[ry] += cnt[rx];    p[rx] = ry;   }      signed main()   {    IO_OP;       int n;    cin >> n;    V<array<int, 3>> a;    for(int i = 1; i <= n; i++) {     int x, y;     cin >> x >> y;     a.PB({x, y, i});     a.PB({y, x, -i});    }    sort(ALL(a));    for(int i = 2; i <= 2 * n + 1; i++) p[i] = i;    for(int i = 1; i <= n; i++) cnt[i * 2] = 1;    set<array<int, 3>> stk;    for(int i = 0; i < n * 2; i++) {     int u = abs(a[i][2]) * 2, v = u + 1;     if(a[i][2] < 0) swap(u, v);     int y = a[i][1];     for(auto tt:stk) {      if(tt[0] > y) break;      unite(tt[1], v);      unite(tt[2], u);      if(find(u) == find(v)) {       cout << -1 <<   n ;       return 0;      }     }     stk.insert({y, u, v});    }    for(int i = 1; i <= n; i++) {     if(find(i * 2) == find(i * 2 + 1)) {      cout << -1 <<   n ;      return 0;     }    }    for(int i = 2; i <= n * 2 + 1; i++) {     G[find(i)].PB(i);    }    int ans = 0;    for(int i = 1; i <= n; i++) {     if(!vis[i]) {      int x = find(i * 2), y = find(i * 2 + 1);      ans += min(cnt[x], cnt[y]);      for(int u:G[x]) vis[u / 2] = 1;     }    }    cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> a;   int main() {     string s;     cin >> s;     int cur = 0, mul = 1;     for (int i = s.length() - 1; i >= 0; --i) {       if (s[i] ==  , ) {         a.push_back(cur);         mul = 1;         cur = 0;       } else {         cur += mul * (s[i] - 48);         mul *= 10;       }     }     a.push_back(cur);     sort(a.begin(), a.end());     int L;     int last = a[0];     bool ok = 0;     printf( %d , a[0]);     L = a[0];     for (int i = 1; i < a.size(); ++i) {       if (a[i] == last) continue;       if (a[i] == last + 1) {         ok = 1;         last = a[i];       } else {         if (ok && L != last) {           printf( -%d , last);           L = last;         }         last = a[i];         printf( ,%d , a[i]);         L = a[i];       }     }     if (ok && L != last) printf( -%d , last);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     string str;     cin >> str;     set<string> visited;     queue<string> s;     s.push(str);     visited.insert(str);     int count = 0, x = 0;     while (!s.empty() && count != k) {       string temp = s.front();       x = x + temp.length();       count++;       s.pop();       for (int i = 0; i < temp.length(); i++) {         string temp2 =   ;         for (int j = 0; j < temp.length(); j++) {           if (j != i) temp2 += temp[j];         }         if (visited.find(temp2) == visited.end()) {           s.push(temp2);           visited.insert(temp2);         }       }     }     if (s.empty() && count < k)       cout <<  -1 ;     else       cout << n * k - x;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m;   int main() {     cin >> n >> m;     if (n > m) {       swap(n, m);     }     if (n % 2 == 0 && m % 2 == 0) {       if (n == 2 && m == 2)         cout << 0 << endl;       else         cout << n * m << endl;       return 0;     }     if (n + m <= 4) {       cout << 0 << endl;       return 0;     }     if (n == 1) {       if (m % 6 == 1 || m % 6 == 5) {         cout << m - 1 << endl;       } else if (m % 6 == 2 || m % 6 == 4) {         cout << m - 2 << endl;       } else if (m % 6 == 3) {         cout << m - 3 << endl;       } else {         cout << m << endl;       }       return 0;     }     if (n % 2 == 1 && m % 2 == 0) {       swap(n, m);     }     if (n % 2 == 0) {       if (m == 3) {         if (n >= 4)           cout << m * n << endl;         else           cout << 4 << endl;         return 0;       }       if (n == 2) {         if (m == 7)           cout << 12 << endl;         else if (m == 3)           cout << 4 << endl;         else           cout << m * n << endl;         return 0;       }       if (n >= 4) {         cout << m * n << endl;         return 0;       }     }     if (n > m) swap(n, m);     cout << m * n - 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   const int mod = 1e9 + 7;   char str[maxn];   long long qp(long long a, long long b) {     long long ret = 1;     while (b) {       if (b & 1) ret = ret * a % mod;       a = a * a % mod;       b >>= 1;     }     return ret;   }   long long inv(long long a) { return qp(a, mod - 2); }   void solve(long long);   int main() {     scanf( %s , str);     long long k;     cin >> k;     solve(k);     return 0;   }   void solve(long long k) {     long long n = (long long)strlen(str);     long long ans = 0;     for (int i = 0; i < n; i++) {       if (str[i] ==  0  || str[i] ==  5 ) {         long long tmp = (qp(2, n * k + i) - qp(2, i) + mod) % mod;         tmp = tmp * inv(qp(2, n) - 1) % mod;         ans = (ans + tmp) % mod;         while (ans < 0) ans += mod;       }     }     cout << ans << endl;     return;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast ,  unroll-loops )   using namespace std;   const int MOD = 1e9 + 7;   const int MAXN = 1010;   const double EPS = 0.000001;   double A[MAXN], B[MAXN];   int n, m;   bool can(double X) {     for (int i = 0; i < n; i++) {       double W_li = m + X;       double TO = W_li / A[i];       if (TO > X) return false;       X -= TO;       double W_la = W_li - TO;       double LN = W_la / B[(i + 1) % n];       if (LN > X) return false;       X -= LN;     }     return true;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> A[i] >> B[i];     double l = 0.0, r = 1e9 + 1;     double ans = 0.0;     bool flag = false;     while (l <= r) {       double mid = (l + r) / 2.0;       if (abs(r - l) < EPS) {         if (can(l)) {           ans = l;         } else if (can(r)) {           ans = r;         }         break;       }       if (can(mid)) {         flag = true;         ans = mid;         r = mid;       } else {         ans = mid;         l = mid;       }     }     if (flag)       cout << setprecision(20) << ans <<   n ;     else       cout << -1 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e5;   vector<int> G[MAXN];   int happiness[MAXN], f[MAXN], tmpAns = 0, ans = 0;   void dfs(int u, int par) {     int oldTmpAns = -1, oldF = -1, oldFPos = -1;     if (~par && f[tmpAns] < happiness[u])       oldTmpAns = tmpAns, f[++tmpAns] = happiness[u];     else {       int pos = lower_bound(f, f + tmpAns, happiness[u]) - f;       oldF = f[pos], oldFPos = pos;       f[pos] = happiness[u];     }     ans = max(ans, tmpAns);     for (int i = 0; i < G[u].size(); i++) {       int v = G[u][i];       if (v == par) continue;       dfs(v, u);     }     if (oldTmpAns != -1) tmpAns = oldTmpAns;     if (oldF != -1) f[oldFPos] = oldF;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> happiness[i];     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       --x, --y;       G[x].push_back(y);       G[y].push_back(x);     }     for (int i = 0; i < n; i++) {       f[0] = happiness[i];       tmpAns = 0;       dfs(i, -1);     }     cout << ans + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, maxx, cmax;   int t[100010];   long long int sum[100010];   int binary_search1(int st, int dr, int pos, int x) {     int sol = 0;     while (st <= dr) {       int m = (st + dr) / 2;       long long int lsum = sum[pos] - sum[m - 1];       lsum = 1LL * (pos - m + 1) * x - lsum;       if (lsum <= k) {         sol = m;         dr = m - 1;       } else         st = m + 1;     }     return sol;   }   int main() {     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &t[i]);     sort(t + 1, t + n + 1);     for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + t[i];     maxx = 1;     cmax = t[1];     for (int i = 1; i <= n; i++) {       int pos = binary_search1(1, i - 1, i, t[i]);       if (pos > 0 && i - pos + 1 > maxx) {         maxx = i - pos + 1;         cmax = t[i];       }     }     printf( %d %d , maxx, cmax);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n;     cin >> n;     long long int arr[n][2];     for (int i = 0; i < n; i++) {       cin >> arr[i][0] >> arr[i][1];     }     long long int x = max(arr[0][0], arr[0][1]);     vector<int> v1;     int fail = 0;     for (int i = 1; i < n; i++) {       long long int y = max(arr[i][0], arr[i][1]);       long long int k = min(arr[i][0], arr[i][1]);       if (y <= x) {         v1.push_back(y);         x = y;       } else if (k <= x) {         v1.push_back(k);         x = k;       } else {         fail = 1;         break;       }     }     if (fail == 1)       cout <<  NO ;     else       cout <<  YES ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int T;   long long r, b, k, g;   long long gcd(long long a, long long b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int main() {     scanf( %d , &T);     while (T--) {       scanf( %lld%lld%lld , &r, &b, &k);       if (r > b) swap(r, b);       g = gcd(r, b);       r /= g, b /= g;       if ((k - 1) * r + 1 >= b)         printf( OBEY n );       else         printf( REBEL n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int month, day;     int cheat[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};     cin >> month >> day;     int n = cheat[month] - 8 + day;     int ans = 1 + (n / 7) + ((n % 7) > 0);     cout << ans;     return 0;   }
#include <bits/stdc++.h>   const int MaxN = 100000, inf = 0x3f3f3f3f;   inline void read(int &ans) {     ans = 0;     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     while (c >=  0  && c <=  9 ) ans = ans * 10 + c - 48, c = getchar();     return;   }   int n, P;   struct Node {     int pos, t;     friend inline bool operator==(const Node &a, const Node &b) {       return a.pos == b.pos && a.t == b.t;     }   };   struct cmp_time {     inline bool operator()(const Node &a, const Node &b) const {       return a.t == b.t ? a.pos > b.pos : a.t > b.t;     }   };   struct cmp_pos {     inline bool operator()(const Node &a, const Node &b) const {       return a.pos > b.pos;     }   };   template <typename Element, typename cmp>   struct Exque {    private:     std::priority_queue<Element, std::vector<Element>, cmp> que, del;       public:     inline int size() { return que.size() - del.size(); }     inline void pop() {       if (del.size() && que.top() == del.top()) del.pop();       return que.pop();     }     inline void push(const Element &x) { return que.push(x); }     inline void delete_(const Element &x) {       return que.top() == x ? que.pop() : del.push(x);     }     inline Element top() {       while (del.size() && que.top() == del.top()) que.pop(), del.pop();       return que.top();     }   };   std::priority_queue<Node, std::vector<Node>, cmp_time> sitting;   std::priority_queue<Node, std::vector<Node>, cmp_pos> preparing;   Exque<Node, cmp_pos> inque;   std::queue<Node> queorder;   long long ans[MaxN + 1];   int cnt = 0;   int main() {     int i, t;     long long curtime = inf;     read(n), read(P);     for (i = 1; i <= n; ++i) {       read(t), sitting.push((Node){i, t});       if (t < curtime) curtime = t;     }     while (sitting.size() && sitting.top().t <= curtime)       preparing.push(sitting.top()), sitting.pop();     if (preparing.size() &&         (!inque.size() || preparing.top().pos < inque.top().pos))       inque.push(preparing.top()), queorder.push(preparing.top()),           preparing.pop();     while (cnt < n) {       ++cnt, ans[queorder.front().pos] = (curtime += P);       while (sitting.size() && sitting.top().t <= curtime)         if (sitting.top().pos < inque.top().pos)           inque.push(sitting.top()), queorder.push(sitting.top()), sitting.pop();         else           preparing.push(sitting.top()), sitting.pop();       inque.delete_(queorder.front()), queorder.pop();       while (sitting.size() && sitting.top().t <= curtime)         preparing.push(sitting.top()), sitting.pop();       if (preparing.size() &&           (!inque.size() || preparing.top().pos < inque.top().pos))         inque.push(preparing.top()), queorder.push(preparing.top()),             preparing.pop();       if (!inque.size() && sitting.size()) {         curtime = sitting.top().t;         while (sitting.size() && sitting.top().t <= curtime)           preparing.push(sitting.top()), sitting.pop();         inque.push(preparing.top()), queorder.push(preparing.top()),             preparing.pop();       }     }     for (i = 1; i <= n; ++i) printf( %I64d  , ans[i]);     putchar(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, z;     cin >> n >> k;     vector<pair<int, int>> h, res;     h.reserve(n);     h.resize(k);     for (int i = 0; i < k; ++i) {       cin >> h[i].first;       h[i].second = i;     }     make_heap(begin(h), end(h));     res.reserve(n);     int64_t total = 0;     for (int i = k; i < n; ++i) {       cin >> z;       h.emplace_back(z, i);       push_heap(begin(h), end(h));       auto& p = h.front();       total += p.first * (int64_t)(i - p.second);       res.emplace_back(p.second, i);       pop_heap(begin(h), end(h));       h.pop_back();     }     for (int i = n; !h.empty(); ++i) {       auto& p = h.front();       total += p.first * (int64_t)(i - p.second);       res.emplace_back(p.second, i);       pop_heap(begin(h), end(h));       h.pop_back();     }     sort(begin(res), end(res));     cout << total << endl << res[0].second + 1;     for (int i = 1; i < n; ++i) cout <<     << res[i].second + 1;     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 998244353;   const int N_MAX = 50;   int pwr(int a, int b) {     if (b == 0) return 1;     if (b & 1) return 1LL * pwr(a, (b ^ 1)) * a % MOD;     int aux = pwr(a, (b >> 1));     return 1LL * aux * aux % MOD;   }   int n;   int invf[N_MAX + 2];   struct Task {     int l, r;   };   Task tasks[N_MAX + 2];   int points[N_MAX * 2 + 2];   struct Segment {     int l, r;   };   Segment segments[N_MAX * 4 + 2];   int cntSeg;   int combrep[N_MAX * 4 + 2][N_MAX + 2];   int lmax[N_MAX + 2][N_MAX + 2];   int rmin[N_MAX + 2][N_MAX + 2];   int invplg[N_MAX + 2][N_MAX + 2];   int dp[N_MAX + 2][N_MAX * 4 + 2][N_MAX + 2];   int sdp[N_MAX + 2][N_MAX * 4 + 2];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = n; i >= 1; i--) {       cin >> tasks[i].l >> tasks[i].r;       points[i * 2 - 1] = tasks[i].l;       points[i * 2] = tasks[i].r;     }     sort(points + 1, points + n * 2 + 1);     segments[++cntSeg] = Segment{points[1], points[1]};     for (int i = 2; i <= n * 2; i++)       if (points[i] > points[i - 1]) {         if (points[i - 1] + 1 <= points[i] - 1)           segments[++cntSeg] = Segment{points[i - 1] + 1, points[i] - 1};         segments[++cntSeg] = Segment{points[i], points[i]};       }     invf[0] = 1;     for (int i = 1; i <= n; i++)       invf[i] = 1LL * invf[i - 1] * pwr(i, MOD - 2) % MOD;     for (int i = 1; i <= cntSeg; i++)       for (int j = 1; j <= n; j++) {         combrep[i][j] = 1;         int lg = segments[i].r - segments[i].l + 1;         for (int k = lg; k <= lg + j - 1; k++)           combrep[i][j] = 1LL * combrep[i][j] * k % MOD;         combrep[i][j] = 1LL * combrep[i][j] * invf[j] % MOD;       }     for (int i = 1; i <= n; i++) {       lmax[i][i - 1] = INT_MIN;       rmin[i][i - 1] = INT_MAX;       invplg[i][i - 1] = 1;       for (int j = i; j <= n; j++) {         lmax[i][j] = max(lmax[i][j - 1], tasks[j].l);         rmin[i][j] = min(rmin[i][j - 1], tasks[j].r);         invplg[i][j] = 1LL * invplg[i][j - 1] *                        pwr(tasks[j].r - tasks[j].l + 1, MOD - 2) % MOD;       }     }     dp[0][0][0] = 1;     sdp[0][0] = 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= cntSeg; j++)         for (int k = 1; k <= i; k++) {           if (lmax[i - k + 1][i] > segments[j].l ||               segments[j].r > rmin[i - k + 1][i])             break;           for (int j1 = 0; j1 < j; j1++) {             dp[i][j][k] += sdp[i - k][j1];             if (dp[i][j][k] >= MOD) dp[i][j][k] -= MOD;           }           dp[i][j][k] = 1LL * dp[i][j][k] * combrep[j][k] % MOD;           dp[i][j][k] = 1LL * dp[i][j][k] * invplg[i - k + 1][i] % MOD;           sdp[i][j] += dp[i][j][k];           if (sdp[i][j] >= MOD) sdp[i][j] -= MOD;         }     int ans = 0;     for (int j = 1; j <= cntSeg; j++) {       ans += sdp[n][j];       if (ans >= MOD) ans -= MOD;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   const long long mod = 1e9 + 7;   const long long inf = 1e9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     sort(v.begin(), v.end());     int sum = 0;     for (int i = 0; i < min(n, m); i++) {       if (v[i] < 0) sum += v[i] * -1;     }     cout << sum;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long sum = 0;     vector<long long> a(n);     for (long long i = 0; i < n; ++i) {       cin >> a[i];       sum += a[i];     }     vector<long long> prime;     for (long long i = 2; i * i <= sum; ++i) {       if (sum % i == 0) {         prime.push_back(i);         while (sum % i == 0) sum /= i;       }     }     if (sum > 1) prime.push_back(sum);     long long ans = LLONG_MAX;     for (long long i : prime) {       long long now = 0;       long long cnt = 0;       for (long long j = 0; j < n; ++j) {         cnt += a[j];         now += min(cnt % i, i - cnt % i);       }       ans = min(ans, now);     }     cout << (ans == LLONG_MAX ? -1 : ans) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<long long> b;   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     b.resize(n);     for (int i = 0; i < n; ++i) cin >> b[i];     long long mi = *min_element(b.begin(), b.end());     long long ma = *max_element(b.begin(), b.end());     if (mi == ma) {       if (mi == 0) {         cout <<  YES n ;         for (int i = 0; i < n; ++i) cout <<  1  ;       } else         cout <<  NO ;       return 0;     }     cout <<  YES n ;     b.push_back(b[0]);     int i = 1;     while (!(b[i] == ma && b[i - 1] < ma)) ++i;     i %= n;     for (int j = 0; j < n - 1; ++j) {       int ii = (i + n - 1) % n;       b[ii] += (j ? b[i] : 2 * b[i]);       i = ii;     }     for (int i = 0; i < n; ++i) cout << b[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1555;   const int INF = 0x3f3f3f3f;   int sx, sy;   int n, m;   char maze[N][N];   int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};   struct node {     int x, y;   } vis[N][N];   bool bfs(int x, int y) {     queue<node> q;     node now, next;     now.x = x;     now.y = y;     q.push(now);     while (!q.empty()) {       now = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         next.x = now.x + dir[i][0];         next.y = now.y + dir[i][1];         if (maze[(next.x % n + n) % n][(next.y % m + m) % m] !=  # ) {           if (vis[(next.x % n + n) % n][(next.y % m + m) % m].x == INF) {             vis[(next.x % n + n) % n][(next.y % m + m) % m].x = next.x;             vis[(next.x % n + n) % n][(next.y % m + m) % m].y = next.y;             q.push(next);           } else if (next.x !=                          vis[(next.x % n + n) % n][(next.y % m + m) % m].x ||                      next.y != vis[(next.x % n + n) % n][(next.y % m + m) % m].y)             return true;         }       }     }     return false;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     memset(vis, INF, sizeof(vis));     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> maze[i][j];         if (maze[i][j] ==  S ) sx = i, sy = j;       }     }     if (bfs(sx, sy))       cout <<  Yes  << endl;     else       cout <<  No  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, ar[1010];   bool b[1010];   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> ar[i];     }     for (int i = 1; i <= n; ++i) {       int yk = i;       while (1) {         if (b[yk]) break;         b[yk] = 1;         yk = ar[yk];       }       cout << yk <<    ;       fill(b, b + 1010, 0);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int bs;   int* cnt;   int ans;   class query {    public:     int l;     int r;     int index;   };   bool cmp(const query a, const query b) {     int s = a.l / bs;     int e = b.l / bs;     if (s == e) return a.r < b.r;     return s < e;   }   void add(int x) {     if (cnt[x] == x)       ans--;     else if (cnt[x] == x - 1)       ans++;     cnt[x]++;   }   void del(int x) {     if (cnt[x] == x)       ans--;     else if (cnt[x] == x + 1)       ans++;     cnt[x]--;   }   int main() {     int n, m, *a;     query* q;     scanf( %d %d , &n, &m);     bs = sqrt(n);     a = (int*)calloc(n + 1, sizeof(int));     cnt = (int*)calloc(n + 1, sizeof(int));     int* an = (int*)calloc(m + 1, sizeof(int));     q = (query*)calloc(m + 1, sizeof(query));     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 0; i < m; i++) {       scanf( %d %d , &q[i].l, &q[i].r);       q[i].index = i;       q[i].l--;       q[i].r--;     }     sort(q, q + m, cmp);     ans = 0;     int l = 0, r = -1;     for (int i = 0; i < m; i++) {       int left = q[i].l;       int right = q[i].r;       while (r < right) {         r++;         if (a[r] < n + 1) add(a[r]);       }       while (r > right) {         if (a[r] < n + 1) del(a[r]);         r--;       }       while (l < left) {         if (a[l] < n + 1) del(a[l]);         l++;       }       while (l > left) {         l--;         if (a[l] < n + 1) add(a[l]);       }       an[q[i].index] = ans;     }     for (int i = 0; i < m; i++) printf( %d n , an[i]);     free(a);     free(an);     free(cnt);     free(q);     return 0;   }