Codeforces 283E Cow Tennis Tournament (扫描线+计数)

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <algorithm>  
#include <queue>  
#include <set>  
#include <ctime>  
#include <cstdlib>  
using namespace std;  


#define inf 0x3f3f3f3f
#define N 100020
#define LL long long
#define mod 1000000009
#define ls (i << 1)
#define rs (ls | 1)
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs

int n, k;
int a[N];

struct node {
	int x, l, r;
	node() {}
	node(int x, int l, int r):x(x), l(l), r(r) {}
	bool operator < (const node &b) const {
		return x < b.x;
	}
}b[N*2];
int cnt;
int s[N<<2], fp[N<<2];

void upd(int ll, int rr, int i) {
	fp[i] ^= 1;
	s[i] = rr - ll + 1 - s[i];
}

void pushDown(int ll, int rr, int i) {
	if(fp[i]) {
		upd(lson);
		upd(rson);
		fp[i] = 0;
	}
}

void update(int l, int r, int ll, int rr, int i) {
	if(l > r) return;
	if(ll == l && rr == r) {
		fp[i] ^= 1;
		s[i] = rr - ll + 1 - s[i];
		return;
	}
	pushDown(ll, rr, i);
	if(r <= md) update(l, r, lson);
	else if(l > md) update(l, r, rson);
	else {
		update(l, md, lson);
		update(md + 1, r, rson);
	}
	s[i] = s[ls] + s[rs];
}

int query(int l, int r, int ll, int rr, int i) {
	if(l > r) return 0;
	if(ll == l && rr == r) return s[i];
	pushDown(ll, rr, i);
	if(r <= md) return query(l, r, lson);
	if(l > md) return query(l, r, rson);
	return query(l, md, lson) + query(md + 1, r, rson);
}

int main() {
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + n + 1);
	for(int i = 1; i <= k; ++i) {
		int x, y;
		scanf("%d%d", &x, &y);
		x = lower_bound(a + 1, a + n + 1, x) - a;
		if(x > n) continue;
		y = upper_bound(a + 1, a + n + 1, y) - a - 1;
		if(y < 1) continue;
		b[++cnt] = node(x, x, y);
		if(y < n) b[++cnt] = node(y + 1, x, y);
	}
	sort(b + 1, b + cnt + 1);
	int j = 1;
	LL ans = 1LL * n * (n - 1) * (n - 2) / 6;
	for(int i = 1; i <= n; ++i) {
		while(j <= cnt && b[j].x <= i) {
			update(b[j].l, b[j].r, 1, n, 1);
			++j;
		}
		int x = 0, y = 0;
		if(i < n) x = query(i + 1, n, 1, n, 1);
		if(i > 1) y = query(1, i - 1, 1, n, 1);
		int w = x + i - 1 - y;
		ans -= 1LL * w * (w - 1) / 2;
	}
	cout << ans << endl;
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值