QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#858506 | #9676. Ancestors | XY_Eleven | 0 | 13ms | 21500kb | C++20 | 6.3kb | 2025-01-16 17:57:53 | 2025-01-16 17:57:55 |
Judging History
answer
#include <bits/stdc++.h>
// #include <windows.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
//#pragma GCC optimize(3)
#define DB double
#define LL long long
#define ULL unsigned long long
#define in128 __int128
#define cint const int
#define cLL const LL
#define For(z,e1,e2) for(int z=(e1);z<=(e2);z++)
#define Rof(z,e1,e2) for(int z=(e2);z>=(e1);z--)
#define For_(z,e1,e2) for(int z=(e1);z<(e2);z++)
#define Rof_(z,e1,e2) for(int z=(e2);z>(e1);z--)
#define inint(e) scanf("%d",&e)
#define inll(e) scanf("%lld",&e)
#define inpr(e1,e2) scanf("%d%d",&e1,&e2)
#define in3(e1,e2,e3) scanf("%d%d%d",&e1,&e2,&e3)
#define outint(e) printf("%d\n",e)
#define outint_(e) printf("%d%c",e," \n"[i==n])
#define outint2_(e,e1,e2) printf("%d%c",e," \n"[(e1)==(e2)])
#define outll(e) printf("%lld\n",e)
#define outll_(e) printf("%lld%c",e," \n"[i==n])
#define outll2_(e,e1,e2) printf("%lld%c",e," \n"[(e1)==(e2)])
#define exc(e) if(e) continue
#define stop(e) if(e) break
#define ret(e) if(e) return
#define ll(e) (1ll*(e))
#define pb push_back
#define ft first
#define sc second
#define pii pair<int,int>
#define pli pair<long long,int>
#define vct vector
#define clean(e) while(!e.empty()) e.pop()
#define all(ev) ev.begin(),ev.end()
#define sz(ev) ((int)ev.size())
#define debug(x) printf("%s=%d\n",#x,x)
#define x0 __xx00__
#define y1 __yy11__
#define ffo fflush(stdout)
cLL mod=998244353,G=404;
// cLL mod[2]={1686688681ll,1666888681ll},base[2]={166686661ll,188868881ll};
template <typename Type> void get_min(Type &w1,const Type w2) { if(w2<w1) w1=w2; } template <typename Type> void get_max(Type &w1,const Type w2) { if(w2>w1) w1=w2; }
template <typename Type> Type up_div(Type w1,Type w2) { return (w1/w2+(w1%w2?1:0)); }
template <typename Type> Type gcd(Type X_,Type Y_) { Type R_=X_%Y_; while(R_) { X_=Y_; Y_=R_; R_=X_%Y_; } return Y_; } template <typename Type> Type lcm(Type X_,Type Y_) { return (X_/gcd(X_,Y_)*Y_); }
template <typename Type> Type md(Type w1,const Type w2=mod) { w1%=w2; if(w1<0) w1+=w2; return w1; } template <typename Type> Type md_(Type w1,const Type w2=mod) { w1%=w2; if(w1<=0) w1+=w2; return w1; }
void ex_gcd(LL &X_,LL &Y_,LL A_,LL B_) { if(!B_) { X_=1ll; Y_=0ll; return ; } ex_gcd(Y_,X_,B_,A_%B_); X_=md(X_,B_); Y_=(1ll-X_*A_)/B_; } LL inv(LL A_,LL B_=mod) { LL X_=0ll,Y_=0ll; ex_gcd(X_,Y_,A_,B_); return X_; }
template <typename Type> void add(Type &w1,const Type w2,const Type M_=mod) { w1=md(w1+w2,M_); } void mul(LL &w1,cLL w2,cLL M_=mod) { w1=md(w1*md(w2,M_),M_); } template <typename Type> Type pw(Type X_,Type Y_,Type M_=mod) { Type S_=1; while(Y_) { if(Y_&1) mul(S_,X_,M_); Y_>>=1; mul(X_,X_,M_); } return S_; }
template <typename Type> Type bk(vector <Type> &V_) { auto T_=V_.back(); V_.pop_back(); return T_; } template <typename Type> Type tp(stack <Type> &V_) { auto T_=V_.top(); V_.pop(); return T_; } template <typename Type> Type frt(queue <Type> &V_) { auto T_=V_.front(); V_.pop(); return T_; }
template <typename Type> Type bg(set <Type> &V_) { auto T_=*V_.begin(); V_.erase(V_.begin()); return T_; } template <typename Type> Type bk(set <Type> &V_) { auto T_=*prev(V_.end()); V_.erase(*prev(V_.end())); return T_; }
mt19937 gen(time(NULL)); int rd() { return abs((int)gen()); }
int rnd(int l,int r) { return rd()%(r-l+1)+l; }
void main_init()
{
}
cint N=1.02e5,Q_=1.02e6;
int n,Q;
vct <int> v[N];
int fa[N];
vct <array<int,3> > q[N];
int mxdep[N],to[N];
vct <array<int,2> > will_mer[N];
void dfs(int p,int dep)
{
will_mer[dep].pb({0,p});
ret(v[p].empty());
int k=v[p][0];
for(auto i:v[p])
{
dfs(i,dep+1);
get_max(mxdep[p],mxdep[i]+1);
if(mxdep[i]>mxdep[k]) k=i;
}
to[p]=k;
vct <int> h=v[p];
int tot=0;
while(!h.empty())
{
tot++;
int len=sz(h);
For_(i,0,len-1) will_mer[tot].pb({h[i],h[i+1]});
vct <int> h2;
for(auto i:h) if(to[i]) h2.pb(to[i]);
h=move(h2);
}
}
int uf[N];
set <int> st[N];
int fin(int w)
{
return ((w==uf[w])?w:(uf[w]=fin(uf[w])));
}
struct BIT
{
tree <int,null_type,greater<int>,rb_tree_tag,tree_order_statistics_node_update> tr[N];
void ins(int l,int r)
{
for(int i=l;i<=n;i+=(i&-i))
tr[l].insert(r);
}
void ers(int l,int r)
{
for(int i=l;i<=n;i+=(i&-i))
tr[l].erase(r);
}
int qry(int l,int r)
{
int sum=0;
r--;
for(int i=l;i;i^=(i&-i))
sum+=tr[i].order_of_key(r);
return sum;
}
}c;
void ins(int l,int r)
{
l++,r--; ret(l>r);
c.ins(l,r);
}
void ers(int l,int r)
{
l++,r--; ret(l>r);
c.ers(l,r);
}
void clr(int w)
{
st[w].erase(st[w].begin());
int lst=0;
for(auto iter:st[w])
{
ers(lst,iter);
lst=iter;
}
}
bool mer(int w1,int w2)
{
w1=fin(w1),w2=fin(w2);
// printf("merge %d %d\n",w1,w2);
if(w1==w2) return false;
if((!w1)||(!w2))
{
w1|=w2;
uf[w1]=0;
clr(w1);
return true;
}
if(sz(st[w1])<sz(st[w2])) swap(w1,w2);
clr(w2);
st[w2].erase(prev(st[w2].end()));
for(auto x:st[w2])
{
auto iter=st[w1].insert(x).ft;
int l=*prev(iter),r=*next(iter);
ers(l,r),ins(l,x),ins(x,r);
}
uf[w2]=w1;
return true;
}
int ans[Q_];
void main_solve()
{
inpr(n,Q);
For(i,1,n) inint(fa[i]),v[fa[i]].pb(i);
For(id,1,Q)
{
int l,r,x; in3(l,r,x);
q[x].pb({l,r,id});
}
dfs(0,0);
For(i,1,n)
{
uf[i]=i;
st[i].insert(0); st[i].insert(i); st[i].insert(n+1);
ins(0,i),ins(i,n+1);
}
int now=n;
For(i,1,n)
{
// printf("i=%d\n",i);
for(auto [x,y]:will_mer[i])
now-=mer(x,y);
// printf("> now=%d\n",now);
for(auto [l,r,id]:q[i])
ans[id]=now-c.qry(l,r);
}
For(i,1,Q) outint(ans[i]);
}
int main()
{
// ios::sync_with_stdio(0); cin.tie(0);
// freopen("ex_block4.in","r",stdin);
// freopen("out.txt","w",stdout);
// srand(time(NULL));
main_init();
// int _; inint(_); For(__,1,_) // T>1 ?
// printf("\n------------\n\n"),
main_solve();
// cerr<<clock()<<'\n';
return 0;
}
/*
*/
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 2ms
memory: 20992kb
input:
7 5 3 1 0 5 3 5 1 1 3 1 5 7 2 1 5 1 4 7 1 4 7 2
output:
2 1 3 3 1
result:
ok 5 number(s): "2 1 3 3 1"
Test #2:
score: 0
Wrong Answer
time: 13ms
memory: 21500kb
input:
1000 1000 686 337 192 336 405 0 108 485 350 762 258 780 179 939 25 657 571 662 119 786 604 224 935 494 685 575 369 178 249 740 954 204 598 592 68 771 498 86 55 38 298 704 239 292 993 286 16 813 719 187 14 476 792 49 944 52 227 720 310 470 900 243 663 950 627 300 728 189 45 610 673 548 873 95 48 841 ...
output:
467 72 670 142 390 600 462 35 779 853 291 435 982 208 932 592 347 676 45 757 472 677 581 622 735 744 268 639 964 728 405 15 57 10 9 16 87 162 19 217 232 24 178 334 103 139 293 400 299 351 529 632 592 296 640 678 715 708 52 465 322 731 440 570 985 488 806 326 275 528 17 352 527 430 77 397 693 419 679...
result:
wrong answer 1st numbers differ - expected: '452', found: '467'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #30:
score: 0
Time Limit Exceeded
input:
50000 200000 42574 43129 47328 17982 40521 6668 12729 32377 201 11940 8599 11734 18349 41045 26854 22540 9897 33419 7463 1243 47272 27135 49050 49111 22435 42539 39924 20272 5843 9308 45963 3283 31185 13692 38952 20583 15885 24802 4773 953 49907 28689 36942 23550 19449 8970 33340 31665 5407 46023 18...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Time Limit Exceeded
Test #67:
score: 0
Time Limit Exceeded
input:
100000 1000000 6457 23693 90928 23592 90440 75018 16865 3342 83718 16731 95103 31510 38719 27886 29093 41955 6596 46409 51839 10527 91993 61074 14405 34833 53674 42363 11490 43757 46191 6058 59164 96938 57858 40178 97523 84164 21582 72243 11267 47368 97058 6637 95208 60092 53943 16441 28363 64965 52...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%