// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Nhan qua khong no chung ta thu gi //
// Vay nen dung oan han //
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// //
// _oo0oo_ //
// o8888888o //
// 88" . "88 //
// (| -_- |) //
// 0\ = /0 //
// ___/`---'\___ //
// .' \\| |// '. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' |_/ | //
// \ .-\__ '-' ___/-. / //
// ___'. .' /--.--\ `. .'___ //
// ."" '< `.___\_<|>_/___.' >' "". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `_. \_ __\ /__ _/ .-` / / //
// =====`-.____`.___ \_____/___.-`___.-'===== //
// `=---=' //
// //
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Duc Phat noi day phu ho Code con chay khong Bug //
// Nam mo a di da phat //
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
#include<bits/stdc++.h>
#ifdef LOCAL
#include "D:\debug.h"
#else
#define cebug(...) "Orz_chUoNgnn"
#endif
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ii pair<int, int>
#define vll vector<ll>
#define vii vector<ii>
#define cd complex<double>
const ll mod = 1e9 + 7;
const ll INF = 1e18L + 5;
const double PI = acos(-1);
const int block = 320;
const int N = 1e6;
int tc, tt = 1;
struct student {
ll a, b, c;
ll in, q;
} a[N + 5];
#define vi vector<student>
int n, q;
vector<int> f;
map<int, int> pos;
ll ans[N + 5];
struct fenwick_tree {
ll ft[N + 5];
void update(int pos, int val) {
int idx = pos;
while(idx <= N) {
ft[idx] += val;
idx += (idx & (-idx));
}
}
ll get(int pos) {
int idx = pos;
ll ans = 0;
while(idx > 0) {
ans += ft[idx];
idx -= (idx & (-idx));
}
return ans;
}
} ft;
vi cdq(int l, int r) {
vi f;
vector<int> record;
if(l == r) {
f.pb(a[l]);
return f;
}
int mid = (l + r)/2;
vi a = cdq(l, mid);
vi b = cdq(mid + 1, r);
int i = 0, j = 0, cnt = 0;
while(i < (int)a.size() && j < (int)b.size()) {
if(a[i].b > b[j].b) {
f.pb(a[i]);
if(a[i].in) {
cnt++;
ft.update(pos[a[i].c], 1);
record.pb(pos[a[i].c]);
}
i++;
}
else {
if(!b[j].in)
ans[b[j].q] += cnt - ft.get(pos[b[j].c]);
f.pb(b[j]);
j++;
}
}
while(i < (int)a.size()) {f.pb(a[i]); i++;}
while(j < (int)b.size()) {
if(!b[j].in)
ans[b[j].q] += cnt - ft.get(pos[b[j].c]);
f.pb(b[j]);
j++;
}
for(auto x: record) ft.update(x, -1);
return f;
}
void solve() {
cin>>n>>q;
for(int i=1; i<=n + q; i++) {
a[i].a = a[i].b = a[i].c = a[i].in = 0;
}
for(int i=1; i<=n; i++) {
cin>>a[i].a>>a[i].b;
a[i].c = a[i].a + a[i].b;
a[i].in = 1;
}
for(int i=n + 1; i<=n + q; i++) {
cin>>a[i].a>>a[i].b>>a[i].c;
a[i].a--;
a[i].b--;
a[i].c--;
a[i].q = i - n;
}
n += q;
for(int i=1; i<=n; i++) {
f.pb(a[i].a);
f.pb(a[i].b);
f.pb(a[i].c);
}
sort(f.begin(), f.end());
f.erase(unique(f.begin(), f.end()), f.end());
for(int i=0; i<(int)f.size(); i++) pos[f[i]] = i + 1;
sort(a + 1, a + 1 + n, [](student x, student y){
if(x.a != y.a) return x.a > y.a;
if(x.b != y.b) return x.b < y.b;
return x.c < y.c;
});
cdq(1, n);
for(int i=1; i<=q; i++)
cout<<ans[i]<<'\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen(".INP", "r", stdin);
// freopen(".OUT", "w", stdout);
for(int tc=1; tc<=tt; tc++) solve();
cerr<<"\nTime elapsed: "<<1000.0*clock()/CLOCKS_PER_SEC<<" ms.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
14 ms |
10340 KB |
Output is correct |
8 |
Correct |
14 ms |
10436 KB |
Output is correct |
9 |
Correct |
14 ms |
10344 KB |
Output is correct |
10 |
Correct |
12 ms |
9964 KB |
Output is correct |
11 |
Correct |
13 ms |
9984 KB |
Output is correct |
12 |
Correct |
5 ms |
9464 KB |
Output is correct |
13 |
Correct |
11 ms |
10180 KB |
Output is correct |
14 |
Correct |
12 ms |
10252 KB |
Output is correct |
15 |
Correct |
11 ms |
10272 KB |
Output is correct |
16 |
Correct |
6 ms |
9724 KB |
Output is correct |
17 |
Correct |
9 ms |
9816 KB |
Output is correct |
18 |
Correct |
4 ms |
9464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
468 ms |
52128 KB |
Output is correct |
2 |
Correct |
453 ms |
52284 KB |
Output is correct |
3 |
Correct |
458 ms |
52992 KB |
Output is correct |
4 |
Correct |
323 ms |
54084 KB |
Output is correct |
5 |
Correct |
217 ms |
50208 KB |
Output is correct |
6 |
Correct |
138 ms |
46764 KB |
Output is correct |
7 |
Correct |
465 ms |
54984 KB |
Output is correct |
8 |
Correct |
468 ms |
51240 KB |
Output is correct |
9 |
Correct |
450 ms |
54092 KB |
Output is correct |
10 |
Correct |
175 ms |
46308 KB |
Output is correct |
11 |
Correct |
273 ms |
46200 KB |
Output is correct |
12 |
Correct |
109 ms |
44152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
468 ms |
52128 KB |
Output is correct |
2 |
Correct |
453 ms |
52284 KB |
Output is correct |
3 |
Correct |
458 ms |
52992 KB |
Output is correct |
4 |
Correct |
323 ms |
54084 KB |
Output is correct |
5 |
Correct |
217 ms |
50208 KB |
Output is correct |
6 |
Correct |
138 ms |
46764 KB |
Output is correct |
7 |
Correct |
465 ms |
54984 KB |
Output is correct |
8 |
Correct |
468 ms |
51240 KB |
Output is correct |
9 |
Correct |
450 ms |
54092 KB |
Output is correct |
10 |
Correct |
175 ms |
46308 KB |
Output is correct |
11 |
Correct |
273 ms |
46200 KB |
Output is correct |
12 |
Correct |
109 ms |
44152 KB |
Output is correct |
13 |
Correct |
694 ms |
53348 KB |
Output is correct |
14 |
Correct |
671 ms |
53292 KB |
Output is correct |
15 |
Correct |
529 ms |
53536 KB |
Output is correct |
16 |
Correct |
542 ms |
51984 KB |
Output is correct |
17 |
Correct |
430 ms |
53864 KB |
Output is correct |
18 |
Correct |
150 ms |
47656 KB |
Output is correct |
19 |
Correct |
761 ms |
54784 KB |
Output is correct |
20 |
Correct |
696 ms |
54840 KB |
Output is correct |
21 |
Correct |
717 ms |
54692 KB |
Output is correct |
22 |
Correct |
190 ms |
43912 KB |
Output is correct |
23 |
Correct |
287 ms |
46960 KB |
Output is correct |
24 |
Correct |
116 ms |
41632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
14 ms |
10340 KB |
Output is correct |
8 |
Correct |
14 ms |
10436 KB |
Output is correct |
9 |
Correct |
14 ms |
10344 KB |
Output is correct |
10 |
Correct |
12 ms |
9964 KB |
Output is correct |
11 |
Correct |
13 ms |
9984 KB |
Output is correct |
12 |
Correct |
5 ms |
9464 KB |
Output is correct |
13 |
Correct |
11 ms |
10180 KB |
Output is correct |
14 |
Correct |
12 ms |
10252 KB |
Output is correct |
15 |
Correct |
11 ms |
10272 KB |
Output is correct |
16 |
Correct |
6 ms |
9724 KB |
Output is correct |
17 |
Correct |
9 ms |
9816 KB |
Output is correct |
18 |
Correct |
4 ms |
9464 KB |
Output is correct |
19 |
Correct |
468 ms |
52128 KB |
Output is correct |
20 |
Correct |
453 ms |
52284 KB |
Output is correct |
21 |
Correct |
458 ms |
52992 KB |
Output is correct |
22 |
Correct |
323 ms |
54084 KB |
Output is correct |
23 |
Correct |
217 ms |
50208 KB |
Output is correct |
24 |
Correct |
138 ms |
46764 KB |
Output is correct |
25 |
Correct |
465 ms |
54984 KB |
Output is correct |
26 |
Correct |
468 ms |
51240 KB |
Output is correct |
27 |
Correct |
450 ms |
54092 KB |
Output is correct |
28 |
Correct |
175 ms |
46308 KB |
Output is correct |
29 |
Correct |
273 ms |
46200 KB |
Output is correct |
30 |
Correct |
109 ms |
44152 KB |
Output is correct |
31 |
Correct |
694 ms |
53348 KB |
Output is correct |
32 |
Correct |
671 ms |
53292 KB |
Output is correct |
33 |
Correct |
529 ms |
53536 KB |
Output is correct |
34 |
Correct |
542 ms |
51984 KB |
Output is correct |
35 |
Correct |
430 ms |
53864 KB |
Output is correct |
36 |
Correct |
150 ms |
47656 KB |
Output is correct |
37 |
Correct |
761 ms |
54784 KB |
Output is correct |
38 |
Correct |
696 ms |
54840 KB |
Output is correct |
39 |
Correct |
717 ms |
54692 KB |
Output is correct |
40 |
Correct |
190 ms |
43912 KB |
Output is correct |
41 |
Correct |
287 ms |
46960 KB |
Output is correct |
42 |
Correct |
116 ms |
41632 KB |
Output is correct |
43 |
Correct |
1041 ms |
75444 KB |
Output is correct |
44 |
Correct |
1055 ms |
76652 KB |
Output is correct |
45 |
Correct |
981 ms |
77980 KB |
Output is correct |
46 |
Correct |
731 ms |
65288 KB |
Output is correct |
47 |
Correct |
549 ms |
67756 KB |
Output is correct |
48 |
Correct |
149 ms |
46512 KB |
Output is correct |
49 |
Correct |
920 ms |
76516 KB |
Output is correct |
50 |
Correct |
913 ms |
76348 KB |
Output is correct |
51 |
Correct |
896 ms |
76352 KB |
Output is correct |
52 |
Correct |
451 ms |
53424 KB |
Output is correct |
53 |
Correct |
347 ms |
51636 KB |
Output is correct |