P14426
先按 坐标排序,然后对于分治区间 ,我们分两种情况,设点对为 :
-
1、左半边没有不符合的点,即 ,我们可以用单调栈维护一下后缀最大 ,用树状数组处理可选点。
-
2、右半边没有不符合的点,那么我们就要找 中的 值最大的,那么要选左端点也只能选比最大值大的,用树状数组维护即可。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 10;
struct node{
int x, y;
}a[N];
bool cmp(node a, node b){
return a.y < b.y;
}
int n, lsh[N], m, tr[N], tr2[N], ans;
void add1(int x, int y){
for(; x <= m; x += (x & -x)) tr[x] += y;
}
void add2(int x, int y){
for(; x <= m; x += (x & -x)) tr2[x] = max(tr2[x], y);
}
int query1(int x){ int res = 0;
for(; x; x -= (x & -x)) res += tr[x];
return res;
}
int query2(int x){ int res = 0;
for(; x; x -= (x & -x)) res = max(res, tr2[x]);
return res;
}
void clear(int x){
for(; x <= m; x += (x & -x)) tr2[x] = 0;
}
void cdq(int l, int r){
if(l >= r) return ;
int mid = (l + r) >> 1;
cdq(l, mid), cdq(mid + 1, r);
sort(a + l, a + mid + 1, cmp);
sort(a + mid + 1, a + r + 1, cmp);
stack<int> s;
for(int i = mid + 1, j = l; i <= r; i++){
for(; j <= mid && a[j].y < a[i].y; j++){
while(!s.empty() && a[s.top()].x < a[j].x) add1(a[s.top()].y, -1), s.pop();
add1(a[j].y, 1), s.push(j);
}
ans += query1(a[i].y) - query1(query2(a[i].x)), add2(a[i].x, a[i].y);
}
while(!s.empty()) add1(a[s.top()].y, -1), s.pop();
for(int i = mid + 1; i <= r; i++) clear(a[i].x);
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y, lsh[++m] = a[i].x, lsh[++m] = a[i].y;
sort(lsh + 1, lsh + 1 + m);
m = unique(lsh + 1, lsh + 1 + m) - lsh - 1;
for(int i = 1; i <= n; i++){
a[i].x = lower_bound(lsh + 1, lsh + 1 + m, a[i].x) - lsh, a[i].y = lower_bound(lsh + 1, lsh + 1 + m, a[i].y) - lsh;
}
sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.x < b.x; });
cdq(1, n);
cout << ans;
return 0;
}P3364
这题相当于P4093,注意递归顺序即可。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 10;
struct node{
int l, s, w, a, x;
}a[N], b[N];
int n, dp[N], tr[N], lsh[N], m, ans = 1;
void add(int x, int y) {
for(; x <= m; x += (x & -x)) tr[x] = max(y, tr[x]);
}
int query(int x){ int res = 0;
for(; x; x -= (x & -x)) res = max(res, tr[x]);
return res;
}
void clear(int x){
for(; x <= m; x += (x & -x)) tr[x] = 0;
}
void cdq(int l, int r){
if(l >= r){
if(l == r) a[l].x = max(a[l].x, 1ll);
return ;
}
int mid = (l + r) >> 1;
sort(a + l, a + r + 1, [](const node &a, const node &b){ return a.l < b.l; }); cdq(l, mid);
sort(a + l, a + mid + 1, [](const node &a, const node &b){ return a.a < b.a; });
sort(a + mid + 1, a + r + 1, [](const node &a, const node &b){ return a.s < b.s; });
int i = mid + 1, j = l;
while(i <= r && j <= mid){
if(a[i].s >= a[j].a) add(a[j].w, a[j].x), j++;
else a[i].x = max(query(a[i].a) + 1, a[i].x), i++;
}
while(j <= mid) add(a[j].w, a[j].x), j++;
while(i <= r) a[i].x = max(query(a[i].a) + 1, a[i].x), i++;
for(int i = l; i <= mid; i++) clear(a[i].w);
cdq(mid + 1, r);
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1, l, s, w, aa; i <= n; i++){
cin >> l >> s >> w >> aa; a[i] = {l, s, w, aa, 0};
lsh[++m] = s, lsh[++m] = w, lsh[++m] = aa;
}
sort(lsh + 1, lsh + 1 + m); m = unique(lsh + 1, lsh + 1 + m) - lsh - 1;
sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.l < b.l; });
for(int i = 1; i <= n; i++){
a[i].w = lower_bound(lsh + 1, lsh + 1 + m, a[i].w) - lsh;
a[i].s = lower_bound(lsh + 1, lsh + 1 + m, a[i].s) - lsh;
a[i].a = lower_bound(lsh + 1, lsh + 1 + m, a[i].a) - lsh;
}
cdq(1, n);
for(int i = 1; i <= n; i++){
ans = max(ans, a[i].x);
}
cout << ans;
return 0;
}P11197
这题的 没用,那么问题就变成了要单 的三维偏序(双 也行),发现可以用二项式反演的形式:
设 为恰好 维满足, 为固定 维满足,用前缀型反演即可,想用后缀就反过来变成不满足就行了。
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
struct node{
int a, b;
}a[N];
int n, t[N], p1[N], p2[N], p3[N];
long long tr[N], ans;
void add(int x, int y){
for(; x <= n; x += (x & -x)) tr[x] += y;
}
int query(int x){ int res = 0;
for(; x; x -= (x & -x)) res += tr[x];
return res;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1, x; i <= n; i++) cin >> x, t[x] = i;
for(int i = 1, x; i <= n; i++) cin >> x, p1[x] = i;
for(int i = 1, x; i <= n; i++) cin >> x, p2[x] = i;
for(int i = 1, x; i <= n; i++) cin >> x, p3[x] = i;
for(int i = 1; i <= n; i++) a[i] = {p1[i], p2[i]};
sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.a < b.a; });
for(int i = 1; i <= n; i++) ans += query(a[i].b), add(a[i].b, 1);
for(int i = 1; i <= n; i++) a[i] = {p1[i], p3[i]}, tr[i] = 0;
sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.a < b.a; });
for(int i = 1; i <= n; i++) ans += query(a[i].b), add(a[i].b, 1);
for(int i = 1; i <= n; i++) a[i] = {p2[i], p3[i]}, tr[i] = 0;
sort(a + 1, a + 1 + n, [](const node &a, const node &b){ return a.a < b.a; });
for(int i = 1; i <= n; i++) ans += query(a[i].b), add(a[i].b, 1);
cout << (ans - 1ll * n * (n - 1) / 2) / 2;
return 0;
}