QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#885844#10053. Post OfficeLyz090 156ms107668kbC++144.8kb2025-02-06 18:53:472025-02-06 18:53:59

Judging History

This is the latest submission verdict.

  • [2025-02-06 18:53:59]
  • Judged
  • Verdict: 0
  • Time: 156ms
  • Memory: 107668kb
  • [2025-02-06 18:53:47]
  • Submitted

answer

#include<iostream>
#include<vector>
using namespace std;
#define N 200010
int n,to[N],m,a[N],b[N],ans;
int qwq,cid[N],td[N],cnt,dep[N],dfn[N],ed[N];
bool vis[N],ins[N],inc[N];
vector<int> g[N],pi[N],pe[N],qi[N],qe[N];
struct node
{
	int len,sum,ma;
	friend node operator+(node a,node b)
	{
		return (node){a.len+b.len,a.sum+b.sum,max(a.ma-b.len+b.sum,b.ma)};
	}
};
class sgtree
{
	public:
		#define sN 30000010
		int cnt,rt[N],ls[sN],rs[sN],qwq=20;
		node a[sN];
		void pushdown(int o,int l,int r)
		{
			if(l==r||ls[o]||rs[o])
			 return;
			int mid=l+r>>1;
			ls[o]=++cnt;
			a[ls[o]]=(node){mid-l+1,0,0};
			rs[o]=++cnt;
			a[rs[o]]=(node){r-mid,0,0};
		}
		void upd(int o,int l,int r)
		{
			int mid=l+r>>1;
			node w1=(node){mid-l+1,0,0};
			node w2=(node){r-mid,0,0};
			if(ls[o])
			 w1=a[ls[o]];
			if(rs[o])
			 w2=a[rs[o]];
			a[o]=w1+w2;
		}
		int update(int o,int l,int r,int x,int k)
		{
//			cerr<<"??"<<l<<" "<<x<<" "<<r<<"|"<<o<<endl;
			if(l>x||r<x||!o)
			 return o;
//			cerr<<"ind:"<<o<<" "<<l<<" "<<r<<"|"<<a[o].len<<endl;
			int d=++cnt;
			a[d]=a[o];
			if(l==r)
			{
				a[d].sum+=k;
				a[d].ma=max(a[d].sum,0);
				return d;
			}
			pushdown(o,l,r);
			int mid=l+r>>1;
			ls[d]=update(ls[o],l,mid,x,k);
			rs[d]=update(rs[o],mid+1,r,x,k);
			upd(d,l,r);
			return d;
		}
		int add(int o,int x,int k)
		{
			int tmp=update(o,1,qwq,x,k);
//			cerr<<"add "<<o<<" "<<x<<" "<<k<<"::"<<tmp<<"|"<<a[tmp].len<<" "<<a[tmp].sum<<endl;
			return tmp;
		}
		int merge(int o1,int o2,int l,int r)
		{
			if(!o1||!o2)
			 return o1+o2;
			if(l==r)
			{
				a[o1].sum+=a[o2].sum;
				a[o1].ma=max(a[o1].sum,0);
				return o1;
			}
			int mid=l+r>>1;
			ls[o1]=merge(ls[o1],ls[o2],l,mid);
			rs[o1]=merge(rs[o1],rs[o2],mid+1,r);
			upd(o1,l,r);
			return o1;
		}
		int Merge(int x,int y)
		{
			return merge(x,y,1,qwq);
		}
		node query(int o,int l,int r,int x,int y)
		{
			if(l>y||r<x)
			 return (node){0,0,0};
			if(!o)
			{
//				cerr<<"empty"<<" "<<l<<" "<<y<<"||"<<min(y,r)-max(l,x)+1<<endl;
				return (node){min(y,r)-max(l,x)+1,0,0};
			}
			if(l>=x&&r<=y)
			{
//				cerr<<o<<" "<<l<<" "<<y<<"||"<<a[o].len<<" "<<a[o].sum<<" "<<a[o].ma<<endl;
				return a[o];
			}
			int mid=l+r>>1;
			node q1=query(ls[o],l,mid,x,y);
			node q2=query(rs[o],mid+1,r,x,y);
//			cerr<<o<<" "<<l<<" "<<r<<"|"<<(q1+q2).len<<" "<<(q1+q2).sum<<endl;
			return q1+q2;
		}
		int ask(int rt)
		{
//			cerr<<"UUU:"<<a[rt].sum<<endl;
			int l=0,r=qwq,ans=qwq;
			while(l<=r)
			{
				int mid=l+r>>1;
				node res=query(rt,1,qwq,1,mid);
//				cerr<<"umnik"<<mid<<"::"<<res.len<<" "<<res.sum<<" "<<res.ma<<endl;
				if(res.sum==a[rt].sum&&res.ma<=0)
				{
					ans=mid;
					r=mid-1;
				}
				else
				{
					l=mid+1;
				}
			}
			return ans;
		}
		int init()
		{
			int u=++cnt;
			ls[u]=0;
			rs[u]=0;
			a[u]=(node){qwq,0,0};
			return u;
		}
}tr;
void dfs0(int u)
{
	vis[u]=1;
	ins[u]=1;
	if(ins[to[u]])
	 inc[to[u]]=1;
	if(vis[to[u]])
	{
		ins[u]=0;
		return;
	}
	dfs0(to[u]);
	ins[u]=0;
}
void dfs1(int u)
{
	dfn[u]=++cnt;
	for(int v:g[u])
	{
		cid[v]=cid[u];
		dep[v]=dep[u]+1;
		td[v]=td[u];
		dfs1(v);
	}
	ed[u]=cnt;
}
void init()
{
	for(int i=1;i<=n;i++)
	if(!vis[i])
	 dfs0(i);
	for(int i=1;i<=n;i++)
	if(inc[i]&&!inc[to[i]])
	{
		cid[i]=++qwq;
		int now=to[i];
		while(!inc[now])
		{
			inc[now]=1;
			cid[now]=cid[i];
			now=to[now];
		}
	}
	for(int i=1;i<=n;i++)
	if(!inc[i])
	 g[to[i]].push_back(i);
	for(int i=1;i<=n;i++)
	if(inc[i])
	{
		td[i]=i;
		dfs1(i);
	}
}
bool check(int x,int y)
{
	if(cid[x]!=cid[y])
	 return 0;
	if(!inc[y]&&!(dfn[x]>=dfn[y]&&dfn[x]<=ed[y]))
	 return 0;
	return 1;
}
void dfs2(int u)
{
	if(!g[u].size())
	 tr.rt[u]=tr.init();
	for(int v:g[u])
	{
		dfs2(v);
		tr.rt[u]=tr.Merge(tr.rt[u],tr.rt[v]);
	}
	for(int x:pi[u])
	{
//		cerr<<u<<" pi:"<<x<<" |"<<dep[a[x]]<<endl;
		tr.rt[u]=tr.add(tr.rt[u],dep[a[x]],1);
	}
	for(int x:pe[u])
	{
//		cerr<<u<<" pe:"<<x<<endl;
		tr.rt[u]=tr.add(tr.rt[u],dep[a[x]],-1);
	}
	int w=tr.ask(tr.rt[u]);
//	cerr<<u<<"-->"<<w<<"-"<<dep[u]<<" = "<<w-dep[u]<<endl;
//	cerr<<"-------------"<<endl;
	ans=max(ans,w-dep[u]);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)
	 cin>>to[i];
	init();
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>a[i]>>b[i];
		if(!check(a[i],b[i]))
		{
			cout<<"-1"<<endl;
			return 0;
		}
		if(!inc[a[i]])
		 pi[a[i]].push_back(i);
		if(!inc[b[i]])
		 pe[b[i]].push_back(i);
		else if(!inc[a[i]])
		 pe[td[a[i]]].push_back(i);
		if(inc[b[i]])
		{
			qi[td[a[i]]].push_back(i);
			qe[b[i]].push_back(i);
		}
	}
	for(int i=1;i<=n;i++)
	if(inc[i])
	 dfs2(i);
	cout<<ans<<endl;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 5ms
