跳到正文

剪贴板

剪贴板 n3pcvaw3

原文
剪贴板信息

剪贴板信息

收录
更新

D1T1

C++
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx;
#define For(i,x,y,...) for(int i=x,##__VA_ARGS__;i<=(y);++i)
#define rFor(i,x,y,...) for(int i=x,##__VA_ARGS__;i>=(y);--i)
#define Rep(i,x,y,...) for(int i=x,##__VA_ARGS__;i<(y);++i)
#define pb emplace_back
#define sz(a) int((a).size())
#define all(a) begin(a),end(a)
#define fi first
#define se second
typedef long long LL; typedef vector<int> Vi; typedef pair<int,int> Pii;
auto ckmax=[](auto &x,auto y) { return x<y ? x=y,true : false; };
auto ckmin=[](auto &x,auto y) { return y<x ? x=y,true : false; };
sfmt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r) { return uniform_int_distribution<>(l,r)(mt); }
#define endl '\n'
namespace IO { const int L = 1<<20; char a[L],*l=a,*r=a;
auto gc=[]() { if(l==r)r=(l=a)+fread(a,1,L,stdin);return l==r?EOF:*l++; };
struct IO {
	template<typename T>IO& operator >> (T &x) {
		x=0;bool f=0;char c;while(!isdigit(c=gc()))f|=c=='-';
		do x=x*10+c-48;while(isdigit(c=gc()));if(f)x=-x;
		return *this;
	}
} io; } using IO::io;
template<typename T=int>T read() { T x; io>>x; return x; }

const int N = 1e6+5;
int n,m,a[N],siz[N];
gp_hash_table<int,int> cnt[N];

Pii operator + (const Pii &a,const Pii &b) {
	if( a.fi == b.fi ) return {a.fi,a.se+b.se};
	return a.se>b.se ? Pii{a.fi,a.se-b.se} : Pii{b.fi,b.se-a.se};
}

#define ls(u) t[u].ch[0]
#define rs(u) t[u].ch[1]
int ind,rt[N];
struct { unsigned rnd; int ch[2],val; Pii sum; } t[N];
auto up=[](int u) { t[u].sum = t[ls(u)].sum + Pii{t[u].val,1} + t[rs(u)].sum; };
int merge(int u,int v) {
	if( !u || !v ) return u | v;
	if( t[u].rnd < t[v].rnd ) return rs(u) = merge(rs(u),v), up(u), u;
	else return ls(v) = merge(u,ls(v)), up(v), v;
}
void ins(int u,int x) {
	++siz[u], ++cnt[u][x];
	t[++ind] = {mt(),0,0,x,{x,1}}, rt[u] = merge(rt[u],ind);
}
int ers(int &u) {
	if( !rs(u) ) { int x = t[u].val; return u = ls(u), x; }
	int x = ers(rs(u));
	return up(u), x;
}

signed main() {
#ifdef FS
	freopen("major.in","r",stdin); freopen("major.out","w",stdout);
#else
	freopen("major.in","r",stdin); freopen("major.out","w",stdout);
#endif
	ios::sync_with_stdio(0);cout.tie(0);
	io>>n>>m; For(i,1,n) For(j,1,edj, edj = read()) ins(i,read());
	while( m-- ) {
		int op=read();
		if( op == 1 ) { int u=read(); ins(u,read()); }
		else if( op == 2 ) {
			int u=read();
			--siz[u], --cnt[u][ers(rt[u])];
		} else if( op == 3 ) {
			Pii res;
			int q=read(); For(i,1,q) io>>a[i], res = res + t[rt[a[i]]].sum;
			if( !res.se ) { cout<<"-1\n"; continue; }
			int sum = 0, x = 0;
			For(i,1,q) {
				sum += siz[a[i]];
				auto it = cnt[a[i]].find(res.fi);
				if( it != cnt[a[i]].end() ) x += it->se;
			}
			if( x > sum/2 ) cout<<res.fi<<endl; else cout<<"-1\n";
		} else {
			int u,v,w; io>>u>>v>>w;
			rt[w] = merge(rt[u],rt[v]), rt[u] = rt[v] = 0;
			siz[w] = siz[u] + siz[v], siz[u] = siz[v] = 0;
			if( sz(cnt[u]) < sz(cnt[v]) ) swap(u,v);
			cnt[w].swap(cnt[u]);
			for(auto &i : cnt[v]) cnt[w][i.fi] += i.se; cnt[v].clear();
		}
	}
	return 0;
}

