-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWOUT.cpp
More file actions
116 lines (107 loc) · 1.91 KB
/
WOUT.cpp
File metadata and controls
116 lines (107 loc) · 1.91 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
// This is a binary index tree based question
// Caution: the BIT is 1-ary
inline int scan()
{
register int n=0;
register char c=getchar_unlocked();
while( c<'0' || c>'9' )
c=getchar_unlocked();
while( c>='0' && c<='9' )
{
n=n*10+c-'0';
c=getchar_unlocked();
}
return n;
}
inline int min(int i,int j)
{
if( i<j ) return i;
else return j;
}
inline int max(int i,int j)
{
if( i>j ) return i;
else return j;
}
int n,h;
int l,r; // for intput
int idx,sum;
int sBIT[1000009]; // the starting point of the BIT
int eBIT[1000009]; // the end point of the BIT
int intv[1000009]; // the number of intervals
inline void test()
{
register int i,j;
n=scan();h=scan();
// height is from 1 to n (inclusive)
memset(sBIT,0,sizeof(sBIT));
memset(eBIT,0,sizeof(eBIT));
for(i=0;i<n;i++)
{
l=scan(); r=scan(); // scan the lower and the upper bounds
l++; r++;
idx=l;
while(idx<=n)
{
sBIT[idx]++;
idx+=(idx&-idx);
}
idx=(r+1);
while(idx<=n)
{
eBIT[idx]++;
idx+=(idx&-idx);
}
}
// all intervals done -> now make the array
memset(intv,0,sizeof(intv));
for(i=1;i<=n;i++) // over the whole range
{
sum=0;
idx=i;
while(idx>0)
{
sum+=sBIT[idx];
idx-=(idx&-idx);
}
idx=i;
while(idx>0)
{
sum-=eBIT[idx];
idx-=(idx&-idx);
}
intv[i-1]=n-sum;
}
/*
cout<<"print the intervals: "<<endl;
for(i=0;i<n;i++)
cout<<intv[i]<<" ";
cout<<endl;
*/
// now find the minimum window of size h
sum=0;
int min_sum=h*n;
for(i=0;i<h;i++)
sum+=intv[i];
for(i=h;i<n;i++)
{
sum+=intv[i];
sum-=intv[i-h];
min_sum=min(min_sum,sum);
}
cout<<min_sum<<endl;
}
int main()
{
int t=scan();
while( t-- )
test();
return 0;
}