[arc068E]Snuke Line-[树状数组]
Description
Solution
假如想直接YY对于每一个d会有多少种商品满足条件,em反正我搞不定。
然后大佬的题解告诉我说:搞不定?那就不搞它啊,反过来不就得了?
好吧。我们来考虑对于每一个d,会有多少种商品无法购买。
我们目前有一些点,组成的集合为(0,d,2d,3d,.....kd)(kd<=m),车子会在这些点停下来。
无法购买的商品i,必定满足li,ri在某两个相邻点之间,也就是说,可以购买的商品,必然会跨过若干个点。
然后还有一个性质:对于d,所有ri-li>=d的i都是可以购买的(显然啦)。
好的那么让我们开工:我们把所有商品按ri-li的大小排序,对于ri-li>=d的直接加上;其他商品,由于ri-li<d,它们最多跨过一个点,则我们只需要枚举跨过某个点的商品个数总和就好,这个通过差分思想,树状数组就ok。
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m;
struct node{int l,r;
}a[];
bool cmp(node x,node y){return x.r-x.l<y.r-y.l;} int tree[];
void add(int id,int x){for(;id<=m;id+=id&-id) tree[id]+=x;}
int query(int id){int re=;for(;id;id-=id&-id) re+=tree[id];return re;}
int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].r);
sort(a+,a+n+,cmp);
for (int i=,j=;i<=m;i++)
{
int cnt=;
for (int t=i;t<=m;t+=i) cnt+=query(t);
printf("%d\n",cnt+n-j);
while (j<n&&a[j+].r-a[j+].l<=i)
{
add(a[j+].l,);
add(a[j+].r+,-);
j++;
}
}
}
[arc068E]Snuke Line-[树状数组]的更多相关文章
- 【AtCoder - 2300】Snuke Line(树状数组)
BUPT2017 wintertraining(15) #9A 题意 有n个纪念品,购买区间是\([l_i,r_i]\).求每i(1-m)站停一次,可以买到多少纪念品. 题解 每隔d站停一次的列车,一 ...
- HDU3465--Life is a Line(树状数组求逆序数,离散化)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- UVALive 6911---Double Swords(贪心+树状数组(或集合))
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
- POJ3928Ping pong[树状数组 仿逆序对]
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3109 Accepted: 1148 Descrip ...
随机推荐
- URAL-1039 Anniversary Party---树形DP入门题
题目链接: https://cn.vjudge.net/problem/URAL-1039 题目大意: 开一个party,每个员工都有一个欢乐值,只有是上司和下属不同时存在时才能欢乐,问怎样安排能有最 ...
- (六)Linux下的压缩命令
======================================================================================== .zip格式的压缩和解 ...
- LVS.md
LVS 概述 简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,官方站点.现在LVS已经是 Linux标准内核的一部分, ...
- Odoo中Application与modules的区别
转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9278681.html 一:Application(应用) application一般是针对大功能的模块,如提供 ...
- js中时间的操作
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1 ...
- 用c写一个小的聊天室程序
1.聊天室程序——客户端 客户端我也用了select进行I/O复用,同时监控是否有来自socket的消息和标准输入,近似可以完成对键盘的中断使用. 其中select的监控里,STDOUT和STDIN是 ...
- Spring 事务传播行为的使用
...
- 常用LLDB指令
print.p: 打印内存地址 po: 打印对象 1.读取内存 memory read/数量格式字节数 内存地址 x/数量格式字节数 内存x/3xw 0x10010 格式:x是16进制,f是浮点, ...
- redis具体使用
key 命名规则:不可包含空格和\n 创建方式: set key value values Strings (Binary-safe strings) Lists Sets Sorted sets ...
- Redis学习笔记(二)
解读Retwis官网例子 Redis需要考虑需要哪些keys以及对应的value使用合适的数据类型进行存储.在retwis例子中,我们需要users,user的粉丝列表, user的关注用户列表等等. ...