题解:P14379 【MX-S9-T2】「LAOI-16」摩天大楼
题意
给定长度为
这里
题解
简单题。
正难则反,考虑求出有多少区间
对于第一种区间,枚举相邻的两个
考虑动态维护上面的两类式子。容易想到开两个 set 分别维护
:::success[代码]
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x) & -(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
const int N = 1e6 + 5;
template<typename T> inline void chk_min(T &x, T y) { x = min(x, y); }
template<typename T> inline void chk_max(T &x, T y) { x = max(x, y); }
int n, q, a[N];
ll sum1, sum2;
set<int> s1, s2;
struct BIT {
int c[N];
inline int query(int x) {
int res = 0;
for (; x; x -= lowbit(x)) res += c[x];
return res;
}
inline int query(int l, int r) { return query(r) - query(l - 1); }
inline void add(int x, int v) { for (; x <= n; x += lowbit(x)) c[x] += v; }
} ft;
ll calc(int x) { return (ll)x * (x - 1) >> 1; }
void upd1(int x, bool tp) {
int d = tp ? 1 : -1;
auto nxt = s2.upper_bound(x), prv = prev(nxt);
int cnt1 = ft.query(*prv + 1, *nxt - 1), cnt2 = cnt1 + d;
ft.add(x, d);
sum2 += calc(cnt2) - calc(cnt1);
prv = prev(s1.lower_bound(x)), nxt = s1.upper_bound(x);
sum1 += (calc(x - *prv - 1) + calc(*nxt - x - 1) - calc(*nxt - *prv - 1)) * d;
if (tp) s1.insert(x); else s1.erase(x);
}
void upd2(int x, bool tp, bool flag = 0) {
int d = tp ? 1 : -1;
auto prv = prev(s2.lower_bound(x)), nxt = s2.upper_bound(x);
sum2 += (calc(ft.query(*prv + 1, x - 1)) + calc(ft.query(x + 1, *nxt - 1)) - calc(ft.query(*prv + 1, *nxt - 1))) * d;
if (tp) s2.insert(x); else s2.erase(x);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> q;
s1.insert(0), s2.insert(0);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[i] == 1) {
ft.add(i, 1);
int prv = *prev(s1.end());
sum1 += calc(i - prv - 1), s1.insert(i);
} else if (a[i] == 2) {
int prv = *prev(s2.end());
sum2 += calc(ft.query(prv + 1, i - 1)), s2.insert(i);
}
}
int prv = *prev(s1.end()); sum1 += calc(n - prv), s1.insert(n + 1);
prv = *prev(s2.end()), sum2 += calc(ft.query(prv + 1, n)), s2.insert(n + 1);
while (q--) {
int x, v; cin >> x >> v;
if (a[x] == 1) upd1(x, 0);
else if (a[x] == 2) upd2(x, 0);
a[x] = v;
if (v == 1) upd1(x, 1);
else if (v == 2) upd2(x, 1);
cout << calc(n) - sum1 - sum2 << '\n';
}
return 0;
}
:::