#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
struct Fenwick{
#define gb(x) (x) & -(x)
int n;
vector<ll> bit;
void init(int _n){
n = _n;
bit.resize(n + 2, 0);
}
void upd(int x, ll val){
if(x == 0)
return;
for(; x <= n; x += gb(x))
bit[x] += val;
}
ll get(int l, int r){
ll res = 0;
--l;
for(; r; r -= gb(r)) res += bit[r];
for(; l; l -= gb(l)) res -= bit[l];
return res;
}
};
struct Point{
int x, y, z, id;
bool operator < (const Point &o){ return x == o.x ? (y == o.y ? (z == o.z ? id < o.id : z >= o.z) : y >= o.y) : x > o.x; }
};
const int N = 2e5 + 3;
int n, q, res[N];
vector<Point> p;
vector<int> xx;
Fenwick fen;
void cdq(int l, int r){
if(l == r) return;
int mid = (l + r) >> 1;
cdq(l, mid); cdq(mid + 1, r);
vector<Point> tmp;
vector<int> ops;
int a = l, b = mid + 1;
while(a <= mid and b <= r){
if(p[a].y >= p[b].y){
if(p[a].id == -1){
fen.upd(p[a].z, 1);
ops.push_back(p[a].z);
}
tmp.push_back(p[a++]);
}
else{
if(p[b].id >= 0){
res[p[b].id] += fen.get(p[b].z, N);
}
tmp.push_back(p[b++]);
}
}
while(a <= mid)
tmp.push_back(p[a++]);
while(b <= r){
if(p[b].id >= 0)
res[p[b].id] += fen.get(p[b].z, N);
tmp.push_back(p[b++]);
}
for(int i = l; i <= r; ++i)
p[i] = tmp[i - l];
for(int x : ops)
fen.upd(x, -1);
vector<Point>().swap(tmp);
vector<int>().swap(ops);
}
void solve(){
cin >> n >> q;
for(int i = 0; i < n; ++i){
int s, t;
cin >> s >> t;
p.push_back({s, t, s + t, -1});
xx.push_back(s + t);
}
for(int i = 0; i < q; ++i){
int a, b, c;
cin >> a >> b >> c;
p.push_back({a, b, c, i});
xx.push_back(c);
}
sort(all(xx));
xx.resize(unique(all(xx)) - xx.begin());
for(int i = 0; i < sz(p); ++i){
p[i].z = lower_bound(all(xx), p[i].z) - xx.begin() + 1;
//debug(i, p[i].z);
}
sort(all(p));
fen.init(N);
cdq(0, sz(p) - 1);
for(int i = 0; i < q; ++i)
cout << res[i] << '\n';
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(0);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
// cin >> test;
for(int i = 1; i <= test; ++i){
// cout << "Case #" << i << ": ";
solve();
}
#ifdef LOCAL
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
return 0;
}
Compilation message
examination.cpp: In function 'int32_t main()':
examination.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
2008 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
6 ms |
2396 KB |
Output is correct |
8 |
Correct |
6 ms |
2396 KB |
Output is correct |
9 |
Correct |
6 ms |
2404 KB |
Output is correct |
10 |
Correct |
6 ms |
2416 KB |
Output is correct |
11 |
Correct |
6 ms |
2392 KB |
Output is correct |
12 |
Correct |
5 ms |
2396 KB |
Output is correct |
13 |
Correct |
5 ms |
2396 KB |
Output is correct |
14 |
Correct |
6 ms |
2396 KB |
Output is correct |
15 |
Correct |
5 ms |
2412 KB |
Output is correct |
16 |
Correct |
5 ms |
2396 KB |
Output is correct |
17 |
Correct |
5 ms |
2388 KB |
Output is correct |
18 |
Correct |
4 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
16532 KB |
Output is correct |
2 |
Correct |
169 ms |
16636 KB |
Output is correct |
3 |
Correct |
168 ms |
18060 KB |
Output is correct |
4 |
Correct |
152 ms |
15628 KB |
Output is correct |
5 |
Correct |
152 ms |
15628 KB |
Output is correct |
6 |
Correct |
128 ms |
15100 KB |
Output is correct |
7 |
Correct |
155 ms |
16380 KB |
Output is correct |
8 |
Correct |
157 ms |
16892 KB |
Output is correct |
9 |
Correct |
155 ms |
17960 KB |
Output is correct |
10 |
Correct |
137 ms |
15596 KB |
Output is correct |
11 |
Correct |
139 ms |
15600 KB |
Output is correct |
12 |
Correct |
130 ms |
14848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
16532 KB |
Output is correct |
2 |
Correct |
169 ms |
16636 KB |
Output is correct |
3 |
Correct |
168 ms |
18060 KB |
Output is correct |
4 |
Correct |
152 ms |
15628 KB |
Output is correct |
5 |
Correct |
152 ms |
15628 KB |
Output is correct |
6 |
Correct |
128 ms |
15100 KB |
Output is correct |
7 |
Correct |
155 ms |
16380 KB |
Output is correct |
8 |
Correct |
157 ms |
16892 KB |
Output is correct |
9 |
Correct |
155 ms |
17960 KB |
Output is correct |
10 |
Correct |
137 ms |
15596 KB |
Output is correct |
11 |
Correct |
139 ms |
15600 KB |
Output is correct |
12 |
Correct |
130 ms |
14848 KB |
Output is correct |
13 |
Correct |
200 ms |
18684 KB |
Output is correct |
14 |
Correct |
196 ms |
16876 KB |
Output is correct |
15 |
Correct |
168 ms |
16376 KB |
Output is correct |
16 |
Correct |
181 ms |
17400 KB |
Output is correct |
17 |
Correct |
179 ms |
17324 KB |
Output is correct |
18 |
Correct |
130 ms |
15356 KB |
Output is correct |
19 |
Correct |
231 ms |
18432 KB |
Output is correct |
20 |
Correct |
191 ms |
16932 KB |
Output is correct |
21 |
Correct |
192 ms |
16888 KB |
Output is correct |
22 |
Correct |
137 ms |
15612 KB |
Output is correct |
23 |
Correct |
139 ms |
16704 KB |
Output is correct |
24 |
Correct |
153 ms |
14844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
2008 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
6 ms |
2396 KB |
Output is correct |
8 |
Correct |
6 ms |
2396 KB |
Output is correct |
9 |
Correct |
6 ms |
2404 KB |
Output is correct |
10 |
Correct |
6 ms |
2416 KB |
Output is correct |
11 |
Correct |
6 ms |
2392 KB |
Output is correct |
12 |
Correct |
5 ms |
2396 KB |
Output is correct |
13 |
Correct |
5 ms |
2396 KB |
Output is correct |
14 |
Correct |
6 ms |
2396 KB |
Output is correct |
15 |
Correct |
5 ms |
2412 KB |
Output is correct |
16 |
Correct |
5 ms |
2396 KB |
Output is correct |
17 |
Correct |
5 ms |
2388 KB |
Output is correct |
18 |
Correct |
4 ms |
2396 KB |
Output is correct |
19 |
Correct |
169 ms |
16532 KB |
Output is correct |
20 |
Correct |
169 ms |
16636 KB |
Output is correct |
21 |
Correct |
168 ms |
18060 KB |
Output is correct |
22 |
Correct |
152 ms |
15628 KB |
Output is correct |
23 |
Correct |
152 ms |
15628 KB |
Output is correct |
24 |
Correct |
128 ms |
15100 KB |
Output is correct |
25 |
Correct |
155 ms |
16380 KB |
Output is correct |
26 |
Correct |
157 ms |
16892 KB |
Output is correct |
27 |
Correct |
155 ms |
17960 KB |
Output is correct |
28 |
Correct |
137 ms |
15596 KB |
Output is correct |
29 |
Correct |
139 ms |
15600 KB |
Output is correct |
30 |
Correct |
130 ms |
14848 KB |
Output is correct |
31 |
Correct |
200 ms |
18684 KB |
Output is correct |
32 |
Correct |
196 ms |
16876 KB |
Output is correct |
33 |
Correct |
168 ms |
16376 KB |
Output is correct |
34 |
Correct |
181 ms |
17400 KB |
Output is correct |
35 |
Correct |
179 ms |
17324 KB |
Output is correct |
36 |
Correct |
130 ms |
15356 KB |
Output is correct |
37 |
Correct |
231 ms |
18432 KB |
Output is correct |
38 |
Correct |
191 ms |
16932 KB |
Output is correct |
39 |
Correct |
192 ms |
16888 KB |
Output is correct |
40 |
Correct |
137 ms |
15612 KB |
Output is correct |
41 |
Correct |
139 ms |
16704 KB |
Output is correct |
42 |
Correct |
153 ms |
14844 KB |
Output is correct |
43 |
Correct |
204 ms |
18932 KB |
Output is correct |
44 |
Correct |
205 ms |
19448 KB |
Output is correct |
45 |
Correct |
205 ms |
20220 KB |
Output is correct |
46 |
Correct |
189 ms |
17148 KB |
Output is correct |
47 |
Correct |
189 ms |
18168 KB |
Output is correct |
48 |
Correct |
132 ms |
15100 KB |
Output is correct |
49 |
Correct |
193 ms |
18684 KB |
Output is correct |
50 |
Correct |
198 ms |
19452 KB |
Output is correct |
51 |
Correct |
190 ms |
19196 KB |
Output is correct |
52 |
Correct |
175 ms |
18236 KB |
Output is correct |
53 |
Correct |
138 ms |
17144 KB |
Output is correct |