/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define vvi vector<vi>
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
struct point {
int x, y, sum, idx;
point(int x, int y, int sum, int idx = 0) : x(x), y(y), sum(sum), idx(idx) {}
bool operator < (const point &o) const {
return x < o.x;
}
};
struct FenwickTree2D {
int n;
vvi data;
vvi BIT;
FenwickTree2D(int n) : n(n) {
BIT.resize(n + 1);
data.resize(n + 1);
}
void fupdate(int x, int y) {
for (; x <= n; x += x & -x)
data[x].emplace_back(y);
}
void fget(int x, int y) {
for (; x > 0; x -= x & -x)
data[x].emplace_back(y);
}
void initBIT() {
for (int i = 1; i <= n; ++i) {
sort(all(data[i]));
BIT[i].resize(isz(data[i]) + 1);
}
}
void update(int x, int yy, int val) {
for (; x <= n; x += x & -x) {
int y = lower_bound(all(data[x]), yy) - data[x].begin() + 1;
for (; y <= isz(data[x]); y += y & -y)
BIT[x][y] += val;
}
}
int get(int x, int yy) {
int res = 0;
for (; x; x -= x & -x) {
int y = lower_bound(all(data[x]), yy) - data[x].begin() + 1;
for (; y > 0; y -= y & -y)
res += BIT[x][y];
}
return res;
}
};
vi r, c;
int n, q, ans[mxN];
vector<point> a, qq;
void solve() {
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
int x, y;
cin >> x >> y;
x = -x, y = -y;
r.emplace_back(y);
c.emplace_back(x + y);
a.emplace_back(x, y, x + y);
}
for (int i = 1; i <= q; ++i) {
int x, y, z;
cin >> x >> y >> z;
x = -x, y = -y, z = -z;
r.emplace_back(y);
c.emplace_back(z);
qq.emplace_back(x, y, z, i);
}
sort(all(a)); sort(all(qq));
sort(all(r)); r.erase(unique(all(r)), r.end());
sort(all(c)); c.erase(unique(all(c)), c.end());
FenwickTree2D fenw(isz(r));
for (auto &[x, y, sum, idx] : a) {
y = lower_bound(all(r), y) - r.begin() + 1;
sum = lower_bound(all(c), sum) - c.begin() + 1;
fenw.fupdate(y, sum);
}
for (auto &[x, y, sum, idx] : qq) {
y = lower_bound(all(r), y) - r.begin() + 1;
sum = lower_bound(all(c), sum) - c.begin() + 1;
fenw.fget(y, sum);
}
fenw.initBIT();
int ptr = 0;
for (int i = 0; i < q; ++i) {
auto [x, y, sum, idx] = qq[i];
while (ptr < n && a[ptr].x <= x) {
fenw.update(a[ptr].y, a[ptr].sum, 1);
++ptr;
}
ans[idx] = fenw.get(y, sum);
}
for (int i = 1; i <= q; ++i) {
cout << ans[i] << "\n";
}
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
1544 KB |
Output is correct |
8 |
Correct |
7 ms |
1372 KB |
Output is correct |
9 |
Correct |
7 ms |
1372 KB |
Output is correct |
10 |
Correct |
4 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
1580 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
6 ms |
1500 KB |
Output is correct |
14 |
Correct |
6 ms |
1372 KB |
Output is correct |
15 |
Correct |
6 ms |
1624 KB |
Output is correct |
16 |
Correct |
6 ms |
1372 KB |
Output is correct |
17 |
Correct |
3 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
297 ms |
29604 KB |
Output is correct |
2 |
Correct |
301 ms |
29568 KB |
Output is correct |
3 |
Correct |
317 ms |
29376 KB |
Output is correct |
4 |
Correct |
91 ms |
10172 KB |
Output is correct |
5 |
Correct |
311 ms |
29644 KB |
Output is correct |
6 |
Correct |
63 ms |
10420 KB |
Output is correct |
7 |
Correct |
274 ms |
29628 KB |
Output is correct |
8 |
Correct |
276 ms |
27836 KB |
Output is correct |
9 |
Correct |
266 ms |
27840 KB |
Output is correct |
10 |
Correct |
296 ms |
29348 KB |
Output is correct |
11 |
Correct |
67 ms |
8896 KB |
Output is correct |
12 |
Correct |
42 ms |
8132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
297 ms |
29604 KB |
Output is correct |
2 |
Correct |
301 ms |
29568 KB |
Output is correct |
3 |
Correct |
317 ms |
29376 KB |
Output is correct |
4 |
Correct |
91 ms |
10172 KB |
Output is correct |
5 |
Correct |
311 ms |
29644 KB |
Output is correct |
6 |
Correct |
63 ms |
10420 KB |
Output is correct |
7 |
Correct |
274 ms |
29628 KB |
Output is correct |
8 |
Correct |
276 ms |
27836 KB |
Output is correct |
9 |
Correct |
266 ms |
27840 KB |
Output is correct |
10 |
Correct |
296 ms |
29348 KB |
Output is correct |
11 |
Correct |
67 ms |
8896 KB |
Output is correct |
12 |
Correct |
42 ms |
8132 KB |
Output is correct |
13 |
Correct |
397 ms |
29344 KB |
Output is correct |
14 |
Correct |
408 ms |
29628 KB |
Output is correct |
15 |
Correct |
280 ms |
29372 KB |
Output is correct |
16 |
Correct |
141 ms |
10172 KB |
Output is correct |
17 |
Correct |
388 ms |
29604 KB |
Output is correct |
18 |
Correct |
73 ms |
10176 KB |
Output is correct |
19 |
Correct |
394 ms |
29376 KB |
Output is correct |
20 |
Correct |
397 ms |
29348 KB |
Output is correct |
21 |
Correct |
379 ms |
27840 KB |
Output is correct |
22 |
Correct |
305 ms |
29388 KB |
Output is correct |
23 |
Correct |
68 ms |
8896 KB |
Output is correct |
24 |
Correct |
43 ms |
8124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
1544 KB |
Output is correct |
8 |
Correct |
7 ms |
1372 KB |
Output is correct |
9 |
Correct |
7 ms |
1372 KB |
Output is correct |
10 |
Correct |
4 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
1580 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
6 ms |
1500 KB |
Output is correct |
14 |
Correct |
6 ms |
1372 KB |
Output is correct |
15 |
Correct |
6 ms |
1624 KB |
Output is correct |
16 |
Correct |
6 ms |
1372 KB |
Output is correct |
17 |
Correct |
3 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
297 ms |
29604 KB |
Output is correct |
20 |
Correct |
301 ms |
29568 KB |
Output is correct |
21 |
Correct |
317 ms |
29376 KB |
Output is correct |
22 |
Correct |
91 ms |
10172 KB |
Output is correct |
23 |
Correct |
311 ms |
29644 KB |
Output is correct |
24 |
Correct |
63 ms |
10420 KB |
Output is correct |
25 |
Correct |
274 ms |
29628 KB |
Output is correct |
26 |
Correct |
276 ms |
27836 KB |
Output is correct |
27 |
Correct |
266 ms |
27840 KB |
Output is correct |
28 |
Correct |
296 ms |
29348 KB |
Output is correct |
29 |
Correct |
67 ms |
8896 KB |
Output is correct |
30 |
Correct |
42 ms |
8132 KB |
Output is correct |
31 |
Correct |
397 ms |
29344 KB |
Output is correct |
32 |
Correct |
408 ms |
29628 KB |
Output is correct |
33 |
Correct |
280 ms |
29372 KB |
Output is correct |
34 |
Correct |
141 ms |
10172 KB |
Output is correct |
35 |
Correct |
388 ms |
29604 KB |
Output is correct |
36 |
Correct |
73 ms |
10176 KB |
Output is correct |
37 |
Correct |
394 ms |
29376 KB |
Output is correct |
38 |
Correct |
397 ms |
29348 KB |
Output is correct |
39 |
Correct |
379 ms |
27840 KB |
Output is correct |
40 |
Correct |
305 ms |
29388 KB |
Output is correct |
41 |
Correct |
68 ms |
8896 KB |
Output is correct |
42 |
Correct |
43 ms |
8124 KB |
Output is correct |
43 |
Correct |
459 ms |
41148 KB |
Output is correct |
44 |
Correct |
444 ms |
41136 KB |
Output is correct |
45 |
Correct |
420 ms |
41152 KB |
Output is correct |
46 |
Correct |
144 ms |
10152 KB |
Output is correct |
47 |
Correct |
445 ms |
41312 KB |
Output is correct |
48 |
Correct |
82 ms |
10148 KB |
Output is correct |
49 |
Correct |
422 ms |
41024 KB |
Output is correct |
50 |
Correct |
460 ms |
41148 KB |
Output is correct |
51 |
Correct |
450 ms |
41148 KB |
Output is correct |
52 |
Correct |
443 ms |
40896 KB |
Output is correct |
53 |
Correct |
74 ms |
8892 KB |
Output is correct |