#include <stdio.h>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <iostream>
#include <memory.h>
using namespace std;
const int N = (int)1e6 + 7;
int n, m;
pair <int, int> ar[N];
struct fen {
int tree[N];
fen() {
memset(tree, 0, sizeof tree);
}
void upd(int pos, int val) {
while (pos < N) {
tree[pos] += val;
pos = pos | pos + 1;
}
}
int get(int r) {
int res = 0;
while (r >= 0) {
res += tree[r];
r = (r & r + 1) - 1;
}
return res;
}
};
fen tr;
struct query {
int id, tp, pos, x, y, val, z;
query() {}
query(int _pos, int _tp, int _x, int _y, int _z, int _val, int _id) {
pos = _pos;
tp = _tp;
x = _x;
id = _id;
val = _val;
z = _z;
y = _y;
}
};
vector <query> vec;
int ans[N];
query tp[N];
void make(int l, int r) {
if (l >= r) return ;
int mid = (l + r) >> 1;
for (int i = l; i <= r; i++) {
if (vec[i].pos <= mid && vec[i].tp == 0) {
tr.upd(vec[i].y, vec[i].val);
} else if (vec[i].pos > mid && vec[i].tp == 1) {
ans[vec[i].id] += vec[i].val * tr.get(vec[i].y);
}
}
for (int i = l; i <= r; i++) {
if (vec[i].pos <= mid && vec[i].tp == 0) {
tr.upd(vec[i].y, -vec[i].val);
}
}
int curl = l;
int curr = mid + 1;
for (int i = l; i <= r; i++) {
if (vec[i].pos <= mid) {
tp[curl++] = vec[i];
} else {
tp[curr++] = vec[i];
}
}
for (int i = l; i <= r; i++) {
vec[i] = tp[i];
}
make(l, mid);
make(mid + 1, r);
}
main() {
scanf("%d %d", &n, &m);
vector <int> vecx, vecy, vecz;
for (int i = 1, x, y; i <= n; i++) {
scanf("%d %d", &x, &y);
vecx.push_back(x);
vecy.push_back(y);
vecz.push_back(x + y);
ar[i] = {x, y};
}
sort(vecx.begin(), vecx.end());
vecx.resize(unique(vecx.begin(), vecx.end()) - vecx.begin());
sort(vecy.begin(), vecy.end());
vecy.resize(unique(vecy.begin(), vecy.end()) - vecy.begin());
sort(vecz.begin(), vecz.end());
vecz.resize(unique(vecz.begin(), vecz.end()) - vecz.begin());
int cnt = 0;
for (int i = 1, x, y, z; i <= n; i++) {
x = lower_bound(vecx.begin(), vecx.end(), ar[i].first) - vecx.begin();
y = lower_bound(vecy.begin(), vecy.end(), ar[i].second) - vecy.begin();
z = lower_bound(vecz.begin(), vecz.end(), ar[i].first + ar[i].second) - vecz.begin();
vec.push_back(query(cnt++, 0, x, y, z, 1, 0));
}
for (int i = 1, x, y, z; i <= m; i++) {
scanf("%d %d %d", &x, &y, &z);
x = lower_bound(vecx.begin(), vecx.end(), x) - vecx.begin();
y = lower_bound(vecy.begin(), vecy.end(), y) - vecy.begin();
z = lower_bound(vecz.begin(), vecz.end(), z) - vecz.begin();
// cout << x << ' ' << y << ' ' << z << endl;
vec.push_back(query(cnt++, 1, vec.size(), vec.size(), z, 1, i));
vec.push_back(query(cnt++, 1, x - 1, vec.size(), z, -1, i));
vec.push_back(query(cnt++, 1, vec.size(), y - 1, z, -1, i));
vec.push_back(query(cnt++, 1, x - 1, y - 1, z, 1, i));
}
sort(vec.begin(), vec.end(), [&](const auto &A, const auto &B) {
if (A.z == B.z) return A.id < B.id;
return A.z > B.z;
});
/*
for (int i = 0; i < vec.size(); i++) {
cout << vec[i].tp << ' ' << vec[i].x << ' ' << vec[i].y << ' ' << vec[i].z << endl;
}
*/
cnt = 0;
for (int i = 0; i < vec.size(); i++) {
vec[i].pos = cnt++;
}
sort(vec.begin(), vec.end(), [&](const auto &A, const auto &B) {
if (A.x == B.x) return A.pos < B.pos;
return A.x < B.x;
});
make(0, vec.size() - 1);
for (int i = 1; i <= m; i++) {
printf("%d\n", ans[i]);
}
}
Compilation message
examination.cpp: In member function 'void fen::upd(int, int)':
examination.cpp:24:20: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
pos = pos | pos + 1;
~~~~^~~
examination.cpp: In member function 'int fen::get(int)':
examination.cpp:31:15: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
r = (r & r + 1) - 1;
~~^~~
examination.cpp: At global scope:
examination.cpp:86:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
examination.cpp: In function 'int main()':
examination.cpp:131:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); i++) {
~~^~~~~~~~~~~~
examination.cpp:87:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
examination.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
examination.cpp:111:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &x, &y, &z);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
5 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
22 ms |
5312 KB |
Output is correct |
8 |
Correct |
21 ms |
5444 KB |
Output is correct |
9 |
Correct |
21 ms |
5312 KB |
Output is correct |
10 |
Correct |
20 ms |
5312 KB |
Output is correct |
11 |
Correct |
20 ms |
5308 KB |
Output is correct |
12 |
Correct |
18 ms |
5312 KB |
Output is correct |
13 |
Correct |
19 ms |
5308 KB |
Output is correct |
14 |
Correct |
19 ms |
5312 KB |
Output is correct |
15 |
Correct |
19 ms |
5312 KB |
Output is correct |
16 |
Correct |
17 ms |
5312 KB |
Output is correct |
17 |
Correct |
20 ms |
5312 KB |
Output is correct |
18 |
Correct |
15 ms |
5312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
603 ms |
34816 KB |
Output is correct |
2 |
Correct |
607 ms |
37176 KB |
Output is correct |
3 |
Correct |
620 ms |
37212 KB |
Output is correct |
4 |
Correct |
566 ms |
36476 KB |
Output is correct |
5 |
Correct |
523 ms |
36540 KB |
Output is correct |
6 |
Correct |
494 ms |
35768 KB |
Output is correct |
7 |
Correct |
606 ms |
37172 KB |
Output is correct |
8 |
Correct |
602 ms |
37308 KB |
Output is correct |
9 |
Correct |
582 ms |
37048 KB |
Output is correct |
10 |
Correct |
475 ms |
36356 KB |
Output is correct |
11 |
Correct |
551 ms |
36412 KB |
Output is correct |
12 |
Correct |
445 ms |
35272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
603 ms |
34816 KB |
Output is correct |
2 |
Correct |
607 ms |
37176 KB |
Output is correct |
3 |
Correct |
620 ms |
37212 KB |
Output is correct |
4 |
Correct |
566 ms |
36476 KB |
Output is correct |
5 |
Correct |
523 ms |
36540 KB |
Output is correct |
6 |
Correct |
494 ms |
35768 KB |
Output is correct |
7 |
Correct |
606 ms |
37172 KB |
Output is correct |
8 |
Correct |
602 ms |
37308 KB |
Output is correct |
9 |
Correct |
582 ms |
37048 KB |
Output is correct |
10 |
Correct |
475 ms |
36356 KB |
Output is correct |
11 |
Correct |
551 ms |
36412 KB |
Output is correct |
12 |
Correct |
445 ms |
35272 KB |
Output is correct |
13 |
Correct |
709 ms |
37872 KB |
Output is correct |
14 |
Correct |
762 ms |
37700 KB |
Output is correct |
15 |
Correct |
671 ms |
37308 KB |
Output is correct |
16 |
Correct |
676 ms |
36956 KB |
Output is correct |
17 |
Correct |
662 ms |
36864 KB |
Output is correct |
18 |
Correct |
533 ms |
35872 KB |
Output is correct |
19 |
Correct |
709 ms |
37688 KB |
Output is correct |
20 |
Correct |
721 ms |
37688 KB |
Output is correct |
21 |
Correct |
699 ms |
37688 KB |
Output is correct |
22 |
Correct |
484 ms |
36464 KB |
Output is correct |
23 |
Correct |
556 ms |
36412 KB |
Output is correct |
24 |
Correct |
441 ms |
35428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Correct |
5 ms |
4216 KB |
Output is correct |
5 |
Correct |
5 ms |
4216 KB |
Output is correct |
6 |
Correct |
5 ms |
4216 KB |
Output is correct |
7 |
Correct |
22 ms |
5312 KB |
Output is correct |
8 |
Correct |
21 ms |
5444 KB |
Output is correct |
9 |
Correct |
21 ms |
5312 KB |
Output is correct |
10 |
Correct |
20 ms |
5312 KB |
Output is correct |
11 |
Correct |
20 ms |
5308 KB |
Output is correct |
12 |
Correct |
18 ms |
5312 KB |
Output is correct |
13 |
Correct |
19 ms |
5308 KB |
Output is correct |
14 |
Correct |
19 ms |
5312 KB |
Output is correct |
15 |
Correct |
19 ms |
5312 KB |
Output is correct |
16 |
Correct |
17 ms |
5312 KB |
Output is correct |
17 |
Correct |
20 ms |
5312 KB |
Output is correct |
18 |
Correct |
15 ms |
5312 KB |
Output is correct |
19 |
Correct |
603 ms |
34816 KB |
Output is correct |
20 |
Correct |
607 ms |
37176 KB |
Output is correct |
21 |
Correct |
620 ms |
37212 KB |
Output is correct |
22 |
Correct |
566 ms |
36476 KB |
Output is correct |
23 |
Correct |
523 ms |
36540 KB |
Output is correct |
24 |
Correct |
494 ms |
35768 KB |
Output is correct |
25 |
Correct |
606 ms |
37172 KB |
Output is correct |
26 |
Correct |
602 ms |
37308 KB |
Output is correct |
27 |
Correct |
582 ms |
37048 KB |
Output is correct |
28 |
Correct |
475 ms |
36356 KB |
Output is correct |
29 |
Correct |
551 ms |
36412 KB |
Output is correct |
30 |
Correct |
445 ms |
35272 KB |
Output is correct |
31 |
Correct |
709 ms |
37872 KB |
Output is correct |
32 |
Correct |
762 ms |
37700 KB |
Output is correct |
33 |
Correct |
671 ms |
37308 KB |
Output is correct |
34 |
Correct |
676 ms |
36956 KB |
Output is correct |
35 |
Correct |
662 ms |
36864 KB |
Output is correct |
36 |
Correct |
533 ms |
35872 KB |
Output is correct |
37 |
Correct |
709 ms |
37688 KB |
Output is correct |
38 |
Correct |
721 ms |
37688 KB |
Output is correct |
39 |
Correct |
699 ms |
37688 KB |
Output is correct |
40 |
Correct |
484 ms |
36464 KB |
Output is correct |
41 |
Correct |
556 ms |
36412 KB |
Output is correct |
42 |
Correct |
441 ms |
35428 KB |
Output is correct |
43 |
Correct |
744 ms |
39632 KB |
Output is correct |
44 |
Correct |
771 ms |
39608 KB |
Output is correct |
45 |
Correct |
752 ms |
39612 KB |
Output is correct |
46 |
Correct |
670 ms |
38024 KB |
Output is correct |
47 |
Correct |
652 ms |
38080 KB |
Output is correct |
48 |
Correct |
507 ms |
35644 KB |
Output is correct |
49 |
Correct |
715 ms |
39460 KB |
Output is correct |
50 |
Correct |
729 ms |
39672 KB |
Output is correct |
51 |
Correct |
702 ms |
39516 KB |
Output is correct |
52 |
Correct |
625 ms |
37916 KB |
Output is correct |
53 |
Correct |
569 ms |
37180 KB |
Output is correct |