 I wrote a solution to the problem, sample data worked but there was a runtime problem. I was sure its because of the data constraints. So, I read the editorial after which I felt my programming proficiency is lacking, I have completed a course on Data Structures at my college, have not done anything on Algorithms yet.

The code I wrote

``````#include <iostream>
using namespace std;

struct timeinterval {
int x,y;
int flag;
};

int main (void) {
int n, groups, aliens, i, j, k, ans = 0;
timeinterval t;
cin >> n;
for (i = 0; i < n; i++) {
cin >> t[i].x >> t[i].y;
t[i].flag = 0;
}
//cout << "Groups" <<endl;
cin >> groups;
for (i = 0; i<groups; i++) {
//cout << "Group" << i+1 <<endl;
cin >> aliens [i];
for (j = 1; j<=aliens[i]; j++) {
cin >> aliens[i][j];
}
}

for (i = 0; i < groups; i++) {
for ( j = 1; j <= aliens[i]; j++) {
for (k = 0; k < n; k++) {
if (aliens[i][j] >= t[k].x && aliens [i][j] <= t[k].y && t[k].flag == 0) {
t[k].flag = 1;
ans++;
}
}
}
cout << ans << endl;
ans = 0;
for (k = 0; k < n; k++)
t[k].flag = 0;

}
return 0;
}
``````

My question is,
1> Is it possible to solve this question like this?
2> Could you point me in the direction of something which I could read and practice for better coding.

It seems to me, that you assume that n and groups < 100, in statement max N is 10^5 and max Q is 5*10^3 and the product of these two is 5*10^8 - too slow. Try the tutorial My opinion is that contests are the best for practice, if you are not Petr, Gennady or ACRush (or few others) there is always problem that teaches you something. There are just two contests in month on CodeChef (long and short), but if you use the break between two contest to solve the problems you didn’t solve in contest, to understand algorithms/approaches used in the solutions, you will perform better next time, FOR SURE.