#include <bits/stdc++.h>
#define all(s) s.begin(), s.end()
#define lb(s, a) lower_bound(all(s), (a)) - s.begin()
using namespace std;
typedef long long ll;
const int ar = 2e5 + 2;
const ll mod = 1e9 + 7;
const int oo = 2e9;
struct BIT {
vector<vector<int>> c, t;
bool b;
BIT(int n) {
b = 0;
c = vector<vector<int>>(n);
t = vector<vector<int>>(n);
}
void init() {
b = 1;
for (int i = 0; i < c.size(); i++) {
sort(c[i].begin(), c[i].end());
c[i].resize(unique(c[i].begin(), c[i].end()) - c[i].begin());
t[i] = vector<int>(c[i].size(), 0);
}
}
void update(int i, int j) {
i++, j++;
while (i <= t.size()) {
if (!b)
c[i - 1].push_back(j);
else {
int k = upper_bound(c[i - 1].begin(), c[i - 1].end(), j) -
c[i - 1].begin();
while (k <= c[i - 1].size()) {
t[i - 1][k - 1]++;
k += k & -k;
}
}
i += i & -i;
}
}
int pre(int i, int j) {
int x = 0;
i++, j++;
while (i) {
int k = upper_bound(c[i - 1].begin(), c[i - 1].end(), j) - c[i - 1].begin();
while (k) {
x += t[i - 1][k - 1];
k -= k & -k;
}
i -= i & -i;
}
return x;
}
int get(int i1, int i2, int j1, int j2) {
int x = pre(i2, j2);
if (i1)
x -= pre(i1 - 1, j2);
if (j1)
x -= pre(i2, j1 - 1);
if (i1 && j1)
x += pre(i1 - 1, j1 - 1);
return x;
}
};
struct Event {
int t, x, y;
int i;
bool a;
bool operator<(Event const &e) const {
if (t == e.t)
return a && !e.a;
return t > e.t;
}
};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "code"
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int n, q;
cin >> n >> q;
vector<Event> tap;
vector<int> td;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
td.push_back(x);
tap.push_back({x + y, x, y, n, 1});
}
for (int i = 0; i < q; i++) {
int x, y, z;
cin >> x >> y >> z;
td.push_back(x);
tap.push_back({z, x, y, i, 0});
}
sort(all(tap));
sort(all(td));
td.resize(unique(all(td)) - td.begin());
BIT bit(td.size());
vector<int> ans(q, 0);
for (auto const &[t, x, y, i, a] : tap)
if (a)
bit.update(lb(td, x), y);
bit.init();
for (auto const &[t, x, y, i, a] : tap) {
if (a)
bit.update(lb(td, x), y);
else
ans[i] = bit.get(lb(td, x), td.size() - 1, y, oo);
}
for (auto const &a : ans)
cout << a << '\n';
}
Compilation message
examination.cpp: In member function 'void BIT::init()':
examination.cpp:25:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i = 0; i < c.size(); i++) {
| ~~^~~~~~~~~~
examination.cpp: In member function 'void BIT::update(int, int)':
examination.cpp:34:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while (i <= t.size()) {
| ~~^~~~~~~~~~~
examination.cpp:40:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | while (k <= c[i - 1].size()) {
| ~~^~~~~~~~~~~~~~~~~~
examination.cpp: In function 'int main()':
examination.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
1448 KB |
Output is correct |
8 |
Correct |
7 ms |
1372 KB |
Output is correct |
9 |
Correct |
7 ms |
1444 KB |
Output is correct |
10 |
Correct |
5 ms |
1372 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
644 KB |
Output is correct |
13 |
Correct |
6 ms |
1368 KB |
Output is correct |
14 |
Correct |
6 ms |
1372 KB |
Output is correct |
15 |
Correct |
6 ms |
1412 KB |
Output is correct |
16 |
Correct |
3 ms |
968 KB |
Output is correct |
17 |
Correct |
4 ms |
1116 KB |
Output is correct |
18 |
Correct |
2 ms |
644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
350 ms |
23504 KB |
Output is correct |
2 |
Correct |
355 ms |
24700 KB |
Output is correct |
3 |
Correct |
348 ms |
23508 KB |
Output is correct |
4 |
Correct |
149 ms |
20936 KB |
Output is correct |
5 |
Correct |
122 ms |
10824 KB |
Output is correct |
6 |
Correct |
54 ms |
9704 KB |
Output is correct |
7 |
Correct |
305 ms |
21636 KB |
Output is correct |
8 |
Correct |
316 ms |
23504 KB |
Output is correct |
9 |
Correct |
271 ms |
21972 KB |
Output is correct |
10 |
Correct |
76 ms |
10204 KB |
Output is correct |
11 |
Correct |
110 ms |
20076 KB |
Output is correct |
12 |
Correct |
41 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
350 ms |
23504 KB |
Output is correct |
2 |
Correct |
355 ms |
24700 KB |
Output is correct |
3 |
Correct |
348 ms |
23508 KB |
Output is correct |
4 |
Correct |
149 ms |
20936 KB |
Output is correct |
5 |
Correct |
122 ms |
10824 KB |
Output is correct |
6 |
Correct |
54 ms |
9704 KB |
Output is correct |
7 |
Correct |
305 ms |
21636 KB |
Output is correct |
8 |
Correct |
316 ms |
23504 KB |
Output is correct |
9 |
Correct |
271 ms |
21972 KB |
Output is correct |
10 |
Correct |
76 ms |
10204 KB |
Output is correct |
11 |
Correct |
110 ms |
20076 KB |
Output is correct |
12 |
Correct |
41 ms |
9708 KB |
Output is correct |
13 |
Correct |
434 ms |
24076 KB |
Output is correct |
14 |
Correct |
383 ms |
25044 KB |
Output is correct |
15 |
Correct |
371 ms |
23760 KB |
Output is correct |
16 |
Correct |
182 ms |
21204 KB |
Output is correct |
17 |
Correct |
133 ms |
10112 KB |
Output is correct |
18 |
Correct |
66 ms |
8940 KB |
Output is correct |
19 |
Correct |
363 ms |
24016 KB |
Output is correct |
20 |
Correct |
374 ms |
24116 KB |
Output is correct |
21 |
Correct |
410 ms |
24024 KB |
Output is correct |
22 |
Correct |
76 ms |
9080 KB |
Output is correct |
23 |
Correct |
117 ms |
21076 KB |
Output is correct |
24 |
Correct |
42 ms |
9704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
1448 KB |
Output is correct |
8 |
Correct |
7 ms |
1372 KB |
Output is correct |
9 |
Correct |
7 ms |
1444 KB |
Output is correct |
10 |
Correct |
5 ms |
1372 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
644 KB |
Output is correct |
13 |
Correct |
6 ms |
1368 KB |
Output is correct |
14 |
Correct |
6 ms |
1372 KB |
Output is correct |
15 |
Correct |
6 ms |
1412 KB |
Output is correct |
16 |
Correct |
3 ms |
968 KB |
Output is correct |
17 |
Correct |
4 ms |
1116 KB |
Output is correct |
18 |
Correct |
2 ms |
644 KB |
Output is correct |
19 |
Correct |
350 ms |
23504 KB |
Output is correct |
20 |
Correct |
355 ms |
24700 KB |
Output is correct |
21 |
Correct |
348 ms |
23508 KB |
Output is correct |
22 |
Correct |
149 ms |
20936 KB |
Output is correct |
23 |
Correct |
122 ms |
10824 KB |
Output is correct |
24 |
Correct |
54 ms |
9704 KB |
Output is correct |
25 |
Correct |
305 ms |
21636 KB |
Output is correct |
26 |
Correct |
316 ms |
23504 KB |
Output is correct |
27 |
Correct |
271 ms |
21972 KB |
Output is correct |
28 |
Correct |
76 ms |
10204 KB |
Output is correct |
29 |
Correct |
110 ms |
20076 KB |
Output is correct |
30 |
Correct |
41 ms |
9708 KB |
Output is correct |
31 |
Correct |
434 ms |
24076 KB |
Output is correct |
32 |
Correct |
383 ms |
25044 KB |
Output is correct |
33 |
Correct |
371 ms |
23760 KB |
Output is correct |
34 |
Correct |
182 ms |
21204 KB |
Output is correct |
35 |
Correct |
133 ms |
10112 KB |
Output is correct |
36 |
Correct |
66 ms |
8940 KB |
Output is correct |
37 |
Correct |
363 ms |
24016 KB |
Output is correct |
38 |
Correct |
374 ms |
24116 KB |
Output is correct |
39 |
Correct |
410 ms |
24024 KB |
Output is correct |
40 |
Correct |
76 ms |
9080 KB |
Output is correct |
41 |
Correct |
117 ms |
21076 KB |
Output is correct |
42 |
Correct |
42 ms |
9704 KB |
Output is correct |
43 |
Correct |
426 ms |
35284 KB |
Output is correct |
44 |
Correct |
407 ms |
35196 KB |
Output is correct |
45 |
Correct |
412 ms |
35712 KB |
Output is correct |
46 |
Correct |
200 ms |
31540 KB |
Output is correct |
47 |
Correct |
139 ms |
11796 KB |
Output is correct |
48 |
Correct |
59 ms |
8424 KB |
Output is correct |
49 |
Correct |
373 ms |
33240 KB |
Output is correct |
50 |
Correct |
393 ms |
36052 KB |
Output is correct |
51 |
Correct |
329 ms |
32612 KB |
Output is correct |
52 |
Correct |
93 ms |
11224 KB |
Output is correct |
53 |
Correct |
125 ms |
30932 KB |
Output is correct |