【codeforces 816B】Karen and Coffee

【题目链接】: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[N],sub[N],n,k,q;
int is[N],sum[N];

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;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626246.html