WA了一个点,和标程对拍了,都是找不到差异。
错的 04_random_01.txt 有没有哪位大佬看看?
谢谢!
#include<iostream>
#include<algorithm>
using namespace std;
#define middle(a, b) ((a + b) / 2)
#define int long long
const int N = 2e5 + 5;
int n, m, K;
long long a[N], p[N];
struct Segment_tree {
struct Node {
Node *ch[2];
long long sum, tot;
Node() {
sum = tot = 0;
ch[0] = ch[1] = nullptr;
}
void upd() {
sum = tot = 0;
if (ch[0] != nullptr) sum += ch[0]->sum, tot += ch[0]->tot;
if (ch[1] != nullptr) sum += ch[1]->sum, tot += ch[1]->tot;
}
} *root = nullptr;
void Modify(Node *&cur, long long pos, long long x, int OMG, int L, int R) {
if (cur == nullptr) cur = new Node();
if (L == R) {
cur->sum += OMG;
cur->tot += x * OMG;
return;
}
int mid = middle(L, R);
if (pos <= p[mid])
Modify(cur->ch[0], pos, x, OMG, L, mid);
else Modify(cur->ch[1], pos, x, OMG, mid + 1, R);
cur->upd();
}
long long Query(Node *cur, long long l, long long r, int L, int R) {
if (cur == nullptr || l > r) return 0;
if (l <= p[L] && p[R] <= r) return cur->tot;
int mid = middle(L, R);
long long ans = 0;
if (l <= p[mid])
ans += Query(cur->ch[0], l, r, L, mid);
if (r > p[mid])
ans += Query(cur->ch[1], l, r, mid + 1, R);
return ans;
}
long long qval_by_rank(Node *cur, long long k, int L, int R) {
if (L == R) return p[L];
long long l_siz = cur->ch[0] == nullptr ? 0 : cur->ch[0]->sum;
int mid = middle(L, R);
if (k <= l_siz) {
if (cur->ch[0] != nullptr) return qval_by_rank(cur->ch[0], k, L, mid);
else return -1145141919810ll;
} else {
if (cur->ch[1] != nullptr) return qval_by_rank(cur->ch[1], k - l_siz, mid + 1 ,R);
else return 1145141919810ll;
}
}
} smt;
signed main() {
#ifdef LOCALs
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin >> n >> m >> K;
for (int i = 1; i <= n; ++i) cin >> a[i], p[i] = a[i];
sort(p + 1, p + 1 + n);
int kcx = (int) (unique(p + 1, p + 1 + n) - p - 1);
for (int i = 1; i <= m; ++i) smt.Modify(smt.root, a[i], a[i], 1, 1, kcx);
for (int i = 1; i + m - 1 <= n; ++i) {
long long two = smt.qval_by_rank(smt.root, K, 1, kcx);
long long one = smt.qval_by_rank(smt.root, 1, 1, kcx);
// cout << one << ' ' << two << '\n';
cout << smt.Query(smt.root, one, two, 1, kcx) << ' ';
smt.Modify(smt.root, a[i + m], a[i + m], 1, 1, kcx);
smt.Modify(smt.root, a[i], a[i], -1, 1, kcx);
}
return 0;
}