1 条题解

  • 0
    @ 2026-1-11 14:44:50

    #include <bits/stdc++.h>
    #define EB emplace_back
    using std::cin;
    using std::cout;
    
    typedef std::pair <int, int> pr;
    const int N = 1023, M = 100054;
    
    struct edge {
    	int u, v, w;
    	edge (int u0 = 0, int v0 = 0, int w0 = 0) : u(u0), v(v0), w(w0) {}
    	friend std::istream & operator >> (std::istream &in, edge &B) {return in >> B.u >> B.v >> B.w;}
    	inline bool operator < (const edge &B) const {return w < B.w;}
    } e[M];
    
    int V, E, n;
    int root[M];
    
    inline void up(int &x, const int y) {x < y ? x = y : 0;}
    inline int max(const int x, const int y) {return x < y ? y : x;}
    
    namespace LCT {
    	#define pa p[nd]
    	struct node {bool rev; int v, p, c[2];} nd[N + M];
    	inline int dir(int x) {return !nd[x].p ? -1 : x == nd[x].pa.c[0] ? 0 : x == nd[x].pa.c[1] ? 1 : -1;}
    	inline void set(int x, int px, int c) {if (nd[x].p = px, ~c) nd[px].c[c] = x;}
    	inline void reverse(int x) {x && (std::swap(nd[x].c[0], nd[x].c[1]), nd[x].rev = !nd[x].rev);}
    	void push_down(int x) {if (nd[x].rev) reverse(nd[x].c[0]), reverse(nd[x].c[1]), nd[x].rev = false;}
    	void pull_down(int x) {if (~dir(x)) pull_down(nd[x].p); push_down(x);}
    	inline void update(int x) {const int l = nd[x].c[0], r = nd[x].c[1]; nd[x].v = max(x - V, 0); if (l) up(nd[x].v, nd[l].v); if (r) up(nd[x].v, nd[r].v);}
    	void rotate(int x) {int y = nd[x].p, d = !dir(x); set(nd[x].c[d], y, !d), set(x, nd[y].p, dir(y)), set(y, x, d), update(y);}
    	void splay(int x) {for (pull_down(x); ~dir(x); rotate(x)) if (~dir(nd[x].p)) rotate(dir(x) ^ dir(nd[x].p) ? x : nd[x].p); update(x);}
    	void access(int x) {for (int y = 0; x; y = x, x = nd[x].p) splay(x), nd[x].c[1] = y, update(x);}
    	void make_root(int x) {access(x), splay(x), reverse(x);}
    	int find_root(int x) {for (access(x), splay(x); push_down(x), nd[x].c[0]; x = nd[x].c[0]); return splay(x), x;}
    	int split(int x, int y) {return make_root(x), access(y), splay(y), y;}
    	void link(int x, int y) {make_root(x), nd[x].p = y;}
    	void cut(int x, int y) {split(x, y), nd[x].p = nd[y].c[0] = 0, update(y);}
    	void trylink(int x, int y) {x == y || (split(x, y), ~dir(x)) || (nd[x].p = y);}
    	void trycut(int x, int y) {split(x, y), nd[y].c[0] == x && !nd[x].c[1] && (nd[x].p = nd[y].c[0] = 0, update(y), 0);}
    }
    
    namespace STex {
    	struct node {int v, lc, rc;} x[2003731 * 2];
    
    	int cnt;
    
    	int add(int id, int L, int R, int h, int v) {
    		int nid = ++cnt; x[nid] = x[id], x[nid].v += v;
    		if (L == R) return nid;
    		int M = (L + R - 1) >> 1;
    		h <= M ? x[nid].lc = add(x[id].lc, L, M, h, v) : x[nid].rc = add(x[id].rc, M + 1, R, h, v);
    		return nid;
    	}
    
    	int prefix(int id, int L, int R, int h) {
    		if (!id || R <= h) return x[id].v;
    		int M = (L + R - 1) >> 1, s = prefix(x[id].lc, L, M, h);
    		if (M < h) s += prefix(x[id].rc, M + 1, R, h);
    		return s;
    	}
    }
    
    void work() {
    	int i, j, x, l, r, q, ans = 0;
    	cin >> V >> E;
    	for (i = 0; i < E; ++i) cin >> e[i];
    	std::sort(e, e + E), root[E] = 0, STex::cnt = 0;
    	for (i = E - 1; i >= 0; --i) {
    		root[i] = root[i + 1], x = LCT::split(e[i].u, e[i].v);
    		if (~LCT::dir(e[i].u))
    			j = LCT::nd[x].v - 1,
    			LCT::cut(e[j].u, V + j + 1), LCT::cut(e[j].v, V + j + 1),
    			root[i] = STex::add(root[i], 0, E - 1, j, -e[j].w);
    		LCT::link(e[i].u, V + i + 1), LCT::link(e[i].v, V + i + 1),
    		root[i] = STex::add(root[i], 0, E - 1, i, e[i].w);
    	}
    	for (cin >> q; q; --q)
    		cin >> l >> r, l -= ans, r -= ans, ans = 0,
    		i = std::lower_bound(e, e + E, edge(0, 0, l)) - e,
    		j = std::lower_bound(e, e + E, edge(0, 0, r + 1)) - e,
    		ans = (j < i ? 0 : STex::prefix(root[i], 0, E - 1, j - 1)),
    		cout << ans << '\n';
    	memset(LCT::nd, 0, (V + E + 1) * sizeof(LCT::node));
    }
    
    int main() {
    	int T;
    	std::ios::sync_with_stdio(false), cin.tie(NULL);
    	for (cin >> T; T; --T) work();
    	return 0;
    }
    
    
    
    • 1

    信息

    ID
    5711
    时间
    5000ms
    内存
    512MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者