2022-06-27 21:47阅读: 64评论: 0推荐: 0

Codeforces 1139F. Dish Shopping

传送门
Difficulty:2500

题目大意

n(1n105) 道菜,每道菜有属性 p,s,b(1p,s,b109) ,有 m(1m105) 每个人有属性 inc,pref(1inc,pref109) 。一个人 j 可以吃一盘菜 i 仅当 piincjsi|biprefj|incjpi ,求每个人能够吃几盘菜。

思路

考虑将第二个条件转化为 biprefjincjpi,biprefjpiincj 。可以将问题转化到二维平面上,每个人转化为点 (incj,prefj) 。将条件进一步转化变为 prefjincj+bi+pi,prefjincj+bipi。于是这些限制条件构成了一个被 y=x+bipi,y=x+bi+pi,x=si,x=pi 所围成的区域,其恰好为一个三角形, 如果 (incj,prefj) 在所围成的三角形中,那么 j 就可以吃 i 。于是问题转化成了对每个人,求其代表的点包含在几个这样的三角形当中,我们可以使用扫描线,沿 x 轴正方向扫描,我们令 y=x+bi+pi 的贡献为 1y=x+bipi 的贡献为 1 。可以推出如果 (incj,prefj) 在三角形 i 内,则 incj+prefjbi+pi,prefjincjbipi 。我们使用两个树状数组 a,b 分别维护 bi+pibipi ,对于第 j 个人,答案为 suma(incj+prefj)sumb(prefjincj1) 。在扫描过程中,一个点上需要做多种操作时,先进行加入,接着查询,最后执行删除。此外还需要离散化,复杂度 O((n+m)log(n+m))

代码

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
using LL = long long;
using LD = long double;
using ULL = unsigned long long;
using PII = pair<int, int>;
using TP = tuple<int, int, int>;
#define all(x) x.begin(),x.end()
#define mst(x,v) memset(x,v,sizeof(x))
#define mk make_pair
//#define int LL
#define lc p*2
#define rc p*2+1
#define endl '\n'
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#pragma warning(disable : 4996)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
const double eps = 1e-8;
const LL MOD = 1000000009;
const LL mod = 998244353;
const int maxn = 100010;

int N, M, p[maxn], s[maxn], b[maxn], inc[maxn], pref[maxn], tot = 0, cnt = 0, A[maxn * 4];
int n, dat[2][maxn * 4], ans[maxn];
struct Line {
	int x, op, id;
	bool operator<(const Line& rhs)
	{
		if (x == rhs.x)
			return op < rhs.op;
		return x < rhs.x;
	}
}L[maxn * 3];

void add(int i, int x, int t)
{
	while (i <= n)
	{
		dat[t][i] += x;
		i += i & (-i);
	}
}

int sum(int i, int t)
{
	int ans = 0;
	while (i)
	{
		ans += dat[t][i];
		i -= i & (-i);
	}

	return ans;
}

int compress(int* ar)
{
	vector<int>xs;
	for (int i = 1; i <= cnt; i++)
		xs.push_back(ar[i]);
	sort(all(xs));
	xs.erase(unique(all(xs)), xs.end());
	for (int i = 1; i <= cnt; i++)
		A[i] = upper_bound(all(xs), A[i]) - xs.begin();

	return xs.size();
}

void solve()
{
	for (int i = 1; i <= N; i++)
		A[++cnt] = p[i] + b[i], A[++cnt] = b[i] - p[i];
	for (int i = 1; i <= M; i++)
		A[++cnt] = inc[i] + pref[i], A[++cnt] = pref[i] - inc[i];
	n = compress(A);
	for (int i = 1; i <= N; i++)
	{
		L[++tot].id = i, L[tot].op = 0, L[tot].x = p[i];
		L[++tot].id = i, L[tot].op = 2, L[tot].x = s[i];
	}
	for (int i = 1; i <= M; i++)
		L[++tot].id = i, L[tot].op = 1, L[tot].x = inc[i];
	sort(L + 1, L + tot + 1);
	for (int i = 1; i <= tot; i++)
	{
		if (L[i].op == 0)
		{
			add(A[L[i].id * 2 - 1], 1, 0);
			add(A[L[i].id * 2], 1, 1);
		}
		else if (L[i].op == 2)
		{
			add(A[L[i].id * 2 - 1], -1, 0);
			add(A[L[i].id * 2], -1, 1);
		}
		else
			ans[L[i].id] = sum(A[L[i].id * 2 - 1 + N * 2], 0) - sum(A[L[i].id * 2 + N * 2] - 1, 1);
	}
	for (int i = 1; i <= M; i++)
		cout << ans[i] << ' ';
	cout << endl;
}

int main()
{
	IOS;
	cin >> N >> M;
	for (int i = 1; i <= N; i++)
		cin >> p[i];
	for (int i = 1; i <= N; i++)
		cin >> s[i];
	for (int i = 1; i <= N; i++)
		cin >> b[i];
	for (int i = 1; i <= M; i++)
		cin >> inc[i];
	for (int i = 1; i <= M; i++)
		cin >> pref[i];
	solve();

	return 0;
}

本文作者:Prgl

本文链接:https://www.cnblogs.com/Prgl/p/16417656.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Prgl  阅读(64)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开