QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#65478 | #5094. 小 H 分糖果 | zqyyy | 10 | 8ms | 6524kb | C++20 | 6.8kb | 2022-12-01 11:52:00 | 2023-10-04 02:47:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
inline char getC() { //Don't to use it in interactive problems!!!
static char *p1, *p2, buf[1<<23];
return p1==p2?(p2=(p1=buf)+fread(buf, 1, 1<<23, stdin), p1==p2?'\n':*p1++):*p1++;
}
inline int read() {
int f=1, r=0; char c=getC();
while (!isdigit(c)) f^=c=='-', c=getC();
while (isdigit(c)) r=r*10+c-48, c=getC();
return f?r:-r;
}
template<class T> inline void write(T x) {
if (x<0) putchar(45), x=-x;
if (x>9) write(x/10); putchar(x%10+48);
}
#define mk make_pair
#define pii pair<int, int>
#define fi first
#define se second
template<class T> inline void ckmin(T &x, T y) {if (y<x) x=y;}
template<class T> inline void ckmax(T &x, T y) {if (x<y) x=y;}
const int N=1e5+7, M=2007, mod=998244353;
inline void inc(int &x, int y) {x+=y; if (x>=mod) x-=mod;}
inline void dec(int &x, int y) {x-=y; if (x<0) x+=mod;}
inline void mul(int &x, int y) {x=(ll)x*y%mod;}
inline int add(int x, int y) {return inc(x, y), x;}
inline int sub(int x, int y) {return dec(x, y), x;}
inline int qpow(int a, ll b) {
int res=1;
for (; b; b>>=1, mul(a, a)) if (b&1) mul(res, a);
return res;
}
int n, Q, B, a[N];
int s_dfn, dfn[N], rev[N], low[N], sz[N], son[N], fa[N], top[N], dep[N];
lll sum;
vector<int> G[N];
struct node {int opt, u, v; ll w;} q[M];
void dfs1(int x) {
sz[x]=1, dep[x]=dep[fa[x]]+1;
for (int y:G[x]) {
if (y==fa[x]) continue;
fa[y]=x, dfs1(y), sz[x]+=sz[y];
if (sz[y]>sz[son[x]]) son[x]=y;
}
}
void dfs2(int x, int tf) {
top[x]=tf, dfn[x]=++s_dfn, rev[s_dfn]=x;
if (!son[x]) {low[x]=s_dfn; return;} dfs2(son[x], tf);
for (int y:G[x]) if (y!=fa[x] && y!=son[x]) dfs2(y, y);
low[x]=s_dfn;
}
int sum_c; ll sum_t;
namespace sgt {
const int M=N*18;
int tot, ls[M], rs[M], cnt[M]; ll t[M];
void modify(int &p, int q, int l, int r, int x, int v) {
p=++tot, t[p]=t[q]+v, cnt[p]=cnt[q]+1; if (l==r) return;
int mid=(l+r)>>1;
if (x<=mid) modify(ls[p], ls[q], l, mid, x, v), rs[p]=rs[q];
else modify(rs[p], rs[q], mid+1, r, x, v), ls[p]=ls[q];
}
ll queryt(int p, int l, int r, int x, int y) {
if (x<=l && r<=y) return t[p]; int mid=(l+r)>>1; if (y<=mid) return queryt(ls[p], l, mid, x, y);
if (x>mid) return queryt(rs[p], mid+1, r, x, y); return queryt(ls[p], l, mid, x, y)+queryt(rs[p], mid+1, r, x, y);
}
void query(int p, int l, int r, int x, int y) {
if (!p) return;
if (x<=l && r<=y) {sum_c+=cnt[p], sum_t+=t[p]; return;}
int mid=(l+r)>>1;
if (x<=mid) query(ls[p], l, mid, x, y);
if (y>mid) query(rs[p], mid+1, r, x, y);
}
void query(int p, int l, int r) {
sum_c=0, sum_t=0, query(p, 1, n, l, r);
}
}
namespace sgt1 {
const int M=N*18;
int tot, ls[M], rs[M]; lll t[M];
void modify(int &p, int q, int l, int r, int x, ll v) {
p=++tot, t[p]=t[q]+v; if (l==r) return;
int mid=(l+r)>>1;
if (x<=mid) modify(ls[p], ls[q], l, mid, x, v), rs[p]=rs[q];
else modify(rs[p], rs[q], mid+1, r, x, v), ls[p]=ls[q];
}
lll query(int p, int l, int r, int x, int y) {
if (x<=l && r<=y) return t[p]; int mid=(l+r)>>1; if (y<=mid) return query(ls[p], l, mid, x, y);
if (x>mid) return query(rs[p], mid+1, r, x, y); return query(ls[p], l, mid, x, y)+query(rs[p], mid+1, r, x, y);
}
}
/*
namespace block {
int s1[N], s2[M], R[M], bel[N];
inline void init() {
for (int i=1; i<=n; i++) bel[i]=(i-1)/B+1;
for (int i=1; i<=bel[n]; i++) R[i]=min(n, i*B);
bel[n+1]=bel[n]+1;
}
inline void add(int p, int v) {
for (int i=p; i<=R[bel[p]]; i++) s1[i]+=v;
for (int i=bel[p]+1; i<=bel[n]; i++) s2[i]+=v;
}
inline int query(int p) {return s1[p]+s2[bel[p]];}
}*/
namespace fwk {
int c[N];
inline void add(int p, int x) {for (; p<=n; p+=p&-p) c[p]+=x;}
inline int ask(int p) {int res=0; for (; p; p&=p-1) res+=c[p]; return res;}
}
int rt[N], rt1[N], tot, cnt, s_seg, seq[M], s_in, seq_in[M];
bool vis[N], ins[N];
pair<int, int> val[N], seg[114];
inline ll calc1(int mid) {
ll res=0; int c=0;
for (int i=1; i<=s_in; i++) if (seq_in[i]>=mid) res+=seq_in[i]-mid;
int p=lower_bound(val+1, val+tot+1, mk(mid, 0))-val;
if (p<=tot) {
//if (mid==3) cout<<sgt::cnt[rt[p]]<<" "<<sgt::t[rt[p]]<<endl;
for (int i=1; i<=s_seg; i++) {
sgt::query(rt[p], seg[i].fi, seg[i].se);
c+=sum_c, res+=sum_t;
}
}
return res-(ll)c*mid;
}
inline lll calc2(int mid, ll w) {
lll ans=0; int c=0;
for (int i=1; i<=s_in; i++)
if (seq_in[i]>=mid) w-=seq_in[i], c++;
else ans+=(ll)seq_in[i]*seq_in[i];
int p=lower_bound(val+1, val+tot+1, mk(mid, 0))-val;
if (p<=tot) {
for (int i=1; i<=s_seg; i++) {
sgt::query(rt[p], seg[i].fi, seg[i].se);
c+=sum_c, w-=sum_t;
}
}
if (--p) for (int i=1; i<=s_seg; i++) ans+=sgt1::query(rt1[p], 1, n, seg[i].fi, seg[i].se);
w+=(ll)c*mid, ans+=(c-w)*(lll)mid*mid+(lll)w*(mid-1)*(mid-1);
return ans;
}
inline lll query(int u, int v, ll w) {
s_seg=0;
while (top[u]^top[v]) {
if (dep[top[u]]>dep[top[v]]) swap(u, v);
seg[++s_seg]={dfn[top[v]], dfn[v]}, v=fa[top[v]];
}
if (dfn[u]>dfn[v]) swap(u, v);
seg[++s_seg]={dfn[u], dfn[v]};
for (int i=1; i<=s_seg; i++) fwk::add(seg[i].fi, 1), fwk::add(seg[i].se+1, -1);
s_in=0;
for (int i=1; i<=cnt; i++) if (fwk::ask(seq[i])) seq_in[++s_in]=a[seq[i]];
for (int i=1; i<=s_seg; i++) fwk::add(seg[i].fi, -1), fwk::add(seg[i].se+1, 1);
int l=0, r=1e9;
while (l<r) {
int mid=(l+r)>>1;
if (calc1(mid)<=w) r=mid;
else l=mid+1;
}
lll ans=(l?calc2(l, w):0)+sum;
for (int i=1; i<=s_seg; i++) ans-=sgt1::query(rt1[tot], 1, n, seg[i].fi, seg[i].se);
for (int i=1; i<=s_in; i++) ans-=(ll)seq_in[i]*seq_in[i];
return ans;
}
inline void work() {
for (int i=1; i<=Q; i++)
if (q[i].opt==1 && !vis[q[i].u]) seq[++cnt]=q[i].u, vis[q[i].u]=true;
for (int i=1; i<=n; i++) if (!vis[i]) val[++tot]={a[i], i};
sort(val+1, val+tot+1), rt[tot+1]=0;
for (int i=tot; i; i--) sgt::modify(rt[i], rt[i+1], 1, n, val[i].se, val[i].fi);
for (int i=1; i<=tot; i++) sgt1::modify(rt1[i], rt1[i-1], 1, n, val[i].se, (ll)val[i].fi*val[i].fi);
for (int i=1; i<=Q; i++)
if (q[i].opt==1) sum-=(ll)a[q[i].u]*a[q[i].u], a[q[i].u]=q[i].v, sum+=(ll)a[q[i].u]*a[q[i].u];
else write(query(q[i].u, q[i].v, q[i].w)), putchar(10);
for (int i=1; i<=cnt; i++) vis[seq[i]]=false;
sgt::tot=sgt1::tot=cnt=tot=0;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
n=read(); int qq=read(); B=sqrt(n);
for (int i=1; i<n; i++) {
int x=read(), y=read();
G[x].push_back(y), G[y].push_back(x);
}
dfs1(1), dfs2(1, 1); //, block::init();
for (int i=1; i<=n; i++) a[dfn[i]]=read();
for (int i=1; i<=n; i++) sum+=(ll)a[i]*a[i];
for (int i=1; i<=qq; i++) {
Q++, q[Q].opt=read(), q[Q].u=read(), q[Q].v=read();
if (q[Q].opt==2) q[Q].w=read();
else q[Q].u=dfn[q[Q].u];
if (Q==B) work(), Q=0;
}
if (Q) work();
return 0;
}
详细
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 4ms
memory: 6308kb
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:
285125508 285374449 285871392 285072359 284419704 284843737 284692039 284936099 285944374 285174668 285019779 284651455 287282253 287175619 284878507 285369672 284880507 285404741 284913527 286053317 288622563 286960150 287194443 288326074 286937403 287883097 288535226 288195055 288643208 288632989 ...
result:
ok 419 lines
Test #2:
score: 0
Accepted
time: 8ms
memory: 6432kb
input:
951 896 6 886 8 809 9 392 12 590 13 643 16 322 17 613 37 605 42 354 44 594 47 905 49 623 53 240 56 546 58 751 60 919 62 74 63 303 64 105 68 578 69 41 41 110 71 402 74 528 75 616 76 83 77 230 83 289 84 354 88 340 89 65 91 785 92 831 93 286 96 101 97 841 99 556 100 700 101 574 102 652 103 534 105 107 ...
output:
323762325 323477550 323340853 323950611 323726768 322803839 322828150 323927499 323452292 322435378 323945420 323767611 322557229 323137116 322736140 322900873 322078759 322286746 322383436 322825503 321513631 320299707 320738958 320752861 319979668 320430841 319960294 319658294 317899682 318822085 ...
result:
ok 443 lines
Subtask #2:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Test #3:
score: 0
Wrong Answer
time: 8ms
memory: 6524kb
input:
903 809 2 4 5 604 6 396 10 637 12 593 22 855 26 586 29 170 34 64 37 559 42 413 44 789 46 584 49 83 52 642 55 688 59 899 61 614 68 572 74 707 79 278 80 32 83 315 86 779 87 323 89 901 91 720 92 463 93 592 98 348 99 644 100 465 102 377 103 835 104 479 107 757 111 207 112 458 116 43 43 623 121 538 124 1...
output:
276805063584166248522 280176247954234120034 277417224646898680385 276526352730593552950 282956038874074986705 277000626431768466948 282841956376132027954 277555271820331288115 282245368152893198717 280944261067543752260 280529311615751226986 277452768032389764786 279717343951191633999 28390258469206...
result:
wrong answer 2nd lines differ - expected: '273874231351955459944', found: '280176247954234120034'
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:
27226016102414613393626 27222683135365131711066 27224103629063100681661 27221607244120669838311 27219047117137492446677 27222635053035794978138 27227414178930370796708 27226133802205329219712 27225622966613584637508 27219505943263517662069 27219987830714690994915 27224970564451719457874 272242110070...
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%