QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#441474 | #5094. 小 H 分糖果 | lqt1217 | 0 | 15ms | 291872kb | C++14 | 4.7kb | 2024-06-14 15:31:26 | 2024-06-14 15:31:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
//#define int long long //__int128
#define int __int128
typedef pair<int, int> pii;
const int N= (int)1e5+ 5, M= N* 2, inf= (int)1e9;
int rd() {
char ch= getchar();
while(ch< '0'|| ch> '9') ch= getchar();
int t= 0;
while(ch>= '0'&& ch<= '9') t= (t<< 3)+ (t<< 1)+ ch- '0', ch= getchar();
return t;
}
void wr(int x) {
if(x> 9) wr(x/ 10);
putchar(x% 10+ '0');
return ;
}
struct node {
int x, s, c, p;
};
int h[N], e[M], ne[M], idx, t[N];
void add(int a, int b) {
e[idx]= b, ne[idx]= h[a], h[a]= idx++ ;
}
int dep[N], fa[N][21], l[N], r[N], id, rid[N];
void dfs(int u, int f) {
dep[u]= dep[f]+ 1;
fa[u][0]= f;
l[u]= ++ id;
rid[id]= u;
for(int i= h[u]; ~i; i= ne[i]) {
int j= e[i];
if(j== f) continue;
dfs(j, u);
}
r[u]= id;
}
bool st;
int rt[N], bit[N], s[N* 35], c[N* 35], tot, ls[N* 35], rs[N* 35], ps[N* 35];
bool ed;
int n, q;
vector<pii> qut;
void ad(int &u, int v, int l, int r, int x, int y) {
u= ++ tot;
s[u]= s[v], c[u]= c[v], ps[u]= ps[v];
ls[u]= ls[v], rs[u]= rs[v];
s[u]+= y* x;
c[u]+= y;
ps[u]+= y* x* x;
if(l== r) return ;
int mid= l+ r>> 1;
if(x<= mid) ad(ls[u], ls[v], l, mid, x, y);
else ad(rs[u], rs[v], mid+ 1, r, x, y);
}
void add(int &u, int l, int r, int x, int y) {
if(! u) u= ++ tot;
s[u]+= y* x;
c[u]+= y;
ps[u]+= y* x* x;
if(l== r) return ;
int mid= l+ r>> 1;
if(x<= mid) add(ls[u], l, mid, x, y);
else add(rs[u], mid+ 1, r, x, y);
}
int m;
node qu(int l, int r, int us, int uc, int up) {
// cout<< l<< ' '<< r<< endl;
int cs= 0, ss= 0, pss= 0;
for(pii i: qut)
cs+= c[i.fi]* i.se, ss+= s[i.fi]* i.se, pss+= ps[i.fi]* i.se;
if(l== r) return {l, ss+ us, cs+ uc, pss+ up};
int mid= l+ r>> 1;
vector<pii> qul, qur;
for(pii i: qut)
qul.pb({ls[i.fi], i.se}), qur.pb({rs[i.fi], i.se});
int cl= 0, sl= 0, psl= 0, cr= 0, sr= 0, psr= 0;
for(pii i: qul)
cl+= c[i.fi]* i.se, sl+= s[i.fi]* i.se, psl+= ps[i.fi]* i.se;
for(pii i: qur)
cr+= c[i.fi]* i.se, sr+= s[i.fi]* i.se, psr+= ps[i.fi]* i.se;
if(sr+ us- mid* (cr+ uc)<= m) {
qut= qul;
return qu(l, mid, us+ sr, cr+ uc, up+ psr);
}
else {
qut= qur;
return qu(mid+ 1, r, us, uc, up);
}
}
void pa(int x, int y, int t) {
for(int i= x; i<= n; i+= i& -i)
add(bit[i], 0, inf, y, t);
}
void pq(int x, int y) {
qut.pb({rt[x], y});
for(int i= x; i; i-= i& -i)
qut.pb({bit[i], y});
}
int lca(int x, int y) {
if(dep[x]< dep[y]) swap(x, y);
for(int i= 20; i>= 0; i-- )
if(dep[fa[x][i]]>= dep[y]) x= fa[x][i];
if(x== y) return x;
for(int i= 20; i>= 0; i-- )
if(fa[x][i]!= fa[y][i]) x= fa[x][i], y= fa[y][i];
return fa[x][0];
}
int ans= 0;
void bl() {
memset(rt, 0, sizeof rt);
memset(ls, 0, sizeof ls);
memset(rs, 0, sizeof rs);
memset(s, 0, sizeof s);
memset(c, 0, sizeof c);
memset(ps, 0, sizeof ps);
ans= 0, tot= 0;
// cout<< 111<< endl;
for(int i= 1; i<= n; i++ )
ad(rt[l[i]], rt[l[fa[i][0]]], 0, inf, t[i], 1), ans+= t[i]* t[i];
// for(int i= 1; i<= n; i++ )
// pa(l[i], t[i], 1), pa(r[i]+ 1, t[i], -1), ans+= t[i]* t[i], wr(tot), puts("");
// wr(tot);
// puts("");
}
signed main() {
// freopen("PanzerSupply.in", "r", stdin);
// freopen("PanzerSupply.out", "w", stdout);
// cout<< (& ed- & st)/ 1024.0/ 1024.0<< endl;
n= rd(), q= rd();
memset(h, -1, sizeof h);
for(int i= 1; i< n; i++ ) {
int a= rd(), b= rd();
add(a, b);
add(b, a);
}
dfs(1, 0);
for(int j= 1; j<= 20; j++ )
for(int i= 1; i<= n; i++ )
fa[i][j]= fa[fa[i][j- 1]][j- 1];
ans= 0;
for(int i= 1; i<= n; i++ )
t[i]= rd();
bl();
while(q-- ) {
int f= rd(), u= rd(), v= rd();
if(f== 1) {
pa(l[u], t[u], -1);
pa(r[u]+ 1, t[u], 1);
ans-= t[u]* t[u];
t[u]= v;
pa(l[u], t[u], 1);
pa(r[u]+ 1, t[u], -1);
ans+= t[u]* t[u];
if(tot>= N* 25) bl();
}
else {
m= rd();
int lc= lca(u, v);
// cout<< lc<< endl;
// cout<< lc<< endl;
qut.clear();
pq(l[u], 1), pq(l[v], 1), pq(l[lc], -1), pq(l[fa[lc][0]], -1);
int ss= 0, pss= 0;
for(pii i: qut)
ss+= s[i.fi]* i.se, pss+= ps[i.fi]* i.se;
// cout<< ss<< ' '<< pss<< endl;
if(m>= ss) {
wr(ans- pss);
puts("");
continue;
}
// cout<< 111<< ' '<< ss<< ' '<< pss<< endl;
// cout<< 111<< endl;
node tmp= qu(0, inf, 0, 0, 0);
int ls= m- tmp.s+ tmp.c* tmp.x;
wr(ls* (tmp.x- 1)* (tmp.x- 1)+ (tmp.c- ls)* tmp.x* tmp.x+ pss- tmp.p+ ans- pss);
puts("");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 15ms
memory: 291872kb
input:
866 841 1 864 4 153 9 559 10 410 11 336 12 417 14 666 18 241 21 184 22 849 23 40 25 783 26 189 28 329 29 216 31 864 34 581 40 131 42 625 45 744 47 723 50 633 51 447 52 454 53 88 55 619 60 259 62 680 67 126 72 371 73 742 80 196 81 536 82 647 85 254 87 172 88 489 93 708 94 227 95 340 96 7 7 91 97 594 ...
output:
286781630 286905808 286437388 285297355 286004201 287065420 285660623 285985491 288811916 286303761 286088495 285637270 288191575 287205339 286489927 285624846 285867378 286925437 285572390 286354942 288851268 289409754 287457719 288943465 289370276 288321045 288609604 288619249 288658780 288866077 ...
result:
wrong answer 1st lines differ - expected: '285125508', found: '286781630'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #6:
score: 0
Time Limit Exceeded
input:
87080 98363 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%