QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#259145#5526. Jewel of Data Structure Problemsydzr00000WA 0ms5080kbC++201.5kb2023-11-20 17:17:042023-11-20 17:17:04

Judging History

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

  • [2023-11-20 17:17:04]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:5080kb
  • [2023-11-20 17:17:04]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int a[200001];
class FenwickTree{
	private:
		int tree[300001],sz=0;
		inline int lowbit(int x)
		{
			return x&(-x);
		}
	public:
		inline void init(int n)
		{
			memset(tree,0,sizeof(tree));
			sz=n;
		}
		inline void update(int pos,int val)
		{
			while(pos<=sz)
			{
				tree[pos]+=val;
				pos+=lowbit(pos);
			}
		}
		inline int query(int pos)
		{
			int sum=0;
			while(pos)
			{
				sum+=tree[pos];
				pos-=lowbit(pos);
			}
			return sum;
		}
};
FenwickTree F;
inline pair<int,int> Swap(int u,int v)
{
	int diff=0,odd=0;
	if(a[u]!=u) 
		diff--;
	if(a[v]!=v)
		diff--;
	if(abs(a[u]-u)&1)
		odd--;
	if(abs(a[v]-v)&1)
		odd--;
	swap(a[u],a[v]);
	if(a[u]!=u) 
		diff++;
	if(a[v]!=v)
		diff++;
	if(abs(a[u]-u)&1)
		odd++;
	if(abs(a[v]-v)&1)
		odd++;
	return make_pair(diff,odd);
}
int main(){
	int n,q;
	scanf("%d %d",&n,&q);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	int diff=0,odd=0;
	for(int i=1;i<=n;i++)
	{
		diff+=(a[i]!=i);
		odd+=(abs(a[i]-i)&1);
	}
	int ans=0;
	F.init(n);
	for(int i=1;i<=n;i++)
	{
		ans+=((i-F.query(a[i]))&1);
		F.update(a[i],1);
	}
	ans&=1;
	while(q--)
	{
		int u,v;
		scanf("%d %d",&u,&v);
		pair<int,int>res=Swap(u,v);
		diff+=res.first;odd+=res.second;
		ans^=1;
		if(!diff)
			printf("-1\n");
		else if(ans&1)
			printf("%d\n",n);
		else if(odd)
			printf("%d\n",n-1);
		else
			printf("%d\n",n-2);
	}
	
	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 5080kb

input:

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

output:

-1
4
5
3
5
3

result:

wrong answer 2nd numbers differ - expected: '5', found: '4'