Submission #143016

#TimeUsernameProblemLanguageResultExecution timeMemory
143016kig9981도로 건설 사업 (JOI13_construction)C++17
100 / 100
1566 ms104856 KiB
#include <bits/stdc++.h> #ifdef NON_SUBMIT #define TEST(n) (n) #define tout cerr #else #define TEST(n) ((void)0) #define tout cin #endif using namespace std; struct lichao { int l, r; long long A, B; lichao() : l(0), r(0), A(0), B(0x7fffffffffffffffLL) {} }; const int MAX=1e9; vector<lichao> node(2); int tree[600001], color[200000]; long long B[200001], ans[500000]; vector<int> x, y; vector<tuple<int,int,int>> Edge, Q; vector<pair<int,int>> P; vector<tuple<int,int,int,int>> S; vector<vector<pair<int,int>>> L, R, E; int get_sign(long long a) { return a<0 ? -1:a>0; } void add_line(long long A, long long B, int p=1, int s=1, int e=MAX) { int m=(s+e)>>1; long long &pA=node[p].A, &pB=node[p].B; long long ys=A*s+B, ym=A*m+B, ye=A*e+B; long long pys=pA*s+pB, pym=pA*m+pB, pye=pA*e+pB; if(pym>ym) { swap(A,pA); swap(B,pB); swap(ys,pys); swap(ym,pym); swap(ye,pye); } if(ys>=pys && ye>=pye) return; if(get_sign(ys-pys)*get_sign(ym-pym)<0 || ym==pym && ys<pys) { if(node[p].l==0) node[p].l=node.size(), node.push_back(lichao()); add_line(A,B,node[p].l,s,m); } else { if(node[p].r==0) node[p].r=node.size(), node.push_back(lichao()); add_line(A,B,node[p].r,m+1,e); } } long long query(int x, int p=1, int s=1, int e=MAX) { if(p==0) return 0x7fffffffffffffffLL; int m=(s+e)>>1; return min(node[p].A*x+node[p].B,x<=m ? query(x,node[p].l,s,m):query(x,node[p].r,m+1,e)); } int find_(int a) { return color[a]=a==color[a] ? a:find_(color[a]); } void union_(int a, int b) { a=find_(a); b=find_(b); color[a]=color[b]=min(a,b); } void update(int n, int v) { for(++n;n<=600000;n+=n&-n) tree[n]+=v; } int get_cnt(int n) { int ret=0; for(++n;n;n-=n&-n) ret+=tree[n]; return ret; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); TEST(freopen("input.txt", "r", stdin)); TEST(freopen("output.txt", "w", stdout)); TEST(freopen("debug.txt", "w", stderr)); int N, M, C, cnt; cin>>N>>M>>C; cnt=N; P.resize(N); for(int i=0;i<N;i++) { int X, Y; cin>>X>>Y; P[i]=make_pair(X,Y); x.push_back(X); y.push_back(Y); color[i]=i; } S.resize(M); for(int i=0;i<M;i++) { int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; x.push_back(x1); x.push_back(x2); y.push_back(y1); y.push_back(y2); S[i]=make_tuple(x1,y1,x2,y2); } sort(x.begin(),x.end()); sort(y.begin(),y.end()); x.resize(unique(x.begin(),x.end())-x.begin()); y.resize(unique(y.begin(),y.end())-y.begin()); E.resize(x.size()); L.resize(x.size()); R.resize(x.size()); for(int i=0;i<N;i++) { int X, Y; tie(X,Y)=P[i]; X=lower_bound(x.begin(),x.end(),X)-x.begin(); Y=lower_bound(y.begin(),y.end(),Y)-y.begin(); E[X].emplace_back(Y,i); P[i]=make_pair(X,Y); } for(int i=0;i<M;i++) { int x1, y1, x2, y2; tie(x1,y1,x2,y2)=S[i]; x1=lower_bound(x.begin(),x.end(),x1)-x.begin(); y1=lower_bound(y.begin(),y.end(),y1)-y.begin(); x2=lower_bound(x.begin(),x.end(),x2)-x.begin(); y2=lower_bound(y.begin(),y.end(),y2)-y.begin(); L[x1].emplace_back(y1,y2); R[x2].emplace_back(y1,y2); S[i]=make_tuple(x1,y1,x2,y2); } for(int c=0;c<x.size();c++) { sort(L[c].begin(),L[c].end()); sort(E[c].begin(),E[c].end()); sort(R[c].begin(),R[c].end()); for(auto n: L[c]) update(n.first,1), update(n.second,1); for(int i=1;i<E[c].size();i++) if(get_cnt(E[c][i-1].first-1)==get_cnt(E[c][i].first)) { Edge.emplace_back(y[E[c][i].first]-y[E[c][i-1].first],E[c][i-1].second,E[c][i].second); } for(auto n: R[c]) update(n.first,-1), update(n.second,-1); } E.clear(); L.clear(); R.clear(); memset(tree,0,sizeof(tree)); E.resize(y.size()); L.resize(y.size()); R.resize(y.size()); for(int i=0;i<N;i++) { int X, Y; tie(X,Y)=P[i]; E[Y].emplace_back(X,i); } for(int i=0;i<M;i++) { int x1, y1, x2, y2; tie(x1,y1,x2,y2)=S[i]; L[y1].emplace_back(x1,x2); R[y2].emplace_back(x1,x2); } for(int c=0;c<y.size();c++) { sort(L[c].begin(),L[c].end()); sort(E[c].begin(),E[c].end()); sort(R[c].begin(),R[c].end()); for(auto n: L[c]) update(n.first,1), update(n.second,1); for(int i=1;i<E[c].size();i++) if(get_cnt(E[c][i-1].first-1)==get_cnt(E[c][i].first)) { Edge.emplace_back(x[E[c][i].first]-x[E[c][i-1].first],E[c][i-1].second,E[c][i].second); } for(auto n: R[c]) update(n.first,-1), update(n.second,-1); } sort(Edge.begin(),Edge.end()); for(int i=0;i<Edge.size();i++) { int w, u, v; tie(w,u,v)=Edge[i]; if(find_(u)!=find_(v)) { union_(u,v); B[cnt-1]=B[cnt]+w; cnt--; } } Q.resize(C); for(int i=0;i<C;i++) { int b, h; cin>>b>>h; Q[i]=make_tuple(h,b,i); } sort(Q.begin(),Q.end()); for(int i=0;i<C;i++) { int h, b, idx; tie(h,b,idx)=Q[i]; for(;cnt<=h;cnt++) add_line(cnt,B[cnt]); ans[idx]=query(b); if(ans[idx]==0x7fffffffffffffffLL) ans[idx]=-1; } for(int i=0;i<C;i++) cout<<ans[i]<<'\n'; return 0; }

Compilation message (stderr)

construction.cpp: In function 'void add_line(long long int, long long int, int, int, int)':
construction.cpp:46:52: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  if(get_sign(ys-pys)*get_sign(ym-pym)<0 || ym==pym && ys<pys) {
                                            ~~~~~~~~^~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:134:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int c=0;c<x.size();c++) {
              ~^~~~~~~~~
construction.cpp:139:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=1;i<E[c].size();i++) if(get_cnt(E[c][i-1].first-1)==get_cnt(E[c][i].first)) {
               ~^~~~~~~~~~~~
construction.cpp:157:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int c=0;c<y.size();c++) {
              ~^~~~~~~~~
construction.cpp:162:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=1;i<E[c].size();i++) if(get_cnt(E[c][i-1].first-1)==get_cnt(E[c][i].first)) {
               ~^~~~~~~~~~~~
construction.cpp:168:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<Edge.size();i++) {
              ~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...