st表求调,90ptsMLE
查看原帖
st表求调,90ptsMLE
1232696
zbbfans楼主2024/11/8 10:06

st表求调

#include <bits/stdc++.h>
using namespace std;
inline int read() {
    int f = 1, k = 0;
    char c = getchar();

    while (c < '0' || c > '9') {
        if (c == '-')
            f = -f;

        c = getchar();
    }

    while (c >= '0' and c <= '9')
        k = k * 10 + c - '0', c = getchar();

    return f * k;
}
inline void print(int x) {
    if (x < 0)
        putchar('-'), x = -x;

    if (x < 10)
        putchar(x + '0');
    else
        print(x / 10), putchar(x % 10 + '0');
}
long long ans;
int n, kk, p;
int a[2000001], k;
int st[2000001][22];
inline bool check(int ll, int mid) {
    int lll = log2(mid - ll + 1);

    if (min(st[ll][lll], st[mid - (1 << lll) + 1][lll]) <= p) {
        return true;
    }

    return false;
}
int main() {
    n = read(), kk = read(), p = read();
	vector<int> mp[kk+1];
    for (int i = 1; i <= n; i++) {
        k = read(), a[i] = read();
        st[i][0] = a[i];
        mp[k].push_back(i);
    }
	
    int len = log2(n);

    for (int j = 1; j <= len; j++) {
        for (int i = 1; i <= n - (1 << j) + 1; i++) {
            st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
        }
    }

    for (int i = 0; i <= kk; i++) {
        int lll = mp[i].size();

        if (lll >= 2) {
            int ll = 1, rr = lll;

            while (ll < rr) {
                int l = ll, r = rr, best = r;

                while (l <= r) {
                    int mid = (l + r) >> 1;

                    if (ll != mid and check(mp[i][ll - 1], mp[i][mid - 1])) {
                        best = mid;
                        r = mid - 1;
                    } else {
                        l = mid + 1;
                    }
                }

                if (best == rr) {
                    if (check(mp[i][ll - 1], mp[i][rr - 1])) {
                        ans++;
                        ll++;

                        while (ll < rr) {
                            if (check(mp[i][ll - 1], mp[i][rr - 1]))
                                ans++;
                            else
                                break;

                            ll++;
                        }
                    }
                } else {
                    ans += (rr - best + 1) * (best - ll);
                }

                ll = best;
            }
        }
    }

    print(ans);
    return 0;
}
2024/11/8 10:06
加载中...