Educational Codeforces Round 4 D 扫描线思想

来源:互联网 发布:天正不能访问到网络锁 编辑:程序博客网 时间:2024/05/22 13:32



链接:戳这里


D. The Union of k-Segments
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others.

Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106) — the number of segments and the value of k.

The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109) each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order.

Output
First line contains integer m — the smallest number of segments.

Next m lines contain two integers aj, bj (aj ≤ bj) — the ends of j-th segment in the answer. The segments should be listed in the order from left to right.

Examples
input
3 2
0 5
-3 2
3 8
output
2
0 2
3 5
input
3 2
0 5
-3 3
3 8
output
1
0 5


题意:

给定n个线段[l,r],问哪些区间正好被线段覆盖了k次。输出这样的区间个数,以及区间


思路:

对于每一条线段,l即为进入,r即为出去。如果当前l进入了之后,使得区间覆盖值==k,那么这个l肯定是可以匹配相应的一个r的。对于r出去以后,相应的之前肯定存在一个l与之匹配


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#include<bitset>#define mst(ss,b) memset((ss),(b),sizeof(ss))///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef long double ld;#define INF (1ll<<60)-1#define Max 1e9using namespace std;vector<pair<ll,int> > V;vector<ll> anw;int n,k;int main(){    scanf("%d%d",&n,&k);    for(int i=1;i<=n;i++){        ll l,r;        scanf("%I64d%I64d",&l,&r);        V.push_back(make_pair(l,-1));        V.push_back(make_pair(r,1));    }    sort(V.begin(),V.end());    int num=0;    for(int i=0;i<V.size();i++){        if(V[i].second==-1){            num++;            if(num==k) anw.push_back(V[i].first);        } else {            if(num==k) anw.push_back(V[i].first);            num--;        }    }    printf("%d\n",anw.size()/2);    for(int i=0;i<anw.size();i+=2) printf("%I64d %I64d\n",anw[i],anw[i+1]);    return 0;}



0 0