QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#879253 | #9973. 魔法少女网站第二部 | zlt | TL | 1941ms | 216072kb | C++14 | 8.0kb | 2025-02-01 22:52:07 | 2025-02-01 22:52:07 |
Judging History
answer
// Problem: P11367 [Ynoi2024] 魔法少女网站第二部
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P11367
// Memory Limit: 512 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define uint unsigned
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;
namespace IO {
const int maxn = 1 << 20;
char ibuf[maxn], *iS, *iT, obuf[maxn], *oS = obuf;
inline char gc() {
return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin), (iS == iT ? EOF : *iS++) : *iS++);
}
template<typename T = int>
inline T read() {
char c = gc();
T x = 0;
bool f = 0;
while (c < '0' || c > '9') {
f |= (c == '-');
c = gc();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = gc();
}
return f ? ~(x - 1) : x;
}
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
struct Flusher {
~Flusher() {
flush();
}
} AutoFlush;
inline void pc(char ch) {
if (oS == obuf + maxn) {
flush();
}
*oS++ = ch;
}
template<typename T>
inline void write(T x) {
static char stk[64], *tp = stk;
if (x < 0) {
x = ~(x - 1);
pc('-');
}
do {
*tp++ = x % 10;
x /= 10;
} while (x);
while (tp != stk) {
pc((*--tp) | 48);
}
}
template<typename T>
inline void writesp(T x) {
write(x);
pc(' ');
}
template<typename T>
inline void writeln(T x) {
write(x);
pc('\n');
}
}
using IO::read;
using IO::write;
using IO::pc;
using IO::writesp;
using IO::writeln;
const int maxn = 2000100;
const int inf = 0x3f3f3f3f;
int n, m, a[maxn], b[maxn], d[maxn], f[maxn], g[maxn], pre[maxn], suf[maxn];
pii p[maxn], q[maxn], h[maxn];
struct node {
int l, r;
} c[maxn];
ll ans[maxn];
struct List {
int hd[maxn * 3], len, val[maxn << 1], nxt[maxn << 1];
inline void add(int x, int y) {
val[++len] = y;
nxt[len] = hd[x];
hd[x] = len;
}
} G, T;
namespace BIT {
int n;
ll c[maxn];
inline void init(int _n) {
n = _n;
for (int i = 0; i <= n; ++i) {
c[i] = 0;
}
}
inline void update(int x, int d) {
if (!d) {
return;
}
for (int i = x; i <= n; i += (i & (-i))) {
c[i] += d;
}
}
inline ll query(int x) {
ll res = 0;
for (int i = x; i; i -= (i & (-i))) {
res += c[i];
}
return res;
}
inline ll query(int l, int r) {
return query(r) - query(l - 1);
}
}
int fa[maxn], e[maxn];
inline int find(int x) {
while (fa[x] != x) {
x = fa[x] = fa[fa[x]];
}
return x;
}
void update(int rt, int l, int r, int ql, int qr, int i) {
int mid = (l + r) >> 1;
if (qr <= mid) {
update(rt << 1, l, mid, ql, qr, i);
} else if (ql > mid) {
update(rt << 1 | 1, mid + 1, r, ql, qr, i);
} else {
T.add(rt, i);
}
}
void dfs(int rt, int l, int r) {
if (l == r) {
return;
}
int mid = (l + r) >> 1;
dfs(rt << 1, l, mid);
dfs(rt << 1 | 1, mid + 1, r);
inplace_merge(p + l, p + mid + 1, p + r + 1);
G.len = 0;
for (int i = l; i <= r; ++i) {
G.hd[i] = 0;
}
for (int i = l; i <= r; ++i) {
d[p[i].scd] = i - l + 1;
}
for (int i = l; i <= r; ++i) {
b[d[i]] = i;
}
for (int i = T.hd[rt]; i; i = T.nxt[i]) {
int j = T.val[i];
G.add(c[j].l, j);
G.add(c[j].r, j);
}
ll s = 0;
int mn = inf, mx = 0, tot = r - l + 1;
int lst0 = 0, lst1 = 0, mx0 = 0, mn1 = inf;
for (int i = 1; i <= tot; ++i) {
if (b[i] <= mid) {
if (lst0) {
f[b[lst0]] = mn1;
}
mn1 = inf;
lst0 = i;
mx0 = max(mx0, b[i]);
} else {
if (lst1) {
g[b[lst1]] = mx0;
}
mx0 = 0;
lst1 = i;
mn1 = min(mn1, b[i]);
}
}
f[b[lst0]] = inf;
g[b[lst1]] = 0;
for (int i = 1; i <= tot; ++i) {
if (b[i] <= mid) {
fa[i] = e[i] = i;
} else {
fa[i] = fa[i - 1];
e[fa[i]] = i;
}
}
fa[0] = e[0] = 0;
for (int i = l; i <= mid; ++i) {
q[i].scd = e[d[i]] + 1;
int t = find(d[i] - 1);
fa[d[i]] = q[i].fst = t;
h[i].scd = f[i];
e[t] = e[d[i]];
if (t) {
t = b[t];
h[i].fst = f[t];
f[t] = min(f[t], f[i]);
}
}
pre[0] = suf[tot + 1] = inf;
for (int i = 1; i <= tot; ++i) {
pre[i] = min(pre[i - 1], b[i] > mid ? b[i] : inf);
}
for (int i = tot; i; --i) {
suf[i] = min(suf[i + 1], b[i] > mid ? b[i] : inf);
}
BIT::init(r - mid);
for (int i = mid; i >= l; --i) {
mn = min(mn, d[i]);
mx = max(mx, d[i]);
int j = q[i].fst, k = q[i].scd;
if (j) {
s += abs(i - b[j]);
}
if (k <= tot) {
s += abs(i - b[k]);
}
if (j && k <= tot) {
s -= abs(b[j] - b[k]);
if (h[i].fst < h[i].scd) {
if (h[i].fst <= r) {
BIT::update(h[i].fst - mid, (mid - max(i, b[j])) * 2 - (mid - max(b[j], b[k])) * 2);
}
if (h[i].scd <= r) {
BIT::update(h[i].scd - mid, (mid - max(i, b[k])) * 2);
}
} else {
if (h[i].fst <= r) {
BIT::update(h[i].fst - mid, (mid - max(i, b[j])) * 2);
}
if (h[i].scd <= r) {
BIT::update(h[i].scd - mid, (mid - max(i, b[k])) * 2 - (mid - max(b[j], b[k])) * 2);
}
}
} else if (!j && k <= tot && h[i].scd <= r) {
BIT::update(h[i].scd - mid, (mid - max(i, b[k])) * 2);
} else if (j && k > tot && h[i].fst <= r) {
BIT::update(h[i].fst - mid, (mid - max(i, b[j])) * 2);
}
for (int _ = G.hd[i]; _; _ = G.nxt[_]) {
int j = G.val[_];
ans[j] += s + BIT::query(c[j].r - mid);
if (suf[mx + 1] <= c[j].r) {
ans[j] += mid - b[mx];
}
if (pre[mn - 1] <= c[j].r) {
ans[j] += mid - b[mn];
}
}
}
s = mx = 0;
mn = inf;
for (int i = 1; i <= tot; ++i) {
if (b[i] > mid) {
fa[i] = e[i] = i;
} else {
fa[i] = fa[i - 1];
e[fa[i]] = i;
}
}
fa[0] = 0;
for (int i = r; i > mid; --i) {
q[i].scd = e[d[i]] + 1;
int t = find(d[i] - 1);
fa[d[i]] = q[i].fst = t;
h[i].scd = g[i];
e[t] = e[d[i]];
if (t) {
t = b[t];
h[i].fst = g[t];
g[t] = max(g[t], g[i]);
}
}
pre[0] = suf[tot + 1] = 0;
for (int i = 1; i <= tot; ++i) {
pre[i] = max(pre[i - 1], b[i] <= mid ? b[i] : 0);
}
for (int i = tot; i; --i) {
suf[i] = max(suf[i + 1], b[i] <= mid ? b[i] : 0);
}
BIT::init(mid - l + 1);
for (int i = mid + 1; i <= r; ++i) {
mn = min(mn, d[i]);
mx = max(mx, d[i]);
int j = q[i].fst, k = q[i].scd;
if (j) {
s += abs(i - b[j]);
}
if (k <= tot) {
s += abs(i - b[k]);
}
if (j && k <= tot) {
s -= abs(b[j] - b[k]);
if (h[i].fst > h[i].scd) {
if (h[i].fst >= l) {
BIT::update(h[i].fst - l + 1, (min(i, b[j]) - mid) * 2 - (min(b[j], b[k]) - mid) * 2);
}
if (h[i].scd >= l) {
BIT::update(h[i].scd - l + 1, (min(i, b[k]) - mid) * 2);
}
} else {
if (h[i].fst >= l) {
BIT::update(h[i].fst - l + 1, (min(i, b[j]) - mid) * 2);
}
if (h[i].scd >= l) {
BIT::update(h[i].scd - l + 1, (min(i, b[k]) - mid) * 2 - (min(b[j], b[k]) - mid) * 2);
}
}
} else if (!j && k <= tot && h[i].scd >= l) {
BIT::update(h[i].scd - l + 1, (min(i, b[k]) - mid) * 2);
} else if (j && k > tot && h[i].fst >= l) {
BIT::update(h[i].fst - l + 1, (min(i, b[j]) - mid) * 2);
}
for (int _ = G.hd[i]; _; _ = G.nxt[_]) {
int j = G.val[_];
ans[j] += s + BIT::query(c[j].l - l + 1, mid - l + 1);
if (suf[mx + 1] >= c[j].l) {
ans[j] += b[mx] - mid;
}
if (pre[mn - 1] >= c[j].l) {
ans[j] += b[mn] - mid;
}
}
}
}
void solve() {
n = read();
m = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
p[i] = pii(a[i], i);
}
for (int i = 1; i <= m; ++i) {
c[i].l = read();
c[i].r = read();
if (c[i].l != c[i].r) {
update(1, 1, n, c[i].l, c[i].r, i);
}
}
dfs(1, 1, n);
for (int i = 1; i <= m; ++i) {
writeln(ans[i]);
}
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1913ms
memory: 216072kb
input:
1999999 1999998 8 9 6 4 1 7 5 13 3 16 2 20 14 10 12 17 21 11 27 29 19 23 15 18 24 26 22 31 28 37 36 25 38 41 34 43 35 30 33 45 51 32 52 42 50 44 39 40 58 56 48 49 46 59 47 63 60 57 54 55 66 53 65 62 67 61 72 75 68 69 71 79 77 64 85 82 74 73 70 87 76 81 78 88 91 90 84 97 80 89 93 99 83 98 96 86 95 10...
output:
918614 3322452 5149417 6621380 6533731 4094511 1379510 12924019 8157295 207612 6762916 1785007 4192984 2683252 8261655 6023203 9129238 1758632 6933498 3993947 4345067 11514930 5784142 3105482 8690594 6483330 5398139 146567 1726131 678045 7787022 2468410 8925300 2915763 2410040 1092694 568436 3475699...
result:
ok 1999998 numbers
Test #2:
score: 0
Accepted
time: 1941ms
memory: 215452kb
input:
2000000 2000000 1 5 10 6 7 2 16 13 3 8 15 12 4 14 17 11 9 22 20 27 30 24 21 28 18 19 25 35 33 29 23 26 43 41 44 39 32 34 31 36 40 42 45 37 48 54 38 46 59 47 52 62 63 49 50 51 58 56 64 53 60 57 61 66 55 65 77 69 75 70 67 76 80 72 74 85 68 71 89 73 79 78 83 90 86 81 96 94 82 97 93 84 98 100 99 88 87 9...
output:
313920 6698345 2961193 3760894 1903023 549714 5500986 1427297 168364 7880151 7786970 4914863 8011527 5473090 10758103 3603965 1922957 7492511 6241425 8034137 3953170 4224180 10918550 8192854 10607899 564778 493566 2426821 12903222 1008307 1113749 5046569 2226134 403996 4119770 874567 2775979 3062657...
result:
ok 2000000 numbers
Test #3:
score: 0
Accepted
time: 369ms
memory: 113720kb
input:
1997 1993006 687 310 1393 747 1050 349 1814 1842 638 1694 321 1613 1034 1724 1579 1292 1761 1387 1213 463 1884 1882 835 316 1722 8 1743 473 978 1274 403 1617 1711 156 1227 40 1858 851 1625 1787 1779 1289 472 1103 1739 1563 507 1384 1804 1079 199 347 1462 1891 198 583 1112 1399 940 1958 1830 318 1312...
output:
1240830 1253194 1124612 1178272 1124252 1207336 1222464 1067970 1182422 1294456 1081384 1134164 1028222 1303360 1176576 1333256 1217496 1184112 826586 1321466 907642 1200660 1338438 1037922 1150322 1107906 1225848 947268 763606 1312232 1092234 1048136 1276728 1282240 1320450 826070 1150624 1089414 1...
result:
ok 1993006 numbers
Test #4:
score: 0
Accepted
time: 289ms
memory: 102000kb
input:
2000 1999000 7 8 1120 3 2 6 1 5 1118 4 15 1119 14 16 18 17 19 12 10 13 11 9 1125 1126 1124 1128 653 1127 1121 1123 1122 1137 1136 620 1130 1129 598 661 599 1160 654 656 655 608 1159 1157 1158 1153 657 1154 585 1155 584 658 588 586 587 1156 609 1133 1132 621 1131 611 597 660 596 1134 595 659 610 1135...
output:
21604 8101 19995 14882 8432 877 5904 13612 1227 3406 25246 19909 10398 3746 992 6920 7365 3098 9999 12602 13469 5809 9235 5502 18869 2545 20293 9800 9162 22085 629 17750 9312 6800 3584 5285 1665 18997 8614 910 8170 3884 2539 16147 4482 6615 4589 56 14542 5125 2243 8915 11027 17562 901 13603 15113 35...
result:
ok 1999000 numbers
Test #5:
score: -100
Time Limit Exceeded
input:
1999998 1999996 1950413 1996247 1995151 1968122 1967590 1953664 1990263 1993028 1840495 1948577 1937627 1996103 1985373 1990286 1941726 1980603 1911368 1997777 1980827 1911708 1857169 1908738 1988483 1981228 1964408 1973964 1988108 1997405 1974863 1955238 1931220 1956903 1979158 1973709 1994885 1927...
output:
23360918224 17249116985 137365596 7383633917 344733205 13693676447 45751450613 41799074078 18749658821 8191544190 21313767745 10345260569 28616032820 24769453037 13508471201 11284312607 46246584 39927786203 32769454327 29036442020 4659551922 13004993101 14277007260 14959218808 41953497036 1454775489...