D1T2

C++
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx;
#define For(i,x,y,...) for(int i=x,##__VA_ARGS__;i<=(y);++i)
#define rFor(i,x,y,...) for(int i=x,##__VA_ARGS__;i>=(y);--i)
#define Rep(i,x,y,...) for(int i=x,##__VA_ARGS__;i<(y);++i)
#define pb emplace_back
#define sz(a) int((a).size())
#define all(a) begin(a),end(a)
#define fi first
#define se second
typedef long long LL; typedef vector<int> Vi; typedef pair<int,int> Pii;
auto ckmax=[](auto &x,auto y) { return x<y ? x=y,true : false; };
auto ckmin=[](auto &x,auto y) { return y<x ? x=y,true : false; };
sfmt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r) { return uniform_int_distribution<>(l,r)(mt); }
#define endl '\n'
namespace IO { const int L = 1<<20; char a[L],*l=a,*r=a;
auto gc=[]() { if(l==r)r=(l=a)+fread(a,1,L,stdin);return l==r?EOF:*l++; };
struct IO {
	template<typename T>IO& operator >> (T &x) {
		x=0;bool f=0;char c;while(!isdigit(c=gc()))f|=c=='-';
		do x=x*10+c-48;while(isdigit(c=gc()));if(f)x=-x;
		return *this;
	}
} io; } using IO::io;
template<typename T=int>T read() { T x; io>>x; return x; }

const int N = 1e3+5;
int n,m,ans,le[N],ri[N],a[N];
bool f[32768],vis[32768];

int fold(int *a) { int s=0; For(i,1,n) s = s<<3 | a[i]; return s; }

bool dp(int s) {
	if( vis[s] ) return f[s]; vis[s] = 1;
	int b[9]; rFor(i,n,1, x = s) b[i] = x&7, x >>= 3;
	For(i,1,n) For(j,2,b[i]) {
		b[i] -= j;
		if( dp(fold(b)) ) return f[s] = 1;
		b[i] += j;
	}
	For(i,1,n) {
		int j = i;
		for(; j <= n && b[j]; ++j) {
			--b[j];
			if( j-i >= 2 && dp(fold(b)) ) return f[s] = 1;
		}
		Rep(k,i,j) ++b[k];
	}
	return 0;
}

void dfs(int u) {
	if( u > n ) {
		if( !m ) ans += dp(fold(a));
		else if( m == 1 )
			For(i,1,n) {
				++a[i];
				if( dp(fold(a)) ) return --a[i], ++ans, void();
				--a[i];
			}
		else
			For(i,1,n) For(j,1,n) {
				++a[i], ++a[j];
				if( dp(fold(a)) ) return --a[i], --a[j], ++ans, void();
				--a[i], --a[j];
			}
//		For(i,1,n) cerr<<a[i]<<" "; cerr<<endl;
		return;
	}
	For(i,le[u],ri[u]) a[u] = i, dfs(u+1);
}

void MAIN() {
	io>>n>>m; For(i,1,n) io>>le[i]>>ri[i];
	dfs(1);
	cout<<ans<<endl;
} signed main() {
#ifdef FS
	freopen("stone2.in","r",stdin);// freopen(".out","w",stdout);
#else
	freopen("stone.in","r",stdin); freopen("stone.out","w",stdout);
#endif
	ios::sync_with_stdio(0);cout.tie(0);
	vis[0] = f[0] = 1;
//	n = 4, a[2] = a[3] = a[4] = 1;
//	cerr<<dp(fold(a));
//	return 0;
	int T=read(); while( T-- ) {
		MAIN();
		ans = 0;
	}
	return 0;
}

D1T3

