QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#863386 | #9676. Ancestors | lichenghan | 0 | 401ms | 74884kb | C++17 | 5.1kb | 2025-01-19 16:32:47 | 2025-01-19 16:32:47 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define USE_FREAD_FWRITE
#define READ_NEGATIVE
#define WRITE_NEGATIVE
#define DEFAULT_TYPE int
namespace IO{
#ifdef USE_FREAD_FWRITE
#define SIZE (1<<20)
char in[SIZE],out[SIZE],*p1=in,*p2=in,*p3=out;
char getchar(){ return p1==p2&&(p2=(p1=in)+fread(in,1,SIZE,stdin),p1==p2)?EOF:*p1++; }
void flush(){ int len=p3-out; fwrite(p3=out,1,len,stdout); }
void putchar(char ch){ if(p3==out+SIZE)flush(); *p3++=(ch); }
struct Flush{~Flush(){flush();}}_;
#else
char getchar(){ return ::getchar(); }
void putchar(char ch){ ::putchar(ch); }
void flush(){}
#endif
template<typename type=DEFAULT_TYPE> inline type read(){
#ifdef READ_NEGATIVE
type x(0);bool flag(0);char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())flag^=ch=='-';
for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
return flag?-x:x;
#else
type x(0);char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
return x;
#endif
}
template<typename type>
inline void write(type x){
#ifdef WRITE_NEGATIVE
if(x<0)x=-x,putchar('-');
#endif
static int Stack[50],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
}
}
using IO::read;
using IO::write;
const int N=1e5+10,M=1e6+10;
int n,m;
vector<int> g[N];
int fa[N],dep[N],dfn[N],ord[N],dfcnt;
vector<int> rdep[N];
int hpre[N];
void dfs(int u){
dep[u]=dep[fa[u]]+1;
if(!rdep[dep[u]].empty()) hpre[u]=rdep[dep[u]].back();
rdep[dep[u]].push_back(u);
dfn[u]=++dfcnt;
ord[dfcnt]=u;
for(int v:g[u]) dfs(v);
}
struct lca_t{
int st[21][N];
inline int choose(int u,int v) const { return dep[u]<dep[v]?u:v; }
inline void init(){
for(int i=1;i<=n;i++) st[0][i]=ord[i];
for(int j=1;j<=20;j++){
auto lst=st[j-1],cur=st[j];
for(int i=1;i<=n-(1<<j)+1;i++) {
cur[i]=choose(lst[i],lst[i+(1<<j>>1)]);
}
}
}
inline int qst(int l,int r) const {
int len=31^__builtin_clz(r-l+1);
return choose(st[len][l],st[len][r-(1<<len)+1]);
}
inline int operator()(int u,int v) const {
if(dfn[u]>dfn[v]) swap(u,v);
int lc=qst(dfn[u],dfn[v]);
if(lc==u) return u;
else return fa[lc];
}
}lca;
struct DS{
struct BIT{
// O(1) prefix add, O(sqrt) pt query
int t1[N],t2[(N>>6)+2],t3[(N>>12)+2];
inline void c(int x,int d){
// printf("c %d %d\n",x,d);
x^=131071;
t1[x]+=d; t2[x>>6]+=d; t3[x>>12]+=d;
}
inline void clear(){
// puts("clear");
memset(this,0,sizeof(BIT));
}
inline int q(int x){
x^=131071;
int ans=0;
for(int i=0;i<(x>>12);i++) ans+=t3[i];
for(int i=(x>>12)<<6;i<(x>>6);i++) ans+=t2[i];
for(int i=(x>>6)<<6;i<=x;i++) ans+=t1[i];
return ans;
}
}T;
int lst[N],nxt[N];
int dlst[N],dnxt[N];
void init(int l,int r){
for(int i=0;i<=n;i++) nxt[i]=lst[i]=0;
// O(n)
T.clear();
for(int i=1;i<=n;i++){
int tmp=0,dtmp=0;
for(int u:rdep[i]) {
if(hpre[u]!=0) dtmp=max(dtmp,hpre[u]);
if(l<=u&&u<=r) {
if(tmp) nxt[tmp]=u,dnxt[tmp]=dtmp;
lst[u]=tmp,dlst[u]=dtmp;
int ht=dtmp-1;
if(!lst[u]) ht=dep[u]-1;
T.c(ht,1);
// printf("%d lst=%d ht=%d\n",u,tmp,ht);
tmp=u; dtmp=0;
}
}
}
for(int i=1;i<=n;i++) {
if(!lst[i]) dlst[i]=dep[i];
if(!nxt[i]) dnxt[i]=dep[i];
}
topx=topy=0;
}
pair<int*,int> stk[M]; int topx;
pair<int,int> stt[M]; int topy;
inline void mv(int&x,int y){ stk[++topx]={&x,x}; x=y; }
inline void ct(int x,int y){ T.c(x,y); stt[++topy]={x,-y}; }
inline void remove(int x){
// O(1) non-amortized
// printf("remove %d lst %d nxt %d ht %d\n",x,lst[x],nxt[x],ht);
ct(min(dlst[x],dnxt[x])-1,-1);
if(dlst[x]<dnxt[x]) mv(dnxt[lst[x]],dnxt[x]);
else mv(dlst[nxt[x]],dlst[x]);
if(nxt[x]) mv(lst[nxt[x]],lst[x]);
if(lst[x]) mv(nxt[lst[x]],nxt[x]);
}
inline void undo(int rx,int ry){
// O(1)
while(topx>rx){
*stk[topx].first=stk[topx].second;
topx--;
}
while(topy>ry){
T.c(stt[topy].first,stt[topy].second);
topy--;
}
}
inline int query(int x){
return T.q(x);
}
}T;
mt19937 e(time(0));
const int B=101,R=N/B+3;
struct query_t{
int l,r,x,id;
};
int ans[M];
vector<query_t> bq[R];
int bl[R];
int main(){
n=read(); m=read();
int rt=0;
for(int i=1;i<=n;i++){
fa[i]=read();
if(!fa[i]) rt=i;
else g[fa[i]].push_back(i);
}
dfs(rt);
lca.init();
for(int i=1;i<=n;i++) if(hpre[i]) hpre[i]=dep[i]-dep[lca(i,hpre[i])];
for(int i=0;i<R;i++) bl[i]=1+i*B;
for(int i=1;i<=m;i++){
int l,r,x;
l=read(); r=read(); x=read();
int bid=upper_bound(bl,bl+R,l)-1-bl;
bq[bid].push_back({l,r,x,i});
}
for(int i=0;i<R;i++) if(bq[i].size()) {
sort(bq[i].begin(),bq[i].end(),[&](const query_t& x,const query_t& y){
return x.r>y.r;
});
T.init(bl[i],n);
int lstr=n;
for(query_t q:bq[i]){
while(lstr>q.r) T.remove(lstr--);
int rx=T.topx,ry=T.topy;
for(int j=bl[i];j<q.l;j++) T.remove(j);
ans[q.id]=T.query(q.x);
T.undo(rx,ry);
}
}
for(int i=1;i<=m;i++){
write(ans[i]); IO::putchar('\n');
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 38600kb
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:
4 8 5 10 8
result:
wrong answer 1st numbers differ - expected: '2', found: '4'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 0
Wrong Answer
time: 401ms
memory: 57408kb
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:
13520 2619 13762 3754 34228 38389 40472 32233 17983 18169 49095 43049 24989 3033 20101 8177 28123 31409 10278 17781 40368 31648 23619 8978 5100 36538 19079 7779 14041 24951 13041 17953 26014 12877 5192 3427 39597 14430 30704 25673 40939 36396 39065 22314 13114 43049 46221 19250 24963 28287 44931 431...
result:
wrong answer 1st numbers differ - expected: '12045', found: '13520'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #67:
score: 0
Wrong Answer
time: 212ms
memory: 74884kb
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:
54192 19365 1345 13988 11510 544 48633 68694 6967 3730 51762 11428 76 4841 43 10122 40690 1191 56195 74678 2765 46239 5390 7162 -1946 4438 53542 31225 61556 38145 30577 2939 7079 66167 20214 49087 10197 46351 27321 7359 2565 21496 3127 35517 4528 2662 4917 7696 39290 -4719 3742 62225 28813 44993 206...
result:
wrong answer 1st numbers differ - expected: '52956', found: '54192'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%