malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll M = 1e4 + 10;   const ll G = 1e3 + 10;   ll n, m, g, r;   vector<ll> d;   ll ansDep = -1;   ll ansT = -1;   bool seen[M][G];   ll ans = 1e17;   void bfs() {     deque<tuple<ll, ll, ll> > q;     q.emplace_front(0, 0, 0);     while (!q.empty()) {       auto at = q.front();       q.pop_front();       ll v = get<0>(at), tempo = get<1>(at), dep = get<2>(at);       if (v == m - 1) {         ans = min(ans, (r + g) * dep + tempo);         continue;       }       seen[v][tempo] = true;       ll distance = d[v + 1] - d[v];       ll nt = distance + tempo;       if (nt <= g and !seen[v + 1][nt % g]) {         seen[v + 1][nt % g] = true;         if (nt == g) {           if (v + 1 != m - 1)             q.emplace_back(v + 1, 0, dep + 1);           else             q.emplace_back(v + 1, g, dep);         } else           q.emplace_front(v + 1, nt, dep);       }       if (v > 0) {         distance = d[v] - d[v - 1];         nt = distance + tempo;         if (nt <= g and !seen[v - 1][nt % g]) {           seen[v - 1][nt % g] = true;           if (nt == g)             q.emplace_back(v - 1, 0, dep + 1);           else             q.emplace_front(v - 1, nt, dep);         }       }     }   }   int main() {     ios::sync_with_stdio(false);     memset(seen, false, sizeof seen);     cin >> n >> m;     d.resize(m);     for (ll &i : d) cin >> i;     sort(d.begin(), d.end());     cin >> g >> r;     bfs();     if (ans == 1e17)       cout <<  -1 n ;     else       cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long res, cur, n, dp[1000006];   struct info {     long long x, y;     long long a;     bool operator<(const info &b) const { return x < b.x; }   };   struct P {     long long x, y;     P operator+(const P &a) const { return (P){x + a.x, y + a.y}; }     P operator-(const P &a) const { return (P){x - a.x, y - a.y}; }     long long operator*(const P &a) const {       double u, v;       if (x == 0 && a.x == 0) return 0;       if (x == 0) {         if (y == 0) return 0;         return 2 * ((y > 0 && a.x < 0) || (y < 0 && a.x > 0)) - 1;       }       if (a.x == 0) {         if (a.y == 0) return 0;         return 2 * ((a.y > 0 && x > 0) || (a.y < 0 && x < 0)) - 1;       }       u = y;       u /= x;       v = a.y;       v /= a.x;       if (u == v) return 0;       return (2 * (v > u) - 1) *              (2 * ((x > 0 && a.x > 0) || (x < 0 && a.x < 0)) - 1);     }     long long V(long long pos, int k) { return (pos - x) * k + y; }   };   vector<info> var;   vector<P> vex;   int main() {     scanf( %d , &n);     var.resize(n + 1);     var[0] = (info){0, INT_MAX, 0};     for (int i = 1; i <= n; i++) {       scanf( %d %d %lld , &var[i].x, &var[i].y, &var[i].a);     }     sort(var.begin(), var.end());     vex.push_back((P){0, 0});     for (int i = 1; i <= n; i++) {       while (cur < vex.size() - 1 &&              vex[cur].V(var[i].x, var[i].y) < vex[cur + 1].V(var[i].x, var[i].y))         cur++;       dp[i] = vex[cur].V(var[i].x, var[i].y) - var[i].a;       P lar = (P){var[i].x, dp[i]};       while (vex.size() > 1 &&              (lar - vex[vex.size() - 2]) *                      (vex[vex.size() - 1] - vex[vex.size() - 2]) <=                  0) {         if (cur == vex.size() - 1) cur--;         vex.pop_back();       }       vex.push_back(lar);     }     for (int i = 0; i <= n; i++) res = max(res, dp[i]);     printf( %lld , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool chkmin(T &x, T y) {     return x > y ? x = y, 1 : 0;   }   template <typename T>   bool chkmax(T &x, T y) {     return x < y ? x = y, 1 : 0;   }   int readint() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   const int cys = 1000000009;   int n, m, tot;   long long d[105][105], siz[105], tmp[105], ans[105], v[20005], nxt[20005],       h[105], fac[105], inv[105], rt[105], f[105][105], rs[105];   bool vis[105];   long long mod(long long x) { return x >= cys ? x - cys : x; }   long long qpow(long long x, long long p) {     long long ret = 1;     for (; p; p >>= 1, x = x * x % cys)       if (p & 1) ret = ret * x % cys;     return ret;   }   void addedge(int x, int y) {     v[++tot] = y;     nxt[tot] = h[x];     h[x] = tot;     v[++tot] = x;     nxt[tot] = h[y];     h[y] = tot;   }   void dfs(int u, int r) {     memset(d[u], 0, sizeof(d[u]));     d[u][0] = 1;     siz[u] = 1;     if (r) rt[u] = r;     int cnt = 0;     for (int p = h[u]; p; p = nxt[p]) {       if (vis[v[p]]) {         cnt++;         continue;       }       vis[v[p]] = 1;       dfs(v[p], r);       for (int i = 0; i <= siz[u] + siz[v[p]]; i++) tmp[i] = 0;       for (int i = 0; i < siz[u]; i++)         for (int j = 0; j <= siz[v[p]]; j++)           tmp[i + j] = mod(tmp[i + j] + d[u][i] * d[v[p]][j] % cys * fac[i + j] %                                             cys * inv[i] % cys * inv[j] % cys);       siz[u] += siz[v[p]];       for (int i = 0; i <= siz[u]; i++) d[u][i] = tmp[i];     }     if (cnt <= 1) d[u][siz[u]] = d[u][siz[u] - 1];     if (u == r) rs[u] = siz[u];   }   int main() {     n = readint();     m = readint();     fac[0] = inv[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % cys;     inv[n] = qpow(fac[n], cys - 2);     for (int i = n - 1; i >= 1; i--) inv[i] = inv[i + 1] * (i + 1) % cys;     for (int i = 1; i <= m; i++) addedge(readint(), readint());     ans[0] = 1;     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         vis[i] = true;         dfs(i, i);         for (int j = 0; j <= n; j++) f[i][j] = d[i][j];       } else {         for (int j = 1; j <= n; j++)           if (rt[j] == rt[i]) vis[j] = 0;         vis[i] = true;         dfs(i, 0);         for (int j = 0; j <= n; j++) f[rt[i]][j] = mod(f[rt[i]][j] + d[i][j]);       }     }     for (int i = 1; i <= n; i++) {       if (rt[i] == i) {         for (int j = 0; j < rs[i]; j++)           f[i][j] = f[i][j] * fac[rs[i] - j - 1] % cys * inv[rs[i] - j] % cys;         for (int j = 0; j <= n; j++) tmp[j] = 0;         for (int j = 0; j <= n; j++)           for (int k = 0; k <= n; k++)             tmp[j + k] = mod(tmp[j + k] + ans[j] * f[i][k] % cys * fac[j + k] %                                               cys * inv[j] % cys * inv[k] % cys);         for (int j = 0; j <= n; j++) ans[j] = tmp[j];       }     }     for (int i = 0; i <= n; i++) printf( %lld n , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int k, l, m, n, i, j, d, fucked = 0;     cin >> k >> l >> m >> n >> d;     int p[100010];     if (k == 1 || l == 1 || m == 1 || n == 1) {       cout << d;     } else {       for (i = 1; i <= d; i++) {         if (i % (k) == 0 || i % (l) == 0 || i % (m) == 0 || i % (n) == 0) {           p[i] = -1;         } else {           p[i] = 0;         }       }       for (j = 1; j <= d; j++) {         if (p[j] == -1) {           fucked++;         }       }       cout << fucked << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return (a / gcd(a, b)) * b;   }   long long int dp[2005][2005];   long long int help(long long int n, long long int k, long long int num) {     if (k <= 0) return 1;     if (dp[k][num] != -1) return dp[k][num];     long long int cnt = 0;     for (long long int j = num; j <= n; j += num) {       cnt = (cnt % 1000000007 + help(n, k - 1, j) % 1000000007) % 1000000007;     }     return dp[k][num] = cnt;   }   void solve() {     long long int n, k;     cin >> n >> k;     long long int ans = help(n, k, 1);     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     memset(dp, -1, sizeof(dp));     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   void calc(int n, long long s, long long m, int i) {     if (n == 0) return;     s = s / 2;     if (s < m) {       calc(n - 1, s, m - s, i + 1);       cout << i <<    ;     } else {       cout << i <<    ;       calc(n - 1, s, m, i + 1);     }   }   int main() {     int N;     long long M;     cin >> N >> M;     long long a = 1;     for (int i = 0; i < N - 1; i++) a *= 2;     calc(N, a, M, 1);     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   int max2;   int two[35];   void DFS(int K, int L1, int R1, int L2, int R2) {     if (((R1 - L1 + 1) <= max2) || ((R2 - L2 + 1) <= max2)) return;     if (K == 0) {       if (1 > max2) max2 = 1;       return;     }     int L3, R3;     if (L1 >= L2)       L3 = L1;     else       L3 = L2;     if (R1 <= R2)       R3 = R1;     else       R3 = R2;     if (R3 >= L3) {       if ((R3 - L3 + 1) > max2) max2 = R3 - L3 + 1;     }     int X = two[K - 1];     char cr1 = 0;     char cr2 = 0;     if ((R1 >= X) && (L1 <= X)) cr1 = 1;     if ((R2 >= X) && (L2 <= X)) cr2 = 1;     if ((cr1) && (cr2)) {       DFS(K - 1, L1, X - 1, 1, R2 - X);       DFS(K - 1, L2, X - 1, 1, R1 - X);     } else if (cr1) {       int nl, nr;       nl = L2;       nr = R2;       if (nl >= X) nl -= X;       if (nr >= X) nr -= X;       DFS(K - 1, L1, X - 1, nl, nr);       DFS(K - 1, 1, R1 - X, nl, nr);     } else if (cr2) {       int nl, nr;       nl = L1;       nr = R1;       if (nl >= X) nl -= X;       if (nr >= X) nr -= X;       DFS(K - 1, L2, X - 1, nl, nr);       DFS(K - 1, 1, R2 - X, nl, nr);     } else {       int nl1, nr1, nl2, nr2;       nl1 = L1;       nr1 = R1;       if (nl1 >= X) nl1 -= X;       if (nr1 >= X) nr1 -= X;       nl2 = L2;       nr2 = R2;       if (nl2 >= X) nl2 -= X;       if (nr2 >= X) nr2 -= X;       DFS(K - 1, nl1, nr1, nl2, nr2);     }   }   int main() {     two[0] = 1;     int i;     for (i = 1; i <= 30; i++) two[i] = two[i - 1] * 2;     int L1, R1, L2, R2;     scanf( %d %d %d %d , &L1, &R1, &L2, &R2);     max2 = 0;     DFS(30, L1, R1, L2, R2);     printf( %d n , max2);     return 0;   }
#include <bits/stdc++.h>   using std::cin;   using std::cout;   using std::endl;   using std::pair;   using std::string;   using std::swap;   using std::vector;   void doit(int& xk, int& yk, int w, int h, int x, int y, int z) {     for (int i = 0; i < x; ++i) {       swap(xk, yk);       swap(w, h);       xk = w - xk + 1;     }     if (y) {       xk = w - xk + 1;     }     for (int i = 0; i < z; ++i) {       swap(xk, yk);       swap(w, h);       yk = h - yk + 1;     }   }   int main() {     int n, m, x, y, z, p;     scanf( %d %d %d %d %d %d , &n, &m, &x, &y, &z, &p);     x %= 4;     y = y & 1;     z %= 4;     while (p--) {       int xk, yk;       scanf(  n%d %d , &yk, &xk);       doit(xk, yk, m, n, x, y, z);       printf( %d %d n , yk, xk);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     for (int i = 0; i < 2 * n + 1; i++) {       for (int j = 0; j < 2 * n + 1; j++) {         if ((i + j - n) >= 0 && i <= n && j <= n && i > 0) {           cout << i + j - n <<    ;         } else if (i <= n && j > n && (n + 1 + i - j) > 1) {           cout << n + i - j <<    ;         } else if (i > n && i < 2 * n && j <= n && (n + 1 + j - i) > 0) {           cout << (n + j - i) <<    ;         } else if (i > n && j > n && (3 * n - i - j) > 0) {           cout << 3 * n - i - j <<    ;         } else if ((i + j - n < 0 && i <= n && j <= n) ||                    i > n && j <= n && n + 1 + j - i <= 0) {           cout <<     ;         }       }       cout << 0 << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int INF = 0x3f3f3f3f;   const int mxN = 61;   ll level[mxN];   int q;   int getlevel(ll x) {     int res = 0;     while ((1LL << res) <= x) {       res++;     }     return res - 1;   }   int main() {     cin.tie(0)->sync_with_stdio(0);     cin >> q;     ll a, b, c;     for (int i = 0; i < q; ++i) {       cin >> a;       if (a == 1 || a == 2) {         cin >> b >> c;         if (a == 1) {           level[getlevel(b)] += c;         } else {           int l = getlevel(b);           for (int i = l; i < 61; ++i) {             level[i] = (level[i] + c * (1LL << (i - l))) % (1LL << i);           }         }       } else {         cin >> b;         int l = getlevel(b);         ll out = b - (1LL << l);         out = (out + level[l]) % (1LL << l);         if (out < 0) {           out = out + (1LL << l);         }         out = (out + (1LL << l));         for (int i = l; i >= 0; --i) {           ll ans = (out - (1LL << i) - level[i]) % (1LL << i);           if (ans < 0) {             ans += (1LL << i);           }           ans = (ans + (1LL << i));           cout << ans <<    ;           out /= 2;         }         cout <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int n, i, x = 0;     cin >> n;     string second;     cin >> second;     vector<long long int> v;     if (second.size() == 1 && second[0] ==  ? ) return cout <<  Yes , 0;     for (i = 0; i < second.size(); ++i) {       if (second[i] ==  ? ) v.push_back(i);       if (i != n - 1 && second[i] == second[i + 1] && second[i] !=  ? )         return cout <<  No , 0;     }     for (i = 0; i < v.size(); ++i) {       if (v[i] == 0 || v[i] == second.size() - 1) return cout <<  Yes , 0;       if (i != v.size() - 1 && v[i] == v[i + 1] - 1) return cout <<  Yes , 0;     }     for (i = 0; i < second.size(); ++i) {       if (second[i] ==  ? ) {         if (i >= 0 && i < n - 1) {           if (second[i - 1] == second[i + 1]) return cout <<  Yes , 0;         }       }     }     cout <<  No ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;      #define ll long long   #define mod 1000000007   #define ffor(i,a,b) for(int i=a;i<b;i++)   #define bfor(i,a,b) for(int i=a-1;i>=b;i--)   #define mp make_pair   #define pb push_back   #define ff first   #define ss second   #define mem(x,y) memset(x,y,sizeof(x))   #define all(x) x.begin(),x.end()   #define SP(x) setprecision(x)   #define sz(x) (int)x.size()   #define fast  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)   #define PI 3.14159265358979323846   #define lb lower_bound   #define ub upper_bound   #define bs binary_search   #define endl   n    #define int long long      void solve()   {       int n;       cin>>n;       int c[n];       int a[n], b[n];          ffor(i,0,n) cin>>c[i];       ffor(i,0,n) cin>>a[i];       ffor(i,0,n) cin>>b[i];          int ans=0;       int dp[n];       mem(dp,0);       dp[1]=abs(a[1]-b[1])+c[1]-1+2;          ffor(i,2,n)       {           if(a[i]==b[i]) dp[i]=c[i]-1+2;           else           {               int x=abs(a[i]-b[i])+c[i]-1+2;               int y=c[i]-1+2+dp[i-1]-abs(a[i]-b[i]);               dp[i]=max(x,y);           }       }       cout<< *max_element(dp,dp+n)<<endl;      }      signed main()   {           #ifndef ONLINE_JUDGE       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);       #endif                   int t;       cin>>t;       while(t--)       {           solve();       }          return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e3 + 5;   int N, M;   long long tree[4][MAXN][MAXN];   int ju(int x, int y) {     int tt = 0;     if (x % 2) tt++;     if (y % 2) tt += 2;     return tt;   }   void Update(int x, int y, long long v) {     int ty = ju(x, y);     for (int i = x; i <= N; i += i & -i)       for (int j = y; j <= N; j += j & -j) tree[ty][i][j] ^= v;   }   long long Sum(int x, int y) {     int ty = ju(x, y);     long long ans = 0;     for (int i = x; i > 0; i -= i & -i)       for (int j = y; j > 0; j -= j & -j) ans ^= tree[ty][i][j];     return ans;   }   int main() {     while (~scanf( %d %d , &N, &M)) {       memset(tree, 0, sizeof(tree));       for (int i = 1; i <= M; ++i) {         int a, b, c, d;         int ty;         long long v;         scanf( %d , &ty);         if (ty == 2) {           scanf( %d %d %d %d %lld , &a, &b, &c, &d, &v);           Update(a, b, v);           Update(c + 1, b, v);           Update(a, d + 1, v);           Update(c + 1, d + 1, v);         } else {           long long ans = 0;           scanf( %d %d %d %d , &a, &b, &c, &d);           ans ^= Sum(c, d);           ans ^= Sum(a - 1, d);           ans ^= Sum(c, b - 1);           ans ^= Sum(a - 1, b - 1);           printf( %lld n , ans);         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 7;   const int N = 4e5 + 10;   vector<int> ans[N];   vector<int> v[N];   int tot;   int vis[N];   int cnt = 0;   void dfs(int x) {     vis[x] = 1;     ans[cnt++ / tot].push_back(x);     for (auto &y : v[x]) {       if (vis[y]) continue;       dfs(y);       ans[cnt++ / tot].push_back(x);     }   }   int main() {     int n, m, k;     cin >> n >> m >> k;     tot = (2 * n + k - 1) / k;     while (m--) {       int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     for (int i = 1; i <= n; i++) {       if (vis[i]) continue;       cnt = (cnt + tot - 1) / tot * tot;       dfs(i);     }     for (int i = 0; i < k; i++) {       if (ans[i].empty()) ans[i].push_back(1);       printf( %d , ans[i].size());       for (auto &x : ans[i]) printf(  %d , x);       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const long long INF = 1e18;   template <typename T>   void read(T &x) {     x = 0;     int s = 1, c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) s = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;     x *= s;   }   template <typename T>   void write(T x) {     if (x < 0) x = -x, putchar( - );     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   template <typename T>   void writeln(T x) {     write(x);     puts(  );   }   const int maxn = 2e2 + 5, mod = 1e9 + 7;   int n, len, Next[maxn], to[maxn][2];   char s[maxn];   long long dp[maxn][maxn][maxn][2];   void Get_Next(char *s, int len) {     Next[1] = 0;     for (int i = 2, j = 0; i <= len; i++) {       while (j > 0 && s[i] != s[j + 1]) j = Next[j];       if (s[i] == s[j + 1]) j++;       Next[i] = j;     }     for (int i = 0; i <= len - 1; i++) {       int j = i;       while (j && s[j + 1] !=  ) ) j = Next[j];       if (s[j + 1] ==  ) ) to[i][1] = j + 1;       j = i;       while (j && s[j + 1] !=  ( ) j = Next[j];       if (s[j + 1] ==  ( ) to[i][0] = j + 1;     }   }   void Add(long long &x, long long y) {     x += y;     if (x >= mod) x -= mod;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> (s + 1);     len = strlen(s + 1);     Get_Next(s, len);     dp[0][0][0][0] = 1;     n *= 2;     for (int i = 0; i <= n - 1; i++) {       for (int k = 0; k <= i; k++) {         for (int j = 0; j <= min(i, len - 1); j++) {           Add(dp[i + 1][k + 1][to[j][0]][to[j][0] == len], dp[i][k][j][0]);           if (k) Add(dp[i + 1][k - 1][to[j][1]][to[j][1] == len], dp[i][k][j][0]);         }         Add(dp[i + 1][k + 1][len][1], dp[i][k][len][1]);         if (k) Add(dp[i + 1][k - 1][len][1], dp[i][k][len][1]);       }     }     cout << dp[n][0][len][1] << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long int mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int l, r, m;       cin >> l >> r >> m;       long long int nn, xx;       long long int ans = LLONG_MAX;       for (long long int i = l; i <= r; i++) {         long long int start = 1, end = 1e10;         long long int flag = 1;         while (start <= end) {           long long int mid = (start + end) / 2;           long long int diff = abs(mid * i - m);           if (diff <= (r - l)) {             if ((mid * i - m) <= 0) {               cout << i <<     << r <<     << r - diff <<   n ;             } else {               cout << i <<     << r - diff <<     << r <<   n ;             }             flag = 0;             break;             end = mid - 1;           } else if (mid * i - m >= 0) {             end = mid - 1;           } else {             start = mid + 1;           }         }         if (flag == 0) break;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 5e18;   const int MK = 10101;   const int MN = 101010;   long long H, M, N, K;   map<long long, long long> mp;   vector<long long> methods;   vector<pair<long long, long long> > ip[MK];   bool pllcmp(pair<long long, long long> p1, pair<long long, long long> p2) {     p1.second = -p1.second;     p2.second = -p2.second;     return p1 < p2;   };   priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,                  std::function<bool(pair<long long, long long>,                                     pair<long long, long long>)> >       pq(pllcmp);   long long dis[MK];   long long op[MN];   void spfa() {     for (int i = 0; i < K; i++) dis[i] = INF;     dis[0] = 0;     queue<int> qe;     qe.push(0);     while (qe.size()) {       long long v = qe.front();       qe.pop();       for (int i = 0; i < (int)methods.size(); i++) {         long long m = methods[i];         long long r = (m + v) % K;         if (dis[r] > dis[v] + m) {           dis[r] = dis[v] + m;           qe.push(r);         }       }     }     pq = priority_queue<         pair<long long, long long>, vector<pair<long long, long long> >,         function<bool(pair<long long, long long>, pair<long long, long long>)> >(         pllcmp);     for (int i = 0; i < N; i++) {       long long a = op[i], c = mp[op[i]];       long long r = a % K;       if (a >= dis[r]) {         pq.push(make_pair(c, i));       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> H >> N >> M >> K;     for (int i = 0; i < N; i++) {       long long a, c;       cin >> a >> c;       a--;       mp[a] = c;       ip[a % K].push_back(make_pair(i, c));       op[i] = a;     }     for (int i = 0; i < (int)ip[0].size(); i++) {       pq.push(make_pair(ip[0][i].second, ip[0][i].first));     }     for (int i = 1; i < K; i++) dis[i] = INF;     for (int i = 0; i < M; i++) {       int t;       cin >> t;       if (t == 1) {         long long x;         cin >> x;         methods.push_back(x);         spfa();       } else if (t == 2) {         long long id, a, c;         cin >> id >> c;         id--;         a = op[id];         mp[a] -= c;         if (dis[a % K] <= a) pq.push(make_pair(mp[a], id));       } else if (t == 3) {         pair<long long, long long> tp;         while (pq.size()) {           tp = pq.top();           if (mp[op[tp.second]] == tp.first) break;           pq.pop();         }         cout << (pq.size() ? tp.first : 0) << endl;         if (pq.size()) mp[op[tp.second]] = 0;       }     }     return 0;   }