Problem:
F: 友達探し
問題
$1$ から $N$ までの番号が付けられた $N$ 人の人に対して，ある部屋の入退室記録が $M$ 個与えられる．
$i$ 番目の入退室記録は $A_i\ B_i\ C_i$ の $3$ つの整数からなり，これは以下を示している．
$C_i = 1$ のとき，時刻 $A_i$ に $B_i$ さんが部屋に入室したことを表す.
$C_i = 0$ のとき，時刻 $A_i$ に $B_i$ さんが部屋から退室したことを表す.
$i$ 番目($1 \leq i \leq N$)の人について，時刻 $T$ までに部屋で一緒に過ごした時間が最も長い人を答えよ．
制約
入力値は全て整数である.
$2 \leq N,\ M \leq 10^5$
$1 \leq T \leq 10^9$
$1 \leq A_i \leq T$
$A_i \leq A_{i+1}$
$1 \leq B_i \leq N$
$(A_i = A_j$ かつ $B_i = B_j$) ならば $i=j$
$0 \leq C_i \leq 1$
時刻 $0$ には部屋に誰もいない
入退室が矛盾する入力は与えられない
部屋に $100$ 人より多くの人がいることはない
入力形式
入力は以下の形式で与えられる.
$N\ M\ T$
$A_1\ B_1\ C_1$
…
$A_M\ B_M\ C_M$
出力
$i$ 行目には $i$ 番目の人について，時刻 $T$ までに部屋で一緒に過ごした時間が最も長い人を出力する．
ただし，自分以外で一緒に過ごした時間が最も長い人が複数人いる場合は，最も小さい番号の人を出力せよ．
また，各行の末尾に改行を出力せよ．
サンプル
サンプル入力 1
3 6 10
1 1 1
2 2 1
4 3 1
5 1 0
6 2 0
6 3 0
サンプル出力 1
2
1
2
サンプル入力 2
3 2 5
1 1 1
2 2 1
サンプル出力 2
2
1
1
$3$ 番目の人は，$1$ 番目の人とも $2$ 番目の人とも一緒にいた時間が $0$ なので，最も小さい $1$ を出力する.

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct PairTime {
    int i;
    int j;
    long long duration;
};

