#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18 + 10
#define INF (ll)1e9
#define MOD (ll)(998244353)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
struct fen{
vector<int> tree;
int n;
fen(int _n = 0){
n = _n;
tree.resize(n + 3, 0);
}
void upd(int p, int c){
for(; p <= n; p += p & -p) tree[p] += c;
}
int get(int p){
int ans = 0;
for(; p > 0; p -= p & -p) ans += tree[p];
return ans;
}
int get(int l, int r){
return get(r) - get(l - 1);
}
};
const int MAX = 1e5 + 10;
int N, Q;
int S[MAX], T[MAX], X[MAX], Y[MAX], Z[MAX], ans[MAX];
void process1(vector<pair<ll, ll>> A, vector<array<ll, 3>> queries){
sort(ALL(A), greater<pair<ll, ll>>());
sort(ALL(queries), greater<array<ll, 3>>());
vector<ll> tmp;
for(auto t: A) tmp.push_back(t.se);
for(auto t: queries) tmp.push_back(t[1]);
cps(tmp);
auto pos = [&](ll c) -> int{
return lower_bound(ALL(tmp), c) - tmp.begin() + 1;
};
fen bit(sz(tmp));
int i = 0;
for(auto x: queries){
while(i < sz(A) && A[i].fi >= x[0]){
bit.upd(pos(A[i].se), 1);
i++;
}
ans[x[2]] += bit.get(pos(x[1]), sz(tmp));
}
}
void process2(vector<pair<ll, ll>> A, vector<array<ll, 4>> queries){
sort(ALL(A), greater<pair<ll, ll>>());
sort(ALL(queries), greater<array<ll, 4>>());
vector<ll> tmp;
for(auto t: A) tmp.push_back(t.se);
for(auto t: queries) tmp.push_back(t[1]), tmp.push_back(t[2]);
cps(tmp);
auto pos = [&](ll c) -> int{
return lower_bound(ALL(tmp), c) - tmp.begin() + 1;
};
fen bit(sz(tmp));
int i = 0;
for(auto x: queries){
while(i < sz(A) && A[i].fi >= x[0]){
bit.upd(pos(A[i].se), 1);
i++;
}
ans[x[3]] += bit.get(pos(x[1]), pos(x[2]));
}
}
void solve(){
cin >> N >> Q;
for(int i = 1; i <= N; i++) cin >> S[i] >> T[i];
for(int i = 1; i <= Q; i++) cin >> X[i] >> Y[i] >> Z[i];
//situation 1: X[i] + Y[i] >= Z[i]
vector<pair<ll, ll>> A;
vector<array<ll, 3>> queries1;
for(int i = 1; i <= N; i++) A.push_back({S[i], T[i]});
for(int i = 1; i <= Q; i++) if(X[i] + Y[i] >= Z[i]){
queries1.push_back({X[i], Y[i], i});
}
process1(A, queries1);
//situation 2: X[i] + Y[i] < Z[i]
//TH1: S[i] <= Z[i] - Y[i]
A.clear();
vector<array<ll, 4>> queries2;
for(int i = 1; i <= N; i++) A.push_back({S[i] + T[i], S[i]});
for(int i = 1; i <= Q; i++) if(X[i] + Y[i] < Z[i]){
queries2.push_back({Z[i], X[i], Z[i] - Y[i], i});
}
process2(A, queries2);
//TH2: S[i] > Z[i] - Y[i] > X[i] -> just count T[i] >- Y[i]
queries1.clear();
A.clear();
for(int i = 1; i <= N; i++) A.push_back({S[i], T[i]});
for(int i = 1; i <= Q; i++) if(X[i] + Y[i] < Z[i]){
queries1.push_back({Z[i] - Y[i] + 1, Y[i], i});
}
process1(A, queries1);
//print result
for(int i = 1; i <= Q; i++){
cout << ans[i] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("task.inp","r",stdin);
// freopen("task.out","w",stdout);
int T = 1;
// cin >> T;
for(; T; T--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
1084 KB |
Output is correct |
8 |
Correct |
5 ms |
1088 KB |
Output is correct |
9 |
Correct |
5 ms |
1084 KB |
Output is correct |
10 |
Correct |
4 ms |
860 KB |
Output is correct |
11 |
Correct |
5 ms |
1112 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
984 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
4 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
16664 KB |
Output is correct |
2 |
Correct |
128 ms |
16712 KB |
Output is correct |
3 |
Correct |
121 ms |
16580 KB |
Output is correct |
4 |
Correct |
85 ms |
15812 KB |
Output is correct |
5 |
Correct |
101 ms |
15828 KB |
Output is correct |
6 |
Correct |
72 ms |
15104 KB |
Output is correct |
7 |
Correct |
129 ms |
16548 KB |
Output is correct |
8 |
Correct |
116 ms |
16596 KB |
Output is correct |
9 |
Correct |
125 ms |
16316 KB |
Output is correct |
10 |
Correct |
91 ms |
15816 KB |
Output is correct |
11 |
Correct |
74 ms |
15920 KB |
Output is correct |
12 |
Correct |
54 ms |
15048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
16664 KB |
Output is correct |
2 |
Correct |
128 ms |
16712 KB |
Output is correct |
3 |
Correct |
121 ms |
16580 KB |
Output is correct |
4 |
Correct |
85 ms |
15812 KB |
Output is correct |
5 |
Correct |
101 ms |
15828 KB |
Output is correct |
6 |
Correct |
72 ms |
15104 KB |
Output is correct |
7 |
Correct |
129 ms |
16548 KB |
Output is correct |
8 |
Correct |
116 ms |
16596 KB |
Output is correct |
9 |
Correct |
125 ms |
16316 KB |
Output is correct |
10 |
Correct |
91 ms |
15816 KB |
Output is correct |
11 |
Correct |
74 ms |
15920 KB |
Output is correct |
12 |
Correct |
54 ms |
15048 KB |
Output is correct |
13 |
Correct |
171 ms |
16724 KB |
Output is correct |
14 |
Correct |
154 ms |
16060 KB |
Output is correct |
15 |
Correct |
127 ms |
16584 KB |
Output is correct |
16 |
Correct |
122 ms |
15984 KB |
Output is correct |
17 |
Correct |
141 ms |
16032 KB |
Output is correct |
18 |
Correct |
71 ms |
14276 KB |
Output is correct |
19 |
Correct |
169 ms |
16780 KB |
Output is correct |
20 |
Correct |
161 ms |
16248 KB |
Output is correct |
21 |
Correct |
194 ms |
18288 KB |
Output is correct |
22 |
Correct |
94 ms |
15808 KB |
Output is correct |
23 |
Correct |
75 ms |
15816 KB |
Output is correct |
24 |
Correct |
54 ms |
15156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
1084 KB |
Output is correct |
8 |
Correct |
5 ms |
1088 KB |
Output is correct |
9 |
Correct |
5 ms |
1084 KB |
Output is correct |
10 |
Correct |
4 ms |
860 KB |
Output is correct |
11 |
Correct |
5 ms |
1112 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
984 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
4 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
125 ms |
16664 KB |
Output is correct |
20 |
Correct |
128 ms |
16712 KB |
Output is correct |
21 |
Correct |
121 ms |
16580 KB |
Output is correct |
22 |
Correct |
85 ms |
15812 KB |
Output is correct |
23 |
Correct |
101 ms |
15828 KB |
Output is correct |
24 |
Correct |
72 ms |
15104 KB |
Output is correct |
25 |
Correct |
129 ms |
16548 KB |
Output is correct |
26 |
Correct |
116 ms |
16596 KB |
Output is correct |
27 |
Correct |
125 ms |
16316 KB |
Output is correct |
28 |
Correct |
91 ms |
15816 KB |
Output is correct |
29 |
Correct |
74 ms |
15920 KB |
Output is correct |
30 |
Correct |
54 ms |
15048 KB |
Output is correct |
31 |
Correct |
171 ms |
16724 KB |
Output is correct |
32 |
Correct |
154 ms |
16060 KB |
Output is correct |
33 |
Correct |
127 ms |
16584 KB |
Output is correct |
34 |
Correct |
122 ms |
15984 KB |
Output is correct |
35 |
Correct |
141 ms |
16032 KB |
Output is correct |
36 |
Correct |
71 ms |
14276 KB |
Output is correct |
37 |
Correct |
169 ms |
16780 KB |
Output is correct |
38 |
Correct |
161 ms |
16248 KB |
Output is correct |
39 |
Correct |
194 ms |
18288 KB |
Output is correct |
40 |
Correct |
94 ms |
15808 KB |
Output is correct |
41 |
Correct |
75 ms |
15816 KB |
Output is correct |
42 |
Correct |
54 ms |
15156 KB |
Output is correct |
43 |
Correct |
189 ms |
18700 KB |
Output is correct |
44 |
Correct |
204 ms |
18708 KB |
Output is correct |
45 |
Correct |
164 ms |
18000 KB |
Output is correct |
46 |
Correct |
120 ms |
17304 KB |
Output is correct |
47 |
Correct |
138 ms |
17272 KB |
Output is correct |
48 |
Correct |
90 ms |
16796 KB |
Output is correct |
49 |
Correct |
185 ms |
18504 KB |
Output is correct |
50 |
Correct |
203 ms |
20132 KB |
Output is correct |
51 |
Correct |
219 ms |
23052 KB |
Output is correct |
52 |
Correct |
129 ms |
17296 KB |
Output is correct |
53 |
Correct |
82 ms |
16576 KB |
Output is correct |