#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int INF = 2e9 + 7;
const int mod = 1e9 + 7;
map<int, int> m[N];
void update(int x, int y, int c) {
for(int i = x; i < N; i += i & -i) {
for(int j = y; j < N; j += j & -j) m[i][j] += c;
}
}
int get(int x, int y) {
int ans = 0;
for(int i = x; i > 0; i -= i & -i) {
for(int j = y; j > 0; j -= j & -j) ans += m[i][j];
}
return ans;
}
struct Fenwick {
int n;
vector<int> v;
Fenwick(int _n = 0) {
n = _n;
v.assign(n + 5, 0);
}
void update(int x, int c) {
for(; x > 0; x -= x & -x) v[x] += c;
}
int get(int x) {
int ans = 0;
for(; x <= n; x += x & -x) ans += v[x];
return ans;
}
} fw1, fw2;
int n, q;
int ans[N];
pair<int, int> a[N];
vector<pair<int, pair<int, int>>> queries[N];
void read() {
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se;
}
vector<int> valB, valC;
void compress() {
for(int i = 1; i <= n; i++) {
valB.push_back(a[i].se);
valC.push_back(a[i].fi + a[i].se);
}
valB.push_back(INF);
valC.push_back(INF);
sort(ALL(valB));
valB.erase(unique(ALL(valB)), valB.end());
sort(ALL(valC));
valC.erase(unique(ALL(valC)), valC.end());
}
void solve() {
sort(a + 1, a + n + 1);
for(int i = 1; i <= q; i++) {
int A, B, C; cin >> A >> B >> C;
int p = lower_bound(a + 1, a + n + 1, mp(A, 0)) - a;
if (p > n) ans[i] = 0;
else {
queries[p].push_back({i, mp(B, C)});
}
}
compress();
fw1 = Fenwick(SZ(valB));
fw2 = Fenwick(SZ(valC));
for(int i = n; i >= 1; i--) {
int b = lower_bound(ALL(valB), a[i].se) - valB.begin() + 1;
fw1.update(b, 1);
int c = lower_bound(ALL(valC), a[i].fi + a[i].se) - valC.begin() + 1;
fw2.update(c, 1);
update(b, c, 1);
// cerr << "BC: " << b << ' ' << c << endl;
for(auto &query: queries[i]) {
int id = query.fi;
int B = query.se.fi;
int C = query.se.se;
B = lower_bound(ALL(valB), B) - valB.begin() + 1;
C = lower_bound(ALL(valC), C) - valC.begin() + 1;
// cerr << id << ' ' << i << ' ' << n - i + 1 << endl;
ans[id] = fw1.get(B) + fw2.get(C) - (n - i + 1);
ans[id] += get(B - 1, C - 1);
// for(int j = i; j <= n; j++) if (a[j].se < query.se.fi && a[j].fi + a[j].se < query.se.se) {
// ++ans[id];
// }
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
Compilation message
examination.cpp: In function 'int main()':
examination.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
8528 KB |
Output is correct |
3 |
Correct |
3 ms |
8528 KB |
Output is correct |
4 |
Correct |
3 ms |
8528 KB |
Output is correct |
5 |
Correct |
3 ms |
8528 KB |
Output is correct |
6 |
Correct |
3 ms |
8700 KB |
Output is correct |
7 |
Correct |
38 ms |
15148 KB |
Output is correct |
8 |
Correct |
40 ms |
15184 KB |
Output is correct |
9 |
Correct |
40 ms |
15180 KB |
Output is correct |
10 |
Correct |
24 ms |
11344 KB |
Output is correct |
11 |
Correct |
32 ms |
14236 KB |
Output is correct |
12 |
Correct |
11 ms |
8784 KB |
Output is correct |
13 |
Correct |
26 ms |
13136 KB |
Output is correct |
14 |
Correct |
32 ms |
13136 KB |
Output is correct |
15 |
Correct |
26 ms |
13136 KB |
Output is correct |
16 |
Correct |
18 ms |
11600 KB |
Output is correct |
17 |
Correct |
23 ms |
11004 KB |
Output is correct |
18 |
Correct |
11 ms |
8696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1511 ms |
180780 KB |
Output is correct |
2 |
Correct |
1684 ms |
183284 KB |
Output is correct |
3 |
Correct |
1662 ms |
182932 KB |
Output is correct |
4 |
Correct |
938 ms |
68204 KB |
Output is correct |
5 |
Correct |
674 ms |
68676 KB |
Output is correct |
6 |
Correct |
278 ms |
13028 KB |
Output is correct |
7 |
Correct |
1598 ms |
182480 KB |
Output is correct |
8 |
Correct |
1680 ms |
183148 KB |
Output is correct |
9 |
Correct |
1601 ms |
182800 KB |
Output is correct |
10 |
Correct |
501 ms |
63760 KB |
Output is correct |
11 |
Correct |
1164 ms |
64364 KB |
Output is correct |
12 |
Correct |
304 ms |
11332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1511 ms |
180780 KB |
Output is correct |
2 |
Correct |
1684 ms |
183284 KB |
Output is correct |
3 |
Correct |
1662 ms |
182932 KB |
Output is correct |
4 |
Correct |
938 ms |
68204 KB |
Output is correct |
5 |
Correct |
674 ms |
68676 KB |
Output is correct |
6 |
Correct |
278 ms |
13028 KB |
Output is correct |
7 |
Correct |
1598 ms |
182480 KB |
Output is correct |
8 |
Correct |
1680 ms |
183148 KB |
Output is correct |
9 |
Correct |
1601 ms |
182800 KB |
Output is correct |
10 |
Correct |
501 ms |
63760 KB |
Output is correct |
11 |
Correct |
1164 ms |
64364 KB |
Output is correct |
12 |
Correct |
304 ms |
11332 KB |
Output is correct |
13 |
Execution timed out |
3075 ms |
291296 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
8528 KB |
Output is correct |
3 |
Correct |
3 ms |
8528 KB |
Output is correct |
4 |
Correct |
3 ms |
8528 KB |
Output is correct |
5 |
Correct |
3 ms |
8528 KB |
Output is correct |
6 |
Correct |
3 ms |
8700 KB |
Output is correct |
7 |
Correct |
38 ms |
15148 KB |
Output is correct |
8 |
Correct |
40 ms |
15184 KB |
Output is correct |
9 |
Correct |
40 ms |
15180 KB |
Output is correct |
10 |
Correct |
24 ms |
11344 KB |
Output is correct |
11 |
Correct |
32 ms |
14236 KB |
Output is correct |
12 |
Correct |
11 ms |
8784 KB |
Output is correct |
13 |
Correct |
26 ms |
13136 KB |
Output is correct |
14 |
Correct |
32 ms |
13136 KB |
Output is correct |
15 |
Correct |
26 ms |
13136 KB |
Output is correct |
16 |
Correct |
18 ms |
11600 KB |
Output is correct |
17 |
Correct |
23 ms |
11004 KB |
Output is correct |
18 |
Correct |
11 ms |
8696 KB |
Output is correct |
19 |
Correct |
1511 ms |
180780 KB |
Output is correct |
20 |
Correct |
1684 ms |
183284 KB |
Output is correct |
21 |
Correct |
1662 ms |
182932 KB |
Output is correct |
22 |
Correct |
938 ms |
68204 KB |
Output is correct |
23 |
Correct |
674 ms |
68676 KB |
Output is correct |
24 |
Correct |
278 ms |
13028 KB |
Output is correct |
25 |
Correct |
1598 ms |
182480 KB |
Output is correct |
26 |
Correct |
1680 ms |
183148 KB |
Output is correct |
27 |
Correct |
1601 ms |
182800 KB |
Output is correct |
28 |
Correct |
501 ms |
63760 KB |
Output is correct |
29 |
Correct |
1164 ms |
64364 KB |
Output is correct |
30 |
Correct |
304 ms |
11332 KB |
Output is correct |
31 |
Execution timed out |
3075 ms |
291296 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |