QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#725269#9584. 顾影自怜diguoTL 362ms234448kbC++202.4kb2024-11-08 16:54:502024-11-08 16:54:50

Judging History

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

  • [2024-11-08 16:54:50]
  • 评测
  • 测评结果:TL
  • 用时:362ms
  • 内存:234448kb
  • [2024-11-08 16:54:50]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ll long long
#define rep(i, x, y) for(int i = x; i <= y; ++i)
#define pb push_back
#define all(x, y) x.begin() + y, x.end()
inline ll read()
{
    ll x = 0, f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();}
    return x * f;
}

const int LGN = 1000000;
int lg2[LGN + 1];
//don't forget init value of log2 and calculate function
void lginit()
{
    rep(i, 2, LGN) lg2[i] = lg2[i / 2] + 1;
}
struct ST
{
    vector<vector<ll>> st;
    ll cal(ll x, ll y)
    {
        ll ret = max(x, y);
        return ret;
    }
    void init(vector<int> &a, int n)
    {
        st.resize(n + 1);
        rep(i, 0, n) st[i].resize(21), st[i][0] = a[i];
        rep(i, 1, 20) for (int j = 1; j + (1 << i) - 1 <= n; ++j)
        st[j][i] = cal(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]);
    }
    ll query(int l, int r)
    {
        int s = lg2[r - l + 1];
        return cal(st[l][s], st[r - (1 << s) + 1][s]);
    }
};

void solve()
{
    int n = read(), k = read();
    vector<int> a(n + 1);
    rep(i, 1, n) a[i] = read();
    ST st;
    st.init(a, n);
    vector b(n + 1, vector<int>());
    rep(i, 1, n)
    {
        b[a[i]].pb(i);
    }
    ll ans = 0;
    rep(i, 1, n)
    {
        int m = b[i].size();
        rep(j, 0, m - k)
        {
            int u = b[i][j], v = b[i][j + k - 1];
            if (st.query(u, v) != i) continue;
            int l, r = u;
            if (j == 0) l = 1;
            else l = b[i][j - 1] + 1;
            while (l < r)
            {
                int mid = l + r >> 1;
                if (st.query(mid, u) == i) r = mid;
                else l = mid + 1;
            }
            int x = l;
            l = v, r = n;
            while (l < r)
            {
                int mid = l + r + 1 >> 1;
                if (st.query(v, mid) == i) l = mid;
                else r = mid - 1;
            }
            int y = l;
            ans += 1ll * (u - x + 1) * (y - v + 1);
        }
    }
    cout << ans;
}

int main()
{
    lginit();
    int t = read();
    while (t--)
    {
        solve();
        if (t) putchar('\n');
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 7408kb

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: 340ms
memory: 234448kb

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: 94ms
memory: 7744kb

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: 362ms
memory: 233972kb

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: