QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#259145 | #5526. Jewel of Data Structure Problems | ydzr00000 | WA | 0ms | 5080kb | C++20 | 1.5kb | 2023-11-20 17:17:04 | 2023-11-20 17:17:04 |
Judging History
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'