QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#619217 | #2448. Make Rounddog Happy | PlentyOfPenalty# | RE | 0ms | 0kb | C++20 | 2.8kb | 2024-10-07 13:32:23 | 2024-10-07 13:32:28 |
answer
#include <bits/stdc++.h>
#define sz(x) ((int)(x).size())
#define all(x) begin(x), end(x)
#ifdef memset0
#define log(...) fprintf(stderr, __VA_ARGS__)
#else
#define endl '\n'
#define log(...) (void(0))
#endif
using namespace std;
using ll = long long;
using lf = long double;
using ull = unsigned long long;
const int N = 3e5 + 10;
int T, n, K, a[N];
int in[N], ad[N], tot, nw, pos[N], sz, t;
int p, r, lst[N];
int nx[N], d[N];
ll ans;
vector<int> app[N], del[N];
int Calc(int r, int x, int y) {
if (r <= x) return 0;
if (x - y + 1 >= d[x]) return r - x;
if (r - y < d[x]) return 0;
return r - (y + d[x] - 1);
}
int main() {
#ifdef memset0
freopen("K.in", "r", stdin);
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> T;
while (T--) {
cin >> n >> K;
ans = 0;
for (int i = 1; i <= n; ++i) in[i] = ad[i] = 0;
for (int i = 1; i <= n; ++i) {
vector<int>().swap(app[i]);
vector<int>().swap(del[i]);
}
for (int i = 1; i <= n; ++i) lst[i] = n + 1;
for (int i = 1; i <= n; ++i) cin >> a[i], d[i] = a[i] - K;
nw = tot = 0;
sz = 0, pos[sz] = n + 1;
p = 1, r = n + 1;
for (int i = n; i >= 1; --i) {
// log("-------------i=%d\n", i);
tot += nw;
r = min(r, lst[a[i]]);
// log("R=%d\n", r);
lst[a[i]] = i;
for (int j : app[i])
if (in[j]) {
++tot;
ad[j] = 1;
++nw;
}
while (sz > 0 && a[pos[sz]] <= a[i]) {
t = pos[sz--];
if (in[t]) {
tot -= Calc(nx[t], t, i);
in[t] = 0;
if (ad[t]) {
ad[t] = 0;
--nw;
}
}
}
// log("sz=%d:\n", sz);
// for (int j = 1; j <= sz; ++j) {
// log("%d%c", pos[j], " \n"[j == sz]);
// }
while (p <= sz && pos[p] >= r) {
++p;
if (p <= sz) {
t = pos[p];
tot -= Calc(nx[t], pos[t], i);
in[t] = 0;
if (ad[t]) ad[t] = 0, --nw;
}
}
// log("p=%d,tot=%d\n", p, tot);
t = nx[i] = pos[sz];
if (p > sz) {
assert(!tot && !nw);
pos[++sz] = i, p = sz;
} else {
pos[++sz] = i;
in[i] = 1;
if (t - d[i] >= i) {
tot += Calc(nx[i], i, i);
ad[i] = 1;
++nw;
if (i - d[i] + 1 > 0) del[min(i - d[i] + 1, i)].push_back(i);
} else {
if (t - d[i] > 0) app[t - d[i]].push_back(i);
if (i - d[i] + 1 > 0) del[i - d[i] + 1].push_back(i);
}
}
for (int j : del[i]) {
if (ad[j]) ad[j] = 0, --nw;
}
// log("LAST=%d\n", Calc(r, pos[p], i));
ans += tot + Calc(r, pos[p], i);
// log("ANS=%lld\n", ans);
}
cout << ans << "\n";
}
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
13 10000 9950 8745 9998 467 7434 1964 9375 9997 48 8597 2568 3761 9498 1576 6348 4819 4870 9997 9043 8623 2147 8768 9555 9709 2657 5124 2631 3010 2249 2212 2997 9996 4320 796 5189 5556 5548 5290 2026 5555 187 9090 9149 908 8714 5238 3236 3986 5510 8571 3020 5118 2522 2465 384 9105 1342 8220 8291 672...