C++
#include "count.h"
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx;
#define For(i,x,y,...) for(int i=x,##__VA_ARGS__;i<=(y);++i)
#define rFor(i,x,y,...) for(int i=x,##__VA_ARGS__;i>=(y);--i)
#define Rep(i,x,y,...) for(int i=x,##__VA_ARGS__;i<(y);++i)
#define pb emplace_back
#define sz(a) int((a).size())
#define all(a) begin(a),end(a)
#define fi first
#define se second
bool memory1;
typedef long long LL; typedef vector<int> Vi; typedef pair<int,int> Pii;
auto ckmax=[](auto &x,auto y) { return x<y ? x=y,true : false; };
auto ckmin=[](auto &x,auto y) { return y<x ? x=y,true : false; };
sfmt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r) { return uniform_int_distribution<>(l,r)(mt); }

info _MR(const info &x,const info &y)
	{ return isempty(x) ? y : isempty(y) ? x : MR(x,y); }
info _MC(const info &x,const info &y)
	{ return isempty(x) ? y : isempty(y) ? x : MC(x,y); }
bool A,C;

const int N = 2e5+5;
int n,q,mm=1,mxd,fa[N],head[N],nxt[N*2],to[N*2],deg[N];
info w[N*2],suf[N];
vector<bool> vis[N*2];
vector<info> f[N*2],g[N];

namespace subA {
info t[18][N];
#define mid (l+r>>1)
void bld(int d=0,int l=1,int r=n-1) {
	if( l == r ) return t[d][l] = w[l], void();
	bld(d+1,l,mid), bld(d+1,mid+1,r);
	t[d][mid] = w[mid], t[d][mid+1] = w[mid+1];
	rFor(i,mid-1,l) t[d][i] = _MC(w[i],t[d][i+1]);
	For(i,mid+2,r) t[d][i] = _MC(w[i],t[d][i-1]);
}
info qry(int ql,int qr,int d=0,int l=1,int r=n-1) {
	if( ql == qr ) return w[ql];
	if( qr <= mid ) return qry(ql,qr,d+1,l,mid);
	if( mid < ql ) return qry(ql,qr,d+1,mid+1,r);
	return _MC(t[d][ql],t[d][qr]);
}
void init() {
	cerr<<"A\n";
	for(int i = 2; i < mm; i += 2) w[i/2] = w[i];
	bld();
}
}

#define eFor(u,i,v) for(int i = head[u], v; v = to[i], i; i = nxt[i])

bool memory2;
void init(int T,int N,int Q,vector<int> Fa,vector<info> E,int M){
	cerr<<(&memory1-&memory2)/1024./1024<<"MB\n";
	n = N, q = Q;
	Rep(i,0,sz(Fa)) {
		int u = Fa[i], v = i+2;
		fa[v] = u, ++deg[u], ++deg[v];
		nxt[++mm] = head[u], head[u] = mm, to[mm] = v, w[mm] = E[i],
		nxt[++mm] = head[v], head[v] = mm, to[mm] = u, w[mm] = E[i];
	}
	A = 3<=T&&T<=4, C = T==9;
	if( A ) subA::init();
	else {
		mxd = C ? 100 : n;
		For(i,2,mm) f[i].assign(mxd+3,emptyinfo), f[i][0] = w[i];
		For(d,1,mxd) For(u,1,n) {
			Vi e; eFor(u,i,v) e.pb(i);
			info pre = suf[sz(e)] = emptyinfo;
			rFor(i,sz(e)-1,0) suf[i] = _MR(f[e[i]][d-1],suf[i+1]);
			Rep(i,0,sz(e))
				f[e[i]^1][d] = _MR(w[e[i]],_MR(pre,suf[i+1])),
				pre = _MR(f[e[i]][d-1],pre);
		}
		For(u,1,n) {
			g[u].assign(mxd+3,emptyinfo), vis[u].assign(mxd+3,0);
			if( deg[u] > 500 ) For(d,1,mxd) {
				vis[u][d] = 1;
				eFor(u,i,v) g[u][d] = _MR(g[u][d],f[i][d-1]);
			}
		}
	}
}

info ask(int u,int d) {
	if( !d ) return emptyinfo;
	if( A ) return subA::qry(max(1,u-d),min(n-1,u+d-1));
	ckmin(d,mxd);
	if( vis[u][d] ) return g[u][d]; vis[u][d] = 1;
	eFor(u,i,v) g[u][d] = _MR(g[u][d],f[i][d-1]);
	return g[u][d];
}

