QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#697233 | #7904. Rainbow Subarray | diguo | TL | 1207ms | 6600kb | C++20 | 3.5kb | 2024-11-01 12:12:46 | 2024-11-01 12:12:47 |
Judging History
answer
/***始まる~***/
#include <bits/stdc++.h>
using namespace std;
#define MULTIINPUT 1
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#else
#include <dbg.h>
#endif
#define all(x, y) x.begin() + y, x.end()
#define mkp make_pair
#define pii pair<int, int>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define rep(name, base, limit) for(int name = base; name <= limit; ++name)
#define per(name, base, limit) for(int name = base; name >= limit; --name)
#define lowbit(x) ((x) & -(x))
#define ls(p) ((p) << 1)
#define rs(p) ((p) << 1 | 1)
const int INF = 0x3f3f3f3f;
const long long LNF = 0x3f3f3f3f3f3f3f3f;
inline long long read()
{
long long 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*10+ch-48;ch=getchar();}
return x*f;
}
struct fenwick
{
vector<ll> tree;
int n;
fenwick(int _n)
{
n = _n;
tree.resize(n + 1);
}
void update(int idx, ll w)
{
for (int i = idx; i <= n; i += lowbit(i)) tree[i] += w;
}
ll query(int idx)
{
ll ret = 0;
for (int i = idx; i; i -= lowbit(i)) ret += tree[i];
return ret;
}
ll query(int l, int r)
{
return query(r) - query(l - 1);
}
};
void solve()
{
int n = read();
ll k = read();
vector<ll> a(n + 1), v;
rep(i, 1, n) a[i] = read() - i, v.pb(a[i]);
ll mx = *max_element(all(a, 1)), mn = *min_element(all(a, 1));
sort(all(v, 0));
v.erase(unique(all(v, 0)), v.end());
int m = v.size();
auto find = [&](ll w)
{
return lower_bound(all(v, 0), w) - v.begin() + 1;
};
int idx = 0, ans = 1;
fenwick trsum(m), trcnt(m);
auto add = [&](int idx)
{
trsum.update(find(a[idx]), a[idx]);
trcnt.update(find(a[idx]), 1);
};
auto del = [&](int idx)
{
trsum.update(find(a[idx]), -a[idx]);
trcnt.update(find(a[idx]), -1);
};
auto cal = [&](ll w) -> ll
{
ll ret = 0;
int idx = find(w);
if (idx <= m) ret += trsum.query(idx, m) - trcnt.query(idx, m) * w;
--idx;
if (idx >= 1) ret += trcnt.query(1, idx) * w - trsum.query(1, idx);
return ret;
};
auto check = [&](int idx) -> bool
{
add(idx);
ll l = mn, r = mx, ans = LNF;
while (l + 1 < r)
{
ll mid = (l + r) / 2;
ll a1 = cal(mid - 1), a2 = cal(mid + 1);
if (a1 > a2) ans = min(ans, a1), l = mid;
else ans = min(ans, a2), r = mid;
}
ans = min({ans, cal(l), cal(r)});
if (ans > k) del(idx);
return (ans <= k);
};
rep(i, 1, n)
{
while (idx < n && check(idx + 1)) ++idx;
ans = max(ans, idx - i + 1);
del(i);
}
cout << ans;
}
//left: mid = l + r, pass: r = mid, else: l = mid + 1
//right: mid = l + r + 1, pass: l = mid, else: r = mid - 1
//m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
//udu: cal(m1) > cal(m2) dud: cal(m1) < cal(m2)
//pass: l = m1 + 1, else: r = m2 - 1;
signed main()
{
//std::ios::sync_with_stdio(false);
#if MULTIINPUT
int _t = read();
while (_t--)
{
#endif
solve();
#if MULTIINPUT
putchar('\n');
}
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3844kb
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: 0
Accepted
time: 1207ms
memory: 6600kb
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:
ok 11102 lines
Test #3:
score: -100
Time Limit Exceeded
input:
1 500000 17244641009859 54748096 75475634 204928248 276927808 84875072 103158867 27937890 322595515 186026685 45468307 69240390 139887597 188586447 373764525 121365644 310156469 185188306 60350786 211308832 384695957 370562147 208427221 35937909 267590963 126478310 275357775 55361535 335993561 36696...