QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#620462 | #7904. Rainbow Subarray | xjt05 | WA | 400ms | 32040kb | C++23 | 2.9kb | 2024-10-07 18:18:08 | 2024-10-07 18:18:09 |
Judging History
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 - 1);
ans -= ((sz - ((sz + 1) / 2 - 1)));
}
else if (mid < op)
{
ans -= ((sz + 1) / 2);
ans += (sz - (sz + 1) / 2);
}
while (!co.empty() && ans > m)
{
ans -= abs(co.front() - mid);
dt(1, q[(ll)co.front()]);
op = mid;
sz--;
mid = b[sa(1, (sz + 1) / 2, 1, cnt)];
co.pop_front();
}
an = max(an, (ll)co.size());
op = mid;
}
}
cout << an << endl;
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 9980kb
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: 400ms
memory: 32040kb
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 3 2 2 6 3 7 2 4 1 2 1 1 3 1 2 5 5 4 5 1 7 5 2 3 3 1 5 6 5 3 4 3 8 3 8 4 6 2 1 4 2 1 2 4 4 4 6 4 1 3 3 1 5 3 5 6 5 1 7 2 3 1 6 3 4 3 2 2 4 2 3 10 1 4 2 2 3 5 1 4 5 5 4 5 5 3 6 3 3 3 8 5 3 5 2 1 5 1 3 2 3 8 1 2 1 2 2 7 3 1 6 7 1 7 4 5 5 6 5 2 7 3 2 6 4 5 5 2 5 2 4 1 3 4 5 2 2 4 1 2 1 2 4 7 2 5 3 3 2...
result:
wrong answer 2nd lines differ - expected: '4', found: '3'