816B. Karen and Coffee 前缀和思维 或 线段树
题意:给出n个[l,r],q个询问a,b,问被包含于[a,b]且这样的区间数大于k个的方案数有多少
思路:预处理所有的区间,对于一个区间我们标记其(左边界)++,(右边界+1)--这样就能通过前缀和维护小于某边界的区间个数了 这题也可以用线段树解,但显然不太合算
/** @Date : 2017-07-01 10:02:30
* @FileName: 816B 前缀和 线段树 二分.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 2e5+20;
const double eps = 1e-8; int n, k, q;
int cnt[N], ans[N];
int main()
{
int l, r;
while(cin >> n >> k >> q)
{
for(int i = 0; i < n; i++)
{
scanf("%d%d", &l, &r);
cnt[l]++;
cnt[r + 1]--;
}
for(int i = 1; i <= N; i++)
{
cnt[i] += cnt[i - 1];
if(cnt[i] >= k)
ans[i] = ans[i - 1] + 1;
else ans[i] = ans[i - 1];
}
while(q--)
{
scanf("%d%d", &l, &r);
printf("%d\n", ans[r] - ans[l - 1]);
}
}
return 0;
}
//标记l++ r--,获取前i个的标记和,大于k说明满足条件
//最终得到个数,前缀和表示到i个可行方案数(不用组合数)
816B. Karen and Coffee 前缀和思维 或 线段树的更多相关文章
- CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- codeforces 816B Karen and Coffee (差分思想)
题目链接 816B Karen and Coffee 题目分析 题意:有个人在学泡咖啡,因此看了很多关于泡咖啡温度的书,得到了n种推荐的泡咖啡温度范围[L1,R1] ,此人将有k种做法推荐的温度记为可 ...
- CF 816B Karen and Coffee【前缀和/差分】
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- BZOJ4627 前缀和 + 权值线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=4627 题意:求序列中和在L到R之间的字串种数. 要求的是和的范围,我们可以考虑先求一个前缀和pre ...
- hdu 5475(打破固定思维OR线段树)
An easy problem Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- codeforces 816B.Karen and Coffee 解题报告
题目链接:http://codeforces.com/contest/816/problem/B 题目意思:给出 n 个recipes,第 i 个(1<= i <=n)recipes 表明 ...
- 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers
我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...
- Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Karen and Coffee CF 816B(前缀和)
Description To stay woke and attentive(专注的) during classes, Karen needs some coffee! Karen, a coffee ...
随机推荐
- C++:const_cast的简单理解
前言:const_cast是我比较头疼的一个知识点,最近查阅了很多资料,也翻看了很多他人的博客,故在此将自己目前学习到的有关const_cast知识做一个简单的总结 一.什么是const_cast 简 ...
- CentOS 7 安装 MySql 8
1-安装 CentOS 7 2-安装 NETCORE SDK SDK 安装文档:https://dotnet.microsoft.com/download/linux-package-m ...
- timestamp 学习
该答案摘抄自CSDN. 哇,奇迹,跨度三年了,不知道楼主是否已经解决了此问题. 路过,简单说一下,timestamp 主要是记录该行的最后修改时间戳, 注意,这个时间戳是不可以转换为时间的,只能标注该 ...
- Spring的初始化:org.springframework.web.context.ContextLoaderListener
在web.xml中配置 <listener> <listener-class>org.springframework.web.context.ContextLoaderL ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- win7 64位机ODBC的数据源DSN添加和移除问题
64位机器上ODBC的操作方法与32位机器是不一样的,如果直接从控制面板上-管理员工具-ODBC进去的话会发现User DSN以及System DSN里面都为空,ADD的时候连ODBC Driver都 ...
- python编码iso-8859-9编码问题
(2018-10-15) 路 2018骞�10鏈�16鏃�8:30鈥斺€�11:00锛屽湪鍏垽涓€搴叕寮€瀹$悊锛氬啀瀹$敵璇�.. (2018-10-15) 路 2018骞�10鏈�16鏃�8: ...
- Vue使用,异步获取日期时间后格式成"/Date(1333245600000+0800)/" 转换成正常格式
js从后台mvc中日期获取,结果格式成"/Date(1333245600000+0800)/"了,当然不能这样展显给用户了,要转换,方法如下: function data_stri ...
- 【bzoj3533】[Sdoi2014]向量集 线段树+STL-vector维护凸包
题目描述 维护一个向量集合,在线支持以下操作:"A x y (|x|,|y| < =10^8)":加入向量(x,y);"Q x y l r (|x|,|y| < ...
- 【Java】使用CSVUtils生成文件并供下载
package com.msk.ds.logic; import java.io.*; import java.util.List; /** * Created by Administrator on ...