답안 #163364

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
163364 2019-11-12T20:04:01 Z TadijaSebez Golf (JOI17_golf) C++11
100 / 100
4345 ms 492892 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=100050;
const int H=4*N;
const int inf=1e9+7;
struct compress
{
	vector<int> all;
	void Add(int x){ all.pb(x);}
	void Build(){ sort(all.begin(),all.end());all.erase(unique(all.begin(),all.end()),all.end());}
	int Get(int x){ return lower_bound(all.begin(),all.end(),x)-all.begin()+1;}
	int sz(){ return all.size();}
} xs,ys;
struct rec
{
	int type,x,l,r;
	rec(int t, int a, int b, int c):type(t),x(a),l(b),r(c){}
};
vector<rec> rs;
int X1[N],Y1[N],X2[N],Y2[N];
vector<pair<int,int>> Add[H],Del[H];
vector<int> fir,las;
int n,sx,sy,ex,ey;
void Build(int type)
{
	for(int i=1;i<=n;i++) Add[X1[i]].pb({Y1[i],Y2[i]}),Del[X2[i]].pb({Y1[i],Y2[i]});
    set<pair<int,int>> st;
    st.insert({1,1});st.insert({ys.sz(),ys.sz()});
    for(int x=1;x<=xs.sz();x++)
	{
        for(auto p:Del[x])
		{
			auto it=st.find(p),l=it,r=it;l--;r++;
			rs.pb(rec(type,x,l->second,r->first));
			st.erase(it);
		}
		if(sx==x)
		{
			auto r=st.lower_bound({sy,-inf}),l=r;l--;
			fir.pb(rs.size());
			rs.pb(rec(type,x,l->second,r->first));
		}
		if(ex==x)
		{
			auto r=st.lower_bound({ey,-inf}),l=r;l--;
			las.pb(rs.size());
			rs.pb(rec(type,x,l->second,r->first));
		}
		for(auto p:Add[x])
		{
			st.insert(p);
			auto it=st.find(p),l=it,r=it;l--;r++;
			rs.pb(rec(type,x,l->second,r->first));
		}
	}
	for(int i=1;i<=xs.sz();i++) Add[i].clear(),Del[i].clear();
	for(int i=1;i<=n;i++) swap(X1[i],Y1[i]),swap(X2[i],Y2[i]);swap(sx,sy);swap(ex,ey);swap(xs,ys);
}
const int M=H*2;
bool was[H];
int dist[H];
queue<int> q;
struct SegmentTree
{
	set<pair<int,int>> node[M];
	SegmentTree(){}
	void Ins(int x, int l, int r, int id)
	{
		for(l+=H,r+=H;l<=r;l>>=1,r>>=1)
		{
			if(l%2==1) node[l++].insert({x,id});
			if(r%2==0) node[r--].insert({x,id});
		}
	}
	void Take(int x, int l, int r, int d)
	{
		for(x+=H;x;x>>=1)
		{
			for(auto it=node[x].lower_bound({l,-inf});it!=node[x].end() && it->first<=r;node[x].erase(it++))
			{
				int id=it->second;
				if(!was[id]) was[id]=1,dist[id]=d+1,q.push(id);
			}
		}
	}
} ST[2];
int main()
{
	scanf("%i %i %i %i",&sx,&sy,&ex,&ey);
	scanf("%i",&n);
	xs.Add(0);xs.Add(inf);ys.Add(0);ys.Add(inf);xs.Add(sx);xs.Add(ex);ys.Add(sy);ys.Add(ey);
	for(int i=1;i<=n;i++) scanf("%i %i %i %i",&X1[i],&X2[i],&Y1[i],&Y2[i]),xs.Add(X1[i]),xs.Add(X2[i]),ys.Add(Y1[i]),ys.Add(Y2[i]);
	xs.Build();ys.Build();
	sx=xs.Get(sx);ex=xs.Get(ex);sy=ys.Get(sy);ey=ys.Get(ey);
	for(int i=1;i<=n;i++) X1[i]=xs.Get(X1[i]),X2[i]=xs.Get(X2[i]),Y1[i]=ys.Get(Y1[i]),Y2[i]=ys.Get(Y2[i]);
	Build(0);Build(1);
	for(int i=0;i<rs.size();i++) ST[rs[i].type].Ins(rs[i].x,rs[i].l,rs[i].r,i);
	for(int i:fir)
	{
		if(rs[i].type==0)
		{
			if(ex==rs[i].x && ey>=rs[i].l && ey<=rs[i].r) return 0*printf("1\n");
		}
		else
		{
			if(ey==rs[i].x && ex>=rs[i].l && ex<=rs[i].r) return 0*printf("1\n");
		}
		q.push(i),was[i]=1;
	}
	while(q.size())
	{
		int x=q.front();
		q.pop();
		ST[rs[x].type^1].Take(rs[x].x,rs[x].l,rs[x].r,dist[x]);
	}
	int ans=1e9+7;
	for(int i:las) if(was[i]) ans=min(ans,dist[i]);
	printf("%i\n",ans+1);
	return 0;
}

Compilation message

golf.cpp: In function 'void Build(int)':
golf.cpp:59:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for(int i=1;i<=n;i++) swap(X1[i],Y1[i]),swap(X2[i],Y2[i]);swap(sx,sy);swap(ex,ey);swap(xs,ys);
  ^~~
golf.cpp:59:60: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for(int i=1;i<=n;i++) swap(X1[i],Y1[i]),swap(X2[i],Y2[i]);swap(sx,sy);swap(ex,ey);swap(xs,ys);
                                                            ^~~~
golf.cpp: In function 'int main()':
golf.cpp:99:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<rs.size();i++) ST[rs[i].type].Ins(rs[i].x,rs[i].l,rs[i].r,i);
              ~^~~~~~~~~~
