QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#611978#7904. Rainbow Subarraymojo__hugoTL 1303ms15672kbC++203.4kb2024-10-05 00:55:492024-10-05 00:55:49

Judging History

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

  • [2024-10-05 00:55:49]
  • 评测
  • 测评结果:TL
  • 用时:1303ms
  • 内存:15672kb
  • [2024-10-05 00:55:49]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define ull unsigned long long
using PII = pair<int, int>;
using ld = long double;
const int N = 5e5 + 5, M = 1e9 + 7;
 
typedef __int128 lll;
istream &operator>>(istream &cin, lll &x) { x=0; static string s; cin>>s; for (char c:s) x=x*10+(c-'0'); return cin; }
ostream &operator<<(ostream &cout, lll x) { static char s[60]; int tp=1; s[0]='0'+(x%10); while (x/=10) s[tp++]='0'+(x%10); while (tp--) cout<<s[tp]; return cout; }
 
mt19937_64 rng{ chrono::steady_clock::now().time_since_epoch().count() };
 
ll n, k, a[N], s[N];
 
template <typename T>
struct Fen{
    int n;
    vector<T> a;   
    Fen(int n_ = 0) {
        init(n_);
    }
    void init(int n_) {
        n = n_;
        a.assign(n + 1, T{});
    }
    void add(int x, const T &v) {
        for (int i = x; i <= n; i += i & -i) {
            a[i] += v;
        }
    }
    T qry(int x) {
        T ans{};
        for (int i = x; i > 0; i -= i & -i) {
            ans += a[i];
        }
        return ans;
    }
    T qry(int l, int r) {
        return qry(r) - qry(l - 1);
    }
    int kth(const T &k) {
        int x = 0;
        T cur{};
        for (int i = 1 << __lg(n); i > 0; i /= 2) {
            if (x + i <= n && cur + a[x + i] < k) {
                x += i;
                cur += a[x];
            }
        }
        return x + 1;
    }
};
 
#define find(x) lower_bound(v.begin(), v.begin() + m - 1, x) - v.begin()+1
 
bool chk(int L) {
    vector<ll>v;
    for (int i = 1;i <= n;i++)v.push_back(a[i]);
    for (int i = 1;i + L - 1 <= n;i++) {
        ll res = s[i + L - 1] - s[i - 1];
        ll x = res / L, y = res / L + 1;
        v.push_back(x), v.push_back(y);
    }
    sort(v.begin(), v.end());
    int m = unique(v.begin(), v.end()) - v.begin();
    Fen<ll>t(m + 2);
    Fen<int>t2(m + 2);
    // lower_bound(v.begin(), v.begin() + m - 1, x) - v.begin()
    for (int i = 1;i < L;i++) {
        int x = find(a[i]);
        t.add(x, a[i]);
        t2.add(x, 1);
    }
    // for (int i = 0;i < m;i++)cout << v[i] << ' ';
    // cout << endl;
    // cout <<L<<' '<< 233 << ' ' << t2.qry(find(13)) << endl;
    for (int i = 1;i + L - 1 <= n;i++) {
        ll res = s[i + L - 1] - s[i - 1];
        int idx1 = find(a[i + L - 1]);
        t.add(idx1, a[i + L - 1]);
        t2.add(idx1, 1);
        int id = t2.kth((L + 1) / 2) - 1;
        ll x = v[id];
        ll y = v[id + 1];
        int idxx = find(x);
        // int idxy = find(y);
        int c1 = t2.qry(idxx);
        // int c2 = t2.qry(idxy);
        ll s1 = c1 * x - 2 * t.qry(idxx) + res - (L - c1) * x;
        // ll s2 = c2 * y - 2 * t.qry(idxy) + res - (L - c2) * y;
        if (s1 <= k)return 1;
        idx1 = find(a[i]);
        t.add(idx1, -a[i]);
        t2.add(idx1, -1);
    }
    return 0;
}
void solve() {
    cin >> n >> k;
    for (int i = 1;i <= n;i++) {
        cin >> a[i];
        a[i] += n - i;
        // cout << a[i] << " \n"[i == n];
        s[i] = s[i - 1] + a[i];
    }
    int l = 1, r = n;
    // cout << chk(4) << endl;
    while (l < r) {
        int mid = l + r + 1 >> 1;
        if (chk(mid))l = mid;
        else r = mid - 1;
    }
    cout << l << endl;
}
 
signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int tt = 1;
    // cout << fixed << setprecision(12);
    cin >> tt;
    while (tt--)solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1303ms
memory: 15672kb

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...

output:


result: