QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#721829#7904. Rainbow SubarrayfutarianTL 0ms19812kbC++113.6kb2024-11-07 16:57:472024-11-07 16:57:47

Judging History

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

  • [2024-11-07 16:57:47]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:19812kb
  • [2024-11-07 16:57:47]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;
const int Len = 5e5 + 5;
inline int read()
{
	int x = 0 , f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-') f = -1;
		ch = getchar();
	}
	while('0' <= ch && ch <= '9')
	{
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar();
	}
	return x * f;
}
void write(int x)
{
	if(x < 0){putchar('-');x = -x;}
	if(x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
inline void writeN(int x){write(x) , putchar('\n');}
#define ll long long
int n,m,tot,rt,x,y,z,a[Len];
struct FHQ
{
	int l,r,ff,ky,val,sz;
	ll sum;
	FHQ(){l = r = ff = ky = val = sz = sum = 0;}
	FHQ(int L,int R,int FF,int KY,int VAL,int SZ,ll SUM){l = L , r = R , ff = FF , ky = KY , val = VAL , sz = SZ , sum = SUM;}
}fhq[Len];
#define ls(p) fhq[p].l
#define rs(p) fhq[p].r
inline void push_up(int p){fhq[p].sz = fhq[ls(p)].sz + fhq[rs(p)].sz + 1;fhq[p].sum = fhq[ls(p)].sum + fhq[rs(p)].sum + fhq[p].val;}
inline int clone(int v)
{
	tot ++;
	fhq[tot] = FHQ(0 , 0 , 0 , rand() , v , 1 , v);
	return tot;
}
void split(int now,int v,int &x,int &y,int lstx,int lsty)
{
	if(!now){x = y = 0;return;}
	if(fhq[now].val <= v)
	{
		x = now;
		fhq[x].ff = lstx;
		split(rs(now) , v , rs(now) , y , x , lsty);
	}
	else
	{
		y = now;
		fhq[y].ff = lsty;
		split(ls(now) , v , x , ls(now) , lstx , y);
	}
	push_up(now);
}
int merge(int x,int y)
{
	if(!x || !y) return x + y;
	if(fhq[x].ky < fhq[y].ky)
	{
		rs(x) = merge(rs(x) , y);
		fhq[rs(x)].ff = x;
		push_up(x);
		return x;
	}
	else 
	{
		ls(y) = merge(x , ls(y));
		fhq[ls(y)].ff = y;
		push_up(y);
		return y;
	}
}
inline void ins(int v)
{
	split(rt , v - 1 , rt , x , 0 , 0);
	rt = merge(rt , clone(v));
	rt = merge(rt , x);
}
inline void del(int v)
{
	split(rt , v - 1 , x , y , 0 , 0);
	split(y , v , y , z , 0 , 0);
	y = merge(ls(y) , rs(y));
	y = merge(y , z);
	rt = merge(x , y);
}
inline void quRak(int v)
{
	split(rt , v - 1 , x , y , 0 , 0);
	writeN(fhq[x].sz + 1);
	rt = merge(x , y);	
}
inline int Prknum(int rk)
{
	int u = rt;
	if(fhq[u].sz < rk) return -1;
	while(u)
	{
		int lsize = fhq[ls(u)].sz;
		if(lsize + 1 == rk) return fhq[u].val;
		else if(lsize + 1 > rk) u = ls(u);
		else rk -= lsize + 1 , u = rs(u);
	}
	return -1;
}
inline void clr()
{
	for(int i = 1 ; i <= tot ; i ++) fhq[i] = FHQ(0 , 0 , 0 , 0 , 0 , 0 , 0);
	tot = rt = x = y = z = 0;
}
ll k;
inline int check(int len)
{
	clr();const int qrk = len / 2 + (len % 2);
	int ql = 0 , nv = 0;ll sm = 0;
	for(int i = 1 ; i <= len ; i ++) ins(a[i]);
	nv = Prknum(qrk);
	split(rt , nv , x , y , 0 , 0);
	sm = 1ll * nv * fhq[x].sz - fhq[x].sum + fhq[y].sum - 1ll * nv * fhq[y].sz;
	rt = merge(x , y);
	ql |= (sm <= k);
	//if(len == 6 && ql) printf("??%d %d\n",1,len);
	if(ql) return 1;
	for(int i = len + 1 ; i <= n ; i ++)
	{
		del(a[i - len]) , ins(a[i]);
		nv = Prknum(qrk);
		x = y = 0;
		split(rt , nv , x , y , 0 , 0);
		sm = 1ll * nv * fhq[x].sz - fhq[x].sum + fhq[y].sum - 1ll * nv * fhq[y].sz;
		rt = merge(x , y);
		ql |= (sm <= k);
		//if(len == 6 && ql) printf("??%d %d %d\n",i - len + 1,i,nv);
		if(ql) return 1;
	}
	return ql;
}
signed main()
{
	srand(time(0));
	int T = read();
	while(T --)
	{
		n = read() , k = read();
		for(int i = 1 ; i <= n ; i ++) a[i] = read() - i;
		//for(int i = 1 ; i <= n ; i ++) printf("%d ",a[i]);puts("");
		int l = 1 , r = n , anss = 0;
		while(l <= r)
		{
			int mid = (l + r) >> 1;
			if(check(mid)) anss = mid , l = mid + 1;
			else r = mid - 1;
		}
		write(anss) , putchar('\n');
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 19812kb

input:

5
7 5
7 2 5 5 4 11 7
6 0
100 3 4 5 99 100
5 6
1 1 1 1 1
5 50
100 200 300 400 500
1 100
3

output:

4
3
5
1
1

result:

ok 5 lines

Test #2:

score: -100
Time Limit Exceeded

input:

11102
2 167959139
336470888 134074578
5 642802746
273386884 79721198 396628655 3722503 471207868
6 202647942
268792718 46761498 443917727 16843338 125908043 191952768
2 717268783
150414369 193319712
6 519096230
356168102 262263554 174936674 407246545 274667941 279198849
9 527268921
421436316 3613460...

output:

1
4
3
2
6
5
7
2
4
1
4
1
1
3
2
2
7
8
7
7
1
7
6
2
4
3
1
6
7
7
3
4
3
9
3
8
6
6
3
1
6
3
1
2
4
6
4
6
4
1
4
7
1
6
3
5
6
6
1
7
5
3
1
6
4
5
3
2
2
6
2
3
10
1
4
3
2
4
5
1
7
5
5
5
8
5
3
6
3
5
5
8
5
4
5
2
1
5
2
3
3
4
8
1
3
1
2
2
8
3
1
6
8
1
8
4
5
6
6
8
4
8
3
2
8
4
5
6
2
6
2
4
1
5
4
5
3
2
4
1
2
1
4
5
8
3
7
3
3
3...

result: