#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } const std::vector> grid_dxs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); } template std::pair operator+(const std::pair &l, const std::pair &r) { return std::make_pair(l.first + r.first, l.second + r.second); } template std::pair operator-(const std::pair &l, const std::pair &r) { return std::make_pair(l.first - r.first, l.second - r.second); } template std::vector sort_unique(std::vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template IStream &operator>>(IStream &is, std::vector &vec) { for (auto &v : vec) is >> v; return is; } template OStream &operator<<(OStream &os, const std::vector &vec); template OStream &operator<<(OStream &os, const std::array &arr); template OStream &operator<<(OStream &os, const std::unordered_set &vec); template OStream &operator<<(OStream &os, const pair &pa); template OStream &operator<<(OStream &os, const std::deque &vec); template OStream &operator<<(OStream &os, const std::set &vec); template OStream &operator<<(OStream &os, const std::multiset &vec); template OStream &operator<<(OStream &os, const std::unordered_multiset &vec); template OStream &operator<<(OStream &os, const std::pair &pa); template OStream &operator<<(OStream &os, const std::map &mp); template OStream &operator<<(OStream &os, const std::unordered_map &mp); template OStream &operator<<(OStream &os, const std::tuple &tpl); template OStream &operator<<(OStream &os, const std::vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } template std::istream &operator>>(std::istream &is, std::tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template OStream &operator<<(OStream &os, const std::tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } template OStream &operator<<(OStream &os, const std::unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::pair &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; } template OStream &operator<<(OStream &os, const std::map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl #define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr) #else #define dbg(x) ((void)0) #define dbgif(cond, x) ((void)0) #endif #include using S = lint; S op(S l, S r) { return max(l, r); } S e() { return 0; } using F = lint; S mapping(F f, S x) { return x + f; } F composition(F f, F g) { return f + g; } F id() { return 0; } // UnionFind Tree (0-indexed), based on size of each disjoint set struct UnionFind { std::vector par, cou; UnionFind(int N = 0) : par(N), cou(N, 1) { iota(par.begin(), par.end(), 0); } int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); } bool unite(int x, int y, bool do_swap) { x = find(x), y = find(y); if (x == y) return false; if (do_swap and cou[x] < cou[y]) std::swap(x, y); par[y] = x, cou[x] += cou[y]; return true; } int count(int x) { return cou[find(x)]; } bool same(int x, int y) { return find(x) == find(y); } std::vector> groups() { std::vector> ret(par.size()); for (int i = 0; i < int(par.size()); ++i) ret[find(i)].push_back(i); ret.erase(std::remove_if(ret.begin(), ret.end(), [&](const std::vector &v) { return v.empty(); }), ret.end()); return ret; } }; // Preorder Euler Tour // (行きがけ順,部分木の頂点クエリ等に有用) struct PreorderEulerTour { int V; // # of vertices of tree int root; std::vector> edges; std::vector subtree_begin, subtree_end; std::vector vis_order; void _build_dfs(int now, int prv) { subtree_begin[now] = vis_order.size(); vis_order.push_back(now); for (auto nxt : edges[now]) if (nxt != prv) _build_dfs(nxt, now); subtree_end[now] = vis_order.size(); } PreorderEulerTour() = default; PreorderEulerTour(const std::vector> &to, int root) : V(to.size()), root(root), edges(to) { assert(root >= 0 and root < V); subtree_begin.resize(V); subtree_end.resize(V); _build_dfs(root, -1); } }; int main() { int N; cin >> N; vector A(N); cin >> A; int T; cin >> T; vector tp(T), X(T), Y(T), add_at(T, -1); vector last_deg(N); UnionFind uf1(N * 2); vector> ch(N); REP(t, T) { cin >> tp[t] >> X[t] >> Y[t]; --X.at(t); if (tp.at(t) == 1) { --Y.at(t); last_deg.at(X.at(t))++; last_deg.at(Y.at(t))++; if (const int u = uf1.find(X.at(t)), v = uf1.find(Y.at(t)); u != v) { int s = ch.size(); ch.push_back({u, v}); uf1.unite(s, u, false); uf1.unite(s, v, false); assert(uf1.find(u) == s); assert(uf1.find(v) == s); } } if (tp.at(t) == 4) add_at.at(t) = uf1.find(X.at(t)); } dbg(last_deg); dbg(add_at); const int root = ch.size(); ch.push_back({}); REP(i, N) { if (!uf1.same(i, root)) { ch.back().push_back(uf1.find(i)); uf1.unite(root, uf1.find(i), false); } } dbg(ch); PreorderEulerTour et(ch, root); dbg(et.subtree_begin); dbg(et.subtree_end); vector> qs(T + 1); int Q; cin >> Q; REP(q, Q) { int s, i; cin >> s >> i; --i; qs.at(s).emplace_back(i, q); } vector single_add(N); // type 2, type 3 vector adj_add(N); // type 3, heavy UnionFind uf(N); constexpr int B = 300; vector> to_all(N); vector> to_heavy(N); atcoder::lazy_segtree tree(ch.size()); vector ret(Q); REP(t, T + 1) { if (t < T) { if (tp.at(t) == 1) { to_all.at(X.at(t)).push_back(Y.at(t)); to_all.at(Y.at(t)).push_back(X.at(t)); if (last_deg.at(Y.at(t)) >= B) to_heavy.at(X.at(t)).push_back(Y.at(t)); if (last_deg.at(X.at(t)) >= B) to_heavy.at(Y.at(t)).push_back(X.at(t)); REP(iter, 2) { if (last_deg.at(X.at(t)) < B and last_deg.at(Y.at(t)) >= B) single_add.at(X.at(t)) -= adj_add.at(Y.at(t)); swap(X.at(t), Y.at(t)); } } else if (tp.at(t) == 2) { single_add.at(X.at(t)) += Y.at(t); } else if (tp.at(t) == 3) { single_add.at(X.at(t)) += Y.at(t); if (last_deg.at(X.at(t)) >= B) { for (int j : to_heavy.at(X.at(t))) single_add.at(j) += Y.at(t); adj_add.at(X.at(t)) += Y.at(t); } else { for (int j : to_all.at(X.at(t))) single_add.at(j) += Y.at(t); } } else if (tp.at(t) == 4) { tree.apply(et.subtree_begin.at(add_at.at(t)), et.subtree_end.at(add_at.at(t)), Y.at(t)); } else { exit(1); } } for (auto [i, q] : qs.at(t)) { lint ans = A.at(i) - single_add.at(i) - tree.get(et.subtree_begin.at(i)); if (last_deg.at(i) < B) { for (int j : to_heavy.at(i)) ans -= adj_add.at(j); } ret.at(q) = max(0LL, ans); } } for (auto x : ret) cout << x << '\n'; }