QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#863428 | #9676. Ancestors | l_rANd | 0 | 311ms | 34964kb | C++20 | 3.2kb | 2025-01-19 17:03:10 | 2025-01-19 17:03:31 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int read(){
int ret=0,f=1;char c=getchar();
while(c>57||c<48){if(c=='-')f= -1;c=getchar();}
while(c<=57&&c>=48)ret=(ret<<3)+(ret<<1)+(c-48),c=getchar();
return ret*f;
}
void write(int x){
if(x<0) x= -x,putchar('-');
if(x>9)write(x/10);
putchar(x%10+48);
}
const int maxn=2e5+5;
const int mod =1e9+7;
int n,m,r,ans[maxn];
vector<int>G[maxn];
int siz[maxn],son[maxn],dep[maxn],dfn[maxn],rnk[maxn],dcnt;
struct node{int x,y,z,t,opt;}cn[maxn];int ccnt=0;
vector<pair<int,int> >ud[maxn];
void dfs1(int u,int f){
dep[u]=dep[f]+1;dfn[u]=++dcnt;rnk[dcnt]=u;siz[u]=1;
for(int v:G[u]){
dfs1(v,u);
siz[u]+=siz[v];
if(siz[son[u]]<siz[v])son[u]=v;
}
}
set<int>s[maxn];
void dfs2(int u){
for(int v:G[u]){
if(v==son[u])continue;
dfs2(v);
for(int i=dfn[v];i<dfn[v]+siz[v];i++)s[dep[rnk[i]]].erase(rnk[i]);
}
if(son[u])dfs2(son[u]);
for(int v:G[u]){
if(v==son[u])continue;
for(int i=dfn[v];i<dfn[v]+siz[v];i++){
int x=rnk[i];
auto it=s[dep[x]].lower_bound(x);
if(it!=s[dep[x]].end())ud[*it].push_back({x,dep[x]-dep[u]});
if(it!=s[dep[x]].begin())ud[x].push_back({*--it,dep[x]-dep[u]});
s[dep[x]].insert(x);
}
}
s[dep[u]].insert(u);
}
bool cmp1(node x,node y){
if(x.x!=y.x)return x.x>y.x;
if(x.y!=y.y)return x.y<y.y;
if(x.z!=y.z)return x.z<y.z;
return x.opt<y.opt;
}
bool cmp2(node x,node y){return x.y>y.y;}
int c[maxn];
inline int lowbit(int x){return x&-x;}
void add(int x,int k){for(;x<=n;x+=lowbit(x))c[x]+=k;}
int query(int x){int res=0;for(;x;x-=lowbit(x))res+=c[x];return res;}
void clear(int x){for(;x<=n;x+=lowbit(x))c[x]=0;}
void cdq(int l,int r){
if(l>=r)return ;
int mid=l+r>>1;
cdq(l,mid),cdq(mid+1,r);
sort(cn+l,cn+mid+1,cmp2),sort(cn+mid+1,cn+r+1,cmp2);
for(int i=mid+1,j=l;i<=r;i++){
if(cn[i].opt==0)continue;
while(j<=mid&&cn[j].y<=cn[i].y){
if(cn[j].opt==0)add(cn[j].z,cn[j].t);
j++;
}
ans[cn[i].t]-=query(cn[i].z);
}
for(int i=l;i<=mid;i++)clear(cn[i].z);
}
void solve(){
n=read(),m=read();
for(int i=1;i<=n;i++){
int x=read();
if(x==0)r=i;
G[x].push_back(i);
}
for(int i=1;i<=m;i++){
int l=read(),r=read(),x=read();
cn[++ccnt]={l,r,x,i,1};
ans[i]+=r-l+1;
}
dfs1(r,0);
dfs2(r);
for(int i=1;i<=n;i++)ud[i].push_back({i,dep[i]});
for(int i=1;i<=n;i++){
sort(ud[i].begin(),ud[i].end());
reverse(ud[i].begin(),ud[i].end());
int mn=n+1;
for(auto[x,y]:ud[i]){
if(y>=mn)continue;
if(mn<=n)cn[++ccnt]={x,i,mn,-1,0};
cn[++ccnt]={x,i,y,1,0};
mn=y;
}
}
// for(int i=1;i<=ccnt;i++){
// cerr<<cn[i].opt<<' '<<cn[i].x<<' '<<cn[i].y<<' '<<cn[i].z<<' '<<cn[i].t<<endl;
// }
sort(cn+1,cn+ccnt+1,cmp1);
cdq(1,ccnt);
for(int i=1;i<=m;i++)write(ans[i]),putchar('\n');
}
signed main(){
int T=1;
while(T--)solve();
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 1ms
memory: 14084kb
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: 3ms
memory: 14280kb
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:
452 67 611 126 329 486 354 25 559 585 184 265 576 116 489 289 147 287 13 282 151 192 146 141 148 131 43 72 69 29 5 -497 -6 -772 -783 -718 -356 -48 -657 64 71 -563 -146 245 -304 -219 97 297 104 194 480 617 551 135 606 654 699 684 -23 441 312 737 -85 -17 106 145 85 -85 -82 -370 29 98 -306 -170 -370 -1...
result:
wrong answer 32nd numbers differ - expected: '15', found: '-497'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 0
Wrong Answer
time: 311ms
memory: 34964kb
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:
12045 1321 12292 2444 32443 36534 38575 30505 16464 16648 47153 41144 23401 1762 18567 6831 26486 29716 8905 16295 38569 29990 22061 7638 3820 34754 17582 6466 12632 23384 11657 16493 24434 11503 3945 2205 37806 13036 29052 24114 39158 34702 37326 20815 11766 41253 44355 17807 23452 26717 43107 4133...
result:
wrong answer 448th numbers differ - expected: '97', found: '-7280'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Runtime Error
Test #67:
score: 0
Runtime Error
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%