P8449 [LSOT-1] 逆序对
前言
昨天 MX 学了平衡树。
第一眼:这不一眼平衡树吗?
哦,我不会板子。只能想想其他办法了。
正言
我们观察到题目的两个特性:
-
- 只用求出逆序对奇偶性。这意味着我们并不需要求出(应该也求不出来)逆序对的数量。
后两个操作比较好想,对于操作
对于第
这样一来,我们只需要考虑操作
我们先将问题变得简单:考虑交换区间
回到操作
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll MOD=1e9+7;
// head
const int N=2e6+5;
int lowbit(int x){return x&-x;}
struct BIT
{
int c[N<<2];
void modify(int x,int k) {for(;x<N;x+=lowbit(x)) c[x]+=k;}
int query(int x) {int sum=0;for(;x;x-=lowbit(x)) sum+=c[x];return sum;}
}tree;
int a[N];
signed main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n,q;cin>>n>>q;
int ans=0;
for(int i=1;i<=n;i++) {
cin>>a[i];
ans+=tree.query(N-1)-tree.query(a[i]);
tree.modify(a[i],1);
}
ans=ans%2;
while(q--)
{
int opt;cin>>opt;
if(opt==1){
int l1,r1,l2,r2;cin>>l1>>r1>>l2>>r2;
ans+=(r1-l1+1)*(r2-r1)+(l2-r1-1)*(r2-l2+1);
}
else if(opt==2){
int l,r,k;cin>>l>>r>>k;
int l1=l,r1=r,l2=r+1,r2=k;
ans+=(r1-l1+1)*(r2-r1)+(l2-r1-1)*(r2-l2+1);
}
else if(opt==3){
int x;cin>>x;
ans+=tree.query(N-1)-tree.query(x);
tree.modify(x,1);
}
else {
int x;cin>>x;
ans+=tree.query(x-1);
tree.modify(x,1);
}
ans%=2;
if(ans) cout<<"odd"<<endl;
else cout<<"even"<<endl;
}
}