제출 #141560

#제출 시각아이디문제언어결과실행 시간메모리
141560cheetose도로 건설 사업 (JOI13_construction)C++11
100 / 100
1014 ms57936 KiB
#include<cstdio> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<bitset> #include<string> #include<stack> #include<set> #include<unordered_set> #include<map> #include<unordered_map> #include<cstring> #include<complex> #include<cmath> #include<iomanip> #include<numeric> #include<algorithm> #include<list> #include<functional> #include<cassert> #define mp make_pair #define pb push_back #define X first #define Y second #define y0 y12 #define y1 y22 #define INF 1987654321987654321 #define PI 3.141592653589793238462643383279502884 #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c)) #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c)) #define MEM0(a) memset((a),0,sizeof(a)); #define MEM_1(a) memset((a),-1,sizeof(a)); #define ALL(a) a.begin(),a.end() #define SYNC ios_base::sync_with_stdio(false);cin.tie(0) using namespace std; typedef long long ll; typedef long double ld; typedef double db; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> Pi; typedef pair<ll, ll> Pll; typedef pair<ld, ld> Pd; typedef vector<int> Vi; typedef vector<ll> Vll; typedef vector<double> Vd; typedef vector<Pi> VPi; typedef vector<Pll> VPll; typedef vector<Pd> VPd; typedef tuple<int, int, int> iii; typedef tuple<int,int,int,int> iiii; typedef tuple<ll, ll, ll> LLL; typedef vector<iii> Viii; typedef vector<LLL> VLLL; typedef complex<double> base; const ll MOD = 1000000007; ll POW(ll a, ll b, ll MMM = MOD) {ll ret=1; for(;b;b>>=1,a=(a*a)%MMM)if(b&1)ret=(ret*a)% MMM; return ret; } ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); } int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 }; int n,m,q; struct rec{ int x1,y1,x2,y2;//x1<x2, y1<y2 }r[200000]; Vi x,y; pair<Pi,int> p[200000]; VPi v[200000]; int tree[600005]; void upd(int i, int k) { while (i <= 600001) { tree[i] += k; i += (i&-i); } } int sum(int i) { int c = 0; while (i > 0) { c += tree[i]; i -= (i&-i); } return c; } int parent[200000]; int find(int a) { if (parent[a] < 0)return a; return parent[a] = find(parent[a]); } void merge(int a, int b) { a = find(a), b = find(b); if (a != b) { parent[a] += parent[b]; parent[b] = a; } } int main() { MEM_1(parent); scanf("%d%d%d",&n,&m,&q); fup(i,0,n-1,1) { scanf("%d%d",&p[i].X.X,&p[i].X.Y); x.pb(p[i].X.X);y.pb(p[i].X.Y); p[i].Y=i; } fup(i,0,m-1,1) { scanf("%d%d%d%d",&r[i].x1,&r[i].y1,&r[i].x2,&r[i].y2); x.pb(r[i].x1);x.pb(r[i].x2);y.pb(r[i].y1);y.pb(r[i].y2); } sort(ALL(x));sort(ALL(y)); x.resize(unique(ALL(x))-x.begin());y.resize(unique(ALL(y))-y.begin()); fup(i,0,n-1,1) { p[i].X.X=lower_bound(ALL(x),p[i].X.X)-x.begin(); p[i].X.Y=lower_bound(ALL(y),p[i].X.Y)-y.begin(); } fup(i,0,m-1,1) { r[i].x1=lower_bound(ALL(x),r[i].x1)-x.begin(); r[i].x2=lower_bound(ALL(x),r[i].x2)-x.begin(); r[i].y1=lower_bound(ALL(y),r[i].y1)-y.begin(); r[i].y2=lower_bound(ALL(y),r[i].y2)-y.begin(); } sort(p,p+n); sort(r,r+m,[&](rec r1,rec r2){ return r1.x1<r2.x1; }); priority_queue<Pi,VPi,greater<Pi> > Q; for(int i=0,j=0;i<n;i++) { while(j<m && r[j].x1<=p[i].X.X) { upd(r[j].y1+1,1); Q.push(mp(r[j].x2,r[j].y1+1)); j++; } while(!Q.empty() && Q.top().X<p[i].X.X) { upd(Q.top().Y,-1); Q.pop(); } if(i>0 && p[i].X.X==p[i-1].X.X) { if(sum(p[i].X.Y+1)-sum(p[i-1].X.Y)==0) { v[p[i].Y].pb(mp(p[i-1].Y,y[p[i].X.Y]-y[p[i-1].X.Y])); v[p[i-1].Y].pb(mp(p[i].Y,y[p[i].X.Y]-y[p[i-1].X.Y])); } } } while(!Q.empty())Q.pop(); sort(p,p+n,[&](pair<Pi,int> p1,pair<Pi,int> p2){ if(p1.X.Y!=p2.X.Y)return p1.X.Y<p2.X.Y; return p1.X.X<p2.X.X; }); sort(r,r+m,[&](rec r1,rec r2){ return r1.y1<r2.y1; }); MEM0(tree); for(int i=0,j=0;i<n;i++) { while(j<m && r[j].y1<=p[i].X.Y) { upd(r[j].x1+1,1); Q.push(mp(r[j].y2,r[j].x1+1)); j++; } while(!Q.empty() && Q.top().X<p[i].X.Y) { upd(Q.top().Y,-1); Q.pop(); } if(i>0 && p[i].X.Y==p[i-1].X.Y) { if(sum(p[i].X.X+1)-sum(p[i-1].X.X)==0) { v[p[i].Y].pb(mp(p[i-1].Y,x[p[i].X.X]-x[p[i-1].X.X])); v[p[i-1].Y].pb(mp(p[i].Y,x[p[i].X.X]-x[p[i-1].X.X])); } } } Viii vv; fup(i,0,n-1,1) { for(Pi P:v[i]) { if(P.X>i) { vv.pb(iii(P.Y,i,P.X)); } } } sort(ALL(vv)); int t=n; Vll s; s.pb(INF); for(auto I:vv) { int z,x,y; tie(z,x,y)=I; if(find(x)==find(y))continue; merge(x,y); t--; s.pb(z); } sort(s.rbegin(),s.rend()); s[0]=0; int N=s.size(); Vll sum=s; fup(i,1,N-1,1)sum[i]+=sum[i-1]; while(q--) { ll x;int y; scanf("%lld%d",&x,&y); if(y<t) { puts("-1"); continue; } ll ans=x*t; y-=t; if(s[y]>x) { ans+=sum[N-1]-sum[y]; ans+=x*y; } else { int l=1,r=y; while(l<=r) { int k=(l+r)>>1; if(s[k]>=x)l=k+1; else r=k-1; } ans+=sum[N-1]-sum[r]; ans+=x*r; } printf("%lld\n",ans); } }

컴파일 시 표준 에러 (stderr) 메시지

construction.cpp: In function 'int main()':
construction.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d",&n,&m,&q);
  ~~~~~^~~~~~~~~~~~~~~~~~~
construction.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&p[i].X.X,&p[i].X.Y);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:116:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d",&r[i].x1,&r[i].y1,&r[i].x2,&r[i].y2);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:223:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%d",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...