golf.cpp:91:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i %i %i",&sx,&sy,&ex,&ey);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
golf.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
golf.cpp:94:114: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=n;i++) scanf("%i %i %i %i",&X1[i],&X2[i],&Y1[i],&Y2[i]),xs.Add(X1[i]),xs.Add(X2[i]),ys.Add(Y1[i]),ys.Add(Y2[i]);
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 94328 KB Output is correct
2 Correct 61 ms 94328 KB Output is correct
3 Correct 65 ms 94328 KB Output is correct
4 Correct 67 ms 94432 KB Output is correct
5 Correct 75 ms 95480 KB Output is correct
6 Correct 77 ms 95480 KB Output is correct
7 Correct 93 ms 95456 KB Output is correct
8 Correct 78 ms 95480 KB Output is correct
9 Correct 70 ms 95480 KB Output is correct
10 Correct 77 ms 95480 KB Output is correct
11 Correct 96 ms 95636 KB Output is correct
12 Correct 69 ms 95480 KB Output is correct
13 Correct 86 ms 95480 KB Output is correct
14 Correct 78 ms 95584 KB Output is correct
15 Correct 67 ms 94812 KB Output is correct
16 Correct 78 ms 95480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 94328 KB Output is correct
2 Correct 61 ms 94328 KB Output is correct
3 Correct 65 ms 94328 KB Output is correct
4 Correct 67 ms 94432 KB Output is correct
5 Correct 75 ms 95480 KB Output is correct
6 Correct 77 ms 95480 KB Output is correct
7 Correct 93 ms 95456 KB Output is correct
8 Correct 78 ms 95480 KB Output is correct
9 Correct 70 ms 95480 KB Output is correct
10 Correct 77 ms 95480 KB Output is correct
11 Correct 96 ms 95636 KB Output is correct
12 Correct 69 ms 95480 KB Output is correct
13 Correct 86 ms 95480 KB Output is correct
14 Correct 78 ms 95584 KB Output is correct
15 Correct 67 ms 94812 KB Output is correct
16 Correct 78 ms 95480 KB Output is correct
17 Correct 82 ms 95724 KB Output is correct
18 Correct 80 ms 95736 KB Output is correct
19 Correct 70 ms 95864 KB Output is correct
20 Correct 87 ms 95736 KB Output is correct
21 Correct 82 ms 95736 KB Output is correct
22 Correct 72 ms 95736 KB Output is correct
23 Correct 81 ms 95864 KB Output is correct
24 Correct 70 ms 95864 KB Output is correct
25 Correct 80 ms 95736 KB Output is correct
26 Correct 82 ms 95736 KB Output is correct
27 Correct 73 ms 94968 KB Output is correct
28 Correct 112 ms 95736 KB Output is correct
29 Correct 68 ms 95608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 94328 KB Output is correct
2 Correct 61 ms 94328 KB Output is correct
3 Correct 65 ms 94328 KB Output is correct
4 Correct 67 ms 94432 KB Output is correct
5 Correct 75 ms 95480 KB Output is correct
6 Correct 77 ms 95480 KB Output is correct
7 Correct 93 ms 95456 KB Output is correct
8 Correct 78 ms 95480 KB Output is correct
9 Correct 70 ms 95480 KB Output is correct
10 Correct 77 ms 95480 KB Output is correct
11 Correct 96 ms 95636 KB Output is correct
12 Correct 69 ms 95480 KB Output is correct
13 Correct 86 ms 95480 KB Output is correct
14 Correct 78 ms 95584 KB Output is correct
15 Correct 67 ms 94812 KB Output is correct
16 Correct 78 ms 95480 KB Output is correct
17 Correct 82 ms 95724 KB Output is correct
18 Correct 80 ms 95736 KB Output is correct
19 Correct 70 ms 95864 KB Output is correct
20 Correct 87 ms 95736 KB Output is correct
21 Correct 82 ms 95736 KB Output is correct
22 Correct 72 ms 95736 KB Output is correct
23 Correct 81 ms 95864 KB Output is correct
24 Correct 70 ms 95864 KB Output is correct
25 Correct 80 ms 95736 KB Output is correct
26 Correct 82 ms 95736 KB Output is correct
27 Correct 73 ms 94968 KB Output is correct
28 Correct 112 ms 95736 KB Output is correct
29 Correct 68 ms 95608 KB Output is correct
30 Correct 3879 ms 300244 KB Output is correct
31 Correct 3707 ms 299924 KB Output is correct
32 Correct 4172 ms 301164 KB Output is correct
33 Correct 3578 ms 301792 KB Output is correct
34 Correct 3660 ms 304840 KB Output is correct
35 Correct 4137 ms 305340 KB Output is correct
36 Correct 4211 ms 304620 KB Output is correct
37 Correct 4345 ms 305164 KB Output is correct
38 Correct 4078 ms 304280 KB Output is correct
39 Correct 4108 ms 304840 KB Output is correct
40 Correct 3178 ms 466384 KB Output is correct
41 Correct 3393 ms 484344 KB Output is correct
42 Correct 2985 ms 414012 KB Output is correct
43 Correct 3085 ms 450016 KB Output is correct
44 Correct 3206 ms 454124 KB Output is correct
45 Correct 3086 ms 464388 KB Output is correct
46 Correct 3237 ms 492892 KB Output is correct
47 Correct 2769 ms 444312 KB Output is correct
48 Correct 2925 ms 464224 KB Output is correct
49 Correct 3112 ms 475220 KB Output is correct
50 Correct 80 ms 95608 KB Output is correct
51 Correct 68 ms 95608 KB Output is correct
52 Correct 75 ms 95608 KB Output is correct