QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#87979#5102. Dungeon CrawlerzokerWA 909ms9024kbC++175.1kb2023-03-14 22:19:202023-03-14 22:19:22

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:19:22]
  • 评测
  • 测评结果:WA
  • 用时:909ms
  • 内存:9024kb
  • [2023-03-14 22:19:20]
  • 提交

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 = -INFL;
    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
*/

詳細信息

Test #1:

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

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: 0
Accepted
time: 2ms
memory: 3732kb

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
106288816
104379596
104405611
104775512
105434682
105291604
103838430
105371370
104677980
104175650
105894571
104509242
103971939
105376499
105223283
104153426
105082245
105413188
104130613
104800548
106846555
104138329
103769253
105456754
104044745
104385328
106973740
105460460
...

result:

ok 100 lines

Test #3:

score: 0
Accepted
time: 2ms
memory: 3604kb

input:

10 6
1 2 4
2 3 7
2 4 8
4 5 6
4 6 4
4 7 6
4 8 7
8 9 3
8 10 10
3 8 1
10 4 7
1 7 3
7 2 9
8 10 3
4 1 6

output:

99
78
97
87
88
93

result:

ok 6 lines

Test #4:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

10 9
9 2 5
9 1 6
9 4 97
9 7 2
9 8 42
9 10 11
9 6 77
9 3 14
9 5 9
4 7 10
7 3 8
8 7 9
1 4 8
10 7 4
7 1 2
10 1 5
10 7 2
8 4 10

output:

352
427
impossible
443
418
427
418
418
407

result:

ok 9 lines

Test #5:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

9 9
2 3 48
9 5 31
7 3 97
4 3 16
1 7 24
5 3 82
8 2 51
6 4 33
1 2 8
3 6 8
1 6 3
9 5 6
2 6 4
5 6 1
9 6 4
2 8 9
4 9 2

output:

530
643
impossible
530
impossible
561
impossible
595
627

result:

ok 9 lines

Test #6:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

8 9
1 7 51
7 6 86
2 3 62
8 4 72
5 6 17
4 1 75
3 1 41
2 3 7
5 8 4
6 1 3
8 6 2
4 2 7
8 5 6
2 1 5
7 1 6
6 7 8

output:

551
impossible
524
558
579
impossible
551
705
524

result:

ok 9 lines

Test #7:

score: 0
Accepted
time: 2ms
memory: 3568kb

input:

9 9
5 4 13
9 2 10
1 9 25
7 6 34
4 2 77
3 8 67
8 1 57
6 9 100
6 4 1
4 1 7
3 2 9
4 9 7
7 9 3
6 2 1
2 8 4
8 6 2
6 5 9

output:

517
545
impossible
530
483
517
642
584
impossible

result:

ok 9 lines

Test #8:

score: 0
Accepted
time: 2ms
memory: 3660kb

input:

10 10
2 4 26
9 8 39
4 5 88
6 3 70
7 6 7
10 4 41
8 3 57
1 6 15
5 6 9
2 8 6
3 9 1
5 7 8
4 7 8
7 6 4
2 7 3
6 8 2
5 4 10
4 8 9
1 5 9

output:

impossible
496
529
441
531
415
566
529
441
523

result:

ok 10 lines

Test #9:

score: 0
Accepted
time: 2ms
memory: 3512kb

input:

10 9
3 2 6
2 1 18
8 1 44
1 9 42
6 3 72
10 8 46
7 10 93
5 3 11
4 10 13
7 3 1
8 2 5
7 5 4
5 10 8
3 1 4
6 4 3
10 1 6
5 10 8
2 10 4

output:

impossible
550
584
impossible
483
impossible
504
impossible
489

result:

ok 9 lines

Test #10:

score: 0
Accepted
time: 475ms
memory: 9024kb

input:

2000 199998
126 244 481188299
718 1159 740107180
1327 1250 248943862
977 1092 780169400
826 27 932696654
1668 638 478193038
229 174 176675890
1251 646 843918836
102 1973 593920932
236 218 165399894
760 151 890198591
232 502 10739184
1961 1409 45917915
548 1840 974742709
1096 630 280975617
1110 1048 ...

output:

1266421864327
impossible
1003453161105
1017793822920
1056758437569
impossible
1249128162612
1233756636475
1354563020262
1275484665657
impossible
impossible
1644448395794
impossible
impossible
impossible
1305598243001
1730425595360
1090858373772
1180211385304
1235543994987
1894692656465
impossible
12...

result:

ok 199998 lines

Test #11:

score: 0
Accepted
time: 909ms
memory: 8880kb

input:

1999 199999
1233 1172 270406027
1233 238 118916774
1233 902 452751000
1233 1868 96683385
1233 605 546735354
1233 82 679125014
1233 1132 938320209
1233 1424 561568050
1233 113 835230774
1233 330 63962348
1233 1758 986726048
1233 1006 214588798
1233 88 433116365
1233 1122 412164831
1233 1496 846865689...

output:

1993724469968
1993337272038
1993488034133
1993680602118
1993694627446
1994104485073
1994062829494
1993917179450
1993435921379
1993814292350
1993428850371
1993220985867
1993782751870
1994075401526
1993846887971
1994090631242
1993936573248
1993321397280
1993351664812
1993207502090
1994025398060
199385...

result:

ok 199999 lines

Test #12:

score: -100
Wrong Answer
time: 543ms
memory: 8644kb

input:

1998 200000
1348 321 767897262
732 563 244247276
1747 898 143621738
952 79 216678072
693 645 383457558
1061 1084 597211706
1068 1108 69631436
1493 1279 46833316
370 83 741751233
668 234 400581789
1254 807 277405261
246 71 317177072
1778 1307 117275225
1679 1090 454166908
423 1563 270094514
130 1297 ...

output:

1975372410410
1975372410410
1977770498458
1973304488995
1973667551620
1977714806794
1976066886120
1976572750087
1975072593898
1977014789030
1976101190261
1974138271828
1976530258945
1973684336396
1974554366471
1975605779407
1976303286574
1973805733832
1976072007649
1976514431746
1975901590393
197920...

result:

wrong answer 3rd lines differ - expected: '1975935828356', found: '1977770498458'