Submission #519499

#TimeUsernameProblemLanguageResultExecution timeMemory
519499rrrr10000Dragon 2 (JOI17_dragon2)C++14
100 / 100
631 ms9664 KiB
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define pb emplace_back #define lb(v,k) (lower_bound(all(v),(k))-v.begin()) #define ub(v,k) (upper_bound(all(v),(k))-v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define dame(a) {out(a);return 0;} #define decimal cout<<fixed<<setprecision(15); #define all(a) a.begin(),a.end() #define rsort(a) {sort(all(a));reverse(all(a));} #define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());} #define popcnt __builtin_popcountll typedef long long ll; typedef pair<ll,ll> P; typedef tuple<ll,ll,ll> PP; typedef tuple<ll,ll,ll,ll> PPP; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; using vvvvi=vector<vvvi>; using vp=vector<P>; using vvp=vector<vp>; using vb=vector<bool>; using vvb=vector<vb>; const ll inf=4001001001001001001; const ll INF=1001001001; const ll mod=1000000007; const double eps=1e-10; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void yesno(T b){if(b)out("yes");else out("no");} template<class T> void YesNo(T b){if(b)out("Yes");else out("No");} template<class T> void YESNO(T b){if(b)out("YES");else out("NO");} template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';} void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} inline ll readll() { ll x = 0; int neg=1; char ch = getchar_unlocked(); while (ch < '0' || ch > '9'){ if(ch=='-')neg=-1; ch = getchar_unlocked(); } while (ch >= '0' && ch <= '9'){ x = (x << 3) + (x << 1) + ch - '0'; ch = getchar_unlocked(); } return x*neg; } struct Point{ ll x,y; Point(){} Point(ll a,ll b):x(a),y(b){} bool ccw(Point a,Point b){ ll dx1=x-a.x,dy1=y-a.y; ll dx2=b.x-a.x,dy2=b.y-a.y; return dx1*dy2-dx2*dy1>0; } }; class BIT{ vi bit;ll n; ll sum(ll i){ ll res=0; for(;i;i-=i&-i)res+=bit[i]; return res; } public: BIT(ll n_):n(n_){bit=vi(n+1);} void add(ll i,ll x){ for(i++;i<=n;i+=i&-i)bit[i]+=x; } ll get(ll a,ll b){ if(b<=a)return 0ll; return sum(b)-sum(a); } ll solve(ll x){ //sum([0,r])>=x となる最小のr if(x<=0)return -1; ll l=0,len=1;while(len<n)len<<=1; for(;len;len>>=1)if(l+len<n&&bit[l+len]<x){ x-=bit[l+len];l+=len; } return l; } }; const ll B=300; int main(){ cout<<fixed<<setprecision(20); ll n,m;cin>>m>>n; vector<vector<Point>> v(n); rep(i,m){ ll a,b,c;cin>>a>>b>>c;c--; v[c].pb(Point(a,b)); } Point a,b; cin>>a.x>>a.y>>b.x>>b.y; auto cmpy=[&](Point s,Point t){ Point x(b.x-a.x,b.y-a.y),y(t.x-s.x,t.y-s.y); if(x.x*y.y==x.y*y.x){ if(a.y==t.y)return a.x<t.x; return a.y<t.y; } return x.x*y.y>x.y*y.x; }; ll q;cin>>q; rep(i,n)sort(all(v[i]),cmpy); vvp L(n),S(n); rep(i,q){ ll a,b;cin>>a>>b;a--;b--; if(v[a].size()==0||v[b].size()==0)continue; if(v[a].size()<=B||v[b].size()>B)S[a].pb(b,i); else L[b].pb(a,i); } vi ans(q); ll cnt_id=0; vvi id(n); rep(i,n)rep(j,v[i].size())id[i].pb(cnt_id++); vector<pair<Point,P>> srt; rep(i,n)rep(j,v[i].size())srt.pb(v[i][j],P(i,j)); // rep(i,n)rep(j,v[i].size())out(v[i][j].Angle(a,b)); { auto comp=[&](pair<Point,P> s,pair<Point,P> t){ Point x=s.fi,y=t.fi; if(x.ccw(a,b)){ x.x=a.x*2-x.x; x.y=a.y*2-x.y; } if(y.ccw(a,b)){ y.x=a.x*2-y.x; y.y=a.y*2-y.y; } return !y.ccw(a,x); }; sort(all(srt),comp); BIT bit(m); rep(i,n)rep(j,v[i].size())if(!v[i][j].ccw(a,b))bit.add(id[i][j],1); for(auto x:srt){ auto p=x.fi; // cout<<p.x<<' '<<p.y<<endl; ll i=x.se.fi,j=x.se.se; if(!p.ccw(a,b))bit.add(id[i][j],-1); for(auto query:S[i]){ ll k=query.fi; if(p.ccw(a,b)){ ll l=lower_bound(all(v[k]),p,cmpy)-v[k].begin(); if(l<v[k].size())l=id[k][l]; else l=id[k].back()+1; ans[query.se]-=bit.get(l,id[k].back()+1); } else{ ll r=lower_bound(all(v[k]),p,cmpy)-v[k].begin(); if(r<v[k].size())r=id[k][r]; else r=id[k].back()+1; ans[query.se]-=bit.get(id[k][0],r); } } for(auto query:L[i]){ ll k=query.fi; if(p.ccw(a,b)){ ll l=lower_bound(all(v[k]),a,cmpy)-v[k].begin(); if(l<v[k].size())l=id[k][l]; else l=id[k].back()+1; ans[query.se]-=bit.get(l,id[k].back()+1); ll r=lower_bound(all(v[k]),p,cmpy)-v[k].begin(); if(r<v[k].size())r=id[k][r]; else r=id[k].back()+1; ans[query.se]+=bit.get(id[k][0],r); } else{ ll l=lower_bound(all(v[k]),p,cmpy)-v[k].begin(); if(l<v[k].size())l=id[k][l]; else l=id[k].back()+1; ans[query.se]+=bit.get(l,id[k].back()+1); ll r=lower_bound(all(v[k]),a,cmpy)-v[k].begin(); if(r<v[k].size())r=id[k][r]; else r=id[k].back()+1; ans[query.se]-=bit.get(id[k][0],r); } } if(p.ccw(a,b))bit.add(id[i][j],1); } } { auto comp=[&](pair<Point,P> s,pair<Point,P> t){ Point x=s.fi,y=t.fi; if(x.ccw(a,b)){ x.x=b.x*2-x.x; x.y=b.y*2-x.y; } if(y.ccw(a,b)){ y.x=b.x*2-y.x; y.y=b.y*2-y.y; } return !y.ccw(b,x); }; sort(all(srt),comp); BIT bit(m); rep(i,n)rep(j,v[i].size())if(!v[i][j].ccw(a,b))bit.add(id[i][j],1); for(auto x:srt){ Point p=x.fi; // cout<<p.x<<' '<<p.y<<endl; ll i=x.se.fi,j=x.se.se; if(!p.ccw(a,b))bit.add(id[i][j],-1); for(auto query:S[i]){ ll k=query.fi; if(p.ccw(a,b)){ ll l=lower_bound(all(v[k]),p,cmpy)-v[k].begin(); if(l<v[k].size())l=id[k][l]; else l=id[k].back()+1; ans[query.se]+=bit.get(l,id[k].back()+1); } else{ ll r=lower_bound(all(v[k]),p,cmpy)-v[k].begin(); if(r<v[k].size())r=id[k][r]; else r=id[k].back()+1; ans[query.se]+=bit.get(id[k][0],r); } } for(auto query:L[i]){ ll k=query.fi; if(p.ccw(a,b)){ ll l=lower_bound(all(v[k]),b,cmpy)-v[k].begin(); if(l<v[k].size())l=id[k][l]; else l=id[k].back()+1; ans[query.se]+=bit.get(l,id[k].back()+1); ll r=lower_bound(all(v[k]),p,cmpy)-v[k].begin(); if(r<v[k].size())r=id[k][r]; else r=id[k].back()+1; ans[query.se]-=bit.get(id[k][0],r); } else{ ll l=lower_bound(all(v[k]),p,cmpy)-v[k].begin(); if(l<v[k].size())l=id[k][l]; else l=id[k].back()+1; ans[query.se]-=bit.get(l,id[k].back()+1); ll r=lower_bound(all(v[k]),b,cmpy)-v[k].begin(); if(r<v[k].size())r=id[k][r]; else r=id[k].back()+1; ans[query.se]+=bit.get(id[k][0],r); } } if(p.ccw(a,b))bit.add(id[i][j],1); // outv(ans); } } for(ll x:ans)out(x); }

Compilation message (stderr)

dragon2.cpp: In function 'int main()':
dragon2.cpp:160:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |                     if(l<v[k].size())l=id[k][l];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:166:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |                     if(r<v[k].size())r=id[k][r];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:175:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  175 |                     if(l<v[k].size())l=id[k][l];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:179:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  179 |                     if(r<v[k].size())r=id[k][r];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:185:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |                     if(l<v[k].size())l=id[k][l];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:189:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  189 |                     if(r<v[k].size())r=id[k][r];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:222:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  222 |                     if(l<v[k].size())l=id[k][l];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:228:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  228 |                     if(r<v[k].size())r=id[k][r];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:237:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  237 |                     if(l<v[k].size())l=id[k][l];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:241:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  241 |                     if(r<v[k].size())r=id[k][r];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:247:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  247 |                     if(l<v[k].size())l=id[k][l];
      |                        ~^~~~~~~~~~~~
dragon2.cpp:251:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  251 |                     if(r<v[k].size())r=id[k][r];
      |                        ~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...