D2T1

C++
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx;
#define For(i,x,y,...) for(int i=x,##__VA_ARGS__;i<=(y);++i)
#define rFor(i,x,y,...) for(int i=x,##__VA_ARGS__;i>=(y);--i)
#define Rep(i,x,y,...) for(int i=x,##__VA_ARGS__;i<(y);++i)
#define pb emplace_back
#define sz(a) int((a).size())
#define all(a) begin(a),end(a)
#define fi first
#define se second
typedef long long LL; typedef vector<int> Vi; typedef pair<int,int> Pii;
auto ckmax=[](auto &x,auto y) { return x<y ? x=y,true : false; };
auto ckmin=[](auto &x,auto y) { return y<x ? x=y,true : false; };
sfmt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
#define endl '\n'
namespace IO { const int L = 1<<20; char a[L],*l=a,*r=a;
auto gc=[]() { if(l==r)r=(l=a)+fread(a,1,L,stdin);return l==r?EOF:*l++; };
struct IO {
	template<typename T>IO& operator >> (T &x) {
		x=0;bool f=0;char c;while(!isdigit(c=gc()))f|=c=='-';
		do x=x*10+c-48;while(isdigit(c=gc()));if(f)x=-x;
		return *this;
	}
} io; } using IO::io;
template<typename T=int>T read() { T x; io>>x; return x; }

typedef unsigned long long uLL;
typedef pair<uLL,uLL> H;

const int N = 1e5+5;
int m;
uLL base[N],rnd[N];
Vi pri;
gp_hash_table<int,bool> vis[N];

void sieve(int n=1.5e6) {
	static bitset<1500005> vis;
	For(i,2,n) {
		if( !vis[i] ) pri.pb(i);
		for(int j = 0; j < sz(pri) && i*pri[j] <= n; ++j) {
			vis[i*pri[j]] = 1;
			if( !(i % pri[j]) ) break;
		}
	}
	For(i,0,1e5) rnd[i] = mt();
	base[0] = 1; For(i,1,1e5) base[i] = base[i-1] * pri.back();
}

struct {
	int n,rt,fa[N],siz[N],dep[N]; bool del[N]; H h[N];
	Vi e[N]; vector<pair<H,int>> son[N];
	void clr() {
		For(i,1,n) e[i].clear(), son[i].clear();
		memset(dep+1,0,sizeof(int)*n), memset(h+1,0,sizeof(H)*n);
	}
	void dfs(int u) {
		siz[u] = 1;
		for(int v : e[u]) dfs(v), ckmax(dep[u],dep[v]), son[u].pb(h[v],v);
		++dep[u], sort(all(son[u]));
		for(int v : e[u])
			h[u].fi += base[siz[v]] * h[v].fi,
			h[u].se ^= pri[dep[v]] * h[v].se;
		h[u].fi ^= rnd[dep[u]], h[u].se *= pri[siz[u]];
	}
	void init() {
		io>>n;
		For(i,1,n) {
			io>>fa[i];
			if( ~fa[i] ) e[fa[i]].pb(i);
			else rt = i;
		}
		dfs(rt);
	}
} a,b;

bool dfs(int u,int v) {
	static bool da[N],db[N];
	if( a.siz[u] < b.siz[v] || a.siz[u]-m > b.siz[v] ) return 0;
	if( vis[u].find(v) != vis[u].end() ) return vis[u][v];
	Rep(i,0,sz(a.son[u]), j = 0) {
		while( j < sz(b.son[v]) && a.son[u][i].fi > b.son[v][j].fi ) ++j;
		if( j == sz(b.son[v]) ) break;
		if( a.son[u][i].fi == b.son[v][j].fi ) {
//			cerr<<a.son[u][i].se<<" "<<b.son[v][j].se<<endl;
			da[a.son[u][i].se] = db[b.son[v][j].se] = 1, ++j;
		}
	}
	Vi eu,ev;
	for(int i : a.e[u]) if( !da[i] ) eu.pb(i); else da[i] = 0;
	for(int i : b.e[v]) if( !db[i] ) ev.pb(i); else db[i] = 0;
//	for(int i : eu) for(int j : ev) assert(a.h[i]!=b.h[j]);
	if( sz(eu) < sz(ev) || sz(eu)-m > sz(ev) || sz(ev) > m ) return vis[u][v] = 0;
	bitset<1<<5> f;
	int U = (1<<sz(ev))-1; f[0] = 1;
	Rep(i,0,sz(eu)) rFor(s,U,0) if( f[s] )
		Rep(j,0,sz(ev)) if( ~s>>j&1 && dfs(eu[i],ev[j]) ) f[s|(1<<j)] = 1;
	return vis[u][v] = f[U];
}

