A - Circuits
不难发现x坐标根本没用,只需要存储y坐标。
题目所求的两条直线y1=ay_1=ay1=a,y2=b(a<b)y_2=b\ (a<b)y2=b (a<b)
我们枚举y2=by_2=by2=b这条线,这条线一定可以是矩形的边界,于是我们扫描矩形边界差分计算当前这条线覆盖的矩形个数,对于这条线没有覆盖的矩形把它丢到线段树中(维护区间+和区间max即可)然后区间查询y1=ay_1=ay1=a覆盖的最大矩形即可。两种相加即是当前的情况的最大数量。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=998244353;
const int N=200010;
int n,m;
struct line
{int id,v,op;bool operator<(const line &o) const{if(v==o.v) return op<o.op;return v<o.v;}
}q[N];
int y[N];
pii a[N];
int find(int x)
{return lower_bound(y+1,y+1+m,x)-y;
}
struct node
{int l,r;int v,lazy;}tree[N*4];
void pushup(int u)
{tree[u].v=max(tree[u<<1].v,tree[u<<1|1].v);
}
void build(int u,int l,int r)
{tree[u]={l,r};if(l==r) {tree[u].v=0;return;}int mid=l+r>>1;build(u<<1,l,mid),build(u<<1|1,mid+1,r);pushup(u);
}
void pushdown(int u)
{if(!tree[u].lazy) return;tree[u<<1].lazy+=tree[u].lazy;tree[u<<1|1].lazy+=tree[u].lazy;tree[u<<1].v+=tree[u].lazy;tree[u<<1|1].v+=tree[u].lazy;tree[u].lazy=0;}
void modify(int u,int l,int r,int v)
{if(l>r) return;if(tree[u].l>=l&&tree[u].r<=r){tree[u].lazy+=v;tree[u].v+=v;return;}pushdown(u);int mid=tree[u].l+tree[u].r>>1; l+tree[u].r>>1;if(l<=mid) modify(u<<1,l,r,v);if(r>mid) modify(u<<1|1,l,r,v);pushup(u);
}
int main()
{IO;int T=1;//cin>>T;while(T--){cin>>n;for(int i=1;i<=n;i++){int t1,t2;cin>>t1>>y[i];cin>>t2>>y[i+n];a[i]={y[i+n],y[i]};y[i]++;q[i]={i,y[i],-1};q[i+n]={i,y[i+n],+1};}sort(y+1,y+1+2*n);m=unique(y+1,y+1+2*n)-y-1;build(1,1,m);sort(q+1,q+1+2*n);int pre=0;int res=0;for(int i=1;i<=2*n;i++){pre+=q[i].op;res=max(res,pre+tree[1].v);if(q[i].op==-1){int id=q[i].id;modify(1,find(a[id].first),find(a[id].second+1)-1,1);}}cout<<res<<'\n';}return 0;
}
这题有一个错误的贪心思路即求两边最大值。
先让第一条线覆盖最多的矩形,然后把这些矩形删除,然后再求出第二条线覆盖最多的矩形。
我也不知道这个贪心思路错在哪里(还没举出反例
先贴一个队友写的错误代码
反例:凑合看吧(懂得都懂
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#define N 20000005
using namespace std;
const int m = 10000000,inf = 20000000;
int n;
struct operation{int c1,c2,r1,r2;
}op[100005];
int x[N],y[N],cans1,cans2;
int read(){char ch = getchar();int re = 0,fl = 1;while(ch<'0'||ch>'9') {if(fl == '-')fl = -1; ch = getchar();}while(ch>='0'&&ch<='9') {re = (re<<1)+(re<<3)+ch-'0'; ch = getchar();}return re*fl;
}
int main(){//freopen("1.in","r",stdin); int r1,r2,c1,c2,rf = inf,rl = 0,cf = inf,cl = 0;int pre = 0,fd = 0; n = read();for(int i=1;i<=n;++i){r1 = read()+m; c1 = read()+m;//r1<r2 c1>c2r2 = read()+m; c2 = read()+m;y[c1+1]--; y[c2]++;op[i].c1 = c1; op[i].c2 = c2; op[i].r1 = r1; op[i].r2 = r2;}pre = 0; fd = -1;for(int i=0;i<=2*m;++i){pre += y[i];if(pre >= cans1){cans1 = pre; fd = i;}}for(int i=1;i<=n;++i)if(op[i].c1 >= fd && op[i].c2 <= fd){y[op[i].c1+1]++; y[op[i].c2]--;}pre = 0;for(int i=0;i<=2*m;++i){pre += y[i];cans2 = max(cans2,pre);}printf("%d\n",cans1+cans2);return 0;
}