#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define hash hashy
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 2e5 + 5, block = 900;
const int oo = 1e18 + 7, mod = 1e9 + 7;
int n, q;
unordered_map<int, int> uomp;
vector<int> vc;
iii queries[N];
ii students1[N], students2[N];
vector<int> srt[N/block];
int bit[N];
bool in[N];
int answer[N];
int counter = 0;
int ind_students[N];
int get(int pos){
int ans = 0;
for(; pos; pos -= pos & -pos) ans += bit[pos];
return ans;
}
bool cmp(int a, int b){
return (queries[a].fi.fi > queries[b].fi.fi);
}
ii que[N];
void process(){
cin >> n >> q;
vector<int> vc;
for(int i = 1; i <= n; i++){
cin >> students1[i].fi >> students1[i].se;
vc.pb(students1[i].se);
students2[i] = {students1[i].fi + students1[i].se, i};
}
//if(n == 100000 && q == 100000 && students1[i].fi == 92665) return;
sort(students2 + 1, students2 + n + 1);
for(int i = 1; i <= q; i++){
cin >> queries[i].fi.fi >> queries[i].fi.se >> queries[i].se;
vc.pb(queries[i].fi.se);
int pos = lower_bound(students2 + 1, students2 + n + 1, make_pair(queries[i].se, 0)) - students2;
srt[pos / block].pb(i);
}
sort(vc.begin(), vc.end());
vc.resize(unique(vc.begin(), vc.end()) - vc.begin());
for(int i = 0; i < vc.size(); i++) uomp[vc[i]] = i + 1;
for(int i = 1; i <= n; i++) ind_students[i] = uomp[students1[i].se];
set<ii> se;
for(int i = n/block + 2; i >= 0; i--){// l = (i * block), r = (i + 1) * block - 1
//cout << i << "\n";
int itr = 0;
for(set<ii>::iterator it = se.begin(); it != se.end(); it++){
itr++;
que[itr] = (*it);
}
//stack<ii> que;
//for(set<ii>::iterator it = se.begin(); it != se.end(); it++) que.push(it);
sort(srt[i].begin(), srt[i].end(), cmp);
for(int j = 1; j <= n + q; j++) bit[j] = 0;
for(int j = 1; j <= n; j++) in[j] = 0;
int lst = min(n + 1, (i + 1) * block);
vector<ii> vc1;
for(int j = max(1, i * block); j < min(n + 1, (i + 1) * block); j++){
int tmp = students2[j].se;
//if(!tmp) cout << j << " " << tmp << "\n";
vc1.pb({students1[tmp].fi, tmp});
}
sort(vc1.begin(), vc1.end());
for(auto it : srt[i]){
//cout << it << " " << i << "\n";
while(1){
if(!itr) break;
ii temp = que[itr];
//int x = que.top().fi;
if(temp.fi < queries[it].fi.fi) break;
//cout << x << " " << que.size() << "\n";
in[temp.se] = 1;
//upd(uomp[students1[students2[j].se].se], -1);
int pos = ind_students[temp.se];
for(; pos <= n + q; pos += pos & -pos) bit[pos]++;
//upd(uomp[students1[temp.se].se], 1);
itr--;
}
int temp = lower_bound(students2 + 1, students2 + n + 1, make_pair(queries[it].se, 0)) - students2;
//cout << it << " " << temp << "\n";
//cout << lst << " " << temp << " " << queries[it].se << "\n";
//continue;
for(int j = lst; j < temp; j++){
if(in[students2[j].se]){
in[students2[j].se] = 0;
//upd(uomp[students1[students2[j].se].se], -1);
int pos = ind_students[students2[j].se];
for(; pos <= n + q; pos += pos & -pos) bit[pos]--;
}
}
for(int j = temp; j < lst; j++){
if(students1[students2[j].se].fi >= queries[it].fi.fi && !in[students2[j].se]){
in[students2[j].se] = 1;
//upd(uomp[students1[students2[j].se].se], 1);
int pos = ind_students[students2[j].se];
for(; pos <= n + q; pos += pos & -pos) bit[pos]++;
}
}
lst = temp;
while(1){
if(!vc1.size()) break;
if(vc1.back().fi < queries[it].fi.fi) break;
int ind = vc1.back().se;
if(!in[ind] && students1[ind].fi + students1[ind].se >= queries[it].se){
in[ind] = 1;
//assert(ind);
int pos = ind_students[ind];
for(; pos <= n + q; pos += pos & -pos) bit[pos]++;
}
vc1.pop_back();
//cout << vc1.size() << "\n";
}
answer[it] = get(n + q) - get(uomp[queries[it].fi.se] - 1);
}
for(int j = i * block; j < min(n + 1, (i + 1) * block); j++){
int tmp = students2[j].se;
//cout << j << " " << tmp << "\n";
se.insert({students1[tmp].fi, tmp});
}
}
//cout << counter << "\n";
//cout << (double)clock() / CLOCKS_PER_SEC;
for(int i = 1; i <= q; i++) cout << answer[i] << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
//freopen("exam.txt", "r", stdin);
process();
}
Compilation message
examination.cpp:20:21: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
20 | const int oo = 1e18 + 7, mod = 1e9 + 7;
| ~~~~~^~~
examination.cpp: In function 'void process()':
examination.cpp:70:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i = 0; i < vc.size(); i++) uomp[vc[i]] = i + 1;
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
15 ms |
884 KB |
Output is correct |
8 |
Correct |
15 ms |
980 KB |
Output is correct |
9 |
Correct |
15 ms |
948 KB |
Output is correct |
10 |
Correct |
16 ms |
708 KB |
Output is correct |
11 |
Correct |
17 ms |
972 KB |
Output is correct |
12 |
Correct |
16 ms |
716 KB |
Output is correct |
13 |
Correct |
7 ms |
972 KB |
Output is correct |
14 |
Correct |
7 ms |
972 KB |
Output is correct |
15 |
Correct |
8 ms |
972 KB |
Output is correct |
16 |
Correct |
5 ms |
972 KB |
Output is correct |
17 |
Correct |
14 ms |
716 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
342 ms |
15920 KB |
Output is correct |
2 |
Correct |
332 ms |
16040 KB |
Output is correct |
3 |
Correct |
381 ms |
16052 KB |
Output is correct |
4 |
Correct |
192 ms |
11964 KB |
Output is correct |
5 |
Correct |
336 ms |
16008 KB |
Output is correct |
6 |
Correct |
240 ms |
11884 KB |
Output is correct |
7 |
Correct |
334 ms |
15984 KB |
Output is correct |
8 |
Correct |
334 ms |
14748 KB |
Output is correct |
9 |
Correct |
340 ms |
14676 KB |
Output is correct |
10 |
Correct |
312 ms |
15800 KB |
Output is correct |
11 |
Correct |
177 ms |
11720 KB |
Output is correct |
12 |
Correct |
173 ms |
11588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
342 ms |
15920 KB |
Output is correct |
2 |
Correct |
332 ms |
16040 KB |
Output is correct |
3 |
Correct |
381 ms |
16052 KB |
Output is correct |
4 |
Correct |
192 ms |
11964 KB |
Output is correct |
5 |
Correct |
336 ms |
16008 KB |
Output is correct |
6 |
Correct |
240 ms |
11884 KB |
Output is correct |
7 |
Correct |
334 ms |
15984 KB |
Output is correct |
8 |
Correct |
334 ms |
14748 KB |
Output is correct |
9 |
Correct |
340 ms |
14676 KB |
Output is correct |
10 |
Correct |
312 ms |
15800 KB |
Output is correct |
11 |
Correct |
177 ms |
11720 KB |
Output is correct |
12 |
Correct |
173 ms |
11588 KB |
Output is correct |
13 |
Correct |
1193 ms |
16300 KB |
Output is correct |
14 |
Correct |
926 ms |
16132 KB |
Output is correct |
15 |
Correct |
337 ms |
15968 KB |
Output is correct |
16 |
Correct |
1132 ms |
12008 KB |
Output is correct |
17 |
Correct |
1259 ms |
16020 KB |
Output is correct |
18 |
Correct |
327 ms |
12064 KB |
Output is correct |
19 |
Correct |
1301 ms |
16196 KB |
Output is correct |
20 |
Correct |
1234 ms |
16096 KB |
Output is correct |
21 |
Correct |
1539 ms |
15300 KB |
Output is correct |
22 |
Correct |
333 ms |
15772 KB |
Output is correct |
23 |
Correct |
183 ms |
11836 KB |
Output is correct |
24 |
Correct |
166 ms |
11584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
15 ms |
884 KB |
Output is correct |
8 |
Correct |
15 ms |
980 KB |
Output is correct |
9 |
Correct |
15 ms |
948 KB |
Output is correct |
10 |
Correct |
16 ms |
708 KB |
Output is correct |
11 |
Correct |
17 ms |
972 KB |
Output is correct |
12 |
Correct |
16 ms |
716 KB |
Output is correct |
13 |
Correct |
7 ms |
972 KB |
Output is correct |
14 |
Correct |
7 ms |
972 KB |
Output is correct |
15 |
Correct |
8 ms |
972 KB |
Output is correct |
16 |
Correct |
5 ms |
972 KB |
Output is correct |
17 |
Correct |
14 ms |
716 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
19 |
Correct |
342 ms |
15920 KB |
Output is correct |
20 |
Correct |
332 ms |
16040 KB |
Output is correct |
21 |
Correct |
381 ms |
16052 KB |
Output is correct |
22 |
Correct |
192 ms |
11964 KB |
Output is correct |
23 |
Correct |
336 ms |
16008 KB |
Output is correct |
24 |
Correct |
240 ms |
11884 KB |
Output is correct |
25 |
Correct |
334 ms |
15984 KB |
Output is correct |
26 |
Correct |
334 ms |
14748 KB |
Output is correct |
27 |
Correct |
340 ms |
14676 KB |
Output is correct |
28 |
Correct |
312 ms |
15800 KB |
Output is correct |
29 |
Correct |
177 ms |
11720 KB |
Output is correct |
30 |
Correct |
173 ms |
11588 KB |
Output is correct |
31 |
Correct |
1193 ms |
16300 KB |
Output is correct |
32 |
Correct |
926 ms |
16132 KB |
Output is correct |
33 |
Correct |
337 ms |
15968 KB |
Output is correct |
34 |
Correct |
1132 ms |
12008 KB |
Output is correct |
35 |
Correct |
1259 ms |
16020 KB |
Output is correct |
36 |
Correct |
327 ms |
12064 KB |
Output is correct |
37 |
Correct |
1301 ms |
16196 KB |
Output is correct |
38 |
Correct |
1234 ms |
16096 KB |
Output is correct |
39 |
Correct |
1539 ms |
15300 KB |
Output is correct |
40 |
Correct |
333 ms |
15772 KB |
Output is correct |
41 |
Correct |
183 ms |
11836 KB |
Output is correct |
42 |
Correct |
166 ms |
11584 KB |
Output is correct |
43 |
Correct |
1413 ms |
21128 KB |
Output is correct |
44 |
Correct |
1310 ms |
25948 KB |
Output is correct |
45 |
Correct |
1037 ms |
26000 KB |
Output is correct |
46 |
Correct |
1192 ms |
15356 KB |
Output is correct |
47 |
Correct |
1161 ms |
24252 KB |
Output is correct |
48 |
Correct |
299 ms |
12796 KB |
Output is correct |
49 |
Correct |
1570 ms |
25952 KB |
Output is correct |
50 |
Correct |
1238 ms |
25836 KB |
Output is correct |
51 |
Correct |
1506 ms |
25776 KB |
Output is correct |
52 |
Correct |
1110 ms |
24248 KB |
Output is correct |
53 |
Correct |
196 ms |
14236 KB |
Output is correct |