Codeforces Round #419 (Div. 2) B. Karen and Coffee (差分标记)
来源:互联网 发布:手机迅雷网络异常 编辑:程序博客网 时间:2024/05/21 14:01
题目链接
http://codeforces.com/contest/816/problem/B
题目大意
给你n个可重叠区间和一个参考值k,然后进行q次询问,每次询问也是一个区间,问你该区间中有多少个整数在n个区间中出现次数>=k
核心思想
这题我们用差分标记对数据处理一下,就可以o(1)查询答案了
差分标记
给定区间的端点li, ri, 在数组中我们将cnt[li]++, nt[ri+1]- -,
这样标记后扫一遍(cnt[i] = cnt[i] +cnt[i-1]),那么区间l, r之间的数在几个区间出现过就都标记出来了
代码
#include<bits/stdc++.h>using namespace std;const int M = 2e5 + 5;int cnt[M], ans[M];int main(){ memset(cnt, 0, sizeof(cnt)); memset(ans, 0, sizeof(ans)); int n, k, q; scanf("%d%d%d", &n, &k, &q); for(int i=0; i<n; ++i) { int l, r; scanf("%d%d", &l, &r); ++cnt[l], --cnt[r+1]; } for(int i=1; i<M; ++i) cnt[i] = cnt[i] + cnt[i-1]; for(int i=0; i<M; ++i) { if(cnt[i] < k) cnt[i]=0;//我们将次数少于k的标为0 else cnt[i] = 1; //次数>=k的标为1 } int sum = 0; for(int i=0; i<M; ++i) { //用前缀和进行统计 sum += cnt[i]; ans[i] = sum; } for(int i=0; i<q; ++i) { int l, r; scanf("%d%d", &l, &r); printf("%d\n", ans[r] - ans[l-1]);//o(1)查询答案 } return 0;}
阅读全文
0 0
- Codeforces Round #419 (Div. 2) B. Karen and Coffee (差分标记)
- CodeForces #419 Div.2 B Karen and Coffee 差分 巧妙
- Codeforces Round #419 (Div. 2) B. Karen and Coffee
- Codeforces Round #419 (Div. 2)Karen and Coffee
- Codeforces Round #419 (Div. 2)Karen and Coffee
- Codeforces Round #419 (Div. 2) B. Karen and Coffee 预处理+技巧
- [前缀和] Codeforces Round #419 (Div. 2)B. Karen and Coffee
- Codeforces Round #419 (Div. 2) B. Karen and Coffee【前缀和求区间覆盖次数】
- CodeForces B. Karen and Coffee
- Codeforces#419 Karen and Coffee
- CodeForce Round #479 B. Karen and Coffee
- Codeforces 816B Karen and Coffee(前缀和)
- codeforces 816-B. Karen and Coffee(前缀和+思维)
- Karen and Coffee codeforces 816B
- Codeforces 816 -B. Karen and Coffee
- Codeforces 816B-Karen and Coffee
- Codeforces 816B Karen and Coffee
- Codeforces Round #419 (Div. 2) (Codeforces 815B) D. Karen and Test 组合数学
- jquery js checkbox勾选/取消勾选的诡异问题
- ==和===比较
- DEVC++下编译helib
- 告知服务器意图的 HTTP 方法
- ionic配置自定义keystore签名文件并直接安装已签名apk到手机
- Codeforces Round #419 (Div. 2) B. Karen and Coffee (差分标记)
- xxx is not in the sudoers file.This incident will be reported.的解决方法
- java 中流程控制语句switch以及break,continue,return的比较
- 一头雾水?也许你该学学结构化设计思维
- Java——继承、枚举
- leetcode 498. Diagonal Traverse
- 应用程序调用_驱动B调用_驱动A
- 用c/c++实现linux下检测网络接口状态
- springMVC 通过服务器下载功能