QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#220466 | #7103. Red Black Tree | salvator_noster# | RE | 0ms | 18568kb | C++20 | 6.7kb | 2023-10-20 13:56:58 | 2023-10-20 13:57:00 |
Judging History
answer
#include <bits/stdc++.h>
template <class T>
inline int qlog(T a) {
double x = a;
return ((*(long long*) & x) >> 52 & 2047) - 1023;
}
#define fopen LilyWhite
void fopen(const char *s) {
static char filename[32];
sprintf(filename, "%s.in", s);
freopen(filename, "r", stdin);
sprintf(filename, "%s.out", s);
freopen(filename, "w", stdout);
}
typedef unsigned int u32;
typedef long long ll;
typedef unsigned long long u64;
#define Please return
#define AC 0
#define cin Mizuhashi
#define cout Parsee
#define endl '\n'
class Reader{
private:
static const int BUF_SIZE = 1 << 22;
char BUF_R[BUF_SIZE], *csy1, *csy2;
#ifndef _LOCAL_RUNNING
#define GC (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1 ++)
#else
char cur;
#define GC (cur = getchar())
#endif
#define IL inline
public:
IL bool eof() {
#ifndef _LOCAL_RUNNING
return csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2);
#else
return cur == EOF;
#endif
}
template <class Ty>
IL Reader& operator >> (Ty &t) {
int u = 0;
char c = GC;
for (t = 0; c < 48 || c > 57; c = GC)
if (c == EOF) break;
else if (c == '-') u = 1;
for ( ; c > 47 && c < 58; c = GC) t = t * 10 + (c ^ 48);
t = u ? -t : t; return *this;
}
IL Reader& operator >> (double &t) {
int tmp, u = 0; char c = GC;
for (tmp = 0; c < 48 || c > 57; c = GC)
if (c == EOF) break;
else if (c == '-') u = 1;
for ( ; c > 47 && c < 58; c = GC) tmp = tmp * 10 + (c ^ 48);
t = (tmp = u ? -tmp : tmp);
if (c == '.') {
double x = 1;
for (c = GC; c > 47 && c < 58; c = GC) t += (x /= 10) * (c ^ 48);
}
return *this;
}
IL Reader& operator >> (char *s) {
char c = GC;
for (*s = 0; c < 33; c = GC) if (c == EOF) return *this;
for ( ; c > 32; c = GC) *s ++ = c;
*s = 0; return *this;
}
IL Reader& operator >> (char &c) {
for (c = GC; c < 33; c = GC) if (c == EOF) return *this;
return *this;
}
}cin;
class Writer{
private:
static const int BUF_SIZE = 1 << 22;
char BUF_W[BUF_SIZE], *csy;
#define IL inline
inline void WC(const char c) {
if (csy - BUF_W == BUF_SIZE) fwrite(BUF_W, 1, BUF_SIZE, stdout), csy = BUF_W;
*csy ++ = c;
}
public:
Writer() : csy(BUF_W) {}
~ Writer() {fwrite(BUF_W, 1, csy - BUF_W, stdout);}
IL void flush() {fwrite(BUF_W, 1, csy - BUF_W, stdout); csy = BUF_W;}
template <class Ty>
IL Writer& operator << (Ty x) {
static int sta[32], top;
if (x < 0) {
WC('-');
do sta[top ++] = - (x % 10); while (x /= 10);
}else do sta[top ++] = x % 10; while (x /= 10);
while (top) WC(sta[-- top] ^ 48);
return *this;
}
IL Writer& operator << (const char &c) {WC(c); return *this;}
IL Writer& operator << (const char *s) {while (*s) WC(*s ++); return *this;}
}cout;
using namespace std;
const int MAX_N = 100000 + 5;
typedef pair <int, int> pii;
int N, M, Q, node[MAX_N];
struct Edge{
int y, prev, w;
}e[MAX_N << 1];
int elast[MAX_N], ecnt;
char color[MAX_N];
int mark[MAX_N];
int sz[MAX_N], bs[MAX_N], fa[MAX_N], dep[MAX_N], tp[MAX_N], dfsn[MAX_N], Tm;
ll sum[MAX_N], val0[MAX_N];
inline bool cmp(int i, int j) {
return dfsn[i] < dfsn[j];
}
void Build(int x, int y, int z) {
ecnt ++;
e[ecnt].y = y;
e[ecnt].prev = elast[x];
e[ecnt].w = z;
elast[x] = ecnt;
}
void dfs0(int u, int fath, ll lastR = 0) {
sz[u] = 1;
bs[u] = 0;
fa[u] = fath;
dep[u] = dep[fath] + 1;
if (color[u]) {
val0[u] = 0;
lastR = sum[u];
}else {
val0[u] = sum[u] - lastR;
}
for (int i = elast[u]; i; i = e[i].prev) {
int v = e[i].y;
if (v != fath) {
sum[v] = sum[u] + e[i].w;
dfs0(v, u, lastR);
sz[u] += sz[v];
if (sz[bs[u]] < sz[v]) bs[u] = v;
}
}
}
void dfs1(int u, int gr) {
tp[u] = gr;
dfsn[u] = ++ Tm;
if (bs[u]) dfs1(bs[u], gr);
for (int i = elast[u]; i; i = e[i].prev) {
int v = e[i].y;
if (v != bs[u] && v != fa[u]) dfs1(v, v);
}
}
int Lca(int u, int v) {
while (tp[u] != tp[v]) {
if (dep[tp[u]] < dep[tp[v]]) v = fa[tp[v]];
else u = fa[tp[u]];
}
return dep[u] < dep[v] ? u : v;
}
ll premax[MAX_N], sufmax[MAX_N], maxsum[MAX_N], res;
vector <int> edge[MAX_N];
pii dfs2(int u) {
int l = N, r = 0;
if (mark[u]) {
l = mark[u]; r = mark[u];
}
if (color[u]) maxsum[u] = 0;
else maxsum[u] = sum[u];
for (auto v : edge[u]) {
pii tmp = dfs2(v);
l = min(l, tmp.first);
r = max(r, tmp.second);
maxsum[u] = max(maxsum[u], maxsum[v]);
}
res = min(res, max(max(premax[l - 1], sufmax[r + 1]), maxsum[u] - sum[u]));
return {l, r};
}
ll VTsolve(int m) {
int tot = 0;
static int a[MAX_N];
for (int i = 1; i < m; i ++) {
a[++ tot] = node[i];
a[++ tot] = Lca(node[i], node[i + 1]);
}
a[++ tot] = node[m];
sort(a + 1, a + tot + 1, cmp);
tot = unique(a + 1, a + tot + 1) - a - 1;
for (int i = 1; i <= tot; i ++) edge[a[i]].clear();
for (int i = 1; i < tot; i ++) {
int lc = Lca(a[i], a[i + 1]);
edge[lc].push_back(a[i + 1]);
}
res = 1e18;
dfs2(a[1]);
return res;
}
void solve() {
cin >> N >> M >> Q;
ecnt = 0; Tm = 0;
memset(elast, 0, sizeof(int) * (N + 1));
memset(color, 0, N + 1);
for (int i = 1; i <= M; i ++) {
int x;
cin >> x;
color[x] = 1;
}
for (int i = 1; i < N; i ++) {
int x, y, z;
cin >> x >> y >> z;
Build(x, y, z);
Build(y, x, z);
}
dfs0(1, 0); dfs1(1, 1);
while (Q --) {
int k;
cin >> k;
ll maxv[2] = {0, 0};
for (int i = 1; i <= k; i ++) {
cin >> node[i];
mark[node[i]] = i;
premax[i] = max(premax[i - 1], val0[node[i]]);
}
sufmax[k + 1] = 0;
for (int i = k; i > 0; i --) sufmax[i] = max(sufmax[i + 1], val0[node[i]]);
sort(node + 1, node + k + 1, cmp);
cout << VTsolve(k) << endl;
for (int i = 1; i <= k; i ++) mark[node[i]] = 0;
}
}
int main() {
int T;
cin >> T;
while (T --) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18568kb
input:
2 12 2 4 1 9 1 2 1 2 3 4 3 4 3 3 5 2 2 6 2 6 7 1 6 8 2 2 9 5 9 10 2 9 11 3 1 12 10 3 3 7 8 4 4 5 7 8 4 7 8 10 11 3 4 5 12 3 2 3 1 2 1 2 1 1 3 1 1 1 2 1 2 3 1 2 3
output:
4 5 3 8 0 0 0
result:
ok 7 lines
Test #2:
score: -100
Runtime Error
input:
522 26 1 3 1 1 4 276455 18 6 49344056 18 25 58172365 19 9 12014251 2 1 15079181 17 1 50011746 8 9 2413085 23 24 23767115 22 2 26151339 26 21 50183935 17 14 16892041 9 26 53389093 1 20 62299200 24 18 56114328 11 2 50160143 6 26 14430542 16 7 32574577 3 16 59227555 3 15 8795685 4 12 5801074 5 20 57457...