梧桐灯 @ 2019-09-25 17:28:41
RT,全WA QAQ
#include <cstdio>
using namespace std;
inline void read (int& s) {
s = 0; int f = 0;
static char c = getchar ();
while (c < '0' || c > '9') {if (c == '-') f = 1; c = getchar ();}
while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c & 15), c = getchar ();
s = f ? -s : s; return ;
}
int qu[19];
inline void write (int s) {
if (!s) {putchar ('0'); return ;}
if (s < 0) {
s = -s;
putchar ('-');
}
int v = 0;
while (s) qu[++v] = s % 10, s /= 10;
while (v) putchar (qu[v--] + '0');
return ;
}
inline int max (const int p, const int q) {return p > q ? p : q;}
inline void swap (int& p, int& q) {int t = p; p = q; q = t;}
const int N = 100003;
int n, h[N], tot, a[N];
int e[N][2];
struct stu {
int v;
int next;
int w;
}s[N << 1];
inline void add (const int x, const int y, const int z) {
++tot;
s[tot].v = y;
s[tot].w = z;
s[tot].next = h[x];
h[x] = tot;
return ;
}
int d[N], fa[N], son[N], sz[N];
int id[N], rv[N], top[N], k;
void dfs1 (const int x, const int pr) {
d[x] = d[pr] + 1;
fa[x] = pr;
sz[x] = 1;
int i, y; for (i = h[x]; i; i = s[i].next) {
y = s[i].v;
if (y == pr) continue;
a[y] = s[i].w;
dfs1 (y, x);
sz[x] += sz[y];
if (sz[y] > sz[son[x]]) son[x] = y;
}
return ;
}
void dfs2 (const int x, const int tf) {
id[x] = ++k;
rv[k] = x;
top[x] = tf;
if (son[x]) dfs2 (son[x], tf);
int i, y; for (i = h[x]; i; i = s[i].next) {
y = s[i].v;
if (!id[x]) dfs2 (y, y);
}
return ;
}
int mx[N << 2];
void Bu (const int v, const int L, const int R) {
if (L == R) {
mx[v] = a[rv[L]];
return ;
}
int mid = L + R >> 1;
Bu (v << 1, L, mid);
Bu (v << 1 | 1, mid + 1, R);
mx[v] = max (mx[v << 1], mx[v << 1 | 1]);
return ;
}
void chan (const int v, const int L, const int R, const int x, const int z) {
if (L == R) {
mx[v] = z;
return ;
}
int mid = L + R >> 1;
if (mid >= x) chan (v << 1, L, mid, x, z);
else chan (v << 1 | 1, mid + 1, R, x, z);
mx[v] = max (mx[v << 1], mx[v << 1 | 1]);
return ;
}
int findmax (const int v, const int L, const int R, const int x, const int y) {
if (L >= x && R <= y) return mx[v];
int mid = L + R >> 1, r = -(1 << 30);
if (mid >= x) r = findmax (v << 1, L, mid, x, y);
if (mid < y) r = max (r, findmax (v << 1 | 1, mid + 1, R, x, y));
return r;
}
inline void work (int x, int y, const int z) {
if (id[x] < id[y]) swap (x, y);
chan (1, 1, n, id[x], z);
return ;
}
inline void ask (int x, int y) {
int ans = -2147483647;
while (top[x] != top[y]) {
if (d[top[x]] < d[top[y]]) swap (x, y);
if (id[top[x]] + 1 <= id[x]) ans = max (ans, findmax (1, 1, n, id[top[x]] + 1, id[x]));
x = fa[top[x]];
}
if (d[x] > d[y]) swap (x, y);
if (id[x] + 1 <= id[y]) ans = max (ans, findmax (1, 1, n, id[x] + 1, id[y]));
write (ans), putchar ('\n');
return ;
}
int main () {
read (n);
int i, x, y, z; for (i = 1; i < n; ++i) {
read (x), read (y), read (z);
e[i][0] = x, e[i][1] = y;
add (x, y, z);
add (y, x, z);
}
dfs1 (1, 0);
dfs2 (1, 1);
Bu (1, 1, n);
char c; while (true) {
c = getchar ();
while (c != 'C' && c != 'Q' && c != 'D') c = getchar ();
if (c == 'D') break;
else if (c == 'C') {
read (x), read (z);
work (e[x][0], e[x][1], z);
}
else {
read (x), read (y);
if (x == y) puts ("0");
else ask (x, y);
}
}
return 0;
}
然而我并不想强调自己是不是妹子
by yuio @ 2019-09-25 17:53:11
信我,下次标题里加上妹子帮忙的人多一倍
by 梧桐灯 @ 2019-09-25 17:53:19
3ks!!
by 梧桐灯 @ 2019-09-25 17:55:25
@yuio 那会一大堆人回
您好,请问您为什么要强调你是妹子呢?
by 空の軌跡 @ 2019-09-25 18:04:29
@光随影走
那你就加上一句话:我强调妹子其实是为了吸引你们这些 BigOld 来
by ZYF_B @ 2019-09-25 18:44:50
BigOld 神奇英语
by Charlie查理 @ 2019-10-05 10:49:31