跳到正文

题解

AT_abc405_f

jojo222约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月16日
篇幅
约 3 分钟

版本

  1. v1当前

这是我的第一个赛后过的 F 喵!

首先,把圆拆成一条先,问题就变成了有多少个符合 ai<cj<bi<dja_i<c_j<b_i<d_j 或 cj<ai<dj<bic_j<a_i<d_j<b_i 的 ai,bia_i,b_i 数。

接下来就可以用树状数组了。比如说 ai<cj<bi<dja_i<c_j<b_i<d_j,可以将 aa 和 bb 都按左端点从小到大排序,然后从头到尾遍历,遍历完所有满足 cj−1<ai<cjc_{j-1}<a_i<c_j 的 aia_i,并单点改,将树状数组的下标 bib_i 加一。遍历玩,接着可以将第 jj 行的答案加上树状数组中 [cj,dj][c_j,d_j] 的区间和。

附代码:

C++
#include <bits/stdc++.h>
using namespace std;
int n, m, q, ans[200001];
struct node
{
	int x, y, id;
	bool operator<(node c) const
		{return x < c.x;}
} a[200001], b[200001];
struct fen_tree
{
	int a[2000001];
	void update(int x)
	{
		for(int i = x; i <= n << 1; i += i & -i)
			a[i]++;
	}
	int query(int x, int y)
	{
		int ans = 0;
		while(y)
		{
			ans += a[y];
			y -= y & -y;
		}
		while(x)
		{
			ans -= a[x];
			x -= x & -x;
		}
		return ans;
	}
} c;
bool cmp(node p, node q)
	{return p.y > q.y;}
int main()
{
	cin >> n >> m;
    for(int i = 1; i <= m; i++)
    	scanf("%d%d", &a[i].x, &a[i].y);
    cin >> q;
    for(int i = 1; i <= q; i++)
    	scanf("%d%d", &b[i].x, &b[i].y), b[i].id = i;
	sort(a + 1, a + 1 + m);
	sort(b + 1, b + 1 + q);
	int i = 1, j = 1;
	while(j <= q)
	{
		while(i <= m && a[i].x < b[j].x)
			c.update(a[i++].y);
		ans[b[j].id] += c.query(b[j].x, b[j].y);
		j++;
	}
	sort(a + 1, a + 1 + m, cmp);
	sort(b + 1, b + 1 + q, cmp);
	memset(c.a, 0, sizeof c.a);
	i = 1, j = 1;
	while(j <= q)
	{
		while(i <= m && a[i].y > b[j].y)
			c.update(a[i++].x);
		ans[b[j].id] += c.query(b[j].x, b[j].y);
		j++;
	}
	for(int i = 1; i <= q; i++)
		printf("%d\n", ans[i]);
    return 0;
}
原文