#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define RREP(i, n) for (int i=(n)-1; i>=0; i--)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define SQ(x) (x)*(x)
#define f first
#define s second
#define pii pair<int, int>
#define endl '\n'
#define pb push_back
const ll mod = 1e9+7;
const ll maxn = 6e5+5;
const ll inf = (1ll<<60);
ll pw(ll x, ll p){
ll ret=1;
while(p>0){
if (p&1){
ret*=x;
ret%=mod;
}
x*=x;
x%=mod;
p>>=1;
}
return ret;
}
ll inv(ll x){
return pw(x, mod-2);
}
vector<int> bit(maxn);
int query(int a){
a=maxn-a-3;
int ret=0;
while(a>0){
ret+=bit[a];
a-=(a&-a);
}
return ret;
}
void modify(int a, int val){
a=maxn-a-3;
while(a<maxn){
bit[a]+=val;
a+=(a&-a);
}
}
struct point{
bool typ; // 0 is point, 1 is query
int x, y, w;
int id;
};
vector<point> vc;
vector<int> sol(200005);
int n, q;
vector<point> tmp;
bool cmp(point A, point B){
if (A.w==B.w) return A.y<B.y;
return A.w<B.w;
}
bool cmp2(point A, point B){
if (A.x==B.x&&A.y==B.y) return A.typ>B.typ;
else if (A.x==B.x) return A.y<B.y;
return A.x<B.x;
}
void run(int l, int r){
if (l>=r) return;
if (vc[l].w==vc[r].w){
tmp.clear();
FOR(i, l, r+1){
tmp.pb(vc[i]);
}
sort(ALL(tmp), cmp2);
RREP(i, r-l+1){
if (tmp[i].typ) {
sol[tmp[i].id]+=query(tmp[i].y);
}
else {
modify(tmp[i].y, 1);
}
}
RREP(i, r-l+1){
if (tmp[i].typ==0){
modify(tmp[i].y, -1);
}
}
return;
}
int mid = (vc[l].w+vc[r].w)>>1, pos=l-1;
FOR(i, l, r+1){
if (vc[i].w<=mid) pos=i;
}
run(l, pos);
run(pos+1, r);
tmp.clear();
FOR(i, l, r+1){
tmp.pb(vc[i]);
}
sort(ALL(tmp), cmp2);
RREP(i, r-l+1){
if (tmp[i].typ==0&&tmp[i].w>mid){
modify(tmp[i].y, 1);
}
if (tmp[i].typ==1&&tmp[i].w<=mid){
sol[tmp[i].id]+=query(tmp[i].y);
}
}
RREP(i, r-l+1){
if (tmp[i].typ==0&&tmp[i].w>mid){
modify(tmp[i].y, -1);
}
}
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n>>q;
vector<int> pos;
REP(i, n){
int x, y; cin>>x>>y;
vc.pb({0, x, y, x+y, i});
pos.pb(x);
pos.pb(y);
pos.pb(x+y);
}
REP(i, q){
int x, y, c; cin>>x>>y>>c;
vc.pb({1, x, y, c, i+n});
pos.pb(x);
pos.pb(y);
pos.pb(c);
}
sort(ALL(pos));
REP(i, n){
vc[i].x=lower_bound(ALL(pos), vc[i].x)-pos.begin()+1;
vc[i].y=lower_bound(ALL(pos), vc[i].y)-pos.begin()+1;
vc[i].w=lower_bound(ALL(pos), vc[i].w)-pos.begin()+1;
}
REP(i, q){
vc[i+n].x=lower_bound(ALL(pos), vc[i+n].x)-pos.begin()+1;
vc[i+n].y=lower_bound(ALL(pos), vc[i+n].y)-pos.begin()+1;
vc[i+n].w=lower_bound(ALL(pos), vc[i+n].w)-pos.begin()+1;
}
sort(ALL(vc), cmp);
run(0, n+q-1);
FOR(i, n, n+q){
cout<<sol[i]<<endl;
}
}
/*
3 3
0 1
0 2
0 6
0 1 1
0 3 5
0 2 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
16 ms |
7472 KB |
Output is correct |
8 |
Correct |
16 ms |
7368 KB |
Output is correct |
9 |
Correct |
16 ms |
7476 KB |
Output is correct |
10 |
Correct |
14 ms |
7384 KB |
Output is correct |
11 |
Correct |
15 ms |
7344 KB |
Output is correct |
12 |
Correct |
10 ms |
7472 KB |
Output is correct |
13 |
Correct |
14 ms |
7532 KB |
Output is correct |
14 |
Correct |
13 ms |
7532 KB |
Output is correct |
15 |
Correct |
13 ms |
7488 KB |
Output is correct |
16 |
Correct |
11 ms |
7468 KB |
Output is correct |
17 |
Correct |
14 ms |
7472 KB |
Output is correct |
18 |
Correct |
7 ms |
7472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
521 ms |
34436 KB |
Output is correct |
2 |
Correct |
524 ms |
34476 KB |
Output is correct |
3 |
Correct |
532 ms |
36956 KB |
Output is correct |
4 |
Correct |
384 ms |
36192 KB |
Output is correct |
5 |
Correct |
491 ms |
36200 KB |
Output is correct |
6 |
Correct |
249 ms |
35624 KB |
Output is correct |
7 |
Correct |
528 ms |
36916 KB |
Output is correct |
8 |
Correct |
524 ms |
36904 KB |
Output is correct |
9 |
Correct |
527 ms |
36776 KB |
Output is correct |
10 |
Correct |
362 ms |
36256 KB |
Output is correct |
11 |
Correct |
327 ms |
36188 KB |
Output is correct |
12 |
Correct |
117 ms |
35440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
521 ms |
34436 KB |
Output is correct |
2 |
Correct |
524 ms |
34476 KB |
Output is correct |
3 |
Correct |
532 ms |
36956 KB |
Output is correct |
4 |
Correct |
384 ms |
36192 KB |
Output is correct |
5 |
Correct |
491 ms |
36200 KB |
Output is correct |
6 |
Correct |
249 ms |
35624 KB |
Output is correct |
7 |
Correct |
528 ms |
36916 KB |
Output is correct |
8 |
Correct |
524 ms |
36904 KB |
Output is correct |
9 |
Correct |
527 ms |
36776 KB |
Output is correct |
10 |
Correct |
362 ms |
36256 KB |
Output is correct |
11 |
Correct |
327 ms |
36188 KB |
Output is correct |
12 |
Correct |
117 ms |
35440 KB |
Output is correct |
13 |
Correct |
708 ms |
37380 KB |
Output is correct |
14 |
Correct |
709 ms |
37488 KB |
Output is correct |
15 |
Correct |
537 ms |
36984 KB |
Output is correct |
16 |
Correct |
612 ms |
36640 KB |
Output is correct |
17 |
Correct |
689 ms |
36536 KB |
Output is correct |
18 |
Correct |
267 ms |
35460 KB |
Output is correct |
19 |
Correct |
712 ms |
37364 KB |
Output is correct |
20 |
Correct |
716 ms |
37496 KB |
Output is correct |
21 |
Correct |
721 ms |
37364 KB |
Output is correct |
22 |
Correct |
371 ms |
36244 KB |
Output is correct |
23 |
Correct |
322 ms |
36132 KB |
Output is correct |
24 |
Correct |
118 ms |
35452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
16 ms |
7472 KB |
Output is correct |
8 |
Correct |
16 ms |
7368 KB |
Output is correct |
9 |
Correct |
16 ms |
7476 KB |
Output is correct |
10 |
Correct |
14 ms |
7384 KB |
Output is correct |
11 |
Correct |
15 ms |
7344 KB |
Output is correct |
12 |
Correct |
10 ms |
7472 KB |
Output is correct |
13 |
Correct |
14 ms |
7532 KB |
Output is correct |
14 |
Correct |
13 ms |
7532 KB |
Output is correct |
15 |
Correct |
13 ms |
7488 KB |
Output is correct |
16 |
Correct |
11 ms |
7468 KB |
Output is correct |
17 |
Correct |
14 ms |
7472 KB |
Output is correct |
18 |
Correct |
7 ms |
7472 KB |
Output is correct |
19 |
Correct |
521 ms |
34436 KB |
Output is correct |
20 |
Correct |
524 ms |
34476 KB |
Output is correct |
21 |
Correct |
532 ms |
36956 KB |
Output is correct |
22 |
Correct |
384 ms |
36192 KB |
Output is correct |
23 |
Correct |
491 ms |
36200 KB |
Output is correct |
24 |
Correct |
249 ms |
35624 KB |
Output is correct |
25 |
Correct |
528 ms |
36916 KB |
Output is correct |
26 |
Correct |
524 ms |
36904 KB |
Output is correct |
27 |
Correct |
527 ms |
36776 KB |
Output is correct |
28 |
Correct |
362 ms |
36256 KB |
Output is correct |
29 |
Correct |
327 ms |
36188 KB |
Output is correct |
30 |
Correct |
117 ms |
35440 KB |
Output is correct |
31 |
Correct |
708 ms |
37380 KB |
Output is correct |
32 |
Correct |
709 ms |
37488 KB |
Output is correct |
33 |
Correct |
537 ms |
36984 KB |
Output is correct |
34 |
Correct |
612 ms |
36640 KB |
Output is correct |
35 |
Correct |
689 ms |
36536 KB |
Output is correct |
36 |
Correct |
267 ms |
35460 KB |
Output is correct |
37 |
Correct |
712 ms |
37364 KB |
Output is correct |
38 |
Correct |
716 ms |
37496 KB |
Output is correct |
39 |
Correct |
721 ms |
37364 KB |
Output is correct |
40 |
Correct |
371 ms |
36244 KB |
Output is correct |
41 |
Correct |
322 ms |
36132 KB |
Output is correct |
42 |
Correct |
118 ms |
35452 KB |
Output is correct |
43 |
Correct |
726 ms |
39312 KB |
Output is correct |
44 |
Correct |
719 ms |
39336 KB |
Output is correct |
45 |
Correct |
700 ms |
39304 KB |
Output is correct |
46 |
Correct |
605 ms |
37856 KB |
Output is correct |
47 |
Correct |
678 ms |
37704 KB |
Output is correct |
48 |
Correct |
303 ms |
35544 KB |
Output is correct |
49 |
Correct |
697 ms |
39312 KB |
Output is correct |
50 |
Correct |
728 ms |
39384 KB |
Output is correct |
51 |
Correct |
699 ms |
39080 KB |
Output is correct |
52 |
Correct |
611 ms |
37800 KB |
Output is correct |
53 |
Correct |
307 ms |
37104 KB |
Output is correct |