#include <bits/stdc++.h> typedef long long ll; #define ls cur << 1 #define rs cur << 1 | 1 using namespace std; const int N = 1e6 + 10; int pre[N], nxt[N]; int main() { ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> q; nxt[0] = -1; for(int i = 1; i <= q; i ++ ) { int op; cin >> op; if(op == 1) { int x; cin >> x; int nn = nxt[x]; pre[i] = x; nxt[x] = i; pre[nn] = i, nxt[i] = nn; } else { int x, y; cin >> x >> y; if(x == y) {cout << 0 << '\n'; continue;} int xx = x; int yy = y; ll ans = 0, res = 0; while(1) { if(xx == y || yy == x) break if(nxt[xx] != -1) { ans += xx xx = nxt[xx] } else if(nxt[yy] != -1) { res += yy yy = nxt[yy] } } if(xx == y){ ans -= x nxt[x] = y pre[y] = x cout << ans << '\n' } else{ res -= y nxt[y] = x pre[x] = y cout << res << '\n' } } } return 0; }
Note.ms
/lzyyyyyyyy