多观察题目性质:0919T3

http://cplusoj.com/d/senior/p/SS230919C

本题难点在于观察题目性质

对于 p = 1 p=1 p=1,必然只能放在自己本身

对于 p = 2 p=2 p=2,首先必然满足对称性

满足对称性后,在往中间扩散时,必然更劣

所以必然其中以一边为1

然后就可以上树状数组了


#include
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||
ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
//mt19937 rand(time(0));
//mt19937_64 rand(time(0));
//srand(time(0));
#define N 1000010
//#define M
//#define mo
//struct node {
//	int x, y, id; 
//}crx[N], f[N];
int crx[N], f[N], rk[N]; 
int n, m, i, j, k, T;
int ans[N], a[N], b[N], c[N], p; 
int e[N], d[N], s[N], l, r, len; 
map<int, int>mp; 

inline void UP(int *a, int n) {
	int bbbb[N], i, j; 
	for(i=1; i<=n; ++i) crx[i]=a[i]; 
	sort(crx+1, crx+n+1); 
	for(i=1; i<=n; ++i) a[i]=lower_bound(crx+1, crx+n+1, a[i])-crx; 
}

struct Tree_Zhuang_Number_Group {
	int cnt[N], i, ans, n; 
	inline void clear() {
		for(i=0; i<=n; ++i) cnt[i]=0; 
	}
	inline void add(int x, int y) {
//		printf("(%lld %lld)\n", x, y); 
		while(x<=n) cnt[x]+=y, x+=x&-x; 
	}
	inline int que(int x) {
		ans=0; while(x) ans+=cnt[x], x-=x&-x; 
		return ans; 
	}
}S1, S2, S;

inline void calc(int op) {
	if(op==2) {
		for(i=1; i<=n; ++i) b[i]=a[i]-i; UP(b, n); 
		for(i=1; i<=n; ++i) c[i]=a[i]+i; UP(c, n);  
		S.clear(); 
	}
	
	
	for(i=1; i<=(n+1)/2; ++i) {
		s[i]+=S.que(c[i]-1); 
		S.add(c[i], 1); 
	}
	S.clear(); 
	for(i=1, l=1, r=0; i<=(n+1)/2; ++i) {
		while(r<2*i-1) S.add(b[++r], 1); 
		while(l<=i) S.add(b[l++], -1); 
		s[i]+=S.que(b[i]-1); 
	}
	for(i=1, j=0; i<=n; ++i) f[++j]=e[i]=a[i]; 
	for(i=1; i<=n; ++i) f[++j]=d[i]=a[i]+i-1; 
	sort(f+1, f+2*n+1); 
	len=unique(f+1,f+n*2+1)-f-1;
	for(i=1; i<=n; ++i) e[i]=lower_bound(f+1, f+len+1, e[i])-f; 
	for(i=1; i<=n; ++i) d[i]=lower_bound(f+1, f+len+1, d[i])-f; 
	S.n=len; 
	S.clear(); 
	for(i=(n+1)/2, r=n; i>=1; --i) {
		while(r>=2*i) S.add(e[r], 1), --r; 
		s[i]+=S.que(d[i]-1); 
	}
	S.n=n; 
}

inline void outp(int x){
	if(x>9) outp(x/10);
	putchar(x%10+48);
}

signed main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	freopen("run.in", "r", stdin);
	freopen("run.out", "w", stdout);
	T=read();
//	while(T--) {
//
//	}
	n=read(); p=read(); 
	for(i=1; i<=n; ++i) a[i]=read(); 
	for(i=1; i<=n; ++i) b[i]=a[i]-i; UP(b, n); 
	for(i=1; i<=n; ++i) c[i]=a[i]+i; UP(c, n); 
	S1.n=S2.n=S.n=n; 
	if(p==1) {
		
		for(i=1; i<=n; ++i) {
			ans[i]+=S.que(b[i]-1); 
			S.add(b[i], 1); 
		}
		S.clear(); 
		for(i=n; i>=1; --i) {
			ans[i]+=S.que(c[i]-1); 
			S.add(c[i], 1); 
		}
		for(i=1; i<=n; ++i) printf("%lld\n", ans[i]+1); 
		return 0; 
	}
	for(i=1; i<=n; ++i) S1.add(b[i], 1); 
	for(i=1; i<=n; ++i) ans[i]=max(ans[i], S1.que(b[i]-1)); 
	for(i=1; i<=n; ++i) S2.add(c[i], 1); 
	for(i=1; i<=n; ++i) ans[i]=max(ans[i], S2.que(c[i]-1)); 
	calc(1); 
	if(n&1) s[(n+1)>>1]=0; 
	for(i=1, j=n; i<=j; ++i, --j) swap(a[i], a[j]), swap(s[i], s[j]); 
	calc(2); 
	for(i=1, j=n; i<=j; ++i, --j) swap(s[i], s[j]); 
	for(i=1; i<=n; ++i) ans[i]=max(ans[i], s[i])+1; 
	for(i=1; i<=n; ++i) outp(ans[i]), puts(""); 
	return 0;
}

你可能感兴趣的:(数据结构,性质)