Codeforces 1139F. Dish Shopping
题目大意
道菜,每道菜有属性 ,有 每个人有属性 。一个人 可以吃一盘菜 仅当 且 ,求每个人能够吃几盘菜。
思路
考虑将第二个条件转化为 。可以将问题转化到二维平面上,每个人转化为点 。将条件进一步转化变为 。于是这些限制条件构成了一个被 所围成的区域,其恰好为一个三角形, 如果 在所围成的三角形中,那么 就可以吃 。于是问题转化成了对每个人,求其代表的点包含在几个这样的三角形当中,我们可以使用扫描线,沿 轴正方向扫描,我们令 的贡献为 , 的贡献为 。可以推出如果 在三角形 内,则 。我们使用两个树状数组 分别维护 与 ,对于第 个人,答案为 。在扫描过程中,一个点上需要做多种操作时,先进行加入,接着查询,最后执行删除。此外还需要离散化,复杂度 。
代码
#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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步