1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <bits/stdc++.h>
#define dbg(x) " [" << #x << ": " << (x) << "] "
using namespace std;
using ll = long long;
template<typename A, typename B>
ostream& operator<<(ostream& out, const pair<A,B>& p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& c) {
    out << "{";
    for(auto it = c.begin(); it != c.end(); it++) {
        if(it != c.begin()) out << ", ";
        out << *it;
    }
    return out << "}";
}

vector<vector<pair<int, ll>>> g;
vector<vector<ll>> dist;
vector<int> sz, par, rem;
vector<vector<pair<ll, pair<int, int>>>> updates;

int dfs_size(int v, int p = -1) {
    if(rem[v]) return 0;
    sz[v] = 1;
    for(auto [to, d] : g[v]) {
        if(to == p) continue;
        sz[v] += dfs_size(to, v);
    }
    return sz[v];
}

int cent(int v, int p, int n) {
    for(auto [to, d] : g[v]) {
        if(to == p || rem[to]) continue;
        if(sz[to] * 2 > n) return cent(to, v, n);
    }
    return v;
}

void compute_dist(int v, ll dst, int p, int rt) {
    dist[v].push_back(dst);
    for(auto [to, d] : g[v]) {
        if(to == p || rem[to]) continue;
        compute_dist(to, dst + d, v, rt);
    }
}

void build(int v, int p = -1) {
    dfs_size(v);
    int c = cent(v, -1, sz[v]);
    compute_dist(c, 0, -1, c);
    par[c] = p;
    rem[c] = 1;
    for(auto [to, d] : g[c]) {
        if(!rem[to]) build(to, c);
    }
}

int timer = 0;
void upd(int v, ll range, int col) {
    int first_v = v;
    int i = 0;
    while(v != -1) {
        ll d = dist[first_v][i];
        if(range >= d) {
            while(!updates[v].empty() && updates[v].back().first <= range - d) {
                updates[v].pop_back();
            }
            updates[v].push_back({range - d, {col, timer}});
        }
        v = par[v];
        i++;
    }
    timer++;
}

int get(int v) {
    int i = 0;
    int col = 0;
    int last_timer = -1;
    int first_v = v;
    while(v != -1) {
        ll d = dist[first_v][i];
        int l = -1;
        int r = (int) updates[v].size() - 1;
        while(l < r) {
            int m = (l + r + 1) >> 1;
            if(updates[v][m].first < d) {
                r = m - 1;
            } else {
                l = m;
            }
        }

        if(l < updates[v].size()) {
            if(updates[v][l].second.second > last_timer) {
                col = updates[v][l].second.first;
                last_timer = updates[v][l].second.second;
            }
        }
        v = par[v];
        i++;
    }
    return col;
}

void solve_subtask1(int n, vector<tuple<int, int, ll>> edges, vector<vector<ll>> queries) {
    g.resize(n);
    sz.resize(n);
    par.resize(n);
    rem.resize(n);
    dist.resize(n);
    updates.resize(n);
    for(auto [a, b, d] : edges) {
        a--;
        b--;
        g[a].push_back({b, d});
        g[b].push_back({a, d});
    }
    build(0);
    for(int i = 0; i < n; i++) {
        reverse(dist[i].begin(), dist[i].end());
    }

    for(int i = 0; i < queries.size(); i++) {
        int op = queries[i][0];
        int a = queries[i][1];
        a--;
        if(op == 3) {
            ll d = queries[i][2];
            int col = queries[i][3];
            upd(a, d, col);
        } else {
            cout << get(a) << '\n';
        }
    }
}

void solve_brute(int n, vector<tuple<int, int, ll>> edges, vector<vector<ll>> queries) {
    vector<vector<pair<int, ll>>> g(n + 1);
    vector<int> col(n + 1);
    for(auto [a, b, d] : edges) {
        g[a].push_back({b, d});
        g[b].push_back({a, d});
    }

    function<void(int, ll, int, int)> dfs = [&](int v, ll d, int c, int p) {
        col[v] = c;
        for(auto [to, dst] : g[v]) {
            if(to == p || dst > d) continue;
            dfs(to, d - dst, c, v);
        }
    };

    for(int i = 0; i < queries.size(); i++) {
        int op = queries[i][0];
        int a = queries[i][1];
        if(op == 1) {
            int b = queries[i][2];
            ll d = queries[i][3];
            g[a].push_back({b, d});
            g[b].push_back({a, d});
        } else if(op == 2) {
            int b = queries[i][2];
            ll d;
            for(auto [x, y] : g[a]) {
                if(b == x) {
                    d = y;
                    break;
                }
            }
            g[a].erase(remove(g[a].begin(), g[a].end(), pair<int, ll>{b, d}), g[a].end());
            g[b].erase(remove(g[b].begin(), g[b].end(), pair<int, ll>{a, d}), g[b].end());
        } else if(op == 3) {
            dfs(a, queries[i][2], queries[i][3], -1);
        } else {
            cout << col[a] << '\n';
        }
    }
}

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

    bool subtask1 = true;

    int n, m, q;
    cin >> n >> m >> q;
    vector<tuple<int, int, ll>> edges;
    for(int i = 0; i < m; i++) {
        ll a, b, d;
        cin >> a >> b >> d;
        edges.push_back({a, b, d});
    }
    if(m != n - 1) subtask1 = false;
    vector<vector<ll>> queries;
    for(int i = 0; i < q; i++) {
        int op;
        cin >> op;
        if(op == 1) {
            int a, b, d;
            cin >> a >> b >> d;
            queries.push_back({op, a, b, d});
            subtask1 = false;
        } else if(op == 2) {
            int a, b;
            cin >> a >> b;
            queries.push_back({op, a, b});
            subtask1 = false;
        } else if(op == 3) {
            ll v, z, k;
            cin >> v >> z >> k;
            queries.push_back({op, v, z, k});
        } else {
            int u;
            cin >> u;
            queries.push_back({op, u});
        }
    }

    if(subtask1) {
        solve_subtask1(n, edges, queries);
        return 0;
    }
    solve_brute(n, edges, queries);

    return 0;
}