QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#74559 | #5094. 小 H 分糖果 | 330312 | 10 | 10ms | 28224kb | C++14 | 3.5kb | 2023-02-02 15:53:25 | 2023-10-04 02:50:40 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define ll __int128
#define Mpr(x,y) make_pair(x,y)
#define PR pair<int,int>
#define fi first
#define se second
using namespace std;
const long long N=2e5+5,M=30,md=998244353,F=1e9;
long long n,q,u,v,cnt,tot,fa[N][M],dep[N],in[N],out[N],opt,a[N],all,sum,els,m,p1,p0;
vector<ll>c[N],g[N];
struct TreeB { ll ls,rs,c0,c1,c2; }T[N*30];
#define ls(x) T[x].ls
#define rs(x) T[x].rs
#define c0(x) T[x].c0
#define c1(x) T[x].c1
#define c2(x) T[x].c2
#define mid (l+r)/2
void up(ll x) { ll ls=ls(x),rs=rs(x); c0(x)=c0(ls)+c0(rs); c1(x)=c1(ls)+c1(rs); c2(x)=c2(ls)+c2(rs); }
void add(ll &x,ll l,ll r,ll pos,ll k)
{
if (!x) x=++cnt;
if (l==r) return c0(x)+=k,c1(x)+=pos*k,c2(x)+=pos*pos*k,void();
if (pos<=mid) add(ls(x),l,mid,pos,k); else add(rs(x),mid+1,r,pos,k); up(x);
}
#undef mid
struct TreeA { ll l,r,rt; }tt[N*4];
#define l(x) tt[x].l
#define r(x) tt[x].r
#define rt(x) tt[x].rt
void build(ll x,ll l,ll r)
{
tt[x]={l,r,++cnt};
if (l==r) return ;
ll mid=(l+r)/2;
build(x*2,l,mid); build(x*2+1,mid+1,r);
}
void ins(ll x,ll l,ll r,ll v,ll k)
{
if (l(x)>r||r(x)<l) return ;
if (l<=l(x)&&r(x)<=r) return add(rt(x),0,F,v,k),void();
ins(x*2,l,r,v,k); ins(x*2+1,l,r,v,k);
}
void dfs(ll x,ll ft)
{
fa[x][0]=ft; in[x]=++tot; dep[x]=dep[ft]+1;
for (ll i=1;i<=20;i++) fa[x][i]=fa[fa[x][i-1]][i-1];
for (auto t:c[x]) if (t!=ft) dfs(t,x); out[x]=tot;
}
ll Lca(ll x,ll y)
{
if (dep[x]<dep[y]) swap(x,y);
for (ll i=20;i>=0;i--) if (dep[fa[x][i]]>=dep[y]) x=fa[x][i];
if (x==y) return x;
for (ll i=20;i>=0;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
void Get(ll x,ll pos,ll k)
{
if (pos<l(x)||pos>r(x)) return ;
g[k].push_back(rt(x)); els+=k>1?-c2(rt(x)):c2(rt(x));
if (l(x)==r(x)) return ;
if (pos<=r(x*2)) Get(x*2,pos,k); else Get(x*2+1,pos,k);
}
void solve(ll l,ll r)
{
if (l==r)
{
ll num=p0;
for (auto t:g[1]) num+=c0(t);
for (auto t:g[2]) num-=c0(t);
// cout<<l<<" "<<r<<" "<<m<<" "<<num<<" "<<m-p1+p0*l<<" "<<all<<" "<<els<<" "<<all-els<<" "<<p0<<" "<<p1<<endl;
m=min(num,m-p1+p0*l);
if (!l) sum+=(num-m)*l*l; else
sum+=(num-m)*l*l+m*(l-1)*(l-1);
return ;
}
ll mid=(l+r)/2;
ll s0=p0,s1=p1,h=0;
for (auto t:g[1])
s0+=c0(rs(t)),s1+=c1(rs(t)),h+=c2(ls(t));
for (auto t:g[2])
s0-=c0(rs(t)),s1-=c1(rs(t)),h-=c2(ls(t));
// cout<<l<<" "<<r<<" "<<mid<<" "<<s0<<" "<<s1<<" "<<s1-s0*mid<<endl;
if (s1-s0*mid<m)
{
for (auto &t:g[1]) t=ls(t);
for (auto &t:g[2]) t=ls(t);
p0=s0; p1=s1; solve(l,mid);
} else
{
for (auto &t:g[1]) t=rs(t);
for (auto &t:g[2]) t=rs(t);
sum+=h; solve(mid+1,r);
}
}
int main()
{
cin>>n>>q;
for (ll i=1;i<n;i++) scanf("%lld%lld",&u,&v),c[u].push_back(v),c[v].push_back(u);
dfs(1,0); build(1,1,n);
for (ll i=1;i<=n;i++)
scanf("%lld",&a[i]),ins(1,in[i],out[i],a[i],1),all+=a[i]*a[i];
while (q--)
{
scanf("%lld%lld%lld",&opt,&u,&v);
if (opt==1) ins(1,in[u],out[u],a[u],-1),all+=v*v-a[u]*a[u],a[u]=v,ins(1,in[u],out[u],a[u],1);
else
{
scanf("%lld",&m); sum=els=p1=p0=0;
ll lca=Lca(u,v); for (ll i=1;i<=2;i++) g[i].clear();
Get(1,in[u],1);
Get(1,in[v],1);
Get(1,in[lca],2);
Get(1,in[fa[lca][0]],2);
// for (auto t:g[1]) cout<<t<<" "; cout<<endl;
// for (auto t:g[2]) cout<<t<<" "; cout<<endl;
solve(0,F); printf("%lld\n",all-els+sum);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 3ms
memory: 24524kb
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: 10ms
memory: 24532kb
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: 0ms
memory: 28224kb
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:
103902478522974282 -2826929753687814296 716063541255406145 -174808375049721290 -603188831837580581 299465326125192708 -1341119657373865854 854110714688013875 -1673770605080614149 -1341164599893104546 -2712481467066822022 751606926746490546 3016182845548359759 643200052420414317 -404326210148257208 -...
result:
wrong answer 1st lines differ - expected: '276805063584166248522', found: '103902478522974282'
Subtask #3:
score: 0
Runtime Error
Test #6:
score: 0
Runtime Error
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:
100%
Accepted
Dependency #2:
0%