QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#113126 | #5686. 种苹果 | myee | TL | 1115ms | 51604kb | C++11 | 10.8kb | 2023-06-16 14:49:21 | 2023-06-16 14:49:23 |
Judging History
answer
// 那就是希望。
// 即便需要取模,也是光明。
// 不弱于教主的魔法,可以规约矩阵乘法,考虑根号。
// 根号重构 + 树剖分块。
// 维护添加点的联通块。
// 每次重构把当前树重新剖分,进行信息的更新。
// 然后两类加点操作可以“新开联通块”或者“联通块内重构结构”。(广义串并联图的方式?)
// 加权操作可以对整颗树上加,各个联通块内的点依次判断是否在修改链上,暴力修改即可。
// 查询操作可以树上使用树剖分块查询,额外点也查询。
// 总复杂度 O(n\sqrt{n\log n}) (n\sim q)
#include <algorithm>
#include <stdio.h>
#include <vector>
typedef long long llt;
typedef unsigned uint;typedef unsigned long long ullt;
typedef bool bol;typedef char chr;typedef void voi;
typedef double dbl;
template<typename T>bol _max(T&a,T b){return(a<b)?a=b,true:false;}
template<typename T>bol _min(T&a,T b){return(b<a)?a=b,true:false;}
template<typename T>T lowbit(T n){return n&-n;}
template<typename T>T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<typename T>T lcm(T a,T b){return(a!=0||b!=0)?a/gcd(a,b)*b:(T)0;}
template<typename T>T exgcd(T a,T b,T&x,T&y){if(b!=0){T ans=exgcd(b,a%b,y,x);y-=a/b*x;return ans;}else return y=0,x=1,a;}
template<typename T>T power(T base,T index,T mod)
{
T ans=1%mod;
while(index)
{
if(index&1)ans=ans*base%mod;
base=base*base%mod,index>>=1;
}
return ans;
}
const uint Block=250,Lim=400000;
uint Siz[Lim+5],Heavy[Lim+5],Fath[Lim+5],Rot[Lim+5],Dep[Lim+5],Dfn[Lim+5],Back[Lim+5],n,tp;
std::vector<std::pair<llt,uint> >W[Lim/Block+5];
std::vector<uint>Way[Lim+5],Del[Lim+5];
llt Val[Lim+5],Tag[Lim/Block+5];
voi dfs1(uint p,uint f){
Fath[p]=f,Heavy[p]=-1,Siz[p]=1;
for(auto s:Way[p])if(s!=f){
Dep[s]=Dep[p]+1,dfs1(s,p),Siz[p]+=Siz[s];
if(!~Heavy[p]||Siz[s]>Siz[Heavy[p]])Heavy[p]=s;
}
}
voi dfs2(uint p,uint r){
Back[Dfn[p]=tp++]=p,Rot[p]=r;if(~Heavy[p])dfs2(Heavy[p],r);
for(auto s:Way[p])if(s!=Fath[p]&&s!=Heavy[p])dfs2(s,s);
}
voi rebuild(){
if(n&&tp-n<300)return;
for(uint i=0;i<=n/Block;i++)for(auto s:W[i])Val[Back[s.second]]=s.first+Tag[i];
for(uint i=0;i<n;i++)if(Del[i].size()){
std::vector<uint>V;
std::sort(Del[i].begin(),Del[i].end());
for(auto s:Way[i]){
auto t=std::lower_bound(Del[i].begin(),Del[i].end(),s);
if(t==Del[i].end()||*t!=s)V.push_back(s);
}
Way[i]=V,Del[i].clear();
}
for(uint i=n;i<tp;i++)for(auto s:Way[i])if(s<n)Way[s].push_back(i);
n=tp,tp=0,dfs1(0,-1),dfs2(0,0);
for(uint i=0;i<=n/Block;i++){
W[i].clear(),Tag[i]=0;
for(uint j=i*Block;j<(i+1)*Block&&j<n;j++)W[i].push_back({Val[Back[j]],j});
std::sort(W[i].begin(),W[i].end());
}
}
voi add_dfn(uint l,uint r,llt w){
if(l>=r)return;
std::vector<std::pair<llt,uint> >A,B;
if(l/Block==r/Block){
for(auto s:W[l/Block])(s.second>=l&&s.second<r?A:B).push_back(s);
for(auto&s:A)s.first+=w;
std::merge(A.begin(),A.end(),B.begin(),B.end(),W[l/Block].begin());
return;
}
for(auto s:W[l/Block])(s.second>=l?A:B).push_back(s);
for(auto&s:A)s.first+=w;
std::merge(A.begin(),A.end(),B.begin(),B.end(),W[l/Block].begin());
A.clear(),B.clear();
for(auto s:W[r/Block])(s.second<r?A:B).push_back(s);
for(auto&s:A)s.first+=w;
std::merge(A.begin(),A.end(),B.begin(),B.end(),W[r/Block].begin());
for(uint i=l/Block+1;i<r/Block;i++)Tag[i]+=w;
}
uint query_dfn(uint l,uint r,llt w){
if(l>=r)return 0;
uint ans=0;
if(l/Block==r/Block){
for(auto s:W[l/Block])if(s.second>=l&&s.second<r)ans+=s.first+Tag[l/Block]>=w;
return ans;
}
for(auto s:W[l/Block])if(s.second>=l)ans+=s.first+Tag[l/Block]>=w;
for(auto s:W[r/Block])if(s.second<r)ans+=s.first+Tag[r/Block]>=w;
for(uint i=l/Block+1;i<r/Block;i++)if(W[i].size()){
if(W[i][0].first+Tag[i]>=w)ans+=W[i].size();
else if(W[i].back().first+Tag[i]>=w){
uint l=0,r=W[i].size(),mid;
while(l<r)if(W[i][mid=(l+r)>>1].first+Tag[i]>=w)r=mid;else l=mid+1;
ans+=W[i].size()-l;
}
}
return ans;
}
bol OnWay(uint u,uint v,uint p){
while(Rot[u]!=Rot[v]){
if(Dep[Rot[u]]<Dep[Rot[v]])std::swap(u,v);
if(Dfn[p]>=Dfn[Rot[u]]&&Dfn[p]<=Dfn[u])return 1;
u=Fath[Rot[u]];
}
if(Dep[u]>Dep[v])std::swap(u,v);
return Dfn[p]>=Dfn[u]&&Dfn[p]<=Dfn[v];
}
voi add_line(uint u,uint v,llt w){
while(Rot[u]!=Rot[v]){
if(Dep[Rot[u]]<Dep[Rot[v]])std::swap(u,v);
add_dfn(Dfn[Rot[u]],Dfn[u]+1,w),u=Fath[Rot[u]];
}
if(Dep[u]>Dep[v])std::swap(u,v);
add_dfn(Dfn[u],Dfn[v]+1,w);
}
uint query_line(uint u,uint v,llt w){
uint ans=0;
while(Rot[u]!=Rot[v]){
if(Dep[Rot[u]]<Dep[Rot[v]])std::swap(u,v);
ans+=query_dfn(Dfn[Rot[u]],Dfn[u]+1,w),u=Fath[Rot[u]];
}
if(Dep[u]>Dep[v])std::swap(u,v);
ans+=query_dfn(Dfn[u],Dfn[v]+1,w);
return ans;
}
std::vector<uint>V;
bol dfs(uint p,uint f,uint t){
V.push_back(p);
if(p==t)return 1;
for(auto s:Way[p])if(s>=n&&s!=f&&dfs(s,p,t))return 1;
V.pop_back();
return 0;
}
voi add(uint u,uint v,llt w){
static bol Gone[Lim+5];for(uint i=n;i<tp;i++)Gone[i]=0;
if(u>=n&&v<n)std::swap(u,v);
if(v>=n){
if(u<n){
std::vector<std::pair<uint,uint> >P;
V={v};Gone[v]=1;
uint t=0;
while(t<V.size()){
uint p=V[t++];
for(auto s:Way[p])if(s<n)P.push_back({p,s});else if(!Gone[s])Gone[s]=1,V.push_back(s);
}
bol op=P.size()==1?0:OnWay(u,P[0].second,P[1].second);
V.clear(),dfs(P[op].first,-1u,v),v=P[op].second;
for(auto s:V)Val[s]+=w;
}
else{
std::vector<std::pair<uint,uint> >P1,P2;
V={u};Gone[u]=1;
uint t=0;
while(t<V.size()){
uint p=V[t++];
for(auto s:Way[p])if(s<n)P1.push_back({p,s});else if(!Gone[s])Gone[s]=1,V.push_back(s);
}
if(Gone[v]){
V.clear(),dfs(u,-1,v);for(auto s:V)Val[s]+=w;
return;
}
V={v};Gone[v]=1;
t=0;
while(t<V.size()){
uint p=V[t++];
for(auto s:Way[p])if(s<n)P2.push_back({p,s});else if(!Gone[s])Gone[s]=1,V.push_back(s);
}
bol op1=P1.size()==1?0:OnWay(P2[0].second,P1[0].second,P1[1].second);
bol op2=P2.size()==1?0:OnWay(P2[0].second,P1[0].second,P2[1].second);
V.clear();
dfs(P1[op1].first,-1u,u),u=P1[op1].second,dfs(P2[op2].first,-1u,v),v=P2[op2].second;
for(auto s:V)Val[s]+=w;
}
}
add_line(u,v,w);
for(uint i=n;i<tp;i++)if(!Gone[i]){
std::vector<std::pair<uint,uint> >P;
V={i};Gone[i]=1;
uint t=0;
while(t<V.size()){
uint p=V[t++];
for(auto s:Way[p])if(s<n)P.push_back({p,s});else if(!Gone[s])Gone[s]=1,V.push_back(s);
}
if(P.size()==2&&OnWay(u,v,P[0].second)&&OnWay(u,v,P[1].second)){
V.clear(),dfs(P[0].first,-1u,P[1].first);for(auto s:V)Val[s]+=w;
}
}
}
uint query(uint u,uint v,llt w){
uint ans=0;
static bol Gone[Lim+5];for(uint i=n;i<tp;i++)Gone[i]=0;
if(u>=n&&v<n)std::swap(u,v);
if(v>=n){
if(u<n){
std::vector<std::pair<uint,uint> >P;
V={v};Gone[v]=1;
uint t=0;
while(t<V.size()){
uint p=V[t++];
for(auto s:Way[p])if(s<n)P.push_back({p,s});else if(!Gone[s])Gone[s]=1,V.push_back(s);
}
bol op=P.size()==1?0:OnWay(u,P[0].second,P[1].second);
V.clear();
dfs(P[op].first,-1u,v);
for(auto s:V)ans+=Val[s]>=w;
v=P[op].second;
}
else{
std::vector<std::pair<uint,uint> >P1,P2;
V={u};Gone[u]=1;
uint t=0;
while(t<V.size()){
uint p=V[t++];
for(auto s:Way[p])if(s<n)P1.push_back({p,s});else if(!Gone[s])Gone[s]=1,V.push_back(s);
}
if(Gone[v]){
V.clear(),dfs(u,-1,v);
for(auto s:V)ans+=Val[s]>=w;
return ans;
}
V={v};Gone[v]=1;
t=0;
while(t<V.size()){
uint p=V[t++];
for(auto s:Way[p])if(s<n)P2.push_back({p,s});else if(!Gone[s])Gone[s]=1,V.push_back(s);
}
bol op1=P1.size()==1?0:OnWay(P2[0].second,P1[0].second,P1[1].second);
bol op2=P2.size()==1?0:OnWay(P2[0].second,P1[0].second,P2[1].second);
V.clear();
dfs(P1[op1].first,-1u,u),u=P1[op1].second,dfs(P2[op2].first,-1u,v),v=P2[op2].second;
for(auto s:V)ans+=Val[s]>=w;
}
}
ans+=query_line(u,v,w);
for(uint i=n;i<tp;i++)if(!Gone[i]){
std::vector<std::pair<uint,uint> >P;
V={i};Gone[i]=1;
uint t=0;
while(t<V.size()){
uint p=V[t++];
for(auto s:Way[p])if(s<n)P.push_back({p,s});else if(!Gone[s])Gone[s]=1,V.push_back(s);
}
if(P.size()==2&&OnWay(u,v,P[0].second)&&OnWay(u,v,P[1].second)){
V.clear(),dfs(P[0].first,-1u,P[1].first);
for(auto s:V)ans+=Val[s]>=w;
}
}
return ans;
}
int main()
{
#ifdef MYEE
freopen("QAQ.in","r",stdin);
freopen("QAQ.out","w",stdout);
#endif
uint q,ans=0;scanf("%u%u",&tp,&q);
for(uint i=0;i<tp;i++)scanf("%lld",Val+i);
for(uint i=1,u,v;i<tp;i++)
scanf("%u%u",&u,&v),Way[--u].push_back(--v),Way[v].push_back(u);
while(q--){
rebuild();
uint op,u,v;int w;
scanf("%u%u",&op,&u),u=(u^ans)-1;
if(op==1){
scanf("%u%d",&v,&w),v=(v^ans)-1,w^=ans,Val[tp]=w;
if(u>=n&&v<n)std::swap(u,v);
if(u<n&&v<n)Del[u].push_back(v),Del[v].push_back(u);
else if(u<n){for(auto&s:Way[v])if(s==u)s=tp;}
else{
for(auto&s:Way[v])if(s==u)s=tp;
for(auto&s:Way[u])if(s==v)s=tp;
}
Way[tp++]={u,v};
}else if(op==2){
scanf("%d",&w),w^=ans,Val[tp]=w;
if(u>=n)Way[u].push_back(tp);
Way[tp++].push_back(u);
}else if(op==3){
scanf("%u%d",&v,&w),v=(v^ans)-1,w^=ans,add(u,v,w);
}else{
scanf("%u%d",&v,&w),v=(v^ans)-1,w^=ans;
printf("%u\n",ans=query(u,v,w));
}
}
return 0;
}
// 那就是希望。
// 即便需要取模,也是光明。
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 31ms
memory: 32684kb
input:
5000 5000 -1201801 4507224 -765313 5795451 -126649 -5052948 470601 -5571705 7680665 -2662502 -689392 -3195719 3416253 -1023134 -3112032 3810606 -6975732 712075 257599 -180764 5190759 177433 -6055975 1555412 7768627 3402404 -872471 -1311920 -4231370 117735 3172664 -1502849 -3929398 -90435 6955032 382...
output:
645 0 0 0 57 0 1665 1087 1455 73 1172 1094 0 1739 1202 1290 0 0 0 569 0 1532 0 358 337 437 0 567 1086 12 73 922 1024 183 25 0 0 0 979 4 3 7 162 305 1285 0 1185 0 1263 402 576 1284 0 0 832 908 0 422 1425 1268 12 0 1692 439 0 0 28 0 384 0 0 1079 1257 1149 541 642 133 966 406 0 0 848 1335 431 0 0 363 8...
result:
ok 1020 lines
Test #2:
score: 0
Accepted
time: 28ms
memory: 32540kb
input:
5000 5000 -994733 862196 -2643618 4810652 2000445 -712322 -3955371 2924820 -675771 -6848147 825176 -5612153 4757907 1475460 1043402 1647007 -1015110 2001651 -6330733 2969460 -815149 6241724 136943 2360333 -3204656 5569099 809569 -4081554 -178998 -1363975 -4486956 -1858705 -59824 845830 4381332 17942...
output:
1469 2442 2 0 35 1869 0 353 993 1572 1095 0 12 1458 0 1659 545 0 0 1243 0 0 1410 911 1701 1660 0 0 84 728 0 5 0 1031 2089 1108 524 225 1237 2233 3 43 1324 327 1085 1049 615 61 1840 1892 0 491 989 242 1058 965 625 0 2592 340 36 0 211 0 146 2168 46 0 227 0 0 0 8 2103 1334 0 23 128 0 0 1307 0 0 0 2280 ...
result:
ok 962 lines
Test #3:
score: 0
Accepted
time: 26ms
memory: 32476kb
input:
5000 5000 1655881 -6171013 -6563069 6407036 -349214 1212406 2942912 -6594577 2008815 -1128329 -2115530 3807690 2938269 3705147 -5102093 -5658055 -2326373 4349357 -299635 825659 877457 3662152 358404 -892346 4685730 5257128 -6518733 -852709 4390588 -3492594 4680660 -386634 1743811 4770445 1735641 -39...
output:
1263 2159 0 107 4 2257 15 1329 172 0 929 0 1465 260 2106 496 685 80 347 1492 0 2638 409 0 177 0 0 0 5 0 2324 0 179 0 0 0 2122 1322 102 412 300 3 1272 0 6 0 1823 0 13 1739 1459 13 1207 278 0 1261 0 684 0 284 639 69 1700 88 243 1529 1970 0 98 2178 0 978 0 1229 1637 1620 660 835 0 0 214 953 203 674 159...
result:
ok 1055 lines
Test #4:
score: 0
Accepted
time: 27ms
memory: 32560kb
input:
5000 5000 1916330 -1277666 -2692038 -1686680 445447 522240 -9102533 4958411 5497747 -6470449 -1304451 -327692 -3951014 -5481922 -71181 -324109 -2416764 -6133434 -2713206 -6507719 -4562989 3531489 2232499 -1303696 3799685 1596321 4508356 -698966 6244189 -3451377 1146222 -1190263 -1224078 -1867687 256...
output:
0 0 0 1099 0 463 0 1182 25 0 784 0 663 1483 2171 0 1057 0 0 1377 0 0 0 218 0 0 70 906 1524 141 89 992 0 1667 0 308 0 77 6 0 148 0 1364 121 513 162 0 1440 1 1588 290 0 380 0 1517 8 1125 0 1362 592 74 0 83 443 857 0 0 0 10 307 1182 6 2352 15 953 31 966 2 536 2104 872 347 0 1846 185 146 1096 0 368 295 ...
result:
ok 1023 lines
Test #5:
score: 0
Accepted
time: 1102ms
memory: 50112kb
input:
200000 200000 3962736 -7195878 -1853269 5312599 2123365 7028251 2312158 123258 -4443494 -5269322 877331 -616200 927434 -1927540 2943960 -7002528 -5869789 2362131 -2612652 -7859698 2076672 3520022 5147597 17824 -54033 5931665 -8193552 4202786 2726128 1137312 5233018 -580693 2414120 4925088 1642992 35...
output:
47152 78705 47731 29708 99910 0 62117 0 44783 61200 5955 2 46232 0 16545 1271 36509 0 90957 1856 0 84922 27751 45838 0 91943 2373 28623 1118 32482 0 3 63630 57801 81142 0 0 0 9458 0 34391 12206 9510 0 0 1284 92189 98017 23372 7 4783 38278 54827 0 0 0 65641 0 13882 16443 908 0 59951 44221 36623 0 897...
result:
ok 200000 lines
Test #6:
score: 0
Accepted
time: 1115ms
memory: 50876kb
input:
200000 200000 -268518 -732548 -901441 -4347722 -192409 4152660 -6078366 -412851 3364009 7704344 323003 -4336546 -1823880 -1675134 -4301206 1362880 -2495946 -7053206 -2570038 2259491 2394744 3832046 -4643805 2294419 -4484291 -7340794 382437 -1938061 4410387 2978726 52156 1753151 -7547313 -5689937 446...
output:
93379 79180 42500 0 34745 0 32820 0 79663 48221 0 3335 12767 0 0 45995 19496 58837 49719 0 642 887 72 0 0 22859 0 0 36154 19833 36991 990 75878 16314 0 21002 50417 81747 39036 34391 65298 0 0 32698 25301 0 39844 14215 8613 23554 79650 4124 25410 8590 60811 0 20040 0 0 52949 308 0 0 61800 22062 45239...
result:
ok 200000 lines
Test #7:
score: 0
Accepted
time: 1081ms
memory: 51604kb
input:
200000 200000 4471605 -2889587 2475793 4794555 5897977 866381 2062774 3733737 -2810691 -3123275 -1593937 543221 -3462221 6330012 -4003863 6366641 2045393 1616425 -6199720 8342761 -855613 -5496332 2625258 -1455774 -6907661 -3748453 -831288 1686462 -5278073 3138676 5730165 2039292 694348 -1314529 -173...
output:
0 421 0 0 9681 7022 7214 0 0 27527 0 31966 0 3457 68018 29401 0 0 55150 3666 3275 1874 30536 0 12447 41 35035 12705 0 5 13675 12373 0 0 52061 77962 1654 0 0 41006 6436 0 3624 3567 35984 66500 70 32054 0 21039 12570 0 0 0 19152 70428 12137 42842 0 0 0 2763 0 38788 43503 52273 6312 12519 86158 39300 0...
result:
ok 200000 lines
Test #8:
score: 0
Accepted
time: 1094ms
memory: 50076kb
input:
200000 200000 -890750 -1886888 4557806 -644020 494194 -2695808 2550714 4029509 1120191 -1432068 4947569 -861074 -5012051 3130809 5048423 2911722 3555406 -4902160 6599828 -5543160 4984807 -184407 -3457542 -7797283 -1069734 1904721 5103428 1063345 3476923 -1785030 4855156 -57227 -5334995 -2809093 2892...
output:
2 0 0 3040 0 28031 83220 20474 0 0 0 50236 95642 0 0 8070 0 117 0 893 2792 0 34697 79143 62228 47204 21758 0 30780 86096 5630 0 0 5183 0 30315 2222 21205 0 15214 24683 0 42490 11122 16553 0 19432 33366 0 28181 76378 13080 95166 4137 56659 2718 0 23634 16356 0 22985 42488 0 7949 0 44522 5532 0 6735 1...
result:
ok 200000 lines
Test #9:
score: -100
Time Limit Exceeded
input:
200000 200000 -3731977 -2214890 -2655693 -4636219 1477879 -6652495 -6122932 -2086233 195642 -707975 -1089540 3930278 524808 653005 7012999 -4338192 -7000512 -4443331 338552 2677375 -9308411 -1663420 3989374 5517790 405209 -2349520 4825993 8728231 4180825 -762268 -5023792 -1018659 -4224802 4763633 -6...
output:
5229 34782 25115 2661 50096 0 39612 26827 0 28126 2078 0 0 34407 9531 0 0 20761 0 0 47962 9851 369 0 120138 0 2 93381 0 2660 26702 47790 0 0 26704 26895 81137 0 3679 0 45393 0 14777 41981 0 7090 71613 6156 20774 3562 0 282 655 7528 15837 3 29871 0 40192 28220 0 12640 0 0 0 0 50855 47380 1280 1829 39...