답안 #141560

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
141560 2019-08-08T11:55:57 Z cheetose 도로 건설 사업 (JOI13_construction) C++11
100 / 100
1014 ms 57936 KB
#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);
	}
}

Compilation message

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);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 8952 KB Output is correct
2 Correct 310 ms 23264 KB Output is correct
3 Correct 309 ms 23264 KB Output is correct
4 Correct 390 ms 32892 KB Output is correct
5 Correct 392 ms 28992 KB Output is correct
6 Correct 316 ms 23516 KB Output is correct
7 Correct 246 ms 33488 KB Output is correct
8 Correct 277 ms 28376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 779 ms 30408 KB Output is correct
2 Correct 787 ms 30588 KB Output is correct
3 Correct 784 ms 30420 KB Output is correct
4 Correct 782 ms 30400 KB Output is correct
5 Correct 548 ms 26712 KB Output is correct
6 Correct 391 ms 29040 KB Output is correct
7 Correct 781 ms 30416 KB Output is correct
8 Correct 471 ms 47324 KB Output is correct
9 Correct 470 ms 47356 KB Output is correct
10 Correct 566 ms 39752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 605 ms 39388 KB Output is correct
2 Correct 605 ms 39620 KB Output is correct
3 Correct 553 ms 35264 KB Output is correct
4 Correct 618 ms 42396 KB Output is correct
5 Correct 478 ms 30504 KB Output is correct
6 Correct 672 ms 42456 KB Output is correct
7 Correct 656 ms 38696 KB Output is correct
8 Correct 603 ms 37368 KB Output is correct
9 Correct 492 ms 45004 KB Output is correct
10 Correct 577 ms 39132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1012 ms 46384 KB Output is correct
2 Correct 661 ms 31400 KB Output is correct
3 Correct 995 ms 39728 KB Output is correct
4 Correct 438 ms 26376 KB Output is correct
5 Correct 968 ms 40140 KB Output is correct
6 Correct 477 ms 26724 KB Output is correct
7 Correct 971 ms 39648 KB Output is correct
8 Correct 1014 ms 44496 KB Output is correct
9 Correct 767 ms 57936 KB Output is correct
10 Correct 766 ms 49096 KB Output is correct
11 Correct 603 ms 39768 KB Output is correct