[APIO2018]新家

137 篇文章 1 订阅
11 篇文章 0 订阅

题目

传送门 to LOJ

思路

以位置为 x x x 轴、时间为 y y y 轴,每个店铺都是一条竖线,一个询问是一个点,要在它所在的 x x x 轴上(过它的水平线)求 max ⁡ ( min ⁡ d i s ) \max(\min dis) max(mindis)

显然这里有两个维度,我们会考虑扫描线或者 c d q \tt cdq cdq 干掉一维。如果选择干掉 x x x 轴,棘手之处在于询问是往左还是往右走到一个店铺。因为不同种类的店铺还不一样。

还是用正常思路,对 y y y 轴扫描线吧。然后询问怎么处理呢?对于这种 max ⁡ ( min ⁡ ) \max(\min) max(min) 的,显然可以二分答案。问题转化为,求一个区间内是否出现了 k k k 个种类的店铺?

我当时被降智了,想了这样的做法:每个种类的店铺,提供的限制无非是,当左端点 l ⩾ L 0 l\geqslant L_0 lL0 时,右端点 r ⩾ R 0 r\geqslant R_0 rR0 。所以线段树套 s e t \rm set set,标记永久化(便于删除)!

这样是 log ⁡ 2 n \log^2n log2n 的,并且不好优化。显然优化的方式一定是线段树上二分,以及找到 k k k 种颜色的共同点(如果不同,那么无法统一到一个线段树上,必然是 log ⁡ 2 n \log^2 n log2n 的了)。

重新回想,我们怎么求区间内不同颜色数量?每个点找到前驱(前面第一个与自己颜色相同的),如果一个点被包含,并且它的前驱没有被包含,那就统计贡献。

上面这个方法就可以忽略颜色的差异性!类似的,对于任意区间:存在一个点,它在区间右侧,且它的前驱在区间左侧,当且仅当这个区间内某种颜色没出现。

由于只需要任意一种颜色足够靠左,故而求前驱的最小值,记为 p r e pre pre 。现在只需找最大的 r r r 使得 p r e x + r ⩽ x − r pre_{x+r}\leqslant x-r prex+rxr

这玩意儿是可以在线段树上二分的,毫无疑问!于是时间复杂度变成了 O ( n log ⁡ n ) \mathcal O(n\log n) O(nlogn)

代码

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long int_;
inline int_ readint(){
	int_ a = 0; char c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}
inline void writeint(int_ x){
	if(x < 0) putchar('-'), x = -x;
	if(x > 9) writeint(x/10);
	putchar((x-x/10*10)^48);
}
inline void getMin(int&a,const int&b){
	if(b < a) a = b;
}
inline void getMax(int&a,const int&b){
	if(a < b) a = b;
}
inline int minInt(int a,const int &b){
	getMin(a,b); return a;
}

struct BestHeap{
	priority_queue< int,vector<int>,
		greater<int> > pq, del;
	bool empty(){
		while(!pq.empty() && !del.empty())
			if(pq.top() == del.top())
				pq.pop(), del.pop();
			else break;
		return pq.empty();
	}
	void push(int x){ pq.push(x); }
	void erase(int x){ del.push(x); }
	int top(){ empty(); return pq.top(); }
};

const int MaxN = 300005;
const int infty = (1<<30)-1;
int n; // length of Segment Tree
int tmp[MaxN]; // real coordinate

namespace SgTree{
	int val[MaxN<<2];
	# define LSON o<<1,l,(l+r)>>1
	# define RSON o<<1|1,((l+r)>>1)+1,r
	void pushUp(int o){
		val[o] = minInt(val[o<<1],val[o<<1|1]);
	}
	void modify(int qid,int qv,int o=1,int l=1,int r=n+1){
		if(l == r) return void(val[o] = qv);
		if((qid<<1) <= l+r) modify(qid,qv,LSON);
		else modify(qid,qv,RSON);
		pushUp(o); // as something has changed
	}
	int query(int qx,int qv=infty,int o=1,int l=1,int r=n+1){
		// printf("val[%d] = %d\n",o,val[o]);
		if(l == r) return tmp[l]; // exit
		if(qx >= tmp[(l+r)/2+1]) // at right part
			return query(qx,qv,RSON);
		int_ ez = (qx<<1)-minInt(qv,val[o<<1|1]);
		// printf("o = %d, ez = %d\n",o,ez);
		if(tmp[(l+r)/2+1] <= ez)
			return query(qx,qv,RSON);
		if(tmp[(l+r)/2] <= ez) return ez;
		getMin(qv,val[o<<1|1]); // attach
		return query(qx,qv,LSON);
	}
}

