QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#730737#9584. 顾影自怜pTL 614ms104204kbC++172.7kb2024-11-09 21:19:342024-11-09 21:19:35

Judging History

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

  • [2024-11-09 21:19:35]
  • 评测
  • 测评结果:TL
  • 用时:614ms
  • 内存:104204kb
  • [2024-11-09 21:19:34]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define pii pair<int, int>
#define Genshin_Impact return
#define start 0
const int mod = 998244353;
const int N = 1e6 + 10;
int a[N];
map<pii, int> pos;
int num[N], cnt[N];
struct node
{
    int l, r, maxn;
} tr[N << 2];
void pushup(int u)
{
    tr[u].maxn = max(tr[u << 1].maxn, tr[u << 1 | 1].maxn);
}

void build(int u, int l, int r)
{
    tr[u] = {l, r, 0};
    if (l == r)
    {
        tr[u].maxn = a[l];
        return;
    }
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    pushup(u);
    return;
}

int query(int u, int l, int r, int x, int op)
{
    if (tr[u].maxn <= x)
        return -1;
    if (tr[u].l == tr[u].r)
    {
        if (tr[u].maxn > x)
            return tr[u].l;
        return -1;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    if (r <= mid)
        return query(u << 1, l, r, x, op);
    else if (l >= mid + 1)
        return query(u << 1 | 1, l, r, x, op);
    else
    {
        if (op)
        {
            int pos = query(u << 1 | 1, l, r, x, op);
            if (pos != -1)
                return pos;
            return query(u << 1, l, r, x, op);
        }
        else
        {
            int pos = query(u << 1, l, r, x, op);
            if (pos != -1)
                return pos;
            return query(u << 1 | 1, l, r, x, op);
        }
    }
}
void solve()
{
    pos.clear();
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        num[i] = 0, cnt[i] = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        cnt[a[i]]++;
        num[i] = cnt[a[i]];
        pos[{a[i], cnt[a[i]]}] = i;
    }
    build(1, 1, n);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int x = a[i], posrr, ns;
        if (cnt[x] >= num[i] + k - 1)
        {
            int posrl = pos[{x, num[i] + k - 1}];
            if (query(1, i, posrl, x, 1) != -1)
                continue;
            if (cnt[x] >= num[i] + k)
                posrr = pos[{x, num[i] + k}];
            else
                posrr = n + 1;
            int q = query(1, posrl, n, x, 0);
            if (q != -1)
                posrr = min(posrr, q);
            int numr = posrr - posrl;
            // cout << i << ": " << x << ' ' << posrl << ' ' << posrr << ' ';
            int l = max(0, query(1, 1, i, x, 1));
            // cout << l << '\n';
            ans += 1ll * (i - l) * numr;
        }
    }
    cout << ans << '\n';
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(20);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    Genshin_Impact start;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
5 2
1 3 3 2 2
4 3
1 4 2 1

output:

7
0

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 515ms
memory: 104204kb

input:

1
1000000 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

500000500000

result:

ok single line: '500000500000'

Test #3:

score: 0
Accepted
time: 103ms
memory: 9796kb

input:

158921
1 1
1
2 1
1 1
2 2
1 1
2 1
1 2
2 2
1 2
2 1
2 1
2 2
2 1
2 1
2 2
2 2
2 2
3 1
1 1 1
3 2
1 1 1
3 3
1 1 1
3 1
1 1 2
3 2
1 1 2
3 3
1 1 2
3 1
1 1 3
3 2
1 1 3
3 3
1 1 3
3 1
1 2 1
3 2
1 2 1
3 3
1 2 1
3 1
1 2 2
3 2
1 2 2
3 3
1 2 2
3 1
1 2 3
3 2
1 2 3
3 3
1 2 3
3 1
1 3 1
3 2
1 3 1
3 3
1 3 1
3 1
1 3 2
3 2...

output:

1
3
1
3
0
3
0
3
1
6
3
1
6
1
0
6
1
0
6
0
0
6
2
0
6
0
0
6
0
0
6
0
0
6
2
0
6
1
0
6
1
0
6
0
0
6
2
0
6
3
1
6
1
0
6
0
0
6
0
0
6
2
0
6
1
0
6
0
0
6
1
0
6
0
0
6
1
0
6
1
0
6
2
0
6
2
0
6
3
1
10
6
3
1
10
3
1
0
10
3
1
0
10
3
1
0
10
1
0
0
10
4
0
0
10
1
0
0
10
1
0
0
10
1
0
0
10
1
0
0
10
4
0
0
10
1
0
0
10
1
0
0
10
...

result:

ok 158921 lines

Test #4:

score: 0
Accepted
time: 614ms
memory: 104196kb

input:

1
1000000 1000
1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 100...

output:

498003996002

result:

ok single line: '498003996002'

Test #5:

score: -100
Time Limit Exceeded

input:

24
217838 1
11125 61539 156386 82530 15545 110745 20051 71328 216682 107717 24565 71482 196259 23920 79507 74383 81434 99615 50571 31499 93241 126374 205674 57290 166999 83044 89340 72667 55001 143151 30341 98608 191197 96249 176106 113045 116438 34417 92531 200248 38119 106697 24629 117110 129552 1...

output:


result: