#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 2;
// int num_node = 0;
int n , q , m;
pair < ll , ll > a[N + 2];
vector < ll > vt;
bool cmp_1(pair < int , int > g , pair < int , int > h){
return g.first < h.first;
}
bool cmp_2(pair < int , int > g , pair < int , int > h){
return g.first < h.first;
}
struct per{
vector <pair < int , int > > bit[N * 4 + 2];
void update(int id , int timer){
while(id <= m){
if(bit[id].empty())bit[id].push_back({timer , 1});
else{
int cur = bit[id].back().second;
bit[id].push_back({timer , cur + 1});
}
id += id &(-id);
}
}
int get(int id , int timer){
int ans = 0;
while(id > 0){
if(bit[id].size()){
int k = upper_bound(bit[id].begin() , bit[id].end() , make_pair(timer , 0), cmp_1) - bit[id].begin();
k--;
if(k >= 0){
ans += bit[id][k].second;
}
}
id -= id &(-id);
}
return ans;
}
}BIT_ONE ,BIT_TWO;
int get_one(ll l , int timer_l , int timer_r){
int k = upper_bound(vt.begin() , vt.end() , l) - vt.begin();
if(k == 0)return 0;
return BIT_ONE.get(m , timer_r) - BIT_ONE.get(k - 1 , timer_r) + BIT_ONE.get(k - 1 , timer_l - 1) - BIT_ONE.get(m , timer_l - 1);
}
int get_two(ll c , int timer_l , int timer_r){
int k = upper_bound(vt.begin() , vt.end() , c) - vt.begin();
if(k == 0)return 0;
return BIT_TWO.get(m , timer_r) - BIT_TWO.get(k - 1 , timer_r) + BIT_TWO.get(k - 1 , timer_l - 1) - BIT_TWO.get(m , timer_l - 1);
}
struct qq{
ll t , c;
int id;
};
vector < qq > query[N + 2];
int ans[N + 2];
void pre(){
for(int i= 1; i <= n ; i ++){
vt.push_back(a[i].second);
vt.push_back(a[i].first + a[i].second);
}
sort(vt.begin() , vt.end());
vt.erase(unique(vt.begin() , vt.end()) , vt.end());
m= vt.size();
for(int i = 1 ; i <= n; i ++){
int k = upper_bound(vt.begin() , vt.end() , a[i].second) - vt.begin();
BIT_ONE.update(k , i);
k = upper_bound(vt.begin() , vt.end() , a[i].first + a[i].second)- vt.begin();
BIT_TWO.update(k , i);
// cout << get_two(100 , 1 , n) << ' ';
}
// for(auto v: vt)cout <<v << ' ';
// cout << BIT_ONE.get(m , n) -BIT_ONE.get(k - 1 , n) << '\n';
// cout <<'\n';
}
int bin(ll t , ll c , int i){
int l = i;
int r = n ;
while(l <= r){
int mid = (l + r) / 2;
if(c - a[mid].first >= t){
l = mid + 1;
}
else r = mid - 1;
}
return l - 1;
}
void solve() {
cin >> n >> q;
for(int i = 1; i <= n ; i ++){
cin >> a[i].first >> a[i].second;
}
sort(a + 1 , a + n + 1);
for(int i = 1 ; i <= q ; i ++){
ll s , t , c;
cin >> s >> t >> c;
int k = lower_bound(a + 1 , a + n + 1, make_pair(s , 0), cmp_2) - a;
query[k].push_back({t , c , i});
vt.push_back(t);
vt.push_back(c);
}
pre();
for(int i =n ;i >= 1; i --){
for(auto cur : query[i]){
int kl = bin(cur.t , cur.c , i);
if(kl >= i){
ans[cur.id] += get_two(cur.c , i , kl);
}
if(kl < n){
ans[cur.id] += get_one(cur.t , kl + 1 , n);
}
}
}
for(int i = 1 ; i <= q; i ++){
cout << ans[i] << '\n';
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
#define _ "joi1."
if (fopen(_ "inp", "r")) {
freopen(_ "inp", "r", stdin);
freopen(_ "out", "w", stdout);
}
solve();
}
Compilation message
examination.cpp: In function 'int main()':
examination.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | freopen(_ "inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
examination.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | freopen(_ "out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
21596 KB |
Output is correct |
2 |
Correct |
9 ms |
21512 KB |
Output is correct |
3 |
Correct |
9 ms |
21596 KB |
Output is correct |
4 |
Correct |
10 ms |
21596 KB |
Output is correct |
5 |
Correct |
9 ms |
21440 KB |
Output is correct |
6 |
Correct |
9 ms |
21596 KB |
Output is correct |
7 |
Correct |
21 ms |
22364 KB |
Output is correct |
8 |
Correct |
16 ms |
22360 KB |
Output is correct |
9 |
Correct |
21 ms |
22364 KB |
Output is correct |
10 |
Correct |
13 ms |
22364 KB |
Output is correct |
11 |
Correct |
14 ms |
22364 KB |
Output is correct |
12 |
Correct |
11 ms |
21964 KB |
Output is correct |
13 |
Correct |
14 ms |
22360 KB |
Output is correct |
14 |
Correct |
14 ms |
22364 KB |
Output is correct |
15 |
Correct |
15 ms |
22360 KB |
Output is correct |
16 |
Correct |
14 ms |
22364 KB |
Output is correct |
17 |
Correct |
14 ms |
22552 KB |
Output is correct |
18 |
Correct |
11 ms |
22104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
258 ms |
50920 KB |
Output is correct |
2 |
Correct |
255 ms |
53692 KB |
Output is correct |
3 |
Correct |
247 ms |
53688 KB |
Output is correct |
4 |
Correct |
137 ms |
53944 KB |
Output is correct |
5 |
Correct |
190 ms |
53824 KB |
Output is correct |
6 |
Correct |
72 ms |
38204 KB |
Output is correct |
7 |
Correct |
225 ms |
53700 KB |
Output is correct |
8 |
Correct |
220 ms |
53172 KB |
Output is correct |
9 |
Correct |
239 ms |
53356 KB |
Output is correct |
10 |
Correct |
152 ms |
52024 KB |
Output is correct |
11 |
Correct |
131 ms |
55112 KB |
Output is correct |
12 |
Correct |
55 ms |
37396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
258 ms |
50920 KB |
Output is correct |
2 |
Correct |
255 ms |
53692 KB |
Output is correct |
3 |
Correct |
247 ms |
53688 KB |
Output is correct |
4 |
Correct |
137 ms |
53944 KB |
Output is correct |
5 |
Correct |
190 ms |
53824 KB |
Output is correct |
6 |
Correct |
72 ms |
38204 KB |
Output is correct |
7 |
Correct |
225 ms |
53700 KB |
Output is correct |
8 |
Correct |
220 ms |
53172 KB |
Output is correct |
9 |
Correct |
239 ms |
53356 KB |
Output is correct |
10 |
Correct |
152 ms |
52024 KB |
Output is correct |
11 |
Correct |
131 ms |
55112 KB |
Output is correct |
12 |
Correct |
55 ms |
37396 KB |
Output is correct |
13 |
Correct |
337 ms |
55676 KB |
Output is correct |
14 |
Correct |
297 ms |
55480 KB |
Output is correct |
15 |
Correct |
245 ms |
53660 KB |
Output is correct |
16 |
Correct |
206 ms |
57740 KB |
Output is correct |
17 |
Correct |
204 ms |
53428 KB |
Output is correct |
18 |
Correct |
80 ms |
38572 KB |
Output is correct |
19 |
Correct |
327 ms |
55736 KB |
Output is correct |
20 |
Correct |
337 ms |
55736 KB |
Output is correct |
21 |
Correct |
347 ms |
55736 KB |
Output is correct |
22 |
Correct |
136 ms |
52080 KB |
Output is correct |
23 |
Correct |
133 ms |
54964 KB |
Output is correct |
24 |
Correct |
55 ms |
37404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
21596 KB |
Output is correct |
2 |
Correct |
9 ms |
21512 KB |
Output is correct |
3 |
Correct |
9 ms |
21596 KB |
Output is correct |
4 |
Correct |
10 ms |
21596 KB |
Output is correct |
5 |
Correct |
9 ms |
21440 KB |
Output is correct |
6 |
Correct |
9 ms |
21596 KB |
Output is correct |
7 |
Correct |
21 ms |
22364 KB |
Output is correct |
8 |
Correct |
16 ms |
22360 KB |
Output is correct |
9 |
Correct |
21 ms |
22364 KB |
Output is correct |
10 |
Correct |
13 ms |
22364 KB |
Output is correct |
11 |
Correct |
14 ms |
22364 KB |
Output is correct |
12 |
Correct |
11 ms |
21964 KB |
Output is correct |
13 |
Correct |
14 ms |
22360 KB |
Output is correct |
14 |
Correct |
14 ms |
22364 KB |
Output is correct |
15 |
Correct |
15 ms |
22360 KB |
Output is correct |
16 |
Correct |
14 ms |
22364 KB |
Output is correct |
17 |
Correct |
14 ms |
22552 KB |
Output is correct |
18 |
Correct |
11 ms |
22104 KB |
Output is correct |
19 |
Correct |
258 ms |
50920 KB |
Output is correct |
20 |
Correct |
255 ms |
53692 KB |
Output is correct |
21 |
Correct |
247 ms |
53688 KB |
Output is correct |
22 |
Correct |
137 ms |
53944 KB |
Output is correct |
23 |
Correct |
190 ms |
53824 KB |
Output is correct |
24 |
Correct |
72 ms |
38204 KB |
Output is correct |
25 |
Correct |
225 ms |
53700 KB |
Output is correct |
26 |
Correct |
220 ms |
53172 KB |
Output is correct |
27 |
Correct |
239 ms |
53356 KB |
Output is correct |
28 |
Correct |
152 ms |
52024 KB |
Output is correct |
29 |
Correct |
131 ms |
55112 KB |
Output is correct |
30 |
Correct |
55 ms |
37396 KB |
Output is correct |
31 |
Correct |
337 ms |
55676 KB |
Output is correct |
32 |
Correct |
297 ms |
55480 KB |
Output is correct |
33 |
Correct |
245 ms |
53660 KB |
Output is correct |
34 |
Correct |
206 ms |
57740 KB |
Output is correct |
35 |
Correct |
204 ms |
53428 KB |
Output is correct |
36 |
Correct |
80 ms |
38572 KB |
Output is correct |
37 |
Correct |
327 ms |
55736 KB |
Output is correct |
38 |
Correct |
337 ms |
55736 KB |
Output is correct |
39 |
Correct |
347 ms |
55736 KB |
Output is correct |
40 |
Correct |
136 ms |
52080 KB |
Output is correct |
41 |
Correct |
133 ms |
54964 KB |
Output is correct |
42 |
Correct |
55 ms |
37404 KB |
Output is correct |
43 |
Correct |
415 ms |
60972 KB |
Output is correct |
44 |
Correct |
393 ms |
60856 KB |
Output is correct |
45 |
Correct |
395 ms |
60088 KB |
Output is correct |
46 |
Correct |
229 ms |
60600 KB |
Output is correct |
47 |
Correct |
296 ms |
61244 KB |
Output is correct |
48 |
Correct |
81 ms |
40252 KB |
Output is correct |
49 |
Correct |
457 ms |
59928 KB |
Output is correct |
50 |
Correct |
368 ms |
59832 KB |
Output is correct |
51 |
Correct |
467 ms |
58860 KB |
Output is correct |
52 |
Correct |
178 ms |
58128 KB |
Output is correct |
53 |
Correct |
131 ms |
57280 KB |
Output is correct |