multiset<int> pos[MaxN];
BestHeap pre[MaxN];
long int RLGL;
void addPos(int id,int x){
	auto r = pos[id].lower_bound(x);
	if(*r == x){
		pos[id].insert(x); return ;
	}
	auto l = r; -- l; // previous
	int ppl = pre[*r].top();
	pre[*r].erase(tmp[*l]);
	pre[*r].push(tmp[x]); // new one
	if(ppl != pre[*r].top())
		SgTree::modify(*r,pre[*r].top());
	ppl = pre[x].top(); // O(1)
	pre[x].push(tmp[*l]); // only new
	pos[id].insert(x); // important
	if(ppl != pre[x].top())
		SgTree::modify(x,pre[x].top());
}
void delPos(int id,int x){
	pos[id].erase(pos[id].find(x));
	auto r = pos[id].lower_bound(x);
	if(*r == x) return ;
	auto l = r; -- l; // previous
	int ppl = pre[*r].top();
	pre[*r].erase(tmp[x]);
	pre[*r].push(tmp[*l]); // old friend
	if(ppl != pre[*r].top())
		SgTree::modify(*r,pre[*r].top());
	ppl = pre[x].top(); // O(1)
	pre[x].erase(tmp[*l]); // remove
	if(ppl != pre[x].top())
		SgTree::modify(x,pre[x].top());
}

struct CMD{
	int x, y, opt, w;
	operator int() const { return y; }
};
CMD cmd[MaxN<<1];
struct Query{
	int x, y, id;
	operator int() const { return y; }
};
Query ask[MaxN<<1];
int xyx[MaxN], cnt[MaxN], foo;
int main(){
	// freopen("data.in","r",stdin);
	// freopen("data.out","w",stdout);
	n = readint(); int k = readint();
	int q = readint();
	for(int i=1; i<=n; ++i){
		tmp[i] = cmd[(i<<1)-1].x =
		cmd[i<<1].x = readint();
		cmd[(i<<1)-1].w = // type
		cmd[i<<1].w = readint();
		cmd[i<<1].y = readint();
		cmd[(i<<1)-1].y = readint()+1;
		cmd[i<<1].opt = 1;
		cmd[(i<<1)-1].opt = 0;
	}
	for(int i=1; i<=q; ++i){
		ask[i].x = readint();
		ask[i].y = readint();
		ask[i].id = i;
	}
	sort(tmp+1,tmp+n+1);
	sort(ask+1,ask+q+1);
	int m = n<<1; // count of commands
	n = unique(tmp+1,tmp+n+1)-tmp-1;
	rep(i,1,n){
		pre[i].push(infty); // avoid empty
		SgTree::modify(i,infty); // clear
	}
	tmp[0] = -infty, tmp[n+1] = infty;
	rep(i,1,k){
		pos[i].insert(0);
		pos[i].insert(n+1);
		pre[n+1].push(tmp[0]);
	}
	SgTree::modify(n+1,-infty); // as definition
	sort(cmd+1,cmd+m+1);
	cmd[m+1].y = ask[q].y+1;
	for(int i=1,qid=1; true; ++i){
		for(; qid<=q&&ask[qid]<cmd[i]; ++qid){
			if(foo != k){ xyx[ask[qid].id] = -1; continue; }
			// if(qid%10000 == 0) printf("qid = %d\n",qid);
			xyx[ask[qid].id] = SgTree::query(ask[qid].x)-ask[qid].x;
		}
		// if(i%10000 == 0) printf("i = %d\n",i);
		if(i == m+1) break;
		int rnk = lower_bound(tmp+1,tmp+n+1,cmd[i].x)-tmp;
		if(!cnt[cmd[i].w]) ++ foo; // remove
		if(cmd[i].opt == 1) // SLOOOOOOW!
			addPos(cmd[i].w,rnk), ++ cnt[cmd[i].w];
		else delPos(cmd[i].w,rnk), -- cnt[cmd[i].w];
		if(!cnt[cmd[i].w]) -- foo; // add
	}
	for(int i=1; i<=q; ++i)
		writeint(xyx[i]), putchar('\n');
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值