QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#298209#4425. CakePhantomThreshold#AC ✓3868ms86716kbC++201.6kb2024-01-05 20:15:472024-01-05 20:15:48

Judging History

你现在查看的是最新测评结果

  • [2024-01-05 20:15:48]
  • 评测
  • 测评结果:AC
  • 用时:3868ms
  • 内存:86716kb
  • [2024-01-05 20:15:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		vector<int> a(n+5),b(n+5);
		vector<int> p(n+5),q(n+5);
		map<pair<int,int>,int> mp;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		int ind=0;
		for(int i=1;i<=n;i++)
		{
			cin>>b[i];
			if(i%2==0)swap(a[i],b[i]);
			if(not mp.contains({a[i],b[i]}))
			{
				mp[{a[i],b[i]}]=++ind;
			}
			p[i]=mp[{a[i],b[i]}];
		}
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		for(int i=1;i<=n;i++)
		{
			cin>>b[i];
			if(i%2==0)swap(a[i],b[i]);
			if(not mp.contains({a[i],b[i]}))
			{
				mp[{a[i],b[i]}]=++ind;
			}
			q[i]=mp[{a[i],b[i]}];
		}
		vector<pair<int,int>> srp,srq;
		for(int i=1;i<=n;i++)
		{
			srp.emplace_back(p[i],i);
			srq.emplace_back(q[i],i);
		}
		vector<int> c(n+5);
		sort(srp.begin(),srp.end());
		sort(srq.begin(),srq.end());
		int ok=1;
		for(int i=0;i<n;i++)
		{
			if(srp[i].first!=srq[i].first)ok=0;
			c[srp[i].second]=srq[i].second;
		}
//		for(int i=1;i<=n;i++)
//			cerr<<c[i]<<' ';
//		cerr<<endl;
		if(ok)
		{
			vector<int> BIT(n+5);
			auto change=[&](int pos,int d)
			{
				for(int x=pos;x<=n;x+=x&-x)
					BIT[x]+=d;
			};
			auto query=[&](int pos)
			{
				int ret=0;
				for(int x=pos;x;x-=x&-x)
					ret+=BIT[x];
				return ret;
			};
			long long ans=0;
			for(int i=1;i<=n;i++)
			{
				ans+=i-1-query(c[i]);
				change(c[i],1);
			}
			cout<<ans<<"\n";
		}
		else
		{
			cout<<-1<<"\n";
		}
	}
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3868ms
memory: 86716kb

input:

4
500000
471518156 319758862 812815356 520822448 129241996 461169933 796713727 608641317 281180101 953966756 749634941 274104949 996181952 88142916 998544672 125597509 991731126 974767231 338911715 674197249 167602044 682799026 226927279 703198907 216742488 8185420 94921423 690039818 859329736 45428...

output:

0
-1
124999750000
62569045381

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 3668ms
memory: 55564kb

input:

4
500000
413978977 447850293 802546055 289227062 321812178 97254226 380555063 167654142 874843339 349071516 10868808 93093321 996630037 33169052 99312773 335065051 278187197 94810404 643530967 947474510 63047182 573006260 8470509 116477506 251504796 899920047 806158195 452300674 859769748 489596554 ...

output:

62623368052
62486676013
62520737719
62506121292

result:

ok 4 lines

Test #3:

score: 0
Accepted
time: 994ms
memory: 4868kb

input:

9082
7
714942297 703160828 709363739 23094554 461254804 159017923 666590460
196100940 70571239 755082029 321718821 55663261 378527017 335969190
321718821 335969190 70571239 755082029 378527017 55663261 714942297
23094554 666590460 703160828 709363739 159017923 461254804 196100940
5
2 1 1 2 1
2 1 2 2...

output:

13
5
0
-1
0
2
10
2
0
5
0
10
15
1
2336
-1
1
-1
-1
4
-1
15
0
9
45
15
3
4
0
11
-1
-1
10
-1
18
10
0
3
1
-1
0
1
0
5
-1
16
-1
-1
-1
15
-1
307294
-1
7
0
0
-1
-1
-1
0
2210
3
1
16
-1
-1
0
-1
0
0
5
5
10
3
0
-1
21
0
7
3
0
32308741
-1
20
-1
0
1
-1
9
6
2249
0
-1
0
55236
2265
1
12
-1
-1
29
0
-1
20
0
2
0
0
1
-1
0
...

result:

ok 9082 lines

Extra Test:

score: 0
Extra Test Passed