#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 7;
const int MX = 1e9 + 1;
int s[N], t[N];
vector <int> f[N];
vector <int> vals[N];
void addupd(int x, int y) {
for (int i = x; i < N; i |= i + 1) vals[i].push_back(y);
}
void upd(int x, int y) {
for (int i = x; i < N; i |= i + 1) {
for (int j = lower_bound(vals[i].begin(), vals[i].end(), y) - vals[i].begin(); j < f[i].size(); j |= j + 1) {
++f[i][j];
}
}
}
int get(int x, int y) {
int res = 0;
for (int i = x; i >= 0; i &= i + 1, --i) {
for (int j = upper_bound(vals[i].begin(), vals[i].end(), y) - vals[i].begin() - 1; j >= 0; j &= j + 1, --j) {
res += f[i][j];
}
}
return res;
}
struct Query {
int a, b, c, id;
bool operator <(const Query &other) const {
return c > other.c;
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, q;
cin >> n >> q;
for (int i = 0; i < n; ++i) cin >> s[i] >> t[i];
vector <int> c;
for (int i = 0; i < n; ++i) c.push_back(MX - s[i]);
sort(c.begin(), c.end());
c.resize(unique(c.begin(), c.end()) - c.begin());
for (int i = 0; i < n; ++i) addupd(lower_bound(c.begin(), c.end(), MX - s[i]) - c.begin(), MX - t[i]);
for (int i = 0; i < N; ++i) {
sort(vals[i].begin(), vals[i].end());
vals[i].resize(unique(vals[i].begin(), vals[i].end()) - vals[i].begin());
f[i].resize(vals[i].size());
}
vector <int> p(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&] (int i, int j) {
return s[i] + t[i] > s[j] + t[j];
});
vector <Query> qr;
for (int i = 0; i < q; ++i) {
int a, b, c;
cin >> a >> b >> c;
qr.push_back({a, b, c, i});
}
sort(qr.begin(), qr.end());
vector <int> ans(q);
int uk = 0;
for (auto qry : qr) {
int a = qry.a, b = qry.b, sm = qry.c, id = qry.id;
while (uk < n && s[p[uk]] + t[p[uk]] >= sm) {
upd(lower_bound(c.begin(), c.end(), MX - s[p[uk]]) - c.begin(), MX - t[p[uk]]);
++uk;
}
ans[id] = get(upper_bound(c.begin(), c.end(), MX - a) - c.begin() - 1, MX - b);
}
for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
}
Compilation message
examination.cpp: In function 'void upd(int, int)':
examination.cpp:21:90: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = lower_bound(vals[i].begin(), vals[i].end(), y) - vals[i].begin(); j < f[i].size(); j |= j + 1) {
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
4984 KB |
Output is correct |
7 |
Correct |
18 ms |
5820 KB |
Output is correct |
8 |
Correct |
17 ms |
5752 KB |
Output is correct |
9 |
Correct |
18 ms |
5772 KB |
Output is correct |
10 |
Correct |
14 ms |
5624 KB |
Output is correct |
11 |
Correct |
16 ms |
5752 KB |
Output is correct |
12 |
Correct |
11 ms |
5496 KB |
Output is correct |
13 |
Correct |
17 ms |
5752 KB |
Output is correct |
14 |
Correct |
17 ms |
5752 KB |
Output is correct |
15 |
Correct |
17 ms |
5756 KB |
Output is correct |
16 |
Correct |
15 ms |
5880 KB |
Output is correct |
17 |
Correct |
12 ms |
5624 KB |
Output is correct |
18 |
Correct |
10 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
485 ms |
21976 KB |
Output is correct |
2 |
Correct |
496 ms |
21836 KB |
Output is correct |
3 |
Correct |
494 ms |
21868 KB |
Output is correct |
4 |
Correct |
285 ms |
18636 KB |
Output is correct |
5 |
Correct |
367 ms |
22160 KB |
Output is correct |
6 |
Correct |
167 ms |
17820 KB |
Output is correct |
7 |
Correct |
483 ms |
21864 KB |
Output is correct |
8 |
Correct |
460 ms |
21796 KB |
Output is correct |
9 |
Correct |
445 ms |
21672 KB |
Output is correct |
10 |
Correct |
347 ms |
22696 KB |
Output is correct |
11 |
Correct |
209 ms |
18344 KB |
Output is correct |
12 |
Correct |
113 ms |
18044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
485 ms |
21976 KB |
Output is correct |
2 |
Correct |
496 ms |
21836 KB |
Output is correct |
3 |
Correct |
494 ms |
21868 KB |
Output is correct |
4 |
Correct |
285 ms |
18636 KB |
Output is correct |
5 |
Correct |
367 ms |
22160 KB |
Output is correct |
6 |
Correct |
167 ms |
17820 KB |
Output is correct |
7 |
Correct |
483 ms |
21864 KB |
Output is correct |
8 |
Correct |
460 ms |
21796 KB |
Output is correct |
9 |
Correct |
445 ms |
21672 KB |
Output is correct |
10 |
Correct |
347 ms |
22696 KB |
Output is correct |
11 |
Correct |
209 ms |
18344 KB |
Output is correct |
12 |
Correct |
113 ms |
18044 KB |
Output is correct |
13 |
Correct |
528 ms |
22308 KB |
Output is correct |
14 |
Correct |
515 ms |
22512 KB |
Output is correct |
15 |
Correct |
494 ms |
21820 KB |
Output is correct |
16 |
Correct |
281 ms |
19052 KB |
Output is correct |
17 |
Correct |
380 ms |
22696 KB |
Output is correct |
18 |
Correct |
166 ms |
18024 KB |
Output is correct |
19 |
Correct |
559 ms |
22452 KB |
Output is correct |
20 |
Correct |
516 ms |
22312 KB |
Output is correct |
21 |
Correct |
522 ms |
22296 KB |
Output is correct |
22 |
Correct |
342 ms |
22672 KB |
Output is correct |
23 |
Correct |
208 ms |
18280 KB |
Output is correct |
24 |
Correct |
115 ms |
17824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
4984 KB |
Output is correct |
7 |
Correct |
18 ms |
5820 KB |
Output is correct |
8 |
Correct |
17 ms |
5752 KB |
Output is correct |
9 |
Correct |
18 ms |
5772 KB |
Output is correct |
10 |
Correct |
14 ms |
5624 KB |
Output is correct |
11 |
Correct |
16 ms |
5752 KB |
Output is correct |
12 |
Correct |
11 ms |
5496 KB |
Output is correct |
13 |
Correct |
17 ms |
5752 KB |
Output is correct |
14 |
Correct |
17 ms |
5752 KB |
Output is correct |
15 |
Correct |
17 ms |
5756 KB |
Output is correct |
16 |
Correct |
15 ms |
5880 KB |
Output is correct |
17 |
Correct |
12 ms |
5624 KB |
Output is correct |
18 |
Correct |
10 ms |
5496 KB |
Output is correct |
19 |
Correct |
485 ms |
21976 KB |
Output is correct |
20 |
Correct |
496 ms |
21836 KB |
Output is correct |
21 |
Correct |
494 ms |
21868 KB |
Output is correct |
22 |
Correct |
285 ms |
18636 KB |
Output is correct |
23 |
Correct |
367 ms |
22160 KB |
Output is correct |
24 |
Correct |
167 ms |
17820 KB |
Output is correct |
25 |
Correct |
483 ms |
21864 KB |
Output is correct |
26 |
Correct |
460 ms |
21796 KB |
Output is correct |
27 |
Correct |
445 ms |
21672 KB |
Output is correct |
28 |
Correct |
347 ms |
22696 KB |
Output is correct |
29 |
Correct |
209 ms |
18344 KB |
Output is correct |
30 |
Correct |
113 ms |
18044 KB |
Output is correct |
31 |
Correct |
528 ms |
22308 KB |
Output is correct |
32 |
Correct |
515 ms |
22512 KB |
Output is correct |
33 |
Correct |
494 ms |
21820 KB |
Output is correct |
34 |
Correct |
281 ms |
19052 KB |
Output is correct |
35 |
Correct |
380 ms |
22696 KB |
Output is correct |
36 |
Correct |
166 ms |
18024 KB |
Output is correct |
37 |
Correct |
559 ms |
22452 KB |
Output is correct |
38 |
Correct |
516 ms |
22312 KB |
Output is correct |
39 |
Correct |
522 ms |
22296 KB |
Output is correct |
40 |
Correct |
342 ms |
22672 KB |
Output is correct |
41 |
Correct |
208 ms |
18280 KB |
Output is correct |
42 |
Correct |
115 ms |
17824 KB |
Output is correct |
43 |
Correct |
559 ms |
26000 KB |
Output is correct |
44 |
Correct |
556 ms |
25844 KB |
Output is correct |
45 |
Correct |
554 ms |
25784 KB |
Output is correct |
46 |
Correct |
297 ms |
21652 KB |
Output is correct |
47 |
Correct |
425 ms |
25996 KB |
Output is correct |
48 |
Correct |
165 ms |
17876 KB |
Output is correct |
49 |
Correct |
524 ms |
25780 KB |
Output is correct |
50 |
Correct |
532 ms |
25784 KB |
Output is correct |
51 |
Correct |
484 ms |
25564 KB |
Output is correct |
52 |
Correct |
396 ms |
26772 KB |
Output is correct |
53 |
Correct |
274 ms |
20708 KB |
Output is correct |