CodeForces - 369E Valera and Queries 树状数组+离线+容斥思想

题目链接:https://vjudge.net/problem/CodeForces-369E

题意:n条线段,m次询问,给出cnt个点,问这些点在多少线段上

题解:如果每个点对于所在的线段上没有影响的话,那么我们就可以建立权值线段树,直接查询即可,但是存在多个点在一条直线的情况,所以就没法这么搞了,所以我们可以把查询的点转化成间隔的线段,计算出有多少线段在这些线段上,这样我们离线记录下来,就是一个二维偏序了,按照左右区间关系排序,再用梳状数组维护下即可。

#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) (x & (-x))
const int N = 3e5 + 10;
const int M = 1e6 + 10;
struct node {
	int l, r;
	int op, id;
	bool operator <(const node &x)const{
		if(r != x.r) return r < x.r;
		else if(l != x.l) return l > x.l;
		else return op < x.op;
	}
}a[N * 4];
int n, m;
int len;
int sum[M];
void update(int x) {
	while(x) {
		sum[x]++;
		x -= lowbit(x); 
	}
}
int query(int x) {
	int res = 0;
	while(x < M) {
		res += sum[x];
		x += lowbit(x);
	}
	return res;
}
int ans[N];
int main() {
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++) {
		scanf("%d %d", &a[i].l, &a[i].r);
		a[i].op = 0;
	}
	len = n;
	int x, y, pre;
	for(int i = 1; i <= m; i++) {
		scanf("%d", &y);
		pre = 1;
		while(y--) {
			scanf("%d", &x);
			if(x != pre) {
				len++;
				a[len].l = pre;
				a[len].r = x - 1;
				a[len].op = 1;
				a[len].id = i;
			}
			pre = x + 1;
		}
		if(pre <= 1000000) {
			len++;
			a[len].l = pre;
			a[len].r = 1000000;
			a[len].id = i;
			a[len].op = 1;
		}
	}
	sort(a + 1, a + 1 + len);
	for(int i = 1; i <= len; i++) {
	//	cout << a[i].id << " " << a[i].l <<" " << a[i].r<<endl;
		if(a[i].op) ans[a[i].id] += query(a[i].l);
		else update(a[i].l);
	}
	for(int i = 1; i <= m; i++)
		printf("%d\n", n - ans[i]);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值