QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#87977#5102. Dungeon CrawlerzokerWA 3ms3676kbC++175.1kb2023-03-14 22:14:052023-03-14 22:17:20

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-14 22:17:20]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3676kb
  • [2023-03-14 22:14:05]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt")

using ll = long long int;
using ull = unsigned long long int;
using vi = vector<ll>;
using ii = pair<ll,ll>;
using vii = vector<ii>;
using ld = long double;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using ordered_set = tree < T, null_type, less<T>,
rb_tree_tag,
tree_order_statistics_node_update >;

#ifdef SA_DEBUG
template <class T>
void print(T a) {cerr << a << endl;}
template <class T, class... V> 
void print(T a, V... b) {cerr << a << ", "; print(b...);}
#define dbg(...) cerr << "[" << __LINE__ << "] " << #__VA_ARGS__ << " :: ", print(__VA_ARGS__)
#else
#define dbg(...) 7
#endif

#define eb emplace_back
#define fi first
#define se second

const ll INFL = 2e18;
const int INF = 1e9;
const double PI = acos(-1);
const ll mod = 1e9+7;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template<class T, class V> 
ostream& operator << (ostream &s, pair<T, V> a){
	s << a.fi << ' ' << a.se; return s;
}

template<class T, class V> 
istream& operator >> (istream &s, pair<T, V> &a){
	s >> a.fi >> a.se; return s;
}

template<class T> 
ostream& operator << (ostream &s, vector<T> a){
	for(int i = 0; i < (int)a.size(); i++){
		if(i > 0)s << ' ' ; 
		s << a[i];
	} return s;
}

template<class T> 
istream& operator >> (istream &s, vector<T> &a){
	for(T &x : a)s >> x; 
	return s;
}

template<class T> 
void input(T a[], int l, int r, istream &s = cin){
	while(l <= r)s >> a[l++];
}

template<class T> 
void output(T a[], int l, int r, bool en = true, ostream &s = cout){
	while(l <= r){ s << a[l++];
		if(l <= r) s << ' ';
	} if(en)s << "\n";
}



const int N = 2e3+3, K = 13, M = 2e5 + 5;
//====================================================================//

ll st[N << 2];
void update(int k, int val, int n){
    k += n;
    st[k] = val;
    while(k > 1){
        st[k >> 1] = max(st[k], st[k ^ 1]);
        k >>= 1;
    }
}
ll query(int l, int r, int n){
    l += n, r += n + 1;
    ll ans = 0;
    while(l < r){
        if(l & 1)ans = max(ans, st[l++]);
        if(r & 1)ans = max(ans, st[--r]);
        l >>= 1;
        r >>= 1;
    }
    return ans;
}

ll dis[N];
ll D[N];
int up[N][K];
ll ans[M];
vector<ii> adj[N];
int tin[N], tout[N];
map<int, vector<int>> khuja[N];
vii khuja2[N];
int T;
void dfs(int ind, int par, ll d){
	up[ind][0] = par;
	for(int i = 1; i < K; i++){
		up[ind][i] = up[up[ind][i - 1]][i - 1];
	}
	tin[ind] = T++;
	dis[ind] = d;
	D[ind] = d;
	for(auto [x, w] : adj[ind]){
		if(x == par)continue;
		dfs(x, ind, d + w);
		dis[ind] = max(dis[ind], dis[x]);
	}
	
	tout[ind] = T++;
}

#define isanc(u,v) (tin[u] <= tin[v] && tout[u] >= tout[v])

ii LCA(int u, int v){
	if(isanc(u, v))return {u, u};
	for(int i = K - 1; i >= 0; i--){
		if(!isanc(up[u][i], v))u = up[u][i];
	}
	return {up[u][0], u};
}
int cnt[N], tim = 0;

