纯暴力AC(不需要归并排序或者树状数组就能过的)
查看原帖
纯暴力AC(不需要归并排序或者树状数组就能过的)
419216
Twig楼主2021/4/16 08:43

rt

#include <algorithm>
#include <cstdio>
#include <cmath>

#define int long long

/***************快读***************/

namespace IO {
char buf[1<<21], *p1 = buf, *p2 = buf, buf1[1<<21];
inline char gc () {return p1 == p2 && (p2 = (p1 = buf) + fread (buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;}

#ifndef ONLINE_JUDGE
#endif

#define gc getchar

template<class I>
inline void read(I &x) {
    x = 0; I f = 1; char c = gc();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = gc(); }
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = gc(); }
    x *= f;
}

template<class I>
inline void write(I x) {
    if(x == 0) {putchar('0'); return;}
    I tmp = x > 0 ? x : -x;
    if(x < 0) putchar('-');
    int cnt = 0;
    while(tmp > 0) {
        buf1[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while(cnt > 0) putchar(buf1[--cnt]);
}

template<class I>
inline void read_(I &a,I &b){
	read(a),read(b);
}

#define in(x) read(x)
#define outn(x) write(x), putchar('\n')
#define out(x) write(x), putchar(' ')

} using namespace IO;

/***************快读***************/

#define maxn 10000010

int n,k;
int Array[maxn];
int Brray[maxn];
int answer;

struct Block_Fold{
	int belong[maxn],l[maxn],r[maxn];
	int tot,block;

	inline void build(){
		block=sqrt(n);
		tot=n/block;
		if(n%block){++tot;}
		for(register int i=1;i<=tot;++i){l[i]=(i-1)*block+1,r[i]=i*block;}
		r[tot]=n;
		for(register int i=1;i<=n;++i){
			// Array[i]=Brray[i]=i;
			belong[i]=(i-1)/block+1;
		}
	}

	inline void change(register int x,register int y){
		if(belong[x]==belong[y]){
			register int tot=0;
			if(Array[x]>Array[y]){--tot;}
			else if(Array[x]<Array[y]){++tot;}
			for(register int i=x+1;i<=y-1;++i){
				if(Array[x]>Array[i]){--tot;}
				else if(Array[x]<Array[i]){++tot;}
				if(Array[y]>Array[i]){++tot;}
				else if(Array[y]<Array[i]){--tot;}
			}
			std::swap(Array[x],Array[y]);
			for(register int i=l[belong[x]];i<=r[belong[x]];++i){Brray[i]=Array[i];}
			std::sort(Brray+l[belong[x]],Brray+r[belong[x]]+1);
			answer+=tot;
			return;
		}
		register int tot=0;
		register int ii=x+1,jj=y-1;
		if(Array[x]>Array[y]){--tot;}
		else if(Array[x]<Array[y]){++tot;}
		for(;belong[ii]==belong[x];++ii){
			if(Array[x]>Array[ii]){--tot;}
			else if(Array[x]<Array[ii]){++tot;}
			if(Array[y]>Array[ii]){++tot;}
			else if(Array[y]<Array[ii]){--tot;}
		}
		for(;belong[jj]==belong[y];--jj){
			if(Array[x]>Array[jj]){--tot;}
			else if(Array[x]<Array[jj]){++tot;}
			if(Array[y]>Array[jj]){++tot;}
			else if(Array[y]<Array[jj]){--tot;}
		}
		for(register int i=belong[ii];i<=belong[jj];++i){
			register int u=std::lower_bound(Brray+l[i],Brray+r[i]+1,Array[x])-(Brray+l[i]);
			register int v=std::lower_bound(Brray+l[i],Brray+r[i]+1,Array[y])-(Brray+l[i]);
			tot-=2*(u-v);
		}
		answer+=tot;
		std::swap(Array[x],Array[y]);
		for(register int i=l[belong[x]];i<=r[belong[x]];++i){Brray[i]=Array[i];}
		for(register int i=l[belong[y]];i<=r[belong[y]];++i){Brray[i]=Array[i];}
		std::sort(Brray+l[belong[x]],Brray+r[belong[x]]+1);
		std::sort(Brray+l[belong[y]],Brray+r[belong[y]]+1);
	}
}blo;

int u,v;

signed main(){
	read(n);
	for(int i=1;i<=n;i++){read(Array[i]);Brray[i]=Array[i];}
	read(k);
	// blo.build();
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){answer+=Array[j]<Array[i];}
	}
	outn(answer);
	while(k--){
		read_(u,v);
		if(Array[u]==Array[v]){outn(answer);continue;}
		if(u>v){std::swap(u,v);}
		blo.change(u,v);
		outn(answer);
	}
	return 0;
}
2021/4/16 08:43
加载中...