【0】简短概括扫描线
众所周知,扫描线是一种离线算法,通过将询问离线,指针扫描一个维度,数据结构维护另一个维度,做到单 处理二维问题(一般这种问题是可差分的)。
本文只讨论扫描线处理离线二维数点问题(矩形面积并可能会更,但最近肯定是咕的)。
【2】离线二维数点
直接看题:【模板】离线二维数点。
本题中的答案是可以差分的(拆成 这一段的贡献和 这一段的贡献)。
将所有需要求解的贡献按照右端点排序(相当于按第一维排序),从左往右扫描第一维,依次加入 ,树状数组维护当前第二维度的信息(query(i) 代表查询已经加入的数中有多少个数小于等于 )。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e6 + 9,M = 2e6 + 9,V = 2e6 + 9;
int n,m;
int a[N],max_a;
int BIT[V];
int lowbit(int x){
return x & (-x);
}
void update(int pos,int v){
for(int i = pos;i <= max_a;i += lowbit(i))
BIT[i] += v;
}
int query(int pos){
int ret = 0;
for(int i = pos;i;i -= lowbit(i))
ret += BIT[i];
return ret;
}
struct QUERY{
int pos,x,typ,id;
} q[N << 1];
bool cmp(QUERY q1,QUERY q2){
return q1.pos < q2.pos;
}
int top;
int ans[M];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= n;i++){
cin >> a[i];
max_a = max(max_a,a[i]);
}
for(int i = 1;i <= m;i++){
int l,r,x;
cin >> l >> r >> x;
q[++top] = (QUERY){l - 1,x,-1,i};
q[++top] = (QUERY){r,x,1,i};
}
sort(q + 1,q + top + 1,cmp);
for(int i = 1;i <= top;i++){
for(int j = q[i - 1].pos + 1;j <= q[i].pos;j++)//加点
update(a[j],1);
ans[q[i].id] += q[i].typ * query(q[i].x);//查询贡献
}
for(int i = 1;i <= m;i++)
cout << ans[i] << '\n';
return 0;
}例题 1:园丁的烦恼
一样的套路,注意要离散化。
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 9,M = 5e5 + 9,V = 2e6 + 9;
int n,m;
struct point{
int x,y;
} p[N];
struct rect{
int x1,x2,y1,y2;
} r[M];
int tmp[2][N + M << 1],top[2];
vector<int> vec[V];
int BIT[V];
int lowbit(int x){
return x & (-x);
}
void update(int pos,int v){
for(int i = pos;i <= top[1];i += lowbit(i))
BIT[i] += v;
}
int query(int pos){
int ret = 0;
for(int i = pos;i;i -= lowbit(i))
ret += BIT[i];
return ret;
}
struct QUERY{
int x,y,typ,id;
} q[N << 2];
bool cmp(QUERY q1,QUERY q2){
return (q1.x ^ q2.x) ? q1.x < q2.x : q1.y < q2.y;
}
int cnt;
long long ans[M];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= n;i++){
cin >> p[i].x >> p[i].y;
tmp[0][++top[0]] = p[i].x;
tmp[1][++top[1]] = p[i].y;
}
for(int i = 1;i <= m;i++){
cin >> r[i].x1 >> r[i].y1 >> r[i].x2 >> r[i].y2;
tmp[0][++top[0]] = r[i].x1;
tmp[1][++top[1]] = r[i].y1;
tmp[0][++top[0]] = r[i].x2;
tmp[1][++top[1]] = r[i].y2;
}
sort(tmp[0] + 1,tmp[0] + top[0] + 1);
sort(tmp[1] + 1,tmp[1] + top[1] + 1);
top[0] = unique(tmp[0] + 1,tmp[0] + top[0] + 1) - tmp[0] - 1;
top[1] = unique(tmp[1] + 1,tmp[1] + top[1] + 1) - tmp[1] - 1;
for(int i = 1;i <= n;i++){
p[i].x = lower_bound(tmp[0] + 1,tmp[0] + top[0] + 1,p[i].x) - tmp[0];
p[i].y = lower_bound(tmp[1] + 1,tmp[1] + top[1] + 1,p[i].y) - tmp[1];
}
for(int i = 1;i <= m;i++){
r[i].x1 = lower_bound(tmp[0] + 1,tmp[0] + top[0] + 1,r[i].x1) - tmp[0];
r[i].y1 = lower_bound(tmp[1] + 1,tmp[1] + top[1] + 1,r[i].y1) - tmp[1];
r[i].x2 = lower_bound(tmp[0] + 1,tmp[0] + top[0] + 1,r[i].x2) - tmp[0];
r[i].y2 = lower_bound(tmp[1] + 1,tmp[1] + top[1] + 1,r[i].y2) - tmp[1];
}
for(int i = 1;i <= n;i++)
vec[p[i].x].push_back(p[i].y);
for(int i = 1;i <= top[0];i++)
sort(vec[i].begin(),vec[i].end());
for(int i = 1;i <= m;i++){
q[++cnt] = (QUERY){r[i].x1 - 1,r[i].y1 - 1,1,i};
q[++cnt] = (QUERY){r[i].x1 - 1,r[i].y2,-1,i};
q[++cnt] = (QUERY){r[i].x2,r[i].y1 - 1,-1,i};
q[++cnt] = (QUERY){r[i].x2,r[i].y2,1,i};
}
sort(q + 1,q + cnt + 1,cmp);
for(int i = 1;i <= cnt;i++){
for(int j = q[i - 1].x + 1;j <= q[i].x;j++)
for(int k = 0;k < vec[j].size();k++)
update(vec[j][k],1);
ans[q[i].id] += q[i].typ * query(q[i].y);
}
for(int i = 1;i <= m;i++)
cout << ans[i] << '\n';
return 0;
}例题 2:HH的项链
稍微需要一点转化。
记 表示上一次 出现的位置(之前没有的记为 ),则对答案有贡献的数是区间 内满足 的数。
这样就转化成了模板题。
#include<bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lchild (root << 1)
#define rchild ((root << 1) + 1)
using namespace std;
const int N = 2e6 + 9,M = 2e6 + 9,V = 2e6 + 9;
int n,m;
int a[N];
int pre[N],last[V];
bool in_range(int l,int r,int L,int R){
return L <= l && r <= R;
}
bool out_range(int l,int r,int L,int R){
return l > R || r < L;
}
struct node{
int sum;
} t[N << 2];
void push_up(int root){
t[root].sum = t[lchild].sum + t[rchild].sum;
}
void update(int root,int l,int r,int pos){
if(l == r){
t[root].sum++;
return;
}
if(pos <= mid)
update(lchild,l,mid,pos);
else
update(rchild,mid + 1,r,pos);
push_up(root);
}
int query(int root,int l,int r,int L,int R){
if(out_range(l,r,L,R))
return 0;
if(in_range(l,r,L,R))
return t[root].sum;
return query(lchild,l,mid,L,R) + query(rchild,mid + 1,r,L,R);
}
struct QUERY{
int pos,x,typ,id;
} q[N << 1];
bool cmp(QUERY q1,QUERY q2){
return q1.pos < q2.pos;
}
int top;
int ans[M];
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];
pre[i] = last[a[i]];
last[a[i]] = i;
}
cin >> m;
for(int i = 1;i <= m;i++){
int l,r;
cin >> l >> r;
q[++top] = (QUERY){l - 1,l - 1,-1,i};
q[++top] = (QUERY){r,l - 1,1,i};
}
sort(q + 1,q + top + 1,cmp);
for(int i = 1;i <= top;i++){
for(int j = q[i - 1].pos + 1;j <= q[i].pos;j++)
update(1,0,n,pre[j]);
ans[q[i].id] += q[i].typ * query(1,0,n,0,q[i].x);
}
for(int i = 1;i <= m;i++)
cout << ans[i] << '\n';
return 0;
}