malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MAX = 5000;   vector<int> edge[MAX + 5];   int p[MAX + 5], c[MAX + 5], lft[MAX + 5], used[MAX + 5], vis[MAX + 5],       cp[MAX + 5], ans[MAX + 5];   int n, m, d, mex, dfn;   bool dfs(int x) {     for (int i = 0; i < edge[x].size(); i++) {       int j = edge[x][i];       if (used[j] != dfn) {         used[j] = dfn;         if (cp[j] == -1 || dfs(cp[j])) {           cp[j] = x;           return true;         }       }     }     return false;   }   int main() {     memset(cp, -1, sizeof(cp));     int i;     scanf( %d%d , &n, &m);     for (i = 1; i <= n; i++) scanf( %d , &p[i]);     for (i = 1; i <= n; i++) scanf( %d , &c[i]);     scanf( %d , &d);     for (i = 1; i <= d; i++) {       scanf( %d , &lft[i]);       vis[lft[i]] = 1;     }     for (i = 1; i <= n; i++) {       if (!vis[i]) edge[p[i]].push_back(c[i]);     }     for (i = d, mex = -1, dfn = 1; i >= 1; i--) {       while (dfs(mex + 1)) {         mex++;         dfn++;       }       ans[i] = mex + 1;       dfn++;       edge[p[lft[i]]].push_back(c[lft[i]]);     }     for (i = 1; i <= d; i++) {       printf( %d n , ans[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s[] = { zero ,     one ,      two ,        three ,     four ,                    five ,     six ,      seven ,      eight ,     nine ,                    ten ,      eleven ,   twelve ,     thirteen ,  fourteen ,                    fifteen ,  sixteen ,  seventeen ,  eighteen ,  nineteen };     string c[] = {  ,        ,       twenty ,   thirty ,  forty ,                    fifty ,  sixty ,  seventy ,  eighty ,  ninety };     int n;     cin >> n;     if (n < 20) {       cout << s[n];     } else {       if (n % 10 == 0)         cout << c[n / 10];       else         cout << c[n / 10] <<  -  << s[n % 10];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e4, SQN = 3e2;   map<int, int> dst_idx, idx_dst;   int n, d, dp[MAXN + 3][SQN + 3], a[MAXN + 3], max_idx;   int fazdp(int idx, int li) {     if (dp[idx][li] != -1) return dp[idx][li];     int di = idx_dst[li];     int ret = 0;     for (int i = -1; i <= 1; i++) {       int dd = di + i;       if (dst_idx.find(dd) == dst_idx.end() || dd <= 0 || idx + dd > max_idx)         continue;       int iidx = idx + dd;       ret = max(ret, fazdp(iidx, dst_idx[dd]));     }     return dp[idx][li] = a[idx] + ret;   }   int main() {     for (int i = 0; i < MAXN + 3; i++)       for (int j = 0; j < SQN + 3; j++) dp[i][j] = -1;     scanf( %d %d , &n, &d);     for (int i = 1; i <= n; i++) {       int aux;       scanf( %d , &aux);       max_idx = max(max_idx, aux);       a[aux]++;     }     int cnt = 0;     for (int i = max(1, d - SQN); i <= min(max_idx, d + SQN); i++) {       dst_idx[i] = cnt;       idx_dst[cnt] = i;       cnt++;     }     printf( %d , a[0] + fazdp(d, dst_idx[d]));   }
#include <bits/stdc++.h>   using namespace std;   long long int d[400001], num[400001];   int decr[400001], inc[400001];   priority_queue<pair<long long int, int>, vector<pair<long long int, int> >,                  greater<pair<long long int, int> > >       H1;   priority_queue<pair<long long int, int> > H2;   int main() {     int i;     int t, m;     long long int n, k, p, o;     scanf( %d , &t);     while (t--) {       scanf( %I64d %d %I64d , &n, &m, &k), o = 0;       for (i = 0; i < m; i++) scanf( %I64d , &p), d[i] = p - o, o = p;       d[m++] = n - o, k += m;       long long int l = 1, r = n;       while (l < r) {         long long int mid = (l + r) / 2;         long long int s = 0;         for (i = 0; i < m; i++) s += (d[i] + mid - 1) / mid;         if (s > k)           l = mid + 1;         else           r = mid;       }       long long int s = 0;       for (i = 0; i < m; i++) num[i] = (d[i] + r - 1) / r, s += num[i];       for (i = 0; i < m; i++) {         if ((s < k) && ((d[i] + num[i] - 1) / num[i] == r)) {           long long int x = (d[i] - 1) / (r - 1) - num[i] + 1;           x = min(x, k - s);           num[i] += x, s += x;         }         l = min(l, d[i] / num[i]);       }       long long int ans = r - l;       for (i = 0; i < m; i++)         H1.push(make_pair(d[i] / num[i], i)),             H2.push(make_pair(d[i] / (num[i] + 1), i));       fill(decr, decr + m, 0), fill(inc, inc + m, 0);       int tt = 10 * m;       while (tt--) {         while (d[H1.top().second] / num[H1.top().second] != H1.top().first)           H1.pop();         while (d[H2.top().second] / (num[H2.top().second] + 1) != H2.top().first)           H2.pop();         int u = H1.top().second, v = H2.top().second;         H1.pop(), H2.pop();         if (u == v) break;         num[u]--, num[v]++;         decr[u] = 1, inc[v] = 1;         if (decr[u] && inc[u]) break;         if (decr[v] && inc[v]) break;         if (num[u] == 0) break;         r = max(r, (d[u] + num[u] - 1) / num[u]);         H1.push(make_pair(d[u] / num[u], u));         if (d[u] / num[u] != d[u] / (num[u] + 1))           H2.push(make_pair(d[u] / (num[u] + 1), u));         if (d[v] / num[v] != d[v] / (num[v] - 1))           H1.push(make_pair(d[v] / num[v], v));         H2.push(make_pair(d[v] / (num[v] + 1), v));         while (d[H1.top().second] / num[H1.top().second] != H1.top().first)           H1.pop();         ans = min(ans, r - H1.top().first);       }       printf( %I64d n , ans);       while (!H1.empty()) H1.pop();       while (!H2.empty()) H2.pop();     }     return 0;   }
#include <bits/stdc++.h>   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(nullptr);     int a;     std::cin >> a;     int d[6];     for (int i = 0; i < 6; ++i) d[i] = a >> i & 1;     std::cout << (d[0] << 4 | d[1] << 1 | d[2] << 3 | d[3] << 2 | d[4] |                   d[5] << 5)               <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, a, b, c, d, ans1, ans2, ans3;     cin >> t;     while (t--) {       ans1 = 0;       ans2 = 0;       ans3 = 0;       cin >> a >> b >> c >> d;       ans1 = a / b;       ans2 = (c - 1) / b;       ans3 = d / b;       ans1 -= ans3;       ans1 += ans2;       cout << ans1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char a[50][50];   int main() {     int n, m;     cin >> n >> m;     for (int j = 0; j < m; j++) a[0][j] =  # ;     for (int j = 0; j < m - 1; j++) a[1][j] =  . ;     a[1][m - 1] =  # ;     for (int i = 2; i < n; i++) {       for (int j = 0; j < m; j++) {         if (i % 2 == 0)           a[i][j] =  # ;         else {           if (j == 0 && a[i - 2][0] ==  . )             a[i][j] =  # ;           else if (j == m - 1 && a[i - 2][m - 1] ==  . )             a[i][j] =  # ;           else             a[i][j] =  . ;         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) cout << a[i][j];       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, number;     cin >> n;     int arr[n + 1];     bool marcado[n + 1];     memset(marcado, 0, sizeof marcado);     for (int i = 1; i <= n; i++) {       cin >> number;       if (!marcado[number] && number <= n) {         marcado[number] = true;         arr[i] = number;       } else         arr[i] = 0;     }     int i, j;     for (i = 1, j = 1; i <= n; i++) {       if (arr[i] != 0) {         cout << arr[i] <<    ;       } else {         while (marcado[j]) j++;         cout << j++ <<    ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, tmp, k, gbd;     cin >> n;     k = 1;     while (k < 10) {       tmp = pow(2, k - 1) * (pow(2, k) - 1);       if (n % tmp == 0) gbd = tmp;       k++;     }     cout << gbd;   }
#include <bits/stdc++.h>   using namespace std;   int n, ans = -1000001;   int main() {     cin >> n;     float a;     for (int i = 1; i <= n; i++) {       cin >> a;       if (floor(sqrt(a)) != sqrt(a)) {         ans = max(ans, (int)a);       }     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n, i, j, s = 0, min1 = INT_MAX, min2 = INT_MIN, od = 0;     cin >> n;     long long int a[n + 5];     for (i = 0; i < n; i++) {       cin >> a[i];       if (a[i] >= 0) {         if (a[i] % 2) od++, min1 = min(min1, a[i]);         s += a[i];       } else {         if (a[i] % 2) min2 = max(min2, a[i]);       }     }     if (s % 2 == 0) {       if (min1 == INT_MAX)         cout << s + min2 <<   n ;       else if (min2 == INT_MIN)         cout << s - min1 <<   n ;       else {         long long int x = s - min1;         long long int y = s + min2;         cout << max(x, y) <<   n ;       }     } else       cout << s <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int main() {     long long int n, x, ans, ans1, y;     cin >> y;     n = (long long int)sqrt((double)y);     for (long long int i = n; i > 0; i--) {       if (y % i == 0) {         x = y / i;         if (gcd(i, x) == 1) {           ans = i;           ans1 = x;           break;         }       }     }     if (y != 1)       cout << ans <<     << ans1 << endl;     else       cout << 1 <<     << 1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   ostream& operator<<(ostream& os, const vector<T>& v) {     for (T i : v) os << i <<    ;     return os;   }   template <class T>   ostream& operator<<(ostream& os, const set<T>& v) {     for (T i : v) os << i <<    ;     return os;   }   template <class T, class S>   ostream& operator<<(ostream& os, const pair<T, S>& v) {     os << v.first <<     << v.second;     return os;   }   template <class T, class S>   ostream& operator<<(ostream& os, const unordered_map<T, S>& v) {     for (auto i : v) os <<  (  << i.first <<  =>  << i.second <<  )  <<    ;     return os;   }   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   #pragma GCC target(         avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   const long long inf = 1e18;   const long long mod = 1e9 + 7;   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   clock_t time_p = clock();   void starboy1299() {     time_p = clock() - time_p;     cerr <<  Time Taken :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ;   }   const long long N = 5e5 + 5;   void solve() {     long long n, p;     cin >> n >> p;     for (long long i = 1; i <= 32; i++) {       long long val = n - p * i;       long long temp = __builtin_popcount(val);       if (temp <= i and val >= i) {         cout << i <<   n ;         return;       }     }     cout << -1 <<   n ;   }   int32_t main() {     fast();     long long t = 1;     for (long long i = 1; i <= t; i++) {       solve();     }     starboy1299();   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, k, ans = 0;     cin >> n >> k;     if (n < 3) {       cout << 0;       return 0;     }     for (int i = 0; i < n; ++i) {       cin >> x;       if (5 - x >= k) ans++;     }     cout << ans / 3;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int arr[4];     for (int i = 0; i < 4; i++) cin >> arr[i];     string strips;     cin >> strips;     int n = strips.length();     int cal = 0;     for (int i = 0; i < n; i++) {       if (strips[i] ==  1 )         cal += arr[0];       else if (strips[i] ==  2 )         cal += arr[1];       else if (strips[i] ==  3 )         cal += arr[2];       else if (strips[i] ==  4 )         cal += arr[3];     }     cout << cal;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void Kinishinaide() {     ios_base::sync_with_stdio(0);     cin.tie(0), cout.tie(0);   }   const long long N = 1e2 + 7;   const long long Mod = 1e9 + 7;   const long long Inf = 1e18 + 7;   int M, L;   long long Aq[N];   long long dp[N], Ans;   int main() {     Kinishinaide();     cin >> M >> L;     memset(dp, Inf, sizeof(dp));     for (int i = 0; i < M; i++) {       cin >> Aq[i];       if (i) {         dp[i] = min(Aq[i], (dp[i - 1] << 1));       } else {         dp[i] = Aq[i];       }     }     long long Sum = false, Ans = Inf;     for (int i = M - 1; i >= 0; i--) {       Sum += 1LL * (L / (1 << i)) * dp[i];       L = L % (1 << i);       Ans = min(Ans, Sum + 1LL * (L > 0) * dp[i]);     }     cout << Ans;   }
#include <bits/stdc++.h>   using namespace std;   const int dx[] = {1, 0, -1, 0};   const int dy[] = {0, -1, 0, 1};   const double EPS = 1e-9;   const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   int main() {     int n, k;     cin >> n >> k;     vector<int> A(n);     for (unsigned int i = 0; i < (n); i++) cin >> A[i];     sort(A.begin(), A.end());     vector<int> B = A;     unique(B.begin(), B.end());     reverse(B.begin(), B.end());     vector<int> C = B;     for (unsigned int i = 0; i < (B.size()); i++) {       C[i] = count(A.begin(), A.end(), B[i]);     }     int kk = 0;     for (unsigned int i = 0; i < (B.size()); i++) {       kk += C[i];       if (kk == k) {         cout << B[i] <<     << B[i] << endl;         return 0;       }     }     cout << -1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long power(long long a, long long b) {     long long x = 1, y = a;     while (b > 0) {       if (b % 2) {         x = (x * y) % 1000000007;       }       y = (y * y) % 1000000007;       b /= 2;     }     return x % 1000000007;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     long long int t = 1, n, m, i, j, k, sum = 0;     while (t--) {       cin >> n;       vector<long long int> v(n);       for (i = 0; i < n; i++) cin >> v[i];       sort((v).begin(), (v).end());       for (i = 1; i < n; i++) {         v[i] += v[i - 1];       }       for (i = 1; i < n; i++) {         sum = (sum + (power(2, i - 1) *                       ((v[n - 1] - v[i - 1] - v[n - i - 1]) % 1000000007)) %                          1000000007) %               1000000007;         sum = sum % 1000000007;       }       cout << sum;     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( -O3 )   using namespace std;   const long long N = 1e6 + 5;   const long long MAX = 1e7 + 9;   long long binarySearch(long long arr[], long long l, long long r, long long x) {     if (r >= l) {       long long mid = l + (r - l) / 2;       if (arr[mid] == x) return mid;       if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);       return binarySearch(arr, mid + 1, r, x);     }     return -1;   }   void pairsort(long long a[], long long b[], long long n) {     pair<long long, long long> pairt[n];     for (long long i = 0; i < n; i++) {       pairt[i].first = a[i];       pairt[i].second = b[i];     }     sort(pairt, pairt + n);     for (long long i = 0; i < n; i++) {       a[i] = pairt[i].first;       b[i] = pairt[i].second;     }   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long isprime(long long n) {     if (n < 2) return 0;     if (n < 4) return 1;     if (n % 2 == 0 or n % 3 == 0) return 0;     for (long long i = 5; i * i <= n; i += 6)       if (n % i == 0 or n % (i + 2) == 0) return 0;     return 1;   }   long long C(long long n, long long r) {     if (r > n - r) r = n - r;     long long ans = 1;     for (long long i = 1; i <= r; i++) {       ans *= n - r + i;       ans /= i;     }     return ans;   }   long long mod = 1e9 + 7;   long long modexpo(long long x, long long p) {     long long res = 1;     x = x % mod;     while (p) {       if (p % 2) res = res * x;       p >>= 1;       x = x * x % mod;       res %= mod;     }     return res;   }   long long countDistinct(long long arr[], long long n) {     sort(arr, arr + n);     long long res = 0;     for (long long i = 0; i < n; i++) {       while (i < n - 1 && arr[i] == arr[i + 1]) i++;       res++;     }     return res;   }   long long int2Bin(string s) {     reverse(s.begin(), s.end());     long long b = 1;     long long x = 0;     for (long long i = 0; i < s.length(); ++i) {       if (s[i] ==  1 ) x += b;       b *= 2;       ;     }     return x;   }   long long getSum(long long n) {     long long sum;     for (sum = 0; n > 0; sum += n % 10, n /= 10)       ;     return sum;   }   long long nex(long long i) { return (i / 10 + 1) * 10 - i; }   bool up[N];   long long a[N], ind, leave[11];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       pair<long long, long long> a[n];       long long i;       for (i = 0; i < n; i++) {         long long x;         cin >> x;         a[i].first = x;         a[i].second = i;       }       sort(a, a + n);       long long ans = 2 * n;       for (i = 1; i < n; i++) {         if (a[i].first == a[i - 1].first)           ans = min(ans, a[i].second - a[i - 1].second + 1);       }       if (ans == 2 * n)         cout <<  -1 n ;       else         cout << ans <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 5;   int n;   char str[N];   vector<int> g[N];   inline void init() {     scanf( %d , &n);     for (int i = 1, u, v; i < n; i++) {       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     scanf( %s , str + 1);     for (int i = 1; i <= n; i++) {       if (str[i] ==  W ) {         g[i].push_back(++n);         g[n].push_back(i);         g[n].push_back(n + 1);         g[n + 1].push_back(n);         g[n].push_back(n + 2);         g[n + 2].push_back(n);         n = n + 2;       }     }   }   int dfs(int p, int fa, int dest) {     if (p == dest) {       return 1;     }     for (auto e : g[p]) {       if (e ^ fa) {         int ret = dfs(e, p, dest);         if (~ret) {           return ret ^ 1;         }       }     }     return -1;   }   vector<int> d3;   inline void solve() {     d3.clear();     for (int i = 1; i <= n; i++) {       if (g[i].size() >= 4) {         puts( White );         return;       }       if (g[i].size() == 3) {         d3.push_back(i);         int _cnt = 0;         for (auto e : g[i]) {           if (g[e].size() >= 2) {             _cnt++;           }         }         if (_cnt >= 2) {           puts( White );           return;         }       }     }     if (d3.size() >= 3) {       puts( White );       return;     }     if (d3.size() == 2) {       puts((dfs(d3[0], 0, d3[1])) ? ( White ) : ( Draw ));     } else {       puts( Draw );     }   }   inline void clear() {     for (int i = 1; i <= n; i++) {       g[i].clear();     }   }   int T;   int main() {     scanf( %d , &T);     while (T--) {       init();       solve();       clear();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     int test = 1;     cin >> test;     while (test--) {       string s;       cin >> s;       int a = -1, b = -1, c = -1;       int ans = s.size();       for (int i = 0; i < s.size(); i++) {         if (s[i] ==  1 )           a = i;         else if (s[i] ==  2 )           b = i;         else           c = i;         if (a != -1 && b != -1 && c != -1) {           int x = min(a, min(b, c));           int y = max(a, max(b, c));           ans = min(ans, y - x + 1);         }       }       if (a != -1 && b != -1 && c != -1)         cout << ans <<   n ;       else         cout << 0 <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long double gr = (1 + sqrt(5)) / 2;   long long n, k, c, res[107], mres;   string s;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> k >> s;     c = 1;     for (int i = 0; i < n; i++) {       if (i > 0) {         if (s[i] == s[i - 1]) {           c++;           if (c == k) {             c = 0;             res[s[i] -  a  + 1]++;           }         } else           c = 1;       }       if (c == k) {         c = 0;         res[s[i] -  a  + 1]++;       }     }     for (int i = 1; i <= 26; i++) mres = max(mres, res[i]);     cout << mres << endl;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> primes;   void sieve() {     vector<int> a(1000000, 0);     for (int u = 2; u <= 1000000; u++) {       if (a[u]) continue;       for (int i = 2 * u; i <= 1000000; i += u) a[i] = u;     }     for (int i = 2; i <= 1000000; i++) {       if (!a[i]) primes.push_back(i);     }   }   int gcd(int a, int b) {     while (1) {       if (a == 0) return b;       b %= a;       if (b == 0) return a;       a %= b;     }   }   int lcm(int a, int b) {     int g = gcd(a, b);     return ((a * b) / g);   }   int digitCount(int n) {     int c = 0;     while (n != 0) {       c += n % 10;       n /= 10;     }     return c;   }   int main() {     int n;     vector<int> ary = {0,   70,  140, 210,  280,  350,  601,  671, 741,                        811, 881, 951, 1202, 1272, 1342, 1412, 1440};     string str;     cin >> str;     n = (str[4] - 48) + (str[3] - 48) * 10 + (str[1] - 48) * 60 +         (str[0] - 48) * 600;     for (int i = 0; i < ary.size(); i++) {       if (n <= ary[i]) {         cout << ary[i] - n << endl;         return 0;       }     }   }
#include <bits/stdc++.h>   using namespace std;   char s[105];   int main(void) {     int n, f;     while (cin >> n) {       scanf( %s , s);       f = 0;       for (int l = (1); l <= (n); ++l) {         for (int j = (0); j <= (n - 4 * l - 1); ++j) {           if (j + 4 * l >= n) break;           if (s[j] ==  *  && s[j + l] ==  *  && s[j + 2 * l] ==  *  &&               s[j + 3 * l] ==  *  && s[j + 4 * l] ==  * ) {             f = 1;             break;           }         }         if (f) break;       }       if (f)         cout <<  yes n ;       else         cout <<  no n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> thing[20005 * 4], query[20005 * 4];   struct Thing {     int cost, happy;   } d[20005];   int cost[20005], f[4005], tree[20005 * 4], tmp[16][4005], ans[20005];   int x, i, n, m, p, y;   void build1(int k, int l, int r) {     if (x <= l && r <= y) {       thing[k].push_back(i);       return;     }     int mid = (l + r) >> 1;     if (x <= mid) build1(k << 1, l, mid);     if (y > mid) build1(k << 1 | 1, mid + 1, r);   }   void build2(int k, int l, int r) {     tree[k] = 1;     int mid = (l + r) >> 1;     if (l == r) {       query[k].push_back(i);       return;     }     if (x <= mid)       build2(k << 1, l, mid);     else       build2(k << 1 | 1, mid + 1, r);   }   void work(int k, int l, int r, int dep) {     if (thing[k].size()) {       memcpy(tmp[dep], f, sizeof(f));       for (int i = 0; i < thing[k].size(); i++) {         int lose = d[thing[k][i]].cost, win = d[thing[k][i]].happy;         for (int j = 4000; j >= lose; j--) f[j] = max(f[j], f[j - lose] + win);       }     }     if (l == r) {       for (int i = 0; i < query[k].size(); i++)         ans[query[k][i]] = f[cost[query[k][i]]];       if (thing[k].size()) memcpy(f, tmp[dep], sizeof(tmp[dep]));       return;     }     int mid = (l + r) >> 1;     work(k << 1, l, mid, dep + 1);     work(k << 1 | 1, mid + 1, r, dep + 1);     if (thing[k].size()) memcpy(f, tmp[dep], sizeof(tmp[dep]));   }   int main() {     scanf( %d %d , &n, &p);     for (i = 1; i <= n; i++) {       scanf( %d%d%d , &d[i].cost, &d[i].happy, &x);       y = x + p - 1;       build1(1, 1, 20000);     }     scanf( %d , &m);     for (i = 1; i <= m; i++) {       scanf( %d%d , &x, &cost[i]);       build2(1, 1, 20000);     }     work(1, 1, 20000, 0);     for (i = 1; i <= m; i++) printf( %d n , ans[i]);   }