#419 Div2 Problem B Karen and Coffee (统计区间重叠部分 && 前缀和)
题目链接 :http://codeforces.com/contest/816/problem/B
题意 :给出 n 表示区间个数,限定值 k 以及问询次数 q,当一个数被大于或等于 k 个区间重复覆盖时才算有效数,每一次问询都是以区间方式给出,例如(L, R)问你在这个L~R的范围内有多少个数是有效数(即包含这些数的区间个数>=k)。
分析 : 这题可以预先统计哪些数被大于或者等于 k 个原始区间重复覆盖,只要提前将这些满足条件的数递增地编上权值构成前缀和序列,然后对于每段问询只要将前缀和序列做个减法即可,例如找到了 a1 a2 a3这三个数构是满足条件的,令sum[a1]=1,sum[a2]=2,sum[a3]=3,如果问询(a2,a3)这个区间满足条件的数,则只要将对应的权值做个减法即可,即sum[a3] - sum[a2-1]。关于如何统计有效的数,可以这样做,令arr数组存储的是区间信息,对于给出的 n 个区间 (L,R),只要将arr[L]++,arr[R+1]--(R+1相当于区间结束标识,这种技巧在POJ Matrix这题便有涉及),然后采用动态规划从前往后扫一遍和,当和>=k的时候那此时这个点便是满足条件的,给其加上权值构建前缀和序列。可能说的有点乱,看代码即知。
#include<bits/stdc++.h>
using namespace std;
;
int arr[maxn], sum[maxn], cnt[maxn];
int main(void)
{
int n, k, q;
scanf("%d %d %d", &n, &k, &q);
; i<n; i++){
int L, R;
scanf("%d %d", &L, &R);
arr[L]++, arr[R+]--;
}
sum[] = arr[];
; i<maxn; i++){
sum[i] = sum[i-] + arr[i];//记录从左到右扫出来的和,其实这个和就代表了区间的个数,由每个区间的左端点贡献
cnt[i] = cnt[i-] + (sum[i]>=k);//cnt为前缀和序列,如果当前的和>=k则对这个点在前缀和序列+1
}
while(q--){
int L, R;
scanf("%d %d", &L, &R);
printf(]);
}
;
}
#419 Div2 Problem B Karen and Coffee (统计区间重叠部分 && 前缀和)的更多相关文章
- #419 Div2 Problem C Karen and Game (贪心 && 暴力)
题目链接:http://codeforces.com/contest/816/problem/C 题意 :给出一个 n*m 的变化后的矩阵,变化前矩阵的元素全是0,变化的规则是选择其中的一行或者一列将 ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)
http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...
- Codeforces Round #419 (Div. 2) B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- codeforces round #419 B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- CF 816B Karen and Coffee【前缀和/差分】
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- B. Karen and Coffee
B. Karen and Coffee time limit per test 2.5 seconds memory limit per test 512 megabytes input standa ...
- Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Codeforces816B Karen and Coffee 2017-06-27 15:18 39人阅读 评论(0) 收藏
B. Karen and Coffee time limit per test 2.5 seconds memory limit per test 512 megabytes input standa ...
随机推荐
- linux shutdown 命令 关机 重启
关机 shutdown -h now 重启 shutdown -r now
- python 并发编程 多线程 线程理论
操作系统比作一家公司,进程相当于一个部门 线程相当于一个部门的成员 进程之间是互相隔离的 一 什么是线程 1. 每启动一个进程 至少有一个线程, 在传统操作系统中,每个进程有一个地址空间,而且默认 ...
- React进阶篇学习
继续上一次基础篇, 分享一些关于React的进阶技术 React 进阶部分 ** context ** ** setState vs forceUpdate ** ** Mixins ** ** HO ...
- 初识MySQL <一>
创建一个 表 create table student( id int(10) not null unique auto_increment primary key, name varchar(30) ...
- 云服务器linux重新挂载指定目录(非扩充)
新买的香港云服务器,系统只能在商家的控制台上安装. 系统和硬盘分开的,根目录空间只有10G.需要重新设置相关目录的大小,如:/usr./var./home等. 以下是自己的解决方法小计. 一.初始的分 ...
- luogu P5342 [TJOI2019]甲苯先生的线段树
传送门 你个好好的省选怎么可以出CF原题啊,你们这个题害人不浅啊,这样子出题像极了cxk,说到cxk,我又想起了他是NBA形象大使,跟我是西游文化大使一样一样的,今年下半年... 别说了,jinsai ...
- luffyapi项目 --短信认证的基本操作
一.开通腾讯云短信 SDK 文档 :https://cloud.tencent.com/document/product/382/11672 1.官网注册实名账号:https://cloud.tenc ...
- EF6
EF讲解 EF(entityframework) ORM框架中的一种 然后 我们来说下 什么是 ORM (对象映射关系) object relation Mapping 微软 为了更简化 使用 ...
- 监控mysql的存储引擎
监控mysql 显示进程状态变量 mysql> show variables like '%thread%'; +----------------------------+----------- ...
- PAT Basic 1042 字符统计 (20 分)
请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过 1000 的字符串.字符串由 ASCII 码表中任意可见字符及空格组成,至少包含 1 个英文字母, ...