染色——差分数组板子题

问题描述

有编号为0到M 的(M+1)个格子,现在有N个操作 (x,y),表示将从x 到 y的格子染色,问一共有多少个格子被染色。

输入

第一行两个整数,分别表示N和M。
接下来有N行,每行两个整数,分别表示x和y。

输出

输出一个整数,表示有多少个格子被染色。
样例输入 Copy

3 10
0 5
2 6
8 9

样例输出 Copy

9

提示

30%的数据满足N,M<=10000
100%的数据满足N,M<=1000000;任何操作保证0<=x<=y<=M。

#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
int num[maxn];
int a,b;
start{
    int n=read,m=read;
    for(int i=1;i<=n;i++){
        a=read,b=read;
        num[a]+=1;
        num[b+1]-=1;
    }
    int cnt=0;
    for(int i=0;i<=m;i++){
        num[i]+=num[i-1];
        if(num[i]) cnt++;
    }
    cout<<cnt<<endl;
    end;
}
 
/**************************************************************
    Language: C++
    Result: 正确
    Time:199 ms
    Memory:5928 kb
****************************************************************/
posted @ 2020-03-21 23:24  PushyTao  阅读(112)  评论(0编辑  收藏  举报