// Comparator to sort PairTime by i then by j
bool cmp_pair(const PairTime &a, const PairTime &b) {
    if(a.i != b.i) return a.i < b.i;
    return a.j < b.j;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int N, M;
    long long T;
    while(cin >> N >> M >> T){
        // Структура для событий
        struct Event {
            long long time;
            int person;
            int type; // 1 = вход, 0 = выход
        };
        vector<Event> events(M);
        for(int i=0;i<M;i++){
            cin >> events[i].time >> events[i].person >> events[i].type;
        }
        
        // Текущее время и набор людей в комнате
        long long current_time = 0;
        vector<int> current_set; // Люди в комнате
        current_set.reserve(100); // По условию, не более 100 человек
        
        // Список пар и времени их совместного пребывания
        vector<PairTime> pair_times;
        pair_times.reserve((long long)M * 100); // Оценка размера
        
        for(int i=0;i<M;i++){
            long long event_time = events[i].time;
            int person = events[i].person;
            int type = events[i].type;
            
            long long duration = event_time - current_time;
            if(duration > 0 && current_set.size() >=2){
                // Перебираем все пары в текущем наборе
                for(int p=0; p < current_set.size(); p++){
                    for(int q=p+1; q < current_set.size(); q++){
                        int a = current_set[p];
                        int b = current_set[q];
                        if(a > b) swap(a, b);
                        pair_times.push_back(PairTime{a, b, duration});
                    }
                }
            }
            
            // Обновляем набор людей в комнате
            if(type ==1){
                current_set.push_back(person);
            }
            else{
                // Удаляем человека из набора
                int pos = -1;
                for(int p=0; p < current_set.size(); p++) if(current_set[p] == person){ pos = p; break;}
                if(pos != -1){
                    current_set.erase(current_set.begin() + pos);
                }
            }
            
            current_time = event_time;
        }
        
        // Обработка оставшегося времени до T
        if(current_time < T && current_set.size() >=2){
            long long duration = T - current_time;
            for(int p=0; p < current_set.size(); p++){
                for(int q=p+1; q < current_set.size(); q++){
                    int a = current_set[p];
                    int b = current_set[q];
                    if(a > b) swap(a, b);
                    pair_times.push_back(PairTime{a, b, duration});
                }
            }
        }
        
        // Сортируем пары по (i, j)
        sort(pair_times.begin(), pair_times.end(), cmp_pair);
        
        // Массивы для хранения максимального времени и соответствующего друга
        vector<long long> max_time(N+1, 0);
        vector<int> max_friend_result(N+1, 0);
        
        // Аггрегируем время для каждой пары и обновляем максимальные значения
        int prev_i = -1, prev_j = -1;
        long long current_sum = 0;
        for(auto &p : pair_times){
            if(p.i != prev_i || p.j != prev_j){
                if(prev_i != -1){
                    // Обновляем для предыдущей пары
                    if(current_sum > max_time[prev_i] || (current_sum == max_time[prev_i] && prev_j < max_friend_result[prev_i])){
                        max_time[prev_i] = current_sum;
                        max_friend_result[prev_i] = prev_j;
                    }
                    if(current_sum > max_time[prev_j] || (current_sum == max_time[prev_j] && prev_i < max_friend_result[prev_j])){
                        max_time[prev_j] = current_sum;
                        max_friend_result[prev_j] = prev_i;
                    }
                }
                // Сбрасываем для новой пары
                prev_i = p.i;
                prev_j = p.j;
                current_sum = p.duration;
            }
            else{
                current_sum += p.duration;
            }
        }
        // Обрабатываем последнюю пару
        if(prev_i != -1){
            if(current_sum > max_time[prev_i] || (current_sum == max_time[prev_i] && prev_j < max_friend_result[prev_i])){
                max_time[prev_i] = current_sum;
                max_friend_result[prev_i] = prev_j;
            }
            if(current_sum > max_time[prev_j] || (current_sum == max_time[prev_j] && prev_i < max_friend_result[prev_j])){
                max_time[prev_j] = current_sum;
                max_friend_result[prev_j] = prev_i;
            }
        }
        
        // Для каждого человека, если он не был в комнате с кем-либо, назначаем минимальный номер другого человека
        for(int person=1; person<=N; person++){
            if(max_time[person]==0){
                // Находим минимальный номер другого человека
                // Так как номера начинаются с 1, минимальный возможный другой номер - 1
                // Если person !=1, назначаем 1, иначе 2
                if(person !=1){
                    max_friend_result[person]=1;
                }
                else{
                    max_friend_result[person]=2;
                }
            }
        }
        
        // Выводим результат для каждого человека
        for(int person=1; person<=N; person++){
            cout << max_friend_result[person] << "\n";
        }
    }
}
accuracy: 1
time_ms: 320
memory_kb: 52684
final_rank: 1

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, t;
    cin >> n >> m >> t;
    vector<vector<pair<int,int>>> lg(n);
    vector<int> in(n);
    vector<int> st;

    auto push = [&](int tm, int x) -> void {
        in[x] = tm;
        st.push_back(x);
    };
    auto pop = [&](int tm, int x) -> void {
        for (int i = 0; i < st.size(); ++i) {
            if (st[i] == x) {
                swap(st[i], st.back());
                st.pop_back();
                i -= 1;
            } else {
                int y = st[i];
                lg[x].push_back({y, tm - max(in[x], in[y])});
                lg[y].push_back({x, tm - max(in[x], in[y])});
            }
        }
    };
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        if (c == 1) push(a, b - 1);
        else pop(a, b - 1);
    }
    vector<int> v;
    for (int i : st) {
        v.push_back(i);
    }
    for (int i : v) {
        pop(t, i);
    }

    vector<int> res(n);
    vector<int> mx(n);
    res[0] = 1;
    for (int i = 0; i < n; ++i) {
        sort(lg[i].begin(), lg[i].end());
        for (int j = 0; j < lg[i].size();) {
            int y = lg[i][j].first;
            int sum = 0;
            while (j < lg[i].size() and lg[i][j].first == y) {
                sum += lg[i][j].second;
                j += 1;
            }
            if (mx[i] < sum) {
                mx[i] = sum;
                res[i] = y;
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        cout << res[i] + 1 << "\n";
    }
}
accuracy: 1
time_ms: 270
memory_kb: 91748
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;

unordered_map<int,int> mp[100010];
signed main(){
    // ios::sync_with_stdio(false);
	// cin.tie(0);
    // cout << fixed << setprecision(20);

    int n,m,t;
    //cin>>n>>m>>t;
    scanf("%d %d %d",&n,&m,&t);
    vector<pair<int,pair<int,int>>> v;
    for(int i=0;i<m;i++){
        int a,b,c;
        //cin>>a>>b>>c;
        scanf("%d %d %d",&a,&b,&c);
        b--;
        v.push_back(make_pair(a,make_pair(b,c)));
    }
    //sort(v.begin(),v.end());
    pair<int,int> ans[n]={};
    ans[0].second = 1;
    int enter[n]={};
    set<int> st;

    for(int i=0;i<m;i++){
        int p = v[i].second.first;
        //cerr << v[i].first << " " << v[i].second.first << " " << v[i].second.second<<endl;
        if(v[i].second.second == 1){
            st.insert(p);
            enter[p] = v[i].first;
        }
        else{
            st.erase(p);
            for(auto j:st){
                int mini=p,ma=j;
                if(mini>ma) swap(mini,ma);
                pair<int,int> pa = make_pair(mini,ma);
                
                int k = mp[mini][ma] += v[i].first - max(enter[p],enter[j]);
                if(ans[p].first == k){
                    ans[p].second = min(ans[p].second,j);
                }
                else if(ans[p].first < k){
                    ans[p] = make_pair(k,j);
                }
                if(ans[j].first == k){
                    ans[j].second = min(ans[j].second,p);
                }
                else if(ans[j].first < k){
                    ans[j] = make_pair(k,p);
                }
            }
        }
    }
    for(auto p:st){
        for(auto j:st){
            if(p>=j) continue;
            int mini = p,ma=j;
            pair<int,int> pa = make_pair(mini,ma);
            int k = mp[mini][ma] += t - max(enter[p],enter[j]);
            if(ans[p].first == k){
                ans[p].second = min(ans[p].second,j);
            }
            else if(ans[p].first < k){
                ans[p] = make_pair(k,j);
            }
            if(ans[j].first == k){
                ans[j].second = min(ans[j].second,p);
            }
            else if(ans[j].first < k){
                ans[j] = make_pair(k,p);
            }
        }
    }
    for(auto i:ans){
        //cout << i.first << " ";
        printf("%d\n",i.second+1);
    }
}
accuracy: 1
time_ms: 610
memory_kb: 169044
final_rank: 9

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() {
    cout << endl;
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest) {
    cout << first << ' ';
    print(rest...);
}

template <typename T>
void print(const vector<T> &v) {
    for (auto e : v) cout << e << ' ';
    cout << endl;
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

void solve() {
    int N, M, T; cin >> N >> M >> T;
    vector<int> in_time(N);
    set<int> in_people;
    vector<pair<int, int> > ans(N);

    vector<pair<pair<int, int>, int> > p(M);
    for (int i = 0; i < M; i++) {
        int a, b, c; cin >> a >> b >> c;
        b--;
        p[i] = make_pair(make_pair(a, b), c);
    }

    sort(p.begin(), p.end());

    for (int i = 0; i < M; i++) {
        int t = p[i].first.first, person = p[i].first.second, c = p[i].second;
        
        if (c == 1) {
            in_people.insert(person);
            in_time[person] = t;
        }
        else if (c == 0) {
            int time0 = t - in_time[person];
            in_people.erase(person);
            for (int e : in_people) {
                int time = min(time0, t - in_time[e]);
                if (time > ans[e].second) {
                    ans[e].first = person;
                    ans[e].second = time;
                }
                if (time > ans[person].second) {
                    ans[person].first = e;
                    ans[person].second = time;
                }
                if (time == ans[e].second) {
                    ans[e].first = min(person, ans[e].first);
                }
                if (time == ans[person].second) {
                    ans[person].first = min(e, ans[person].first);
                }
            }
        }
    }

    if (!in_people.empty()) {
        for (int e : in_people) {
            for (int e2 : in_people) {
                if (e == e2) continue;
                int time = min(T - in_time[e], T - in_time[e2]);
                if (time == ans[e].second) {
                    ans[e].first = min(e2, ans[e].first);
                }
                if (time > ans[e].second) {
                    ans[e].first = e2;
                    ans[e].second = time;
                }
            }
        }
    }

    for (int i = 0; i < N; i++) {
        if (i == 0 && ans[i].first == 0) cout << 2 << '\n';
        else cout << ans[i].first + 1 << '\n';
    }
}

int main() {
    //cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 0.2682926829268293
time_ms: 20
memory_kb: 3516
final_rank: 19

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() {
    cout << endl;
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest) {
    cout << first << ' ';
    print(rest...);
}

template <typename T>
void print(const vector<T> &v) {
    for (auto e : v) cout << e << ' ';
    cout << endl;
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

void solve() {
    int N, M, T; cin >> N >> M >> T;
    vector<int> in_time(N);
    set<int> in_people;
    vector<pair<int, int> > ans(N);

    vector<pair<pair<int, int>, int> > p(M);
    for (int i = 0; i < M; i++) {
        int a, b, c; cin >> a >> b >> c;
        b--;
        p[i] = make_pair(make_pair(a, b), c);
    }

    sort(p.begin(), p.end());

    for (int i = 0; i < M; i++) {
        int t = p[i].first.first, person = p[i].first.second, c = p[i].second;
        
        if (c == 1) {
            in_people.insert(person);
            in_time[person] = t;
        }
        else if (c == 0) {
            int time0 = t - in_time[person];
            in_people.erase(person);
            for (int e : in_people) {
                int time = min(time0, t - in_time[e]);
                if (time > ans[e].second) {
                    ans[e].first = person;
                    ans[e].second = time;
                }
                if (time > ans[person].second) {
                    ans[person].first = e;
                    ans[person].second = time;
                }
            }
        }
    }

    if (!in_people.empty()) {
        for (int e : in_people) {
            for (int e2 : in_people) {
                if (e == e2) continue;
                int time = min(T - in_time[e], T - in_time[e2]);
                if (time == ans[e].second) {
                    ans[e].first = min(e2, ans[e].first);
                }
                if (time > ans[e].second) {
                    ans[e].first = e2;
                    ans[e].second = time;
                }
            }
        }
    }

    for (int i = 0; i < N; i++) {
        if (i == 0 && ans[i].first == 0) cout << 2 << '\n';
        else cout << ans[i].first + 1 << '\n';
    }
}

int main() {
    //cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 0.2682926829268293
time_ms: 20
memory_kb: 3568
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
#include <set>
namespace std {
	template<>
	struct hash<typename std::pair<int, int>> {
		size_t operator()(const std::pair<int, int>& pair) const {
			return (pair.first << 16) | (pair.second);
		}
	};
}
std::pair<int, int> pair(int a, int b) {
	if (a > b) std::swap(a, b);
	return std::make_pair(a, b);
}
struct Query {
	int a, b, c;
	bool operator<(const Query& query) const {
		return a == query.a ? (c == 0 && query.c == 1) : a < query.a;
	}
};
int main() {
	std::cin.tie(0); std::cin.sync_with_stdio(false);
	int n, m, t; std::cin >> n >> m >> t;
	std::vector<int> in_time(n);
	std::unordered_set<int> room; room.reserve(100);
	std::unordered_map<std::pair<int, int>, int> history; history.reserve(m * 100);
	std::vector<Query> query(m); 
	for (auto& q : query) {
		std::cin >> q.a >> q.b >> q.c; --q.b;
	}
	std::sort(query.begin(), query.end());
	for (const auto q:query) {
		if (q.c == 0) {
			room.erase(q.b);
			for (const auto other : room) {
				history[pair(other, q.b)] += q.a - std::max(in_time[other], in_time[q.b]);
			}
		}
		else {
			room.insert(q.b);
			in_time[q.b] = q.a;
		}
	}
	for (const auto self : room) {
		for (const auto other : room) if (self < other) {
			history[pair(self, other)] += t - std::max(in_time[self], in_time[other]);
		}
	}
	std::vector<std::pair<int, int>> friends(n, std::make_pair(0, -1)); friends[0].first = 1;
	for (const auto p : history) {
		if (p.second == 0) continue;
		if (friends[p.first.first].second < p.second) {
			friends[p.first.first] = std::make_pair(p.first.second, p.second);
		}
		else if (friends[p.first.first].second == p.second && friends[p.first.first].first > p.first.second) {
			friends[p.first.first] = std::make_pair(p.first.second, p.second);
		}
		if (friends[p.first.second].second < p.second) {
			friends[p.first.second] = std::make_pair(p.first.first, p.second);
		}
		else if (friends[p.first.second].second == p.second && friends[p.first.second].first > p.first.first) {
			friends[p.first.second] = std::make_pair(p.first.first, p.second);
		}
	}
	for (const auto f : friends) {
		std::cout << f.first + 1 << '\n';
	}
}
accuracy: 1
time_ms: 480
memory_kb: 201284
final_rank: 8

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 3105.cc: Find a Friend
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef pair<int,int> pii;
typedef set<pii> spii;

/* global variables */

int ets[MAX_N], maxts[MAX_N], maxis[MAX_N];
spii rset;

/* subroutines */

/* main */

int main() {
  int n, m, t;
  scanf("%d%d%d", &n, &m, &t);

  maxis[0] = 1;

  while (m--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    b--;

    if (c == 1) { // enter
      ets[b] = a;
      rset.insert(pii(a, b));
    }
    else { // leave
      int tb = a - ets[b];
      rset.erase(pii(ets[b], b));
      ets[b] = 0;

      for (spii::iterator sit = rset.begin(); sit != rset.end(); sit++) {
	int d = sit->second;
	int tbd = min(a - ets[d], tb);

	if (maxts[b] < tbd || (maxts[b] == tbd && maxis[b] > d))
	  maxts[b] = tbd, maxis[b] = d;
	if (maxts[d] < tbd || (maxts[d] == tbd && maxis[d] > b))
	  maxts[d] = tbd, maxis[d] = b;
      }
    }
  }

  int pi = -1;
  for (spii::iterator sit = rset.begin(); sit != rset.end(); sit++) {
    int a = sit->first, b = sit->second;
    if (pi >= 0) {
      int tb = t - a;
      if (maxts[b] < tb || (maxts[b] == tb && maxis[b] > pi))
	maxts[b] = tb, maxis[b] = pi;
      if (maxts[pi] < tb || (maxts[pi] == tb && maxis[pi] > b))
	maxts[pi] = tb, maxis[pi] = b;
      if (pi > b) pi = b;
    }
    else
      pi = b;
  }

  for (int i = 0; i < n; i++) printf("%d\n", maxis[i] + 1);
  return 0;
}
accuracy: 0.2682926829268293
time_ms: 20
memory_kb: 3268
final_rank: 18

code_snippet: //
// Created by yamunaku on 2019/10/06.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> mti;
typedef vector<ll> vl;
typedef vector<vector<ll>> mtl;


int main(){
    CFS;
    int n, m, t;
    cin >> n >> m >> t;
    map<int,int> mp[100000];
    map<int, int> st;
    int a, b, c, tt;
    rep(i, m){
        cin >> a >> b >> c;
        b--;
        if(c == 1){
            st[b] = a;
        }else{
            tt = st[b];
            st.erase(st.find(b));
            for(auto &x: st){
                mp[min(b, x.first)][max(b, x.first)] += a - max(tt, x.second);
            }
        }
    }
    while(!st.empty()){
        b = st.begin()->first;
        tt = st.begin()->second;
        st.erase(st.begin());
        for(auto &x: st){
            mp[min(b, x.first)][max(b, x.first)] += t - max(tt, x.second);
        }
    }
    vector<pair<int, int>> ans(n, {0, 0});
    ans[0] = {0, 1};
    rep(i,n){
        for(auto &q: mp[i]){
            ans[i] = min(ans[i], {-q.second, q.first});
            ans[q.first] = min(ans[q.first], {-q.second, i});
        }
    }
    rep(i, n){
        cout << ans[i].second + 1 << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 900
memory_kb: 180596
final_rank: 15

code_snippet: #include <iostream>
#include <utility>
#include <vector>
#include <set>

using namespace std;
typedef pair<int, int> P;

int n, m, t;
vector<P> vec[100005];
set<int> S;
int sum[100005];
bool used[100005];
vector<int> uvec;

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m >> t;
	int a, b, c;
	for(int i = 1; i <= m; i++){
		cin >> a >> b >> c;
		if(c == 1){
			for(auto it = S.begin(); it != S.end(); it++){
				vec[*it].push_back(make_pair(b, -a));
				vec[b].push_back(make_pair(*it, -a));
			}
			S.insert(b);
		}
		else{
			S.erase(b);
			for(auto it = S.begin(); it != S.end(); it++){
				vec[*it].push_back(make_pair(b, a));
				vec[b].push_back(make_pair(*it, a));
			}
		}
	}
	for(auto it = S.begin(); it != S.end(); it++){
		for(auto it2 = S.begin(); it2 != S.end(); it2++){
			if(*it == *it2) continue;
			vec[*it].push_back(make_pair(*it2, t));
		}
	}
	
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < vec[i].size(); j++){
			int id = vec[i][j].first;
			if(!used[id]){
				used[id] = true;
				uvec.push_back(id);
			}
			sum[id] += vec[i][j].second;
		}
		P ans = make_pair(0, -1);
		if(i == 1) ans.second = -2;
		for(int j = 0; j < uvec.size(); j++){
			int id = uvec[j];
			ans = max(ans, make_pair(sum[id], -id));
			used[id] = false;
			sum[id] = 0;
		}
		uvec.clear();
		cout << -ans.second << "\n";
	}
	flush(cout);
	
	return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 172972
final_rank: 4

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005

struct Info{
    Info(int arg_index,int arg_value){
        index = arg_index;
        value = arg_value;
    }

    int index,value;
};

int N,M;
int T,table[SIZE];
vector<Info> info[SIZE];

int main(){

    scanf("%d %d %d",&N,&M,&T);

    set<int> SET;

    int TIME;
    int index,command;

    for(int i = 0; i < M; i++){

        scanf("%lld %d %d",&TIME,&index,&command);
        index--;

        if(command == 0){ //退室命令

            SET.erase(index);

            for(auto another: SET){

                info[index].push_back(Info(another,TIME));
                info[another].push_back(Info(index,TIME));
            }

        }else{ //入室命令

            for(auto another: SET){

                info[index].push_back(Info(another,-TIME));
                info[another].push_back(Info(index,-TIME));
            }

            SET.insert(index);
        }
    }

    for(auto a: SET){
        for(auto b: SET){
            if(a >= b)continue;

            info[a].push_back(Info(b,T));
            info[b].push_back(Info(a,T));
        }
    }

    for(int i = 0; i < N; i++){

        table[i] = 0;
    }

    for(int i = 0; i < N; i++){

        vector<int> V;

        for(int k = 0; k < info[i].size(); k++){

            V.push_back(info[i][k].index);

            table[info[i][k].index] += info[i][k].value;
        }
        sort(V.begin(),V.end());
        V.erase(unique(V.begin(),V.end()),V.end());

        int maximum = 0;
        int ans = 0;
        if(i == 0){
            ans = 1;
        }

        for(int k = 0; k < V.size(); k++){

            if(maximum < table[V[k]]){
                maximum = table[V[k]];
                ans = V[k];
            }
            table[V[k]] = 0;
        }

        printf("%d\n",ans+1);
    }

    return 0;
}
accuracy: 1
time_ms: 650
memory_kb: 172832
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <assert.h>
using namespace std;
int MOD = 1000000007;

struct FastSet {
	vector<int> list;
	vector<int> pos;
	void init(int N) {
		pos.resize(N, -1);
		list.reserve(N);
	}
	void insert_all() {
		list.clear();
		list.resize(pos.size());
		for (int i = 0; i < list.size(); i++) {
			pos[i] = list[i] = i;
		}
	}
	void insert(int a) {
		if (pos[a] == -1) {
			pos[a] = list.size();
			list.push_back(a);
		}
	}
	void erase(int a) {
		if (pos[a] >= 0) {
			swap(list[pos[a]], list.back());
			pos[list[pos[a]]] = pos[a];
			pos[a] = -1;
			list.pop_back();
		}
	}
	void flip(int a) {
		if (pos[a] == -1) {
			pos[a] = list.size();
			list.push_back(a);
		}
		else {
			swap(list[pos[a]], list.back());
			pos[list[pos[a]]] = pos[a];
			pos[a] = -1;
			list.pop_back();
		}
	}
	inline int size() {
		return list.size();
	}
	inline void erase_all() {
		for (int i : list) {
			pos[i] = -1;
		}
		list.clear();
	}
};
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, M, T;
	cin >> N >> M >> T;
	int A, B, C;
	vector < pair<int, pair<int, int> > > vp(M);
	vector<vector<int> > V(N);
	for (int i = 0; i < M; i++) {
		cin >> A >> B >> C;
		vp[i].first = A;
		vp[i].second.first = C;
		vp[i].second.second = B - 1;
		V[B - 1].push_back(A);
	}

	for (int i = 0; i < N; i++) {
		if ((int)V[i].size() % 2 == 1) {
			V[i].push_back(T);
		}
	}


	sort(vp.begin(), vp.end());
	vector<int> cur(N, 0);


	FastSet fs;
	fs.init(N);
	vector<map<int, int> > vm(N);
	for (int i = 0; i < M; i++) {
		A = vp[i].first;
		B = vp[i].second.second;
		C = vp[i].second.first;
		//assert(A == V[B][cur[B]]);
		cur[B]++;
		if (C == 0) {
			fs.erase(B);
		}
		else {
			//assert(cur[B] % 2 == 1);

			int t = V[B][cur[B]];
			if (t > A) {
				for (const int &j : fs.list) {
					//assert(cur[i] % 2 == 1);
					//assert(val > 0);
					//cerr << " " << B << " " << i << " " << val << endl;
					vm[B][j] += min(t, V[j][cur[j]]) - A;
					//vm[j][B] += val;
				}
				fs.insert(B);
				//assert(fs.size() <= 100);
			}
		}
	}
	vector<int> res(N, 0);
	res[0] = 1;
	vector<int> mx(N, 0);
	for (int i = 0; i < N; i++) {

		vector<pair<int, int> > vp;
		for (auto &m : vm[i]) {
			vp.push_back(m);
		}
		for (auto &m : vp) {
			int t = m.second;
			auto a = vm[m.first].find(i);
			if (a != vm[m.first].end()) {
				t += (*a).second;
			}
			if (mx[i] <= t) {
				if (mx[i] < t) {
					mx[i] = t;
					res[i] = m.first;
				}
				else {
					res[i] = min(res[i], m.first);
				}
			}
			if (mx[m.first] <= t) {
				if (mx[m.first] < t) {
					mx[m.first] = t;
					res[m.first] = i;
				}
				else {
					res[m.first] = min(res[m.first], i);
				}
			}
		}
	}
	for(int i=0;i<N;i++){
		cout << res[i] + 1 << '\n';
	}
	cout << flush;
	return 0;
}
accuracy: 1
time_ms: 620
memory_kb: 128904
final_rank: 7

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

int A[SIZE], B[SIZE], C[SIZE];
map<int, int> mm[SIZE];

ll ans[SIZE], in[SIZE];

int main(){
  int N, M, T;
  set<int> ss;

  cin >> N >> M >> T;

  for(int i=0; i<M; i++) {
    cin >> A[i] >> B[i] >> C[i];
    B[i]--;
  }

  for(int i=0; i<M; i++) {
    if (C[i] == 0) {
      auto it = ss.find(B[i]);
      ss.erase(it);

      for(int p : ss) {
        int a = p, b = B[i];
        int add = A[i] - max(in[a], in[b]);
        if (a > b) swap(a, b);
        mm[a][b] += add;
      }

      in[B[i]] = 0;

    } else {

      ss.insert(B[i]);
      in[B[i]] = A[i];
    }
  }

  for(int p : ss) {
    for(int q : ss) {
      if (p < q) {
        int add = T - max(in[p], in[q]);
        mm[p][q] += add;
      }
    }
  }

  const ll e8 = 100000000LL;

  for(int i=0; i<N; i++) {
    ans[i] = 0 * e8 + N - (i == 0);
  }

  for(int i=0; i<N; i++) {
    for(auto p : mm[i]) {
      int a = i;
      int b = p.first;
      int v = p.second;

      ans[a] = max(ans[a], v * e8 + N - b);
      ans[b] = max(ans[b], v * e8 + N - a);
    }
  }

  for(int i=0; i<N; i++) {
    int p = N - ans[i] % e8 + 1;

    printf("%d\n", p);
  }


  return 0;
}
accuracy: 1
time_ms: 840
memory_kb: 187580
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define pr(...) cerr<< GET_MACRO(__VA_ARGS__,pr8,pr7,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl
#define pr1(a) (#a)<<"="<<(a)<<" "
#define pr2(a,b) pr1(a)<<pr1(b)
#define pr3(a,b,c) pr1(a)<<pr2(b,c)
#define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d)
#define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e)
#define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f)
#define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g)
#define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h)
#define prArr(a) {cerr<<(#a)<<"={";int i=0;for(auto t:(a))cerr<<(i++?", ":"")<<t;cerr<<"}"<<endl;}
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL<<60)+1e9; // ~ 1.15 * 1e18
const Int mod = (1e9)+7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int,Int>;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
template<class T1, class T2> ostream& operator<<(ostream& o,pair<T1,T2> p){return o<<"("<<p.first<<","<<p.second<<")";}
template<class T1, class T2, class T3> ostream& operator<<(ostream& o,tuple<T1,T2,T3> t){
  return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";}
template<class T1, class T2> istream& operator>>(istream& i,pair<T1,T2> &p){return i>>p.first>>p.second;}
template<class T> ostream& operator<<(ostream& o,vector<T> a){Int i=0;for(T t:a)o<<(i++?" ":"")<<t;return o;}
template<class T> istream& operator>>(istream& i,vector<T> &a){for(T &t:a)i>>t;return i;}
//INSERT ABOVE HERE


signed main(){
  srand((unsigned)time(NULL));
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  Int N, M, T;
  cin>>N>>M>>T;

  vector<unordered_map<_int,_int> > cnt(N);
  vector<P> rnk(N, P(0, 0));
  rnk[0] = P(0, -1);
  map<Int, Int> room;

  auto calc =[&](Int a, Int b, Int c){
    if(c == 0){
      Int start1 = room[b];
      Int end1 = a;

      room.erase(b);
      for(auto p:room){
        Int t, start2; tie(t, start2) = p;
        Int score = end1 - max(start1, start2);
        if(b < t){
          cnt[b][t] += score;
          _int val = cnt[b][t];
          Max(rnk[b], P(val, -t));
          Max(rnk[t], P(val, -b));
        }
        else{
          cnt[t][b] += score;
          _int val = cnt[t][b];
          Max(rnk[b], P(val, -t));
          Max(rnk[t], P(val, -b));

        }
      }
    }
    if(c == 1) {
      room[b] = a;
    }
  };


  for(Int i=0;i<M;i++){
    Int a, b, c;
    cin>>a>>b>>c; b--;
    if(a > T) continue;
    calc(a, b, c);
  }

  for(Int i=0;i<N;i++){
    if(!room.count(i)) continue;
    calc(T, i, 0);
  }

  for(auto ans:rnk){
    cout<<-ans.second+1<<endl;
  }





  return 0;
}
accuracy: 1
time_ms: 690
memory_kb: 167668
final_rank: 12

code_snippet: #include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <utility>
#define llint long long

using namespace std;
typedef pair<llint, llint> P;

llint n, m, t;
set<llint> S;
unordered_map<llint, llint> mp;
P ans[100005];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m >> t;
	llint a, b, c;
	for(int i = 1; i <= m; i++){
		cin >> a >> b >> c;
		if(c == 1){
			for(auto it = S.begin(); it != S.end(); it++){
				int u = *it, v = b;
				if(u > v) swap(u, v);
				mp[u*100000+v] -= a;
			}
			S.insert(b);
		}
		else{
			S.erase(b);
			for(auto it = S.begin(); it != S.end(); it++){
				llint u = *it, v = b;
				if(u > v) swap(u, v);
				mp[u*100000+v] += a;
			}
		}
	}
	vector<llint> vec;
	for(auto it = S.begin(); it != S.end(); it++) vec.push_back(*it);
	for(int i = 0; i < vec.size(); i++){
		for(int j = 0; j < vec.size(); j++){
			if(i >= j) continue;
			mp[vec[i]*100000+vec[j]] += t;
		}
	}
	
	for(int i = 1; i <= n; i++){
		ans[i] = make_pair(0, 1);
		if(i == 1) ans[i] = make_pair(0, 2);
	}
	for(auto it = mp.begin(); it != mp.end(); it++){
		llint u = it->first / 100000, v = it->first % 100000;
		ans[u] = min(ans[u], make_pair(-it->second, v));
		ans[v] = min(ans[v], make_pair(-it->second, u));
	}
	for(int i = 1; i <= n; i++){
		cout << ans[i].second << "\n";
	}
	flush(cout);
	
	return 0;
}
accuracy: 0.2926829268292683
time_ms: 780
memory_kb: 80716
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll MOD=1000000007;
//const ll MOD=998244353;

#define endl "\n"

template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,const vector<T> &A){ll i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}

const ll MX=100100;


signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N,M,T;
  cin>>N>>M>>T;
  vector<pair<int,pair<int,int>>> A(M);
  cin>>A;
  for(auto &I:A){swap(I.S.F,I.S.S);}
  sort(A.begin(),A.end());
  set<int> S;
  vector<vector<pll>> ev(N);
  pair<int,int> ans[MX];
  for(int i=0;i<N;i++){ans[i]={0,0};}
  ans[0]={0,-1};
  for(int i=0;i<M;i++){
    int a=A[i].F,b=A[i].S.S,c=A[i].S.F;
    b--;
    if(c==1){
      for(auto &I:S){
        ev[I].push_back({b,-a});
        ev[b].push_back({I,-a});
      }
      S.insert(b);
    }
    else{
      S.erase(b);
      for(auto &I:S){
        ev[I].push_back({b,a});
        ev[b].push_back({I,a});
      }
    }
  }
  vector<int> rem;
  for(auto &I:S){rem.push_back(I);}
  for(auto &b:rem){
    int a=T;
    S.erase(b);
    for(auto &I:S){
      ev[I].push_back({b,a});
      ev[b].push_back({I,a});
    }
  }
  vector<ll> bk(N,0);
  for(int i=0;i<N;i++){
    for(auto &I:ev[i]){
      bk[I.F]+=I.S;
      ans[i]=max(ans[i],{bk[I.F],-1*I.F});
    }
    for(auto &I:ev[i]){
      bk[I.F]-=I.S;
    }
    cout<<ans[i].S*-1+1<<endl;
  }
  
  
  
  return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 226868
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;

unordered_map<int,int> mp[100010];
signed main(){
    // ios::sync_with_stdio(false);
	// cin.tie(0);
    // cout << fixed << setprecision(20);

    int n,m,t;
    //cin>>n>>m>>t;
    scanf("%d %d %d",&n,&m,&t);
    vector<pair<int,pair<int,int>>> v;
    for(int i=0;i<m;i++){
        int a,b,c;
        //cin>>a>>b>>c;
        scanf("%d %d %d",&a,&b,&c);
        b--;
        v.push_back(make_pair(a,make_pair(b,c)));
    }
    //sort(v.begin(),v.end());
    pair<int,int> ans[n]={};
    ans[0].second = 1;
    int enter[n]={};
    set<int> st;

    for(int i=0;i<m;i++){
        int p = v[i].second.first;
        //cerr << v[i].first << " " << v[i].second.first << " " << v[i].second.second<<endl;
        if(v[i].second.second == 1){
            st.insert(p);
            enter[p] = v[i].first;
        }
        else{
            st.erase(p);
            for(auto j:st){
                int mini=p,ma=j;
                if(mini>ma) swap(mini,ma);
                pair<int,int> pa = make_pair(mini,ma);
                
                int k = mp[mini][ma] += v[i].first - max(enter[p],enter[j]);
                if(ans[p].first == k){
                    ans[p].second = min(ans[p].second,j);
                }
                else if(ans[p].first < k){
                    ans[p] = make_pair(k,j);
                }
                if(ans[j].first == k){
                    ans[j].second = min(ans[j].second,p);
                }
                else if(ans[j].first < k){
                    ans[j] = make_pair(k,p);
                }
            }
        }
    }
    for(auto p:st){
        for(auto j:st){
            if(p>=j) continue;
            int mini = p,ma=j;
            pair<int,int> pa = make_pair(mini,ma);
            int k = mp[mini][ma] += t - max(enter[p],enter[j]);
            if(ans[p].first == k){
                ans[p].second = min(ans[p].second,j);
            }
            else if(ans[p].first < k){
                ans[p] = make_pair(k,j);
            }
            if(ans[j].first == k){
                ans[j].second = min(ans[j].second,p);
            }
            else if(ans[j].first < k){
                ans[j] = make_pair(k,p);
            }
        }
    }
    for(auto i:ans){
        //cout << i.first << " ";
        printf("%d\n",i.second+1);
    }
}
accuracy: 1
time_ms: 610
memory_kb: 169052
final_rank: 10

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

int N,M,T;
vector<int> A,B,C;

pair<int,int> ans[100010];
int in[100010];

void update(int a,int b,int t){
  if(ans[a].first<t) ans[a]=mkp(t,b);
  else if(ans[a].first==t&&ans[a].second>b) ans[a]=mkp(t,b);
}

int main(){
  cin>>N>>M>>T;
  A.resize(M);
  B.resize(M);
  C.resize(M);
  rep(i,M) cin>>A[i]>>B[i]>>C[i];

  vector<int> last(N+1,0);
  for(int i=0;i<M;i++){
    if(C[i]==0) last[B[i]]=0;
    else last[B[i]]=1;
  }
  for(int i=1;i<=N;i++){
    if(last[i]>0){
      A.push_back(T);
      B.push_back(i);
      C.push_back(0);
    }
    if(i!=1) ans[i]=mkp(0,1);
    else ans[i]=mkp(0,2);
  }

  queue<int> Q;
  vector<vector<pair<int,int>>> v(N+1);
  for(int i=0;i<C.size();i++){
    if(C[i]==1){
      Q.push(B[i]);
      in[B[i]]=A[i];
      continue;
    }

    queue<int> nq;
    while(!Q.empty()){
      int t=Q.front();
      Q.pop();
      if(t!=B[i]){
        nq.push(t);
        int a=t,b=B[i];
        if(a>b) swap(a,b);
        int cost=A[i]-max(in[a],in[b]);
        v[a].push_back(mkp(b,cost));
        v[b].push_back(mkp(a,cost));
        //update(a,b,mp[mkp(a,b)]);
      }
    }

    while(!nq.empty()){
      Q.push(nq.front());
      nq.pop();
    }
  }

  vector<int> num(N+1,0);
  for(int i=1;i<=N;i++){
      for(int j=0;j<v[i].size();j++){
          num[v[i][j].first]+=v[i][j].second;
      }
      for(int j=0;j<v[i].size();j++){
          update(i,v[i][j].first,num[v[i][j].first]);
          num[v[i][j].first]=0;
      }
  }

  for(int i=1;i<=N;i++) cout<<ans[i].second<<endl;

  return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 93044
final_rank: 3

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

int A[SIZE], B[SIZE], C[SIZE];
unordered_map<ll, int> mm;

ll ans[SIZE], in[SIZE];

int main(){
  int N, M, T;
  set<int> ss;

  cin >> N >> M >> T;

  for(int i=0; i<M; i++) {
    cin >> A[i] >> B[i] >> C[i];
    B[i]--;

    assert(i == 0 || A[i-1] <= A[i]);
  }

  for(int i=0; i<M; i++) {
    if (C[i] == 0) {
      auto it = ss.find(B[i]);
      ss.erase(it);

      for(int p : ss) {
        int a = p, b = B[i];
        int add = A[i] - max(in[a], in[b]);
        if (a > b) swap(a, b);
        mm[a * N + b] += add;
      }

      assert(in[B[i]]);
      in[B[i]] = 0;

    } else {
      assert(in[B[i]] == 0);

      ss.insert(B[i]);
      in[B[i]] = A[i];
    }
  }

  for(int p : ss) {
    for(int q : ss) {
      if (p < q) {
        int add = T - max(in[p], in[q]);
        mm[p * N + q] += add;
      }
    }
  }

  const ll e8 = 100000000LL;

  for(int i=0; i<N; i++) {
    ans[i] = 0 * e8 + N - (i == 0);
  }

  for(auto p : mm) {
    int a = p.first / N;
    int b = p.first % N;
    int v = p.second;

    debug(p);

    ans[a] = max(ans[a], v * e8 + N - b);
    ans[b] = max(ans[b], v * e8 + N - a);
  }


  for(int i=0; i<N; i++) {
    int p = N - ans[i] % e8 + 1;

    printf("%d\n", p);
  }


  return 0;
}
accuracy: 0.2926829268292683
time_ms: 740
memory_kb: 81384
final_rank: 16

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<cstdio>
using namespace std;
int N,M,T;
vector<pair<pair<int,int>,int> >ti;
int main()
{
	scanf("%d%d%d",&N,&M,&T);
	map<int,int>A;
	for(int i=0;i<M;i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		if(c==1)
		{
			A[b]=a;
		}
		else
		{
			int t=A[b];
			A.erase(A.find(b));
			for(map<int,int>::iterator it=A.begin();it!=A.end();it++)
			{
				int id=it->first,x=a-max(t,it->second);
				ti.push_back(make_pair(make_pair(b,id),x));
				ti.push_back(make_pair(make_pair(id,b),x));
			}
		}
	}
	for(map<int,int>::iterator it=A.begin();it!=A.end();)
	{
		int id=it->first,x=it->second;
		it++;
		for(map<int,int>::iterator itt=it;itt!=A.end();itt++)
		{
			int jd=itt->first,ttt=T-max(x,itt->second);
			ti.push_back(make_pair(make_pair(id,jd),ttt));
			ti.push_back(make_pair(make_pair(jd,id),ttt));
		}
	}
	int id=0;
	sort(ti.begin(),ti.end());
	for(int i=1;i<=N;i++)
	{
		int ans=i==1?2:1;
		int anstime=0;
		if(id==ti.size()||ti[id].first.first!=i)
		{
			printf("%d\n",ans);
			continue;
		}
		int now=ti[id].second;
		id++;
		while(id<ti.size()&&ti[id].first.first==i)
		{
			if(ti[id-1].first.second==ti[id].first.second)
			{
				now+=ti[id].second;
			}
			else
			{
				if(anstime<now)
				{
					anstime=now;
					ans=ti[id-1].first.second;
				}
				now=ti[id].second;
			}
			id++;
		}
		if(anstime<now)
		{
			anstime=now;
			ans=ti[id-1].first.second;
		}
		printf("%d\n",ans);
	}
}
accuracy: 1
time_ms: 700
memory_kb: 101316
final_rank: 5

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-5;
const ld pi = acos(-1.0);

unordered_map<int, int> mp[100000];
void solve() {
	int n, m; cin >> n >> m;
	int t; cin >> t;
	set<int> st;
	vector<int> a(m), b(m), c(m);
	rep(i, m) {
		cin >> a[i] >> b[i] >> c[i]; b[i]--;
	}
	rep(i, m) {
		int le = i;
		while (i + 1 < m&&a[i] == a[i + 1]) {
			i++;
		}
		int len = t - a[i];
		Rep1(j, le, i) {
			if (c[j] == 0) {
				st.erase(b[j]);
				for (auto itr = st.begin(); itr != st.end(); itr++) {
					if (*itr < b[j]) {
						mp[*itr][b[j]] -= len;
					}
					else {
						mp[b[j]][*itr] -= len;
					}
				}
			}
			else {
				for (auto itr = st.begin(); itr != st.end();itr++) {
					if (*itr < b[j]) {
						mp[*itr][b[j]] += len;
					}
					else {
						mp[b[j]][*itr] += len;
					}
				}
				st.insert(b[j]);
			}
		}
	}
	vector<int> ma(n, 0);
	vector<int> ans(n, mod);
	rep(i, n) {
		for (auto itr = mp[i].begin(); itr != mp[i].end(); itr++) {
			int id = (*itr).first;
			int val = (*itr).second;
			if (ma[i] < val) {
				ma[i] = val; ans[i] = id;
			}
			else if (ma[i] == val)ans[i] = min(ans[i], id);
			if (ma[id] < val) {
				ma[id] = val; ans[id] = i;
			}
			else if (ma[id] == val)ans[id] = min(ans[id], i);
		}
	}
	rep(i, n) {
		if (ma[i] == 0)ans[i] = 0;
		if (i == 0 && ans[i] == 0)ans[i] = 1;
		cout << ans[i] + 1 << endl;
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
	return 0;
}
accuracy: 1
time_ms: 890
memory_kb: 168316
final_rank: 13