http://codeforces.com/contest/612/problem/D

D. The Union of k-Segments
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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个区间求被覆盖K次的区间。

题解:用线段扫描左端点(用-1标记)进入tmp++,右端点(用1标记)出来tmp--,当tmp为k时就是答案区间。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm> 
#define pii pair<int,int> 
#define pb push_back 
#define mp make_pair 
using namespace std;
vector<int>ans;
vector<pii>s; 
int n,k; 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        int a,b; 
        cin>>a>>b;
        s.pb(mp(a,-1));
        s.pb(mp(b,1)); 
    } 
    sort(s.begin(),s.end());
    int tmp=0; 
    for(int i=0;i<s.size();i++)
    {
        if(s[i].second==-1)
        {
            tmp++; 
            if(tmp==k)ans.pb(s[i].first); 
        }
        else
        {
            if(tmp==k)ans.pb(s[i].first); 
            tmp--;
        } 
    } 
    cout<<ans.size()/2<<endl; 
    for(int i=0;i<ans.size()/2;i++)
    {
        cout<<ans[2*i]<<" "<<ans[2*i+1]<<endl; 
    } 
} 
原文地址:https://www.cnblogs.com/lhclqslove/p/7403848.html