QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#620513#7904. Rainbow Subarrayxjt05WA 441ms26648kbC++233.1kb2024-10-07 18:42:452024-10-07 18:42:46

Judging History

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

  • [2024-10-07 18:42:46]
  • 评测
  • 测评结果:WA
  • 用时:441ms
  • 内存:26648kb
  • [2024-10-07 18:42:45]
  • 提交

answer

#include<iostream>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const ll maxn = 5e5 + 10;
struct s
{
	ll l, r;
	ll cs;
}p[maxn << 2];
ll fa[maxn << 2];
void build(ll i, ll l, ll r)
{
	p[i].l = l; p[i].r = r;
	p[i].cs = 0;
	if (l == r)
	{
		//cout<<99<<endl;
		fa[l] = i;
		return;
	}
	build(i << 1, l, (l + r) >> 1);
	build(i << 1 | 1, (l + r >> 1) + 1, r);
}
void upd(ll i, ll x)
{
	if (p[i].l == p[i].r)
	{
		p[i].cs++;
		//cout<<p[i].cs<<endl;
		return;
	}
	ll mid = (p[i].l + p[i].r) >> 1;
	ll i1 = i << 1;
	ll i2 = i << 1 | 1;
	if (x <= mid)
		upd(i1, x);
	if (x >= mid + 1)
		upd(i2, x);
	p[i].cs = p[i1].cs + p[i2].cs;
	//cout<<p[i].cs<<endl;
}
void dt(ll i, ll x)
{
	if (p[i].l == p[i].r)
	{
		p[i].cs--;
		//cout<<p[i].cs<<endl;
		return;
	}
	ll mid = (p[i].l + p[i].r) >> 1;
	ll i1 = i << 1;
	ll i2 = i << 1 | 1;
	if (x <= mid)
		dt(i1, x);
	if (x >= mid + 1)
		dt(i2, x);
	p[i].cs = p[i1].cs + p[i2].cs;
}
ll query(ll i, ll l, ll r)
{
	ll ans = 0;
	if (l == p[i].l && p[i].r == r)
	{
		ans += p[i].cs;
		return ans;
	}
	ll i1 = i << 1;
	ll i2 = i << 1 | 1;
	if (l <= p[i1].r)
	{
		if (r <= p[i1].r)
		{
			ans += query(i1, l, r);
		}
		else
			ans += query(i1, l, p[i1].r);
	}
	if (r >= p[i2].l)
	{
		if (l >= p[i2].l)
			ans += query(i2, l, r);
		else
			ans += query(i2, p[i2].l, r);
	}
	return ans;
}
ll sa(ll i, ll k, ll l, ll r)
{
	if (l == r)
	{
		return l;
	}
	ll ans = 0;
	ll i1 = i << 1, i2 = i << 1 | 1;
	if (k <= p[i1].cs)
	{
		ans = sa(i1, k, l, p[i1].r);
	}
	else
		ans = sa(i2, k - p[i1].cs, p[i2].l, r);
	return ans;
}
ll a[500005];
ll b[500005];
map<ll, ll>q;
set<ll>f;
int main()
{
	ll t;
	cin >> t;
	while (t--)
	{
		q.clear();
		f.clear();
		ll n, m;
		cin >> n >> m;
		for (ll i = 1; i <= n; i++)
		{
			cin >> a[i];
			a[i] -= i;
			f.insert(a[i]);
		}
		ll cnt = 0;
		for (auto j : f)
		{
			cnt++;
			q[j] = cnt;
			b[cnt] = j;
		}
		deque<ll>co;
		build(1, 1, cnt);
		ll sz = 0;
		ll ans = 0;
		ll op;
		ll an = 1;
		for (ll i = 1; i <= n; i++)
		{
			if (co.empty())
			{
				co.push_back(a[i]);
				upd(1, q[a[i]]);
				sz++;
				op = b[sa(1, (sz + 1) / 2, 1, cnt)];
				ans = 0;
			}
			else
			{
				co.push_back(a[i]);
				ans += abs(a[i] - op);
				upd(1, q[a[i]]);
				sz++;
				ll mid = b[sa(1, (sz + 1) / 2, 1, cnt)];
				if (mid > op)
				{
					ans += ((sz - 1) / 2) * abs(op - mid);
					ans -= (sz - ((sz - 1) / 2)) * abs(op - mid);
				}
				else if (mid < op)
				{
					ans -= ((sz + 1) / 2) * abs(op - mid);
					ans += (sz - (sz + 1) / 2) * abs(op - mid);
				}
				while (!co.empty() && ans > m)
				{
					ans -= abs(co.front() - mid);
					dt(1, q[(ll)co.front()]);
					sz--;
					mid = b[sa(1, (sz + 1) / 2, 1, cnt)];
					if (mid > op)
					{
						ans -= ((sz + 1) / 2) * abs(op - mid);
						ans += (sz - (sz + 1) / 2) * abs(op - mid);
					}
					op = mid;
					co.pop_front();
				}
				an = max(an, (ll)co.size());
				op = mid;
				//co.pop_front();
			}
		}
		cout << an << endl;
	}
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 9716kb

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
Wrong Answer
time: 441ms
memory: 26648kb

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
5
4
6
4
1
4
7
1
6
3
5
6
6
1
7
5
3
1
6
3
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
3
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
7
3
8
3
2
8
4
5
6
2
6
2
4
1
5
4
5
3
2
4
1
2
1
4
3
8
3
7
3
3
3...

result:

wrong answer 46th lines differ - expected: '6', found: '5'