Editorial for Chọn Đội tuyển HSGQG TPHCM 2025 - Ảnh đẹp
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Nhận thấy rằng ta có thể quy bài toán về dạng truy vấn 3D offline cổ điển. Bài toán này có thể giải được bằng Chia để trị CDQ (https://wiki.vnoi.info/algo/basic/chen-danqi-dnc), kết hợp với các kỹ thuật thường gặp khác như rời rạc hóa (nén tọa độ), và Fenwick Tree.
Độ phức tạp: ~O((N+Q) \log^2 (N+Q))~.
#pragma GCC optimize("O3") #include <bits/stdc++.h> #define ll long long #define ii pair<ll,ll> #define fi first #define se second #define pb push_back #define eb emplace_back #define lwb lower_bound #define upb upper_bound #define ld double #define ins insert #define del erase #define ull unsigned long long using namespace std; const ll big=1e5+9; const ll sml=1e3+9; const ll inf=1e18; const ll mod=1e9+7; const ld pi=3.1415926535109793L; const ld eps=1e-12L; struct tr{ll fi,se,th; tr(ll _fi=0, ll _se=0, ll _th=0){fi=_fi; se=_se; th=_th;}}; struct qu{ll fi,se,th,fo; qu(ll _fi=0, ll _se=0, ll _th=0, ll _fo=0){fi=_fi; se=_se; th=_th; fo=_fo;}}; ll mxz(ll &t, ll val){if (t<val){t=val; return 1;} return 0;} ll mnz(ll &t, ll val){if (t>val){t=val; return 1;} return 0;} ld mxz(ld &t, ld val){if (t<val){t=val; return 1;} return 0;} ld mnz(ld &t, ld val){if (t>val){t=val; return 1;} return 0;} ll qpw(ll n, ll k, ll m=mod){ll p=1, t=n%m; while (k){if (k&1) p=p*t%m; t=t*t%m; k>>=1;} return p;} ll ran(){ ll s=0; for (ll i=0; i<2; ++i) s^=(ll)rand()<<(i*14ll); return abs(s); } ll ran(ll l, ll r){ return ran()%(r-l+1)+l; } ld ranl(ld l, ld r){ return ran(l*1e6,r*1e6)*1e-6; } ll n,m,q,k; struct custom_map { ll sz=0; vector<ii> val; ll init(ll _sz){ sz=_sz*1.6+ran(0,1000); val.resize(sz,ii(-1,0)); return 0; } ll upd(ll p){ ll i=((p+big)+p/2ll)*37ll%sz; while (val[i].fi!=-1){ if (val[i].fi==p){ ++val[i].se; return 0; } ++i; if (i==sz) i=0; } val[i].fi=p; val[i].se=1; return 0; } ll get(ll p){ ll i=((p+big)+p/2ll)*37ll%sz; while (val[i].fi!=-1){ if (val[i].fi==p){ return val[i].se; } ++i; if (i==sz) i=0; } return 0; } } fw_tri; vector<ll> comx, comy; ll szx, szy; ll add(ll x, ll y){ ++x; ++y; for (; x; x-=x&-x){ll z=y; for (; z; z-=z&-z) fw_tri.upd(x*(szy+3)+z);} return 0; } ll get(ll x, ll y){ ll s=0; ++x; ++y; for (; x<=szx; x+=x&-x){ll z=y; for (; z<=szy; z+=z&-z) s+=fw_tri.get(x*(szy+3)+z);} return s; } vector<qu> que; ll ans[big]; ll solve(){ cin>>n>>q; for (ll i=1; i<=n; ++i){ ll x,y; cin>>x>>y; que.eb(x+y,0,x,y); } for (ll i=1; i<=q; ++i){ ll x,y,z; cin>>x>>y>>z; que.eb(z,i,x,y); comx.eb(x); comy.eb(y); } sort(que.begin(),que.end(),[](const qu &x, const qu &y){ if (x.fi!=y.fi) return x.fi>y.fi; if (x.se!=y.se) return x.se<y.se; if (x.th!=y.th) return x.th<y.th; return x.fo<y.fo; }); sort(comx.begin(),comx.end()); comx.erase(unique(comx.begin(),comx.end()),comx.end()); sort(comy.begin(),comy.end()); comy.erase(unique(comy.begin(),comy.end()),comy.end()); szx=(ll)comx.size(); szy=(ll)comy.size(); fw_tri.init(n*log2(n)*log2(n)*0.3); for (const qu &t: que){ ll x=(upb(comx.begin(),comx.end(),t.th)-comx.begin()-1); ll y=(upb(comy.begin(),comy.end(),t.fo)-comy.begin()-1); if (t.se==0){ add(x,y); } else { ans[t.se]=get(x,y); } } for (ll i=1; i<=q; ++i){ cout<<ans[i]<<"\n"; } return 0; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("INPUT.TXT","r",stdin); // freopen("OUTPUT.TXT","w",stdout); srand(time(0)); // cout<<fixed<<setprecision(4); ll tst=1; // cin>>tst; for (; tst; --tst) solve(); return 0; }
Comments