【USACO2021 Convoluted Intervals 】题解
题目
The cows are hard at work trying to invent interesting new games to play. One of
their current endeavors involves a set of intervals
(), where the th interval starts at position on
the number line and ends at position . Both and are
integers in the range , where .
To play the game, Bessie chooses some interval (say, the th interval) and her
cousin Elsie chooses some interval (say, the th interval, possibly the same
as Bessie's interval). Given some value , they win if
.
For every value of in the range , please count the number of
ordered pairs for which Bessie and Elsie can win the game.
奶牛们正在努力尝试发明有趣的新游戏来玩。他们目前的工作之一与一组 个区间()有关,其中第 个区间从数轴上的 位置开始,并在位置 结束。 和 均为 范围内的整数,其中 。
这个游戏的玩法是,Bessie 选择某个区间(假设是第 个区间),而她的表妹 Elsie 选择某个区间(假设是第 个区间,可能与 Bessie 所选的的区间相同)。给定某个值 ,如果 ,则她们获胜。
对范围 内的每个值 ,请计算使得 Bessie 和 Elsie 可以赢得游戏的有序对 的数量。
思路
用差分思想考虑,所有 的地方+1,所以 的地方减1。
然而这是 ,但我们发现 的值域很小,所以我们可以用桶存起来,然后按值域遍历即可,就变成 。
总结
对于一些过于重复的计算,如果值域很小,我们可以尝试用桶存起来,减去很多过于重复的计算。
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 5010
int n, m, i, j, k;
int a[N], b[N], x[N], y[N], c[N*2];
signed main()
{
scanf("%lld%lld", &n, &m);
for(i=1; i<=n; ++i) scanf("%lld%lld", &j, &k), x[j]++, y[k]++;
for(i=0; i<=m; ++i)
for(j=0; j<=m; ++j)
c[i+j]+=x[i]*x[j], c[i+j+1]-=y[i]*y[j];
for(i=k=0; i<=2*m; ++i) printf("%lld\n", k+=c[i]);
return 0;
}
本文来自博客园,作者:zhangtingxi,转载请注明原文链接:https://www.cnblogs.com/zhangtingxi/p/15716558.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战