void MAIN() {
	a.init(), b.init();
	cout<<(dfs(a.rt,b.rt)?"Yes\n":"No\n");
} signed main() {
#ifdef FS
	freopen("iso.in","r",stdin); freopen("iso.out","w",stdout);
#else
	freopen("iso.in","r",stdin); freopen("iso.out","w",stdout);
#endif
	ios::sync_with_stdio(0);cout.tie(0);
	sieve();
	read(); int T; io>>T>>m; while( T-- ) {
		MAIN();
		a.clr(), b.clr();
		For(i,1,a.n) vis[i].clear();
//		cerr<<"\n---\n";
	}
	return 0;
}

D2T2

C++
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx;
#define For(i,x,y,...) for(int i=x,##__VA_ARGS__;i<=(y);++i)
#define rFor(i,x,y,...) for(int i=x,##__VA_ARGS__;i>=(y);--i)
#define Rep(i,x,y,...) for(int i=x,##__VA_ARGS__;i<(y);++i)
#define pb emplace_back
#define sz(a) int((a).size())
#define all(a) begin(a),end(a)
#define fi first
#define se second
typedef long long LL; typedef vector<int> Vi; typedef pair<int,int> Pii;
auto ckmax=[](auto &x,auto y) { return x<y ? x=y,true : false; };
auto ckmin=[](auto &x,auto y) { return y<x ? x=y,true : false; };
sfmt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r) { return uniform_int_distribution<>(l,r)(mt); }
#define endl '\n'
namespace IO { const int L = 1<<20; char a[L],*l=a,*r=a;
auto gc=[]() { if(l==r)r=(l=a)+fread(a,1,L,stdin);return l==r?EOF:*l++; };
struct IO {
	template<typename T>IO& operator >> (T &x) {
		x=0;bool f=0;char c;while(!isdigit(c=gc()))f|=c=='-';
		do x=x*10+c-48;while(isdigit(c=gc()));if(f)x=-x;
		return *this;
	}
} io; } using IO::io;
template<typename T=int>T read() { T x; io>>x; return x; }

const int N = 1e6+5, inf = 0x3f3f3f3f;
const LL infl = 0x3f3f3f3f3f3f3f3f;
int n,m,le[N],ri[N],mn[N],a[N];
LL ans;
Vi o;
vector<Pii> b[N];

#define no return cout<<"-1\n",void();
namespace sub {
bool B=1;
vector<LL> f[N];
void main() {
	memset(a+1,0,sizeof(int)*n);
	For(i,1,m) B &= le[i] == ri[i];
	For(i,0,n) f[i].assign(sz(o)+3,infl);
	if( B ) {
		cerr<<"B\n";
		For(i,1,m) {
			if( a[le[i]] && a[le[i]] != mn[i] ) no;
			a[le[i]] = mn[i];
		}
	} else {
		For(i,1,m) For(j,le[i],ri[i]) a[j] = mn[i];
	}
	f[0][0] = 0;
	For(i,1,n) For(j,0,sz(o)) if( f[i-1][j] < infl ) {
		auto dp=[&](int k) {
			LL res = f[i-1][j];
			Rep(l,1,i) res += a[l]>k;
			if( !a[i] ) For(l,i+1,n) res += a[l]&&k>a[l];
			return res;
		};
		if( a[i] ) ckmin(f[i][j],dp(a[i]));
		else For(k,j,sz(o)) ckmin(f[i][k],dp(k));
	}
	cout<<*min_element(all(f[n]))<<endl;
}
}

