1. 程式人生 > >Karen and Coffee 【CodeForces - 816B】【字首和思想】

Karen and Coffee 【CodeForces - 816B】【字首和思想】

題目連結


  很好的一道題,剛看到這道題的時候,我們看法各不一,敲了線段樹、還有敲了個狀壓DP的,但是最後T在第9發,然後想到了其他的做法,真的很有意思的字首思想。

  看看程式碼吧,會很驚喜的:


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 200005;
int N, K, Q, pre[maxN], L, R;
bool flag;
struct Eddge
{
    int l, r;
    Eddge(int a=0, int b=0):l(a), r(b) {}
}a[maxN];
struct Question
{
    int l, r, id;
    Question(int a=0, int b=0, int c=0):l(a), r(b), id(c) {}
}ques[maxN];
bool cmp_eddge(Eddge e1, Eddge e2) { return e1.l==e2.l?(e1.r>e2.r):(e1.l<e2.l); }
bool cmp_ques(Question e1, Question e2) { return e1.l==e2.l?(e1.r>e2.r):(e1.l<e2.l); }
bool cmp_id(Question e1, Question e2) { return e1.id<e2.id; }
int main()
{
    while(scanf("%d%d%d", &N, &K, &Q)!=EOF)
    {
        memset(pre, 0, sizeof(pre));
        for(int i=1; i<=N; i++)
        {
            scanf("%d%d", &L, &R);
            pre[L]++;
            pre[R+1]--;
        }
        for(int i=1; i<maxN; i++) pre[i] += pre[i-1];
        for(int i=1; i<maxN; i++) { pre[i] = (pre[i]>=K?1:0); pre[i] += pre[i-1]; }
        while(Q--)
        {
            scanf("%d%d", &L, &R);
            printf("%d\n", pre[R] - pre[L-1] );
        }
    }
    return 0;
}