-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsweep.cpp
More file actions
78 lines (67 loc) · 1.78 KB
/
sweep.cpp
File metadata and controls
78 lines (67 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//Given N intervals and Q points . For each points find, in how many intervals it lies.
// for ex- point 5 lies in three intervals out of given five intervals: [2,7] , [5,5] , [3,9] , [11,16] and [15,33]
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <utility>
std::vector<std::pair<int,std::pair<int,int>> > st;
void sweep(int n , int q )
{
std::vector<int>ans(q,0);
sort(st.begin(),st.end());
int sum=0,nn=st.size();
for(int i = 0 ; i < nn ; i++ )
{
int xx = st[i].first ;
int type = st[i].second.first ;
int id = st[i].second.second ;
if(type == -1)
{
sum++;
}
else if(type == 1 )
{
sum--;
}
else
{
ans[id]=sum;
}
}
for(int i = 0 ; i < q ; i++ )
{
printf("%d\n", ans[i]);
}
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n , q ;
scanf("%d %d",&n,&q);
int x , y ;
for(int i = 0 ; i < n ; i++ )
{
scanf("%d %d",&x,&y) ;
st.push_back({x,{-1,i}}) ;
st.push_back({y,{1,i}});
}
for(int i = 0 ; i < q ; i++ )
{
scanf("%d",&x);
st.push_back({x,{0, i}});
}
sweep(n,q);
return 0 ;
}
//-----------------------------------------Another variation of line sweep algorithm-------------------
/*
Given N intervals and a value k, remove minimal no. of intervals such that no points has cover of more than k intervals
*/
// problem : https://codeforces.com/contest/1249/problem/D2
// solution : https://codeforces.com/contest/1249/submission/63202336