#include using namespace std; #define int long long #define MAX_V 114514 typedef pair P; vector G[MAX_V],C[MAX_V],T[MAX_V],ord,low,belong; vector

B; int V; bool is_bridge(int u,int v){ if(ord[u]>ord[v]) swap(u,v); return ord[u]=0){ low[u]=min(low[u],ord[v]); }else{ dfs(v,u,k); low[u]=min(low[u],low[v]); } if(is_bridge(u,v)) B.push_back(P(u,v)); } } void fill_component(int c,int u){ C[c].push_back(u); belong[u]=c; for(int v:G[u]){ if(belong[v]>=0||is_bridge(u,v)) continue; fill_component(c,v); } } void add_component(int u,int &k){ if(belong[u]>=0) return; fill_component(k++,u); } void biconnectedgraph(int n){ int k=0; ord.clear(); ord.resize(n,-1); low.clear(); low.resize(n); belong.clear(); belong.resize(n,-1); for(int u=0;u=0) continue; dfs(u,-1,k); } k=0; for(int i=0;i<(int)B.size();i++){ add_component(B[i].first,k); add_component(B[i].second,k); } add_component(0,k); V=k; for(int i=0;i<(int)B.size();i++){ int u=belong[B[i].first],v=belong[B[i].second]; T[u].push_back(v); T[v].push_back(u); } } struct HLDecomposition { vector> g; // vid, head, heavy, parent は必須 // depth, inv は使用する機能によっては不要 vector vid, head, heavy, parent, depth, inv; HLDecomposition(int n) : g(n), vid(n, -1), head(n), heavy(n, -1), parent(n), depth(n), inv(n) {} // 辺 (u, v) を追加する void add(int u, int v) { g[u].push_back(v); //g[v].push_back(u); } // 構築する void build() { dfs(0, -1); bfs(); } int dfs(int curr, int prev) { parent[curr] = prev; int sub = 1, max_sub = 0; for (int next : g[curr]) if (next != prev) { depth[next] = depth[curr] + 1; int sub_next = dfs(next, curr); sub += sub_next; if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next; } return sub; } void bfs() { int k = 0; queue q({ 0 }); while (!q.empty()) { int h = q.front(); q.pop(); for (int i = h; i != -1; i = heavy[i]) { vid[i] = k++; inv[vid[i]] = i; head[i] = h; for (int j : g[i]) if (j != parent[i] && j != heavy[i]) q.push(j); } } } // 頂点属性の for_each void for_each(int u, int v, function f) { if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]); if (head[u] != head[v]) for_each(u, parent[head[v]], f); } }; struct RMQ{ int n; vector > dat; const set def; RMQ(){} RMQ(int n_){init(n_);} void init(int n_){ n=1; while(n0){ k=(k-1)/2; assert(!dat[k].count(a)); dat[k].insert(a); } } void remove(int k,int a){ //cout<0){ k=(k-1)/2; assert(dat[k].count(a)); dat[k].erase(a); } } int query(int a,int b,int k,int l,int r){ if(r<=a||b<=l) return -1; if(a<=l&&r<=b){ if(dat[k].empty()) return -1; return *--dat[k].end(); } int vl=query(a,b,k*2+1,l,(l+r)/2); int vr=query(a,b,k*2+2,(l+r)/2,r); return max(vl,vr); } int query(int a,int b){ //cout<>n>>e>>q; for(int i=0;i>s>>t; s--;t--; //cout<"< m; for(int i=0;i>d; if(d==1){ int u,w; cin>>u>>w; u--; u=belong[u]; //cout<>s>>t; s--;t--; s=belong[s],t=belong[t]; int ans=-1; hl.for_each(s, t, [&](int l, int r) { ans = max(ans,rmq.query(l, r + 1)); //cout<