#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 ...
随机推荐
- 异常1(Exception)
父类 :Throwable(可抛出的) 有两个子类:Error(错误) Exception(异常) Error是所有错误类的父类,Exception是所有异常类的父类. 如图所示: 格式: ...
- XSS绕过WAF的姿势
初始测试 1.使用无害的payload,类似<b>,<i>,<u> 观察响应,判断应用程序是否被HTML编码,是否标签被过滤,是否过滤<>等等: 2.如 ...
- 【转帖】Linux系统上面qemu 模拟arm
零基础在Linux系统搭建Qemu模拟arm https://blog.csdn.net/weixin_42489042/article/details/81145038 自己没搞定 改天再试试 感谢 ...
- Oracle恢复ORA-00600: 内部错误代码, 参数: [kcratr_scan_lastbwr] 问题的简单解决
Oracle恢复ORA-00600: 内部错误代码, 参数: [kcratr_scan_lastbwr] 1. 简单处理 sqlplus / as sysdba startup mount recov ...
- python 科学计数法转数值
猜测python应该是有现成的模块可以解决该问题,不过没找到,所以自己简单写了个函数处理: def tranform(inputString): num_value = re.compile('^[0 ...
- java 集合 队列(Queue)
特点:特殊线性表,先进先出(FIFO first-in-first-out) 方法: java5中 添加java.util.Queue接口,java.util.Collection 的扩展类 add( ...
- noip2013day1-货车运输
题目描述 \(A\)国有\(n\)座城市,编号从 \(1\)到\(n\),城市之间有 \(m\) 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 \(q\) 辆货车在运输货物, 司机们想知 ...
- python中对多态和多态性的理解
python中对多态的理解 一.多态 多态是指一类事物有多种形态,比如动物类,可以有猫,狗,猪等等.(一个抽象类有多个子类,因而多态的概念依赖于继承) import abc class Animal( ...
- 使用procedump捕获未处理异常的dump
-ma full memory dump, always do this on 2003 as 4gb is not much and it is good to have the heap -mp ...
- 15. AutoMapper 之映射继承(Mapping Inheritance)
https://www.jianshu.com/p/e4f05403bd13 映射继承(Mapping Inheritance) 映射继承有两个功能: 从基类或接口配置继承映射配置 运行时多态映射 继 ...