memory: 38744kb

input:

3000
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1...

output:

0

result:

wrong answer 1st lines differ - expected: '1119', found: '0'

Subtask #2:

score: 0
Wrong Answer

Test #8:

score: 9
Accepted
time: 3ms
memory: 36468kb

input:

5
2 1 1 3 1
5
1 2
4 1
5 1
3 1
4 1

output:

3

result:

ok single line: '3'

Test #9:

score: 9
Accepted
time: 2ms
memory: 36600kb

input:

5
2 1 2 3 3
5
3 2
5 2
5 1
4 2
2 1

output:

4

result:

ok single line: '4'

Test #10:

score: 9
Accepted
time: 2ms
memory: 36544kb

input:

5
2 1 1 2 4
5
5 1
4 1
3 2
5 2
4 2

output:

4

result:

ok single line: '4'

Test #11:

score: 0
Wrong Answer
time: 2ms
memory: 38636kb

input:

5
2 1 1 1 1
5
1 2
3 1
2 1
4 2
2 1

output:

1

result:

wrong answer 1st lines differ - expected: '2', found: '1'

Subtask #3:

score: 0
Wrong Answer

Test #34:

score: 0
Wrong Answer
time: 28ms
memory: 88640kb

input:

2
1 1
200000
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1...

output:

