【codeforces 816B】Karen and Coffee
2017-10-04 18:44
369 查看
【题目链接】:http://codeforces.com/contest/816/problem/B
【题意】
给你很多个区间[l,r];
1<=l<=r<=2e5
一个数字如果被k个以上的区间覆盖到;
则称之为特殊数字;
给你q个询问区间;
问你这q个区间里面
每个区间里面有多少个特殊数字;
【题解】
对于覆盖的区间l,r
add[l]++,sub[r+1]++,
然后顺序枚举
now+=add[i];
now-=sub[i];
如果now>=k则i是一个特殊数字;
写个前缀和O(1)输出答案.
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define mp make_pair #define pb push_back #define fi first #define se second #define ms(x,y) memset(x,y,sizeof x) #define Open() freopen("F:\\rush.txt","r",stdin) #define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii; typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1}; const int dy[9] = {0,0,0,-1,1,-1,1,-1,1}; const double pi = acos(-1.0); const int N = 2e5+100; int add ,sub ,n,k,q; int is ,sum ; int main(){ //Open(); Close(); cin >> n >> k >> q; rep1(i,1,n){ int x,y; cin >> x >> y; add[x]++,sub[y+1]++; } int now = 0; rep1(i,1,(int) 2e5){ now+=add[i]; now-=sub[i]; if (now>=k){ is[i] = 1; } } sum[0] = 0; rep1(i,1,(int) 2e5){ sum[i] = sum[i-1] + is[i]; } rep1(i,1,q){ int x,y; cin >> x >> y; cout << sum[y]-sum[x-1] << endl; } return 0; }
相关文章推荐
- [刷题]Codeforces 816B - Karen and Coffee
- Codeforces 816B-Karen and Coffee
- Karen and Coffee 816B
- CodeForces-816B:Karen and Coffee (简单线段树)
- Codeforces 816 -B. Karen and Coffee
- codeforces 816-B. Karen and Coffee(前缀和+思维)
- codeforces Karen and Coffee (区间贡献 思维)
- [CF816B] Karen and Coffee(前缀和,思维)
- codeforces Karen and Coffee (区间贡献 思维)
- Codeforces Round #419 (Div. 2)Karen and Coffee
- Codeforces 816A-Karen and Morning
- 【组合数+找规律】codeforces 815B - Karen and Test
- codeforces round #419 B. Karen and Coffee
- codeforces Karen and Coffee (区间贡献 思维)
- [刷题]Codeforces 815A - Karen and Game
- Codeforces Round #419 (Div. 2) B. Karen and Coffee【前缀和求区间覆盖次数】
- codeforces Karen and Coffee (区间贡献 思维)
- Codeforces Round #419 (Div. 2) B. Karen and Coffee 预处理+技巧
- codeforces Karen and Coffee (区间贡献 思维)
- Codeforces Round #419 (Div. 2) B. Karen and Coffee (差分标记)