Skip to content

Commit f3e8075

Browse files
authored
Create interval queries
This is a detailed solution to provide intuition on solving CP problems based on answering queries about time interval intersection. Have a look into at the question: http://codeforces.com/gym/294377/problem/E
1 parent 6509fb8 commit f3e8075

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
// your code goes here
6+
// this program will give an intuition behind the solution for CP questions based on interval quries. Refer to the question
7+
//http://codeforces.com/gym/294377/problem/E
8+
9+
//this following two lines are used for fast input-output
10+
ios_base::sync_with_stdio(false);
11+
cin.tie(NULL);
12+
13+
int t;
14+
cin>>t;
15+
while(t--) //running the program for 't' test cases
16+
{
17+
int n;
18+
cin>>n; //number of students in th college
19+
20+
//declare an array of max size (based on the input constraint) to indicate the timeline of all the students
21+
int N=2*100000;
22+
int a[N]={0};
23+
24+
for(int i=0;i<n;i++)
25+
{
26+
int x,y;
27+
cin>>x>>y;
28+
// while taking the entering and leaving time of each student, increase the (x-1)th index by one and decrease the (y-1)th index by 1.
29+
a[x-1]+=1;
30+
a[y-1]-=1;// Here the student is outside the college during the yth sec, hence we subtract 1 from (y-1)th index. Else we would subtract the same from yth index
31+
}
32+
for(int i=1;i<2*100000;i++)
33+
{
34+
// Summing the value in the current index by its previous index's value will give the total number of students at a perticular instance of time on the timeline a[N]
35+
a[i]+=a[i-1];
36+
}
37+
int q;
38+
cin>>q;
39+
while(q--)// running a loop to ans all the queries
40+
{
41+
int pp;
42+
cin>>pp;// The required time to output number of students present at that particular instance
43+
44+
// We can find out the number of students present at that perticular time by accessing the value stored in the array 'a' at (pp-1) index
45+
cout<<a[pp-1]<<endl;
46+
}
47+
}
48+
return 0;
49+
}

0 commit comments

Comments
 (0)