19

result:

wrong answer 1st lines differ - expected: '200000', found: '19'

Subtask #4:

score: 0
Wrong Answer

Test #41:

score: 0
Wrong Answer
time: 94ms
memory: 98012kb

input:

200000
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...

output:

0

result:

wrong answer 1st lines differ - expected: '199401', found: '0'

Subtask #5:

score: 0
Wrong Answer

Test #51:

score: 0
Wrong Answer
time: 14ms
memory: 40012kb

input:

2
2 1
200000
2 1
2 1
1 2
2 1
1 2
1 2
2 1
1 2
1 2
2 1
2 1
1 2
2 1
2 1
1 2
1 2
2 1
1 2
1 2
2 1
2 1
1 2
1 2
1 2
2 1
2 1
2 1
2 1
1 2
2 1
2 1
2 1
1 2
2 1
1 2
1 2
1 2
1 2
2 1
2 1
2 1
2 1
1 2
2 1
1 2
1 2
1 2
2 1
1 2
1 2
1 2
1 2
2 1
2 1
2 1
1 2
1 2
1 2
2 1
2 1
1 2
1 2
1 2
1 2
1 2
1 2
1 2
2 1
1 2
1 2
2 1
2 1...

output:

0

result:

wrong answer 1st lines differ - expected: '100031', found: '0'

Subtask #6:

score: 0
Wrong Answer

Test #60:

score: 0
Wrong Answer
time: 156ms
memory: 107668kb

input:

200000
1 1 1 3 3 2 5 3 4 6 9 3 3 9 14 13 4 2 18 9 3 11 20 13 7 13 14 6 13 2 22 14 5 9 19 7 28 22 10 37 37 26 15 39 18 31 18 19 22 6 4 22 29 30 43 38 33 39 19 10 14 25 35 5 3 50 34 13 60 44 31 47 67 27 52 26 48 30 18 63 76 80 49 16 39 16 59 77 60 26 84 50 54 36 75 77 72 77 1 45 13 20 86 19 56 9 47 82...

output:

19

result:

wrong answer 1st lines differ - expected: '119708', found: '19'

Subtask #7:

score: 0
Wrong Answer

Test #97:

score: 0
Wrong Answer
time: 142ms
memory: 88848kb

input:

200000
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101...

output:

19

result:

wrong answer 1st lines differ - expected: '100667', found: '19'