#include "bits/stdc++.h"
using namespace std;
#ifdef N_N_C
#include "debug.h"
#else
#define cebug(...) "Arya"
#endif
#define ll long long
const int N=1e6+5;
const int mod=1e9+7;
struct Point{
int x,y,z,id,v;
bool operator < (const Point &o){
if(x==o.x){
if(y==o.y) return z>o.z;
return y>o.y;
}
return x<o.x;
}
};
vector <Point> v,v1;
int n,q,ans[N],ans1[N];
vector <int> compress;
int f(int x){
return lower_bound(compress.begin(),compress.end(),x)-compress.begin()+1;
}
struct BIT{
#define gb(x) (x&(-x))
int n;
vector <int> bit;
void init(int _n){
bit.resize(_n,0);
n=_n;
}
void update(int pos, int val){
int i=pos;
for (; i<n; i+=gb(i)) bit[i]+=val;
}
int get(int pos){
int ans=0,i=pos;
for (; i; i-=gb(i)) ans+=bit[i];
return ans;
}
}fw;
void cdq(int l, int r){
if(l+1==r) return;
int mid=(l+r)>>1;
cdq(l,mid);
cdq(mid,r);
vector <Point> tmp;
vector <int> record;
int a=l,b=mid;
while(a<mid and b<r){
if(v[a].y<v[b].y) fw.update(v[a].z,v[a].v),record.push_back(a),tmp.push_back(v[a++]);
else ans[v[b].id]+=fw.get(v[b].z-1),tmp.push_back(v[b++]);
}
while(a<mid) tmp.push_back(v[a++]);
while(b<r) ans[v[b].id]+=fw.get(v[b].z-1),tmp.push_back(v[b++]);
for(auto a:record) fw.update(v[a].z,-v[a].v);
for(int i=l; i<r; i++) v[i]=tmp[i-l];
vector <int> ().swap(record);
vector <Point> ().swap(tmp);
}
void sol(){
// count pair i j : i_x<j_x, i_x<j_y, i_z<j_z
fw.init(N);
cin>>n>>q;
for(int i=1; i<=n; i++){
int x,y;
cin>>x>>y;
v.push_back({-x,-y,-x-y,0,1});
compress.push_back(-x);
compress.push_back(-y);
compress.push_back(-x-y);
}
for(int i=1; i<=q; i++){
int x,y,z;
cin>>x>>y>>z;
v.push_back({-x+1,-y+1,-z+1,i,0});
compress.push_back(-x+1);
compress.push_back(-y+1);
compress.push_back(-z+1);
}
sort(compress.begin(),compress.end());
compress.erase(unique(compress.begin(),compress.end()),compress.end());
for(auto &[x,y,z,i,v]:v){
x=f(x);
y=f(y);
z=f(z);
}
sort(v.begin(),v.end());
cdq(0,(int)v.size());
for(int i=1; i<=q; i++) cout<<ans[i]<<"\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen(".inp", "r", stdin);
// freopen(".out", "w", stdout);
int tt=1;
//cin>>tt;
while(tt--){
sol();
}
cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
3 ms |
4188 KB |
Output is correct |
3 |
Correct |
3 ms |
4188 KB |
Output is correct |
4 |
Correct |
4 ms |
4356 KB |
Output is correct |
5 |
Correct |
2 ms |
4348 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
10 ms |
4956 KB |
Output is correct |
8 |
Correct |
10 ms |
4956 KB |
Output is correct |
9 |
Correct |
11 ms |
4844 KB |
Output is correct |
10 |
Correct |
8 ms |
4976 KB |
Output is correct |
11 |
Correct |
8 ms |
4956 KB |
Output is correct |
12 |
Correct |
6 ms |
4836 KB |
Output is correct |
13 |
Correct |
9 ms |
4824 KB |
Output is correct |
14 |
Correct |
9 ms |
4840 KB |
Output is correct |
15 |
Correct |
9 ms |
4952 KB |
Output is correct |
16 |
Correct |
6 ms |
4808 KB |
Output is correct |
17 |
Correct |
9 ms |
4816 KB |
Output is correct |
18 |
Correct |
5 ms |
4832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
307 ms |
21872 KB |
Output is correct |
2 |
Correct |
340 ms |
21828 KB |
Output is correct |
3 |
Correct |
313 ms |
21892 KB |
Output is correct |
4 |
Correct |
249 ms |
21360 KB |
Output is correct |
5 |
Correct |
214 ms |
21364 KB |
Output is correct |
6 |
Correct |
170 ms |
20856 KB |
Output is correct |
7 |
Correct |
288 ms |
21872 KB |
Output is correct |
8 |
Correct |
281 ms |
21880 KB |
Output is correct |
9 |
Correct |
280 ms |
21540 KB |
Output is correct |
10 |
Correct |
182 ms |
21364 KB |
Output is correct |
11 |
Correct |
212 ms |
21364 KB |
Output is correct |
12 |
Correct |
117 ms |
20600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
307 ms |
21872 KB |
Output is correct |
2 |
Correct |
340 ms |
21828 KB |
Output is correct |
3 |
Correct |
313 ms |
21892 KB |
Output is correct |
4 |
Correct |
249 ms |
21360 KB |
Output is correct |
5 |
Correct |
214 ms |
21364 KB |
Output is correct |
6 |
Correct |
170 ms |
20856 KB |
Output is correct |
7 |
Correct |
288 ms |
21872 KB |
Output is correct |
8 |
Correct |
281 ms |
21880 KB |
Output is correct |
9 |
Correct |
280 ms |
21540 KB |
Output is correct |
10 |
Correct |
182 ms |
21364 KB |
Output is correct |
11 |
Correct |
212 ms |
21364 KB |
Output is correct |
12 |
Correct |
117 ms |
20600 KB |
Output is correct |
13 |
Correct |
331 ms |
21872 KB |
Output is correct |
14 |
Correct |
316 ms |
21876 KB |
Output is correct |
15 |
Correct |
315 ms |
21876 KB |
Output is correct |
16 |
Correct |
302 ms |
21448 KB |
Output is correct |
17 |
Correct |
228 ms |
21496 KB |
Output is correct |
18 |
Correct |
174 ms |
20852 KB |
Output is correct |
19 |
Correct |
355 ms |
21912 KB |
Output is correct |
20 |
Correct |
315 ms |
21876 KB |
Output is correct |
21 |
Correct |
334 ms |
21924 KB |
Output is correct |
22 |
Correct |
173 ms |
21364 KB |
Output is correct |
23 |
Correct |
245 ms |
20448 KB |
Output is correct |
24 |
Correct |
123 ms |
20080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
3 ms |
4188 KB |
Output is correct |
3 |
Correct |
3 ms |
4188 KB |
Output is correct |
4 |
Correct |
4 ms |
4356 KB |
Output is correct |
5 |
Correct |
2 ms |
4348 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
10 ms |
4956 KB |
Output is correct |
8 |
Correct |
10 ms |
4956 KB |
Output is correct |
9 |
Correct |
11 ms |
4844 KB |
Output is correct |
10 |
Correct |
8 ms |
4976 KB |
Output is correct |
11 |
Correct |
8 ms |
4956 KB |
Output is correct |
12 |
Correct |
6 ms |
4836 KB |
Output is correct |
13 |
Correct |
9 ms |
4824 KB |
Output is correct |
14 |
Correct |
9 ms |
4840 KB |
Output is correct |
15 |
Correct |
9 ms |
4952 KB |
Output is correct |
16 |
Correct |
6 ms |
4808 KB |
Output is correct |
17 |
Correct |
9 ms |
4816 KB |
Output is correct |
18 |
Correct |
5 ms |
4832 KB |
Output is correct |
19 |
Correct |
307 ms |
21872 KB |
Output is correct |
20 |
Correct |
340 ms |
21828 KB |
Output is correct |
21 |
Correct |
313 ms |
21892 KB |
Output is correct |
22 |
Correct |
249 ms |
21360 KB |
Output is correct |
23 |
Correct |
214 ms |
21364 KB |
Output is correct |
24 |
Correct |
170 ms |
20856 KB |
Output is correct |
25 |
Correct |
288 ms |
21872 KB |
Output is correct |
26 |
Correct |
281 ms |
21880 KB |
Output is correct |
27 |
Correct |
280 ms |
21540 KB |
Output is correct |
28 |
Correct |
182 ms |
21364 KB |
Output is correct |
29 |
Correct |
212 ms |
21364 KB |
Output is correct |
30 |
Correct |
117 ms |
20600 KB |
Output is correct |
31 |
Correct |
331 ms |
21872 KB |
Output is correct |
32 |
Correct |
316 ms |
21876 KB |
Output is correct |
33 |
Correct |
315 ms |
21876 KB |
Output is correct |
34 |
Correct |
302 ms |
21448 KB |
Output is correct |
35 |
Correct |
228 ms |
21496 KB |
Output is correct |
36 |
Correct |
174 ms |
20852 KB |
Output is correct |
37 |
Correct |
355 ms |
21912 KB |
Output is correct |
38 |
Correct |
315 ms |
21876 KB |
Output is correct |
39 |
Correct |
334 ms |
21924 KB |
Output is correct |
40 |
Correct |
173 ms |
21364 KB |
Output is correct |
41 |
Correct |
245 ms |
20448 KB |
Output is correct |
42 |
Correct |
123 ms |
20080 KB |
Output is correct |
43 |
Correct |
361 ms |
20088 KB |
Output is correct |
44 |
Correct |
351 ms |
19892 KB |
Output is correct |
45 |
Correct |
381 ms |
20084 KB |
Output is correct |
46 |
Correct |
298 ms |
19892 KB |
Output is correct |
47 |
Correct |
225 ms |
20000 KB |
Output is correct |
48 |
Correct |
167 ms |
20084 KB |
Output is correct |
49 |
Correct |
356 ms |
19884 KB |
Output is correct |
50 |
Correct |
365 ms |
20132 KB |
Output is correct |
51 |
Correct |
397 ms |
20084 KB |
Output is correct |
52 |
Correct |
208 ms |
20016 KB |
Output is correct |
53 |
Correct |
227 ms |
20108 KB |
Output is correct |