#include<bits/stdc++.h>
#define all(x) (x).begin() , (x).end()
#define pll pair<long long, long long>
#define pii pair<int , int>
#define fi first
#define se second
#define bit(i,j) ((j >> i) & 1)
using namespace std;
#define int long long
const long long inf = 1e18+1;
const int mod = 1e9+7;
const int MAXN = 3e6+100;
struct BIT {
int b[MAXN], n;
void init(int _n) {
n = _n;
for(int i = 0 ; i <= n ; ++i) b[i] = 0;
}
inline int lowbit(int x) { return x & (-x); }
void update(int x, int v) {
for(int i = x ; i <= n ; i += lowbit(i)) b[i] += v;
}
int query(int x) {
int ans = 0;
for(int i = x ; i > 0 ; i -= lowbit(i)) ans += b[i];
return ans;
}
} bit;
struct Pt{
int x , y , z , i , t;
};
Pt a[MAXN];
int ans[MAXN];
void cdq(int l , int r){
if(l == r) return ;
int mid = l + r >> 1;
cdq(l , mid) ; cdq(mid+1 , r);
int i = l, j = mid+1 ,sum = 0;
vector<int> record;
vector<Pt> tmp;
while(i <= mid && j <= r){
if(a[i].y > a[j].y) {
if(a[i].t == 0) bit.update(a[i].z , 1) , sum ++ , record.push_back(a[i].z);
tmp.push_back(a[i++]);
}
else{
ans[a[j].i] += sum - bit.query(a[j].z);
tmp.push_back(a[j++]);
}
}
while(i <= mid) tmp.push_back(a[i++]);
while(j <= r) ans[a[j].i] += sum - bit.query(a[j].z), tmp.push_back(a[j++]);
for(int i = l ; i <= r ; i++) a[i] = tmp[i-l];
for(auto i : record) bit.update(i , -1);
vector<int> ().swap(record);
vector<Pt> ().swap(tmp);
}
map<int , int> mp;
int32_t main(){
//freopen("KNIGHTGAME.inp", "r", stdin);
//freopen("KNIGHTGAME.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
int n , q; cin >> n >> q;
vector<int> comp;
for(int i = 1 ; i <= n ; i++) cin >> a[i].x >> a[i].y, a[i].i = i , a[i].z = a[i].x + a[i].y, a[i].t = 0 , comp.push_back(a[i].z);
for(int j = 1 ; j <= q ; j ++){
int x , y , z; cin>> x >> y >> z;
a[n + j] = {x-1 , y-1 , z-1 , n + j , 1};
comp.push_back(z-1);
}
sort(all(comp));
comp.erase(unique(comp.begin() , comp.end()) , comp.end());
for(int i = 0 ; i < comp.size() ;i++) mp[comp[i]] = i+1;
for(int i = 1 ; i <= n + q ; i++) a[i].z = mp[a[i].z];
sort(a+1, a+n+q+1, [&](auto a, auto b) {
return (a.x == b.x ? (a.y == b.y ? a.z < b.z : a.y < b.y) : a.x > b.x);
});
bit.init(n + q);
cdq(1 , n + q);
for(int i = 1 ; i <= q ; i++) cout << ans[i + n] << "\n";
return 0;
}
Compilation message
examination.cpp: In function 'void cdq(long long int, long long int)':
examination.cpp:39:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int mid = l + r >> 1;
| ~~^~~
examination.cpp: In function 'int32_t main()':
examination.cpp:77:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i = 0 ; i < comp.size() ;i++) mp[comp[i]] = i+1;
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
6 ms |
5400 KB |
Output is correct |
8 |
Correct |
6 ms |
5400 KB |
Output is correct |
9 |
Correct |
6 ms |
5400 KB |
Output is correct |
10 |
Correct |
6 ms |
5400 KB |
Output is correct |
11 |
Correct |
5 ms |
5400 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
5 ms |
5240 KB |
Output is correct |
14 |
Correct |
6 ms |
5224 KB |
Output is correct |
15 |
Correct |
5 ms |
5224 KB |
Output is correct |
16 |
Correct |
4 ms |
5252 KB |
Output is correct |
17 |
Correct |
6 ms |
5408 KB |
Output is correct |
18 |
Correct |
3 ms |
5084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
41128 KB |
Output is correct |
2 |
Correct |
233 ms |
41108 KB |
Output is correct |
3 |
Correct |
233 ms |
41120 KB |
Output is correct |
4 |
Correct |
210 ms |
40524 KB |
Output is correct |
5 |
Correct |
177 ms |
40608 KB |
Output is correct |
6 |
Correct |
134 ms |
36516 KB |
Output is correct |
7 |
Correct |
238 ms |
40596 KB |
Output is correct |
8 |
Correct |
223 ms |
41124 KB |
Output is correct |
9 |
Correct |
220 ms |
40412 KB |
Output is correct |
10 |
Correct |
168 ms |
39844 KB |
Output is correct |
11 |
Correct |
196 ms |
39844 KB |
Output is correct |
12 |
Correct |
112 ms |
36516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
41128 KB |
Output is correct |
2 |
Correct |
233 ms |
41108 KB |
Output is correct |
3 |
Correct |
233 ms |
41120 KB |
Output is correct |
4 |
Correct |
210 ms |
40524 KB |
Output is correct |
5 |
Correct |
177 ms |
40608 KB |
Output is correct |
6 |
Correct |
134 ms |
36516 KB |
Output is correct |
7 |
Correct |
238 ms |
40596 KB |
Output is correct |
8 |
Correct |
223 ms |
41124 KB |
Output is correct |
9 |
Correct |
220 ms |
40412 KB |
Output is correct |
10 |
Correct |
168 ms |
39844 KB |
Output is correct |
11 |
Correct |
196 ms |
39844 KB |
Output is correct |
12 |
Correct |
112 ms |
36516 KB |
Output is correct |
13 |
Correct |
287 ms |
43756 KB |
Output is correct |
14 |
Correct |
280 ms |
43664 KB |
Output is correct |
15 |
Correct |
234 ms |
41104 KB |
Output is correct |
16 |
Correct |
261 ms |
41892 KB |
Output is correct |
17 |
Correct |
229 ms |
41368 KB |
Output is correct |
18 |
Correct |
137 ms |
36048 KB |
Output is correct |
19 |
Correct |
284 ms |
44260 KB |
Output is correct |
20 |
Correct |
287 ms |
43856 KB |
Output is correct |
21 |
Correct |
280 ms |
43668 KB |
Output is correct |
22 |
Correct |
170 ms |
40604 KB |
Output is correct |
23 |
Correct |
194 ms |
39824 KB |
Output is correct |
24 |
Correct |
112 ms |
36476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
6 ms |
5400 KB |
Output is correct |
8 |
Correct |
6 ms |
5400 KB |
Output is correct |
9 |
Correct |
6 ms |
5400 KB |
Output is correct |
10 |
Correct |
6 ms |
5400 KB |
Output is correct |
11 |
Correct |
5 ms |
5400 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
5 ms |
5240 KB |
Output is correct |
14 |
Correct |
6 ms |
5224 KB |
Output is correct |
15 |
Correct |
5 ms |
5224 KB |
Output is correct |
16 |
Correct |
4 ms |
5252 KB |
Output is correct |
17 |
Correct |
6 ms |
5408 KB |
Output is correct |
18 |
Correct |
3 ms |
5084 KB |
Output is correct |
19 |
Correct |
242 ms |
41128 KB |
Output is correct |
20 |
Correct |
233 ms |
41108 KB |
Output is correct |
21 |
Correct |
233 ms |
41120 KB |
Output is correct |
22 |
Correct |
210 ms |
40524 KB |
Output is correct |
23 |
Correct |
177 ms |
40608 KB |
Output is correct |
24 |
Correct |
134 ms |
36516 KB |
Output is correct |
25 |
Correct |
238 ms |
40596 KB |
Output is correct |
26 |
Correct |
223 ms |
41124 KB |
Output is correct |
27 |
Correct |
220 ms |
40412 KB |
Output is correct |
28 |
Correct |
168 ms |
39844 KB |
Output is correct |
29 |
Correct |
196 ms |
39844 KB |
Output is correct |
30 |
Correct |
112 ms |
36516 KB |
Output is correct |
31 |
Correct |
287 ms |
43756 KB |
Output is correct |
32 |
Correct |
280 ms |
43664 KB |
Output is correct |
33 |
Correct |
234 ms |
41104 KB |
Output is correct |
34 |
Correct |
261 ms |
41892 KB |
Output is correct |
35 |
Correct |
229 ms |
41368 KB |
Output is correct |
36 |
Correct |
137 ms |
36048 KB |
Output is correct |
37 |
Correct |
284 ms |
44260 KB |
Output is correct |
38 |
Correct |
287 ms |
43856 KB |
Output is correct |
39 |
Correct |
280 ms |
43668 KB |
Output is correct |
40 |
Correct |
170 ms |
40604 KB |
Output is correct |
41 |
Correct |
194 ms |
39824 KB |
Output is correct |
42 |
Correct |
112 ms |
36476 KB |
Output is correct |
43 |
Correct |
325 ms |
49060 KB |
Output is correct |
44 |
Correct |
322 ms |
49052 KB |
Output is correct |
45 |
Correct |
313 ms |
48560 KB |
Output is correct |
46 |
Correct |
304 ms |
48556 KB |
Output is correct |
47 |
Correct |
268 ms |
49068 KB |
Output is correct |
48 |
Correct |
141 ms |
36004 KB |
Output is correct |
49 |
Correct |
312 ms |
48552 KB |
Output is correct |
50 |
Correct |
312 ms |
49228 KB |
Output is correct |
51 |
Correct |
317 ms |
49052 KB |
Output is correct |
52 |
Correct |
260 ms |
49124 KB |
Output is correct |
53 |
Correct |
204 ms |
42316 KB |
Output is correct |