蒟蒻弱弱的问一句: 原题给的数据范围都是1e5级别的,可是我按照1e5开分分钟爆了。之后我补了个0,全部开成1e6,一些点还是爆炸。直到我全部乘上个3,才过去。请问一下大佬们,这个数组的大小是怎么算的??
附上可能出现玄学错误的丑陋代码
#include <bits/stdc++.h>
using namespace std;
#define N 3000100
inline int read(){
int x = 0;
char c = getchar();
while(!isdigit(c)){
c = getchar();
}
while(isdigit(c)){
x = (x << 1) + (x << 3) + (c ^ '0');
c = getchar();
}
return x;
}
struct node{
int v, next;
}t[N << 1];
int f[N];
int n, q;
int siz[N], son[N], fa[N], deth[N];
int top[N], dfn[N], rev[N], id = 0;
int w[N], c[N], T[N];
int bian = 0;
inline void add(int u, int v){
t[++bian] = (node){v, f[u]}, f[u] = bian;
t[++bian] = (node){u, f[v]}, f[v] = bian;
return;
}
void dfs1(int u, int father){
deth[u] = deth[father] + 1;
fa[u] = father;
siz[u] = 1;
int maxn = -1;
for(int i = f[u]; i;i = t[i].next){
int v = t[i].v;
if(v != fa[u]){
dfs1(v, u);
siz[u] += siz[v];
if(siz[v] > maxn){
son[u] = v;
maxn = siz[v];
}
}
}
return ;
}
void dfs2(int u, int rt){
dfn[u] = ++id, rev[id] = u;
top[u] = rt;
if(!son[u]) return ;
dfs2(son[u], rt);
for(int i = f[u]; i;i = t[i].next){
int v = t[i].v;
if(v != fa[u] && v != son[u]){
dfs2(v, v);
}
}
return ;
}
struct tree{
int val, w; // max num and you know
int lson, rson;
} e[N];
int cnt = 0;
inline void pushup(int o){
e[o].val = max(e[e[o].lson].val, e[e[o].rson].val);
e[o].w = e[e[o].lson].w + e[e[o].rson].w;
return ;
}
void build(int& o, int l, int r, int x, int k){
if(!o) o = ++cnt;
if(l > x || r < x)return ;
if(l == r){
e[o].val = e[o].w = k;
return ;
}
int mid = l + r >> 1;
build(e[o].lson, l, mid, x, k);
build(e[o].rson, mid + 1, r, x, k);
pushup(o);
return ;
}
void del(int& o, int l, int r, int x ){
if(!o) return ;
if(l > x || r < x) return ;
if(l == r){
e[o].val = e[o].w = 0; //delete the point
return ;
}
int mid = l + r >> 1;
del(e[o].lson, l, mid, x);
del(e[o].rson, mid + 1, r, x);
pushup(o); // do not forget to update the data of the tree
return ;
}
int query_sum(int& o, int l, int r, int in, int end){
if(!o) return 0;
if(r < in || l > end) return 0;
if(l >= in && r <= end){
return e[o].w;
}
int mid = l + r >> 1;
return query_sum(e[o].lson, l, mid, in, end) + query_sum(e[o].rson, mid + 1, r, in, end);
}
int query_max(int& o, int l, int r, int in, int end){
if(!o) return 0;
if(l > end || r < in)return 0;
if(l >= in && end >= r){
return e[o].val;
}
int mid = l + r >> 1;
return max(query_max(e[o].lson, l, mid, in, end), query_max(e[o].rson, mid + 1, r, in, end));
}
int ask_he(int x, int y){
int k = c[x];
int ans = 0;
while(top[x] != top[y]){
if(deth[top[x]] < deth[top[y]]) swap(x, y);
ans += query_sum(T[k], 1, n, dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if(deth[x] > deth[y]) swap(x, y);
ans += query_sum(T[k], 1, n, dfn[x], dfn[y]);
return ans;
}
int ask_maxn(int x, int y){
int ans = -(~0 >> 1);
int k = c[x];
while(top[x] != top[y]){
if(deth[top[x]] < deth[top[y]]) swap(x, y);
ans = max(ans, query_max(T[k], 1, n, dfn[top[x]], dfn[x]));
x = fa[top[x]];
}
if(deth[x] > deth[y]) swap(x, y);
ans = max(ans, query_max(T[k], 1, n, dfn[x], dfn[y]));
return ans;
}
int main(){
// freopen("hh.txt", "r", stdin);
n = read(), q = read();
for(int i = 1;i <= n; i++){
w[i] = read(), c[i] = read();
}
for(int i = 1;i < n; i++){
int x = read(), y = read();
add(x, y);
}
dfs1(1, 0); dfs2(1, 1);
for(int i = 1;i <= n; i++){
build(T[c[rev[i]]], 1, n, i, w[rev[i]]);
}
char temp[50];
while(q--){
scanf("%s", temp);
int x, k;
if(temp[1] == 'C'){
x = read(), k = read();
del(T[c[x]], 1, n, dfn[x]);
c[x] = k;
build(T[c[x]], 1, n, dfn[x], w[x]);
}
else if(temp[1] == 'W'){
x = read(), k = read();
del(T[c[x]], 1, n, dfn[x]);
w[x] = k;
build(T[c[x]], 1, n, dfn[x], w[x]);
}
else if(temp[1] == 'S'){
int x = read(), y = read();
printf("%d\n", ask_he(x, y));
}
else if(temp[1] == 'M'){
int x = read(), y = read();
printf("%d\n", ask_maxn(x, y));
}
}
return 0;
}