QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#786167 | #6404. Shuttle Tour | StayAlone | WA | 157ms | 109912kb | C++14 | 4.3kb | 2024-11-26 20:31:34 | 2024-11-26 20:31:36 |
Judging History
answer
#include <bits/stdc++.h>
#define rep1(i, l, r) for (int i = l; i <= int(r); ++i)
#define rep2(i, l, r) for (int i = l; i >= int(r); --i)
#define rer(i, l, r, a) rep1(i, l, r) read(a[i])
#define ptc putchar
#define il inline
#define eb emplace_back
#define ef emplace_front
#define mp make_pair
#define fst first
#define snd second
#define sqr(x) ((x) * (x))
#define ls(x) x << 1
#define rs(x) x << 1 | 1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// typedef __int128 bll;
// typedef unsigned __int128 ubll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5 + 10, LOGN = log2(MAXN) + 1, inf = ~0U >> 2, INF = ~0U >> 1;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
namespace stupid_lrc {
template <typename T> il bool read(T &x) {
x = 0; bool f = false; char ch;
while (!isdigit(ch = getchar())) {
f ^= !(ch ^ '-');
if (ch == EOF) return false;
}
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
if (f) x = ~x + 1; return true;
}
il int read() {int x; read(x); return x;}
template <typename T> il bool read(pair <T, T> &x) {return read(x.fst) && read(x.snd);}
template <typename T> il void gmin(T &x, T y) {x = x < y ? x : y;}
template <typename T> il void gmax(T &x, T y) {x = x > y ? x : y;}
template <typename T, typename ...L>
il bool read(T &x, L &...y) {return read(x) && read(y...);}
template <typename T> il T lowbit(const T &x) {return x & -x;}
}
using namespace stupid_lrc;
int n, q, cnt, dep[MAXN], a[MAXN], dfn[MAXN], edfn[MAXN], stp, id[MAXN];
int f[MAXN][20]; ll sum[MAXN]; char s[MAXN];
vector <pii> lnk[MAXN];
struct {
#define STZ MAXN << 2
int mii[STZ], maa[STZ];
il void pushup(int x) {
mii[x] = min(mii[ls(x)], mii[rs(x)]);
maa[x] = max(maa[ls(x)], maa[rs(x)]);
}
il void build(int x, int l, int r, int t) {
if (l == r) {
if (t == id[l] && a[l]) maa[x] = mii[x] = dfn[l];
else maa[x] = -1, mii[x] = inf;
return;
} int mid = l + r >> 1;
build(ls(x), l, mid, t); build(rs(x), mid + 1, r, t);
pushup(x);
}
il void upd(int x, int l, int r, int k, int t) {
if (l == r) {
if (t == id[l] && a[l]) maa[x] = mii[x] = dfn[l];
else maa[x] = -1, mii[x] = inf;
return;
} int mid = l + r >> 1;
k <= mid ? upd(ls(x), l, mid, k, t) : upd(rs(x), mid + 1, r, k, t);
pushup(x);
}
il pii query(int x, int l, int r, int ql, int qr) {
if (l > qr || r < ql) return mp(-1, inf);
if (l >= ql && r <= qr) return mp(maa[x], mii[x]);
int mid = l + r >> 1; auto p = query(ls(x), l, mid, ql, qr), q = query(rs(x), mid + 1, r, ql, qr);
return {max(p.fst, q.fst), min(p.snd, p.snd)};
}
} T[55];
il void dfs(int x, int p, int t) {
dfn[x] = ++stp; id[x] = t; edfn[stp] = x;
bool fg = 0; f[x][0] = p; dep[x] = dep[p] + 1;
for (auto t : lnk[x]) if (t.fst ^ p) {
sum[t.fst] = sum[x] + t.snd;
if (fg) dfs(t.fst, x, ++cnt);
else dfs(t.fst, x, cnt), fg = 1;
}
}
il ll dis(int x, int y) {
ll ans = sum[x] + sum[y];
if (dep[x] < dep[y]) swap(x, y);
rep2(i, 17, 0) if (dep[x] - dep[y] >= 1 << i) x = f[x][i];
if (x == y) return ans - sum[x] * 2;
rep2(i, 17, 0) if (f[x][i] ^ f[y][i]) x = f[x][i], y = f[y][i];
return ans - sum[f[x][0]] * 2;
}
il void solve() {
read(n, q); cnt = 0; scanf("%s", s + 1);
rep1(i, 1, n) a[i] = s[i] & 15;
int u, v, w;
rep1(i, 2, n) read(u, v, w), lnk[u].eb(v, w), lnk[v].eb(u, w);
dfs(1, 0, ++cnt);
rep1(j, 1, 17) rep1(i, 1, n) f[i][j] = f[f[i][j - 1]][j - 1];
rep1(i, 1, cnt) T[i].build(1, 1, n, i);
assert(cnt <= 50);
while (q--) {
int op = read();
if (op == 1) {
int x = read(); a[x] ^= 1;
T[id[x]].upd(1, 1, n, x, id[x]);
} else {
vector <int> dot; int l, r; read(l, r);
rep1(i, 1, cnt) {
int x, y; tie(x, y) = T[i].query(1, 1, n, l, r);
if (x >= 1 && x <= n) dot.eb(edfn[x]);
if (y >= 1 && y <= n && x != y) dot.eb(edfn[y]);
} sort(begin(dot), end(dot), [&](const int &x, const int &y){return dfn[x] < dfn[y];});
if (dot.empty()) {
puts("-1");
continue;
} int q = dot.size(); ll ans = 0;
rep1(i, 0, q - 1) ans += dis(dot[i], dot[(i + 1) % q]);
printf("%lld\n", ans);
}
}
}
int main() {
for (int T = 1; T--; ) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 24356kb
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: -100
Wrong Answer
time: 157ms
memory: 109912kb
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:
wrong answer 306th numbers differ - expected: '3143542348', found: '0'