转化一下,令 $a_x = y$,再做一遍这题,就可以过了。
这个转化挺显然的吧。
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 15;
int n, a[N], pos[N];
long long ans = 0;
struct Tree {
int l, r;
int Min, cnt;
int flag;
} tr[N << 2];
void pushup(int u) {
tr[u].Min = min(tr[u << 1].Min, tr[u << 1 | 1].Min);
tr[u].cnt = 0;
if (tr[u << 1].Min == tr[u].Min) tr[u].cnt += tr[u << 1].cnt;
if (tr[u << 1 | 1].Min == tr[u].Min) tr[u].cnt += tr[u << 1 | 1].cnt;
}
void pushdown(int u) {
if (tr[u].flag) {
tr[u << 1].Min += tr[u].flag;
tr[u << 1 | 1].Min += tr[u].flag;
tr[u << 1].flag += tr[u].flag;
tr[u << 1 | 1].flag += tr[u].flag;
tr[u].flag = 0;
}
}
void build(int u, int l, int r) {
tr[u].l = l, tr[u].r = r;
if (l == r) {
tr[u].Min = 0, tr[u].cnt = 1;
return;
}
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void change(int u, int l, int r, int d) {
if (tr[u].r < l || tr[u].l > r) return;
if (tr[u].l >= l && tr[u].r <= r) {
tr[u].Min += d, tr[u].flag += d;
return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) change(u << 1, l, r, d);
if (r > mid) change(u << 1 | 1, l, r, d);
pushup(u);
}
int query(int u, int l, int r) {
if (tr[u].r < l || tr[u].l > r) return 0;
if (tr[u].l >= l && tr[u].r <= r) {
if (tr[u].Min <= 1) return tr[u].cnt;
return 0;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1, res = 0;
if (l <= mid) res += query(u << 1, l, r);
if (r > mid) res += query(u << 1 | 1, l, r);
return res;
}
bool st[N];
int main() {
scanf("%d", &n);
for (int i = 1, x, y; i <= n; i++) {
scanf("%d%d", &x, &y);
a[x] = y, pos[y] = x;
}
build(1, 1, n);
for (int i = 1; i <= n; i++) {
change(1, 1, i, 1);
if (st[pos[i] - 1]) change(1, 1, a[pos[i] - 1], -1);
if (st[pos[i] + 1]) change(1, 1, a[pos[i] + 1], -1);
ans += query(1, 1, i);
st[pos[i]] = 1;
}
printf("%lld\n", ans);
return 0;
}
咋写cf题啊,不是提交不了吗,邦账号也不行啊
那就在 CF 提交啊