void fetch(int ind, int par, ll val){
	ii mx1 = {-1, -1};
	ii mx2 = {-1, -1};
	for(auto [x, y] : adj[ind]){
		if(x == par)continue;
		if(mx1.fi < dis[x]){
			mx2 = mx1;
			mx1 = {dis[x], x};
		}else if(mx2.fi < dis[x]){
			mx2 = {dis[x], x};
		}
	}
	for(auto [x, y] : adj[ind]){
		if(x == par)continue;
		ll temp = val;
		ll t2 = -INFL;
		if(mx1.se != x)t2 = max(t2, mx1.fi);
		if(mx2.se != x)t2 = max(t2, mx2.fi);
		temp = max(temp, t2);
		cnt[ind] = tim;
		update(tim++, t2 - 2 * D[ind], N);
		fetch(x, ind, temp);
		update(--tim, -INFL, N);
		for(auto z : khuja[ind][x]){
			ans[z] = max(temp, ans[z]);
		}
	}
	khuja[ind].clear();
	ll t2 = dis[ind] - 2 * D[ind];
	for(auto [l, idx] : khuja2[ind]){
		ll gt = max(t2, query(cnt[l], tim - 1, N));
		//dbg(gt);
		ans[idx] = max(ans[idx], gt + 2 * D[l]);
	}
	khuja2[ind].clear();
}
void testcase(){
	int n, q;
	cin >> n >> q;
	ll tot = 0;
	for(int i = 1; i < n; i++){
		int x, y, z;
		cin >> x >> y >> z;
		adj[x].eb(y, z);
		adj[y].eb(x, z);
		tot += 2 * z;
	}
	
	vector<vector<tuple<int, int, int>>> qr(n + 1);
	for(int i = 0; i < q; i++){
		int s, k, t;
		cin >> s >> k >> t;
		qr[s].eb(k, t, i);
	}
	for(int i = 1; i <= n; i++){
		if(qr[i].empty())continue;
		T = 0;
		dfs(i, i, 0);
		for(auto [k, t, idx] : qr[i]){
			auto [l, l2] = LCA(k, t);
			
			if(l == k){
				ans[idx] = dis[i];
				continue;
			}
			if(l == t){
				ans[idx] = -1;
				continue;
			}
			assert(isanc(l, l2));
			assert(isanc(l2, k));
			assert(isanc(l, t));
			assert(!isanc(l2, t));
			khuja[l][l2].eb(idx);
			khuja2[k].eb(l, idx);
			
		}
		
		fetch(i, i, -1);
	}
	
	for(int i = 0; i < q; i++){
		if(ans[i] == -1)cout << "impossible\n";
		else {
			cout << tot - ans[i] << "\n";
		}
	}
}





int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	int TT = 1;
	//cin >> T;
	
	for(int qq = 1; qq <= TT; ++qq){
		//cout << "Case #" << qq << ": ";
		testcase();
	}
	return 0;
}
/*
6 1597352862016328480
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3576kb

input:

22 5
1 2 7
2 3 5
3 4 8
3 6 6
3 7 3
2 5 6
5 8 2
8 9 11
2 10 16
1 11 12
11 12 4
11 13 9
1 14 25
14 15 4
15 16 5
15 17 6
15 18 1
15 19 8
14 20 7
20 21 9
20 22 17
1 19 9
1 9 19
1 8 9
1 9 8
2 22 11

output:

316
293
293
impossible
314

result:

ok 5 lines

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 3676kb

input:

100 100
1 2 289384
2 3 930887
2 4 692778
4 5 636916
4 6 747794
4 7 238336
4 8 885387
8 9 760493
8 10 516650
8 11 641422
8 12 202363
8 13 490028
8 14 368691
8 15 520060
8 16 897764
16 17 513927
16 18 180541
16 19 383427
16 20 89173
16 21 455737
16 22 5212
16 23 595369
16 24 702568
16 25 956430
16 26 ...

output:

103526917
103484292
105618004
104379596
101938600
104775512
105434682
105291604
103838430
105092828
104677980
104175650
104829514
104509242
103971939
105376499
105223283
104153426
105082245
105413188
104130613
104800548
106818964
104138329
103769253
105456754
104044745
104385328
106973740
105460460
...

result:

wrong answer 3rd lines differ - expected: '106288816', found: '105618004'