func_code_string: #include <bits/stdc++.h>   using namespace std;   class T {    private:     long node[262145];     long low[262145];     long high[262145];     void update(long n);     long amount(long a, long b, long n, long n_a, long n_b);     long firstUp(long k, long n, long n_a, long n_b);     long firstDown(long k, long n, long n_a, long n_b);       public:     long amount(long a, long b);     long firstUp(long k);     long firstDown(long k);     void pop(long n);     void push(long n);     long size();     T();   } WaitTree, DestTree;   T::T() {     for (long i = 0; i < 262145; i++) {       low[i] = 1000000000;       high[i] = -1000000000;     }   }   long T::size() { return node[1]; }   long T::amount(long a, long b, long n, long n_a, long n_b) {     if (a > n_b || b < n_a) return 0;     if (n_a >= a && n_b <= b) return node[n];     return amount(a, b, 2 * n, n_a, (n_a + n_b) / 2) +            amount(a, b, 2 * n + 1, (n_a + n_b) / 2 + 1, n_b);   }   long T::firstDown(long k, long n, long n_a, long n_b) {     if (node[n] == 0 || n_a > k) return -1000000000;     if (n > 131071) return n - 131071;     if (n_b > k) {       long a = firstDown(k, 2 * n + 1, (n_a + n_b) / 2 + 1, n_b);       if (a > 0) return a;       return firstDown(k, 2 * n, n_a, (n_a + n_b) / 2);     } else       return high[n];   }   long T::firstUp(long k, long n, long n_a, long n_b) {     if (node[n] == 0 || n_b < k) return 1000000000;     if (n > 131071) return n - 131071;     if (n_a < k) {       long a = firstUp(k, 2 * n, n_a, (n_a + n_b) / 2);       if (a < 1000000000) return a;       return firstUp(k, 2 * n + 1, (n_a + n_b) / 2 + 1, n_b);     } else       return low[n];   }   long T::firstUp(long k) { return firstUp(k, 1, 1, 131071 + 1); }   long T::firstDown(long k) { return firstDown(k, 1, 1, 131071 + 1); }   long T::amount(long a, long b) { return amount(a, b, 1, 1, 131071 + 1); }   void T::update(long n) {     n /= 2;     while (n > 0) {       low[n] = (low[2 * n + 1] < low[2 * n] ? low[2 * n + 1] : low[2 * n]);       high[n] = (high[2 * n] > high[2 * n + 1] ? high[2 * n] : high[2 * n + 1]);       node[n] = node[2 * n] + node[2 * n + 1];       n /= 2;     }   }   void T::pop(long n) {     node[131071 + n]--;     if (node[131071 + n] == 0) {       low[131071 + n] = 1000000000;       high[131071 + n] = -1000000000;     }     update(131071 + n);   }   void T::push(long n) {     high[131071 + n] = n;     low[131071 + n] = n;     node[131071 + n]++;     update(131071 + n);   }   struct A {     long id;     long time, wait, dest;   };   bool operator<(const A &a, const A &b) { return a.time < b.time; }   vector<A> P(100005);   class X {    public:     long id, floor;     X(long _id, long _floor);   };   X::X(long _id, long _floor) {     id = _id;     floor = _floor;   }   vector<X> Waiting[100005];   vector<X> Dest[100005];   long long Arrive[100005];   long Arrived;   long N, M;   void in() {     scanf( %ld %ld , &N, &M);     for (long i = 0; i < N; i++) {       scanf( %ld %ld %ld , &P[i].time, &P[i].wait, &P[i].dest);       P[i].id = i;     }     sort(P.begin(), P.begin() + N);   }   void processEvent(long f, long long t, long &nextP) {     X x(0, 0);     while (P[nextP].time == t && nextP < N) {       Waiting[P[nextP].wait].push_back(X(P[nextP].id, P[nextP].dest));       WaitTree.push(P[nextP].wait);       nextP++;     }     while (Waiting[f].size() > 0) {       x = Waiting[f].back();       Dest[x.floor].push_back(X(x.id, x.floor));       WaitTree.pop(f);       DestTree.push(x.floor);       Waiting[f].pop_back();     }     while (Dest[f].size() > 0) {       x = Dest[f].back();       Arrive[x.id] = t;       Arrived++;       DestTree.pop(f);       Dest[f].pop_back();     }   }   void solve() {     const int UP = 1;     const int DOWN = 2;     int dir;     long nextMove;     long nextP = 0;     long p_up, p_down;     long f = 1;     long long t = 0;     while (Arrived < N) {       processEvent(f, t, nextP);       p_up = p_down = 0;       p_up += WaitTree.amount(f, 131071 + 1);       p_up += DestTree.amount(f, 131071 + 1);       p_down += WaitTree.amount(1, f);       p_down += DestTree.amount(1, f);       if (p_up >= p_down)         dir = UP;       else         dir = DOWN;       if (dir == UP) {         nextMove = 1000000000;         nextMove = (nextMove < WaitTree.firstUp(f) - f ? nextMove                                                        : WaitTree.firstUp(f) - f);         nextMove = (nextMove < DestTree.firstUp(f) - f ? nextMove                                                        : DestTree.firstUp(f) - f);         if (nextP < N)           nextMove =               (nextMove < P[nextP].time - t ? nextMove : P[nextP].time - t);         t += nextMove;         if (DestTree.size() > 0 || WaitTree.size() > 0) {           f += nextMove;         }       }       if (dir == DOWN) {         nextMove = 1000000000;         nextMove =             (nextMove < f - WaitTree.firstDown(f) ? nextMove                                                   : f - WaitTree.firstDown(f));         nextMove =             (nextMove < f - DestTree.firstDown(f) ? nextMove                                                   : f - DestTree.firstDown(f));         if (nextP < N)           nextMove =               (nextMove < P[nextP].time - t ? nextMove : P[nextP].time - t);         t += nextMove;         if (DestTree.size() > 0 || WaitTree.size() > 0) {           f -= nextMove;         }       }     }   }   void out() {     for (long i = 0; i < N; i++) printf( %I64d n , Arrive[i]);   }   int main() {     in();     solve();     out();     return 0;   }   