void dfs(int u) {
	if( u > n ) {
		LL res=0; For(i,1,n) For(j,i+1,n) res += a[i]>a[j];
		ckmin(ans,res);
		return;
	}
	For(i,1,sz(o)) {
		a[u] = i;
		for(auto &j : b[u]) {
			bool flg = 0;
			For(k,j.fi,u)
				if( a[k] < j.se ) goto togo;
				else if( a[k] == j.se ) flg = 1;
			if( !flg ) goto togo;
		}
		dfs(u+1);
		togo:;
	}
}

void MAIN() {
	io>>n>>m; For(i,1,m) io>>le[i]>>ri[i]>>mn[i], o.pb(mn[i]);
	sort(all(o)), o.erase(unique(all(o)),o.end());
	For(i,1,m) mn[i] = lower_bound(all(o),mn[i])-o.begin()+1;
	if( n > 17 || m > 17 ) return sub::main();
	For(i,1,m) b[ri[i]].pb(le[i],mn[i]);
	ans = infl, dfs(1);
	cout<<(ans<infl?ans:-1)<<endl;
} signed main() {
#ifdef FS
	freopen("bubble.in","r",stdin); freopen("out","w",stdout);
#else
	freopen("bubble.in","r",stdin); freopen("bubble.out","w",stdout);
#endif
	ios::sync_with_stdio(0);cout.tie(0);
	int T=read(); while( T-- ) {
		MAIN();
		o.clear();
		For(i,1,n) b[i].clear();
	}
	return 0;
}

D2T3

C++
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx;
#define For(i,x,y,...) for(int i=x,##__VA_ARGS__;i<=(y);++i)
#define rFor(i,x,y,...) for(int i=x,##__VA_ARGS__;i>=(y);--i)
#define Rep(i,x,y,...) for(int i=x,##__VA_ARGS__;i<(y);++i)
#define pb emplace_back
#define sz(a) int((a).size())
#define all(a) begin(a),end(a)
#define fi first
#define se second
typedef long long LL; typedef vector<int> Vi; typedef pair<int,int> Pii;
auto ckmax=[](auto &x,auto y) { return x<y ? x=y,true : false; };
auto ckmin=[](auto &x,auto y) { return y<x ? x=y,true : false; };
sfmt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r) { return uniform_int_distribution<>(l,r)(mt); }
#define endl '\n'
namespace IO { const int L = 1<<20; char a[L],*l=a,*r=a;
auto gc=[]() { if(l==r)r=(l=a)+fread(a,1,L,stdin);return l==r?EOF:*l++; };
struct IO {
	template<typename T>IO& operator >> (T &x) {
		x=0;bool f=0;char c;while(!isdigit(c=gc()))f|=c=='-';
		do x=x*10+c-48;while(isdigit(c=gc()));if(f)x=-x;
		return *this;
	}
} io; } using IO::io;
template<typename T=int>T read() { T x; io>>x; return x; }

const int N = 605;
int X,n,m,q,l[N],r[N],a[N];
LL ans,val[N];
vector<Pii> b[N];

void dfs(int u,LL res) {
	if( u > n )
	 return ckmax(ans,res), void();
	For(i,l[u],r[u]) {
		a[u] = i;
		int cnt=0;
		for(auto j : b[u]) if( abs(a[u]-a[j.fi]) > j.se ) goto togo;
		Rep(j,1,u) cnt += abs(a[u]-a[j]) <= 1;
		dfs(u+1,res+2e6*cnt+val[i]);
		togo:;
	}
}

void MAIN() {
	io>>X>>n>>m>>q;
	For(i,1,n) io>>l[i]>>r[i];
	For(i,1,m, x,y,z) {
		io>>x>>y>>z; if( x > y ) swap(x,y);
		b[y].pb(x,z);
	}
	while( q-- ) {
		Rep(i,2,X) io>>val[i];
		ans = LLONG_MIN, dfs(1,0);
		cout<<ans+1000000ll*n<<endl;
	}
} signed main() {
#ifdef FS
	freopen("qip.in","r",stdin); freopen("out","w",stdout);
#else
	freopen("qip.in","r",stdin); freopen("qip.out","w",stdout);
#endif
	ios::sync_with_stdio(0);cout.tie(0);
	read(); int T=read(); while( T-- ) {
		MAIN();
		For(i,1,n) b[i].clear();
	}
	return 0;
}