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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()

using vi = vector<int>;
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using pull = pair<ull, ull>;
using vll = vector<ll>;
using ii = pair<int, int>;

const ll BIG_INF = 1e18;
const int N = 2e5 + 5;
const int LOG = 19;

ll dists[LOG][N];
int centro_anc[N];
int centro_level[N];
vector<vector<pair<ll, ii>>> centro_stacks(N);

void solve_centro(int n, vector<vector<ii>>& g, vector<vector<ll>>& queries)
{
    vi blocked(n + 1);
    int CENTRO = -1;
    int SZ = 0;

    function<void(int, int, int, ll)> dfs_dists = [&](int v, int level, int p, ll d)
    {
        dists[level][v] = d;
        for (auto [u, w] : g[v])
        {
            if (u == p or blocked[u])
            {
                continue;
            }
            dfs_dists(u, level, v, d + w);
        }
    };

    function<int(int, int)> dfs_SZ = [&](int v, int p)
    {
        int roz = 1;
        bool czy_centro = true;
        for (auto [u, w] : g[v])
        {
            if (u == p or blocked[u])
            {
                continue;
            }
            int child_size = dfs_SZ(u, v);
            czy_centro &= child_size * 2 < SZ;
            roz += child_size;
        }
        if (czy_centro and (roz * 2 >= SZ))
        {
            CENTRO = v;
        }
        return roz;
    };

    function<void(int, int, int)> decompose = [&](int start, int level, int p)
    {
        SZ = dfs_SZ(start, -1);
        dfs_SZ(start, -1);
        
        int centro = CENTRO;
        centro_anc[centro] = p;
        centro_level[centro] = level;
        centro_stacks[centro].pb({BIG_INF, {-1, 0}});
        dfs_dists(centro, level, -1, 0);
        blocked[centro] = true;

        for (auto [neigh, _] : g[centro])
        {
            if (blocked[neigh])
            {
                continue;
            }
            decompose(neigh, level + 1, centro);
        }
    };

    decompose(1, 0, -1);
    
    auto update = [&](int start, ll z, int col, int time)
    {
        int centro = start;
        while (centro != -1)
        {
            ll dist = dists[centro_level[centro]][start];
            while(centro_stacks[centro].back().f <= z - dist)
            {
                centro_stacks[centro].pop_back();
            }

            if (z - dist >= 0)
            {
                centro_stacks[centro].pb({z - dist, {time, col}});
            }
            centro = centro_anc[centro];
        }
    };

    auto ask = [&](int start)
    {
        int res = 0;
        int when_res = -1;

        int centro = start;
        while (centro != -1)
        {
            ll dist = dists[centro_level[centro]][start];
            
            pair<ll, ii> value = {dist, {-1, -1}};
            auto it = lower_bound(centro_stacks[centro].rbegin(), centro_stacks[centro].rend(), value);
            assert(it != centro_stacks[centro].rend());

            if (it->s.f > when_res)
            {
                res = it->s.s;
                when_res = it->s.f;
            }

            centro = centro_anc[centro];
        }
        return res;
    };

    FOR (time, sz(queries))
    {
        int rodz = queries[time][0];

        if (rodz == 1)
        {
            assert(false);
        }
        else if (rodz == 2)
        {
            assert(false);
        }
        else if (rodz == 3)
        {
            long long v = queries[time][1], z = queries[time][2], k = queries[time][3];
            update(v, z, k, time);
        }
        else
        {
            int v = queries[time][1];
            cout << ask(v) << '\n';
        }
    }    
}

void solve()
{
    int n, m, q;
    cin >> n >> m >> q;

    vector<pair<ii, int>> edges(m);
    vector<vector<ll>> queries(q);

    FOR (i, m)
    {
        cin >> edges[i].f.f >> edges[i].f.s >> edges[i].s;
    }

    bool centro = (m == n - 1);
    FOR (i, q)
    {
        int rodz;
        cin >> rodz;
        centro &= rodz >= 3;

        queries[i].resize((rodz == 2 ? 3 : rodz == 4 ? 2 : 4));
        queries[i][0] = rodz;

        for (int j = 1; j < sz(queries[i]); j++)
        {
            cin >> queries[i][j];
        }
    }

    vector<vector<ii>> g(n + 1);
    FOR (i, m)
    {
        int a = edges[i].f.f, b = edges[i].f.s, c = edges[i].s;
        g[a].pb({b, c});
        g[b].pb({a, c});
    }

    if (centro)
    {
        solve_centro(n, g, queries);
        return;
    }

    vi cols(n + 1);
    
    auto usun = [&](int a, int b)
    {
        FOR (i, sz(g[a]))
        {
            if (g[a][i].f == b)
            {
                g[a].erase(g[a].begin() + i);
                break;
            }
        }
    };

    function<void(int, int, ll, int k)> dfs = [&](int start, int p, ll c, int k)
    {
        cols[start] = k;
        for (auto [u, w] : g[start])
        {
            if (u == p)
                continue;
            if (w <= c)
                dfs(u, start, c - w, k);
        }
    };

    FOR (_, q)
    {
        int rodz = queries[_][0];

        if (rodz == 1)
        {
            int a = queries[_][1], b = queries[_][2], c = queries[_][3];
            g[a].pb({b, c});
            g[b].pb({a, c});
        }
        else if (rodz == 2)
        {
            int a = queries[_][1], b = queries[_][2];

            usun(a, b);
            usun(b, a);
        }
        else if (rodz == 3)
        {
            long long v = queries[_][1], z = queries[_][2], k = queries[_][3];
            dfs(v, -1, z, k);
        }
        else
        {
            int v = queries[_][1];
            cout << cols[v] << "\n";
        }
    }
}

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

    int tests = 1;
    // cin >> tests;

    for (int test = 1; test <= tests; test++)
    {
        solve();
    }

    return 0;
}