QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#139102 | #6404. Shuttle Tour | thenymphsofdelphi | WA | 146ms | 268236kb | C++20 | 5.6kb | 2023-08-12 17:48:03 | 2023-08-12 17:48:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 2e5 + 5, K = 50, LN = 18;
int n, q;
bool a[N];
vpii adj[N];
int h[N];
ll d[N];
int sz[N];
int ctrtour, tin[N], tout[N];
int nxt[N];
void dfs_par(int u, int p = -1){
for (auto &[v, w]: adj[u]){
if (v == p){
continue;
}
h[v] = h[u] + 1;
d[v] = d[u] + w;
dfs_par(v, u);
}
for (auto itr = adj[u].begin(); itr != adj[u].end(); itr++){
if (itr->fi == p){
adj[u].erase(itr);
break;
}
}
}
void dfs_sz(int u){
sz[u] = 1;
for (auto &edge: adj[u]){
int v = edge.fi;
dfs_sz(v);
sz[u] += sz[v];
if (sz[adj[u][0].fi] < sz[v]){
swap(adj[u][0], edge);
}
}
}
void dfs_tour(int u){
tin[u] = ++ctrtour;
for (auto &[v, w]: adj[u]){
nxt[v] = (v == adj[u][0].fi ? nxt[u] : v);
dfs_tour(v);
}
tout[u] = ctrtour;
}
int min_h(int u, int v){
return h[u] < h[v] ? u : v;
}
int max_h(int u, int v){
return h[u] > h[v] ? u : v;
}
int ctrtour_lca, tour_lca[N], tin_lca[N];
int sptb[LN + 1][N * 2];
void dfs_lca(int u){
tour_lca[++ctrtour_lca] = u;
tin_lca[u] = ctrtour_lca;
for (auto &[v, w]: adj[u]){
dfs_lca(v);
tour_lca[++ctrtour_lca] = u;
}
}
int lca(int u, int v){
if (tin_lca[u] > tin_lca[v]){
swap(u, v);
}
int z = __lg(tin_lca[v] - tin_lca[u] + 1);
return min_h(sptb[z][tin_lca[u]], sptb[z][tin_lca[v] - (1 << z) + 1]);
}
ll dist(int u, int v){
int w = lca(u, v);
return d[u] + d[v] - 2 * d[w];
}
int cnt_chain = 0;
int idx_chain[N];
struct segment_tree{
static pair <int, int> merge_pair(const pair <int, int>& lhs, const pair <int, int>& rhs){
return pair{min_h(lhs.fi, rhs.fi), max_h(lhs.se, rhs.se)};
}
struct node{
pair <int, int> mnmx[K];
node(){
fill(mnmx, mnmx + cnt_chain, pair{N - 1, N - 2});
}
friend node merge(const node& lhs, const node& rhs){
node ans;
For(i, 0, cnt_chain){
ans.mnmx[i] = merge_pair(lhs.mnmx[i], rhs.mnmx[i]);
}
return ans;
}
};
node seg[1 << 19];
void build(int id, int l, int r){
seg[id] = node();
if (l == r){
seg[id].mnmx[idx_chain[l]] = pair{a[l] ? l : N - 1, a[l] ? l : N - 2};
return;
}
int mid = (l + r) >> 1;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
seg[id] = merge(seg[id * 2], seg[id * 2 + 1]);
}
void update(int id, int l, int r, int i){
if (l == r){
a[l] ^= 1;
seg[id].mnmx[idx_chain[l]] = pair{a[l] ? l : N - 1, a[l] ? l : N - 2};
return;
}
int mid = (l + r) >> 1;
if (i <= mid){
update(id * 2, l, mid, i);
}
else{
update(id * 2 + 1, mid + 1, r, i);
}
seg[id] = merge(seg[id * 2], seg[id * 2 + 1]);
}
node query(int id, int l, int r, int u, int v){
if (u <= l and r <= v){
return seg[id];
}
int mid = (l + r) >> 1;
if (v < mid + 1){
return query(id * 2, l, mid, u, v);
}
if (mid < u){
return query(id * 2 + 1, mid + 1, r, u, v);
}
return merge(query(id * 2, l, mid, u, v), query(id * 2 + 1, mid + 1, r, u, v));
}
} seg;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
h[N - 2] = -1; h[N - 1] = N;
cin >> n >> q;
ForE(u, 1, n){
char c; cin >> c;
a[u] = c - '0';
}
For(i, 1, n){
int u, v, w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
dfs_par(1);
dfs_sz(1);
nxt[1] = 1;
dfs_tour(1);
dfs_lca(1);
ForE(i, 1, ctrtour_lca){
sptb[0][i] = tour_lca[i];
}
For(j, 1, LN + 1){
ForE(i, 1, ctrtour_lca - (1 << j) + 1){
sptb[j][i] = min_h(sptb[j - 1][i], sptb[j - 1][i + (1 << (j - 1))]);
}
}
ForE(u, 1, n){
if (nxt[u] == u){
idx_chain[u] = cnt_chain++;
}
}
ForE(u, 1, n){
idx_chain[u] = idx_chain[nxt[u]];
}
seg.build(1, 1, n);
while (q--){
int type; cin >> type;
if (type == 1){
int u;
cin >> u;
seg.update(1, 1, n, u);
}
else{
int l, r;
cin >> l >> r;
segment_tree::node val = seg.query(1, 1, n, l, r);
vector <int> subset;
int w = -1;
For(i, 0, cnt_chain){
if (val.mnmx[i].fi == N - 1){
continue;
}
w = (w == -1 ? val.mnmx[i].fi : lca(w, val.mnmx[i].fi));
subset.emplace_back(val.mnmx[i].se);
}
if (w == -1){
cout << -1 << endl;
continue;
}
subset.emplace_back(w);
sort(bend(subset), [&](int u, int v){
return tin[u] < tin[v];
});
ll ans = 0;
For(i, 0, isz(subset)){
int j = i + 1;
if (j == isz(subset)) j = 0;
ans += dist(subset[i], subset[j]);
}
cout << ans << endl;
}
}
}
/*
==================================================+
INPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 225200kb
input:
5 6 10110 1 2 1 1 3 10 2 4 100 3 5 1000 2 1 5 1 3 2 1 5 2 2 4 2 5 5 2 1 1
output:
222 202 0 -1 0
result:
ok 5 number(s): "222 202 0 -1 0"
Test #2:
score: 0
Accepted
time: 146ms
memory: 228412kb
input:
50 200000 00100100100001101010110100000111100011101110011010 14 47 940241413 11 43 331483591 37 38 496247070 47 46 832459694 7 15 16479291 1 30 402388666 30 8 513064064 46 50 739311480 5 4 761894947 32 41 631669659 17 24 715786564 35 20 763151642 32 33 492466005 39 11 186023146 9 7 805081251 3 42 25...
output:
17149847378 -1 26540740138 29613692754 21307948558 27003443184 11893407374 18332625722 20412196350 20281573062 29727468956 9593307160 9380710234 23682775470 16688997988 2856655228 14982588156 0 -1 9140136614 26193602866 22558599316 24807163344 19126284190 8533875940 7695830712 18079494744 0 27004673...
result:
ok 100000 numbers
Test #3:
score: 0
Accepted
time: 144ms
memory: 228536kb
input:
50 200000 10010001110011101101101011010011100101101010111001 1 25 560163135 2 7 696374536 33 39 14485266 39 22 456690113 4 5 267811886 18 6 657680382 3 43 865617406 25 14 885515220 41 34 919525052 42 50 410634144 8 3 487403277 3 30 701549676 2 43 54223104 7 34 691078766 14 23 323352590 26 48 4936558...
output:
17177676326 31373486130 15290175638 8192974494 22734716092 27802380120 6315957348 0 15780401446 15929384564 26423899248 9639532596 21199366790 26413065782 -1 29063665908 29925313598 28651879288 17144176262 16526083688 28206620550 26873163342 14840246174 32414950818 29985336496 0 11937422088 11131990...
result:
ok 100000 numbers
Test #4:
score: 0
Accepted
time: 139ms
memory: 229240kb
input:
50 200000 01011001001010000100001101100111011001101011101000 34 29 654100339 33 5 947063765 45 25 962067151 11 13 509130797 26 47 988757358 49 22 75267557 7 11 431530851 46 1 531969122 22 43 449891945 34 6 526679193 50 17 499004364 22 23 226725950 26 48 203414490 11 44 900725507 10 29 451714017 35 2...
output:
24080362406 0 0 21418182584 28358635244 28257750030 24520294678 21418182584 0 15335211126 28621468542 18664505530 19335499776 32374868794 23618866752 26801803500 24116134918 27676993638 30222353942 25612316674 20504702130 28400398734 29472795250 24400110084 20586186786 25612316674 0 30067400886 1297...
result:
ok 100000 numbers
Test #5:
score: 0
Accepted
time: 8ms
memory: 217832kb
input:
2 10 01 1 2 340134039 2 1 2 1 1 2 1 2 1 2 2 1 1 1 2 2 2 2 1 2 2 2 2 1 1
output:
0 680268078 0 0 -1
result:
ok 5 number(s): "0 680268078 0 0 -1"
Test #6:
score: -100
Wrong Answer
time: 139ms
memory: 268236kb
input:
200000 100 1100001001001101010000111111011011101010001011011011011010101111010001111010101100101001010000110100100011111010011100101010010001011010111100101110010000101010000100111100000011001100111101110011000011010011011011100101100111101010111101110111001111111010001010010000110110100000111000111...
output:
134638535005270 134625564826354 134619234638990 134623807844748 134597171722000 134510281892308 134637369126266 134623807844748 134622988254850 134685596224110 134622088694690 134527518700736 134625564826354 134665126082108 134623807844748 134617701845252 134665126082108 134632207396746 134625564826...
result:
wrong answer 1st numbers differ - expected: '187263442365796', found: '134638535005270'