Submission #163359

# Submission time Handle Problem Language Result Execution time Memory
163359 2019-11-12T20:00:53 Z TadijaSebez Golf (JOI17_golf) C++11
100 / 100
4580 ms 492820 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)
		{
			while(1)
			{
				auto it=node[x].lower_bound({l,-inf});
				if(it==node[x].end() || it->first>r) break;
				int id=it->second;
				node[x].erase(it);
				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:102: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:94: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:95:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
golf.cpp:97: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]);
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 63 ms 94328 KB Output is correct
2 Correct 70 ms 94328 KB Output is correct
3 Correct 63 ms 94328 KB Output is correct
4 Correct 66 ms 94464 KB Output is correct
5 Correct 84 ms 95632 KB Output is correct
6 Correct 77 ms 95548 KB Output is correct
7 Correct 69 ms 95480 KB Output is correct
8 Correct 68 ms 95480 KB Output is correct
9 Correct 86 ms 95480 KB Output is correct
10 Correct 72 ms 95456 KB Output is correct
11 Correct 76 ms 95480 KB Output is correct
12 Correct 69 ms 95480 KB Output is correct
13 Correct 76 ms 95532 KB Output is correct
14 Correct 73 ms 95608 KB Output is correct
15 Correct 69 ms 94892 KB Output is correct
16 Correct 75 ms 95480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 94328 KB Output is correct
2 Correct 70 ms 94328 KB Output is correct
3 Correct 63 ms 94328 KB Output is correct
4 Correct 66 ms 94464 KB Output is correct
5 Correct 84 ms 95632 KB Output is correct
6 Correct 77 ms 95548 KB Output is correct
7 Correct 69 ms 95480 KB Output is correct
8 Correct 68 ms 95480 KB Output is correct
9 Correct 86 ms 95480 KB Output is correct
10 Correct 72 ms 95456 KB Output is correct
11 Correct 76 ms 95480 KB Output is correct
12 Correct 69 ms 95480 KB Output is correct
13 Correct 76 ms 95532 KB Output is correct
14 Correct 73 ms 95608 KB Output is correct
15 Correct 69 ms 94892 KB Output is correct
16 Correct 75 ms 95480 KB Output is correct
17 Correct 71 ms 95736 KB Output is correct
18 Correct 92 ms 95864 KB Output is correct
19 Correct 72 ms 95736 KB Output is correct
20 Correct 75 ms 95736 KB Output is correct
21 Correct 165 ms 95736 KB Output is correct
22 Correct 78 ms 95736 KB Output is correct
23 Correct 77 ms 95864 KB Output is correct
24 Correct 73 ms 95736 KB Output is correct
25 Correct 82 ms 95736 KB Output is correct
26 Correct 72 ms 95744 KB Output is correct
27 Correct 79 ms 94968 KB Output is correct
28 Correct 69 ms 95612 KB Output is correct
29 Correct 71 ms 95608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 94328 KB Output is correct
2 Correct 70 ms 94328 KB Output is correct
3 Correct 63 ms 94328 KB Output is correct
4 Correct 66 ms 94464 KB Output is correct
5 Correct 84 ms 95632 KB Output is correct
6 Correct 77 ms 95548 KB Output is correct
7 Correct 69 ms 95480 KB Output is correct
8 Correct 68 ms 95480 KB Output is correct
9 Correct 86 ms 95480 KB Output is correct
10 Correct 72 ms 95456 KB Output is correct
11 Correct 76 ms 95480 KB Output is correct
12 Correct 69 ms 95480 KB Output is correct
13 Correct 76 ms 95532 KB Output is correct
14 Correct 73 ms 95608 KB Output is correct
15 Correct 69 ms 94892 KB Output is correct
16 Correct 75 ms 95480 KB Output is correct
17 Correct 71 ms 95736 KB Output is correct
18 Correct 92 ms 95864 KB Output is correct
19 Correct 72 ms 95736 KB Output is correct
20 Correct 75 ms 95736 KB Output is correct
21 Correct 165 ms 95736 KB Output is correct
22 Correct 78 ms 95736 KB Output is correct
23 Correct 77 ms 95864 KB Output is correct
24 Correct 73 ms 95736 KB Output is correct
25 Correct 82 ms 95736 KB Output is correct
26 Correct 72 ms 95744 KB Output is correct
27 Correct 79 ms 94968 KB Output is correct
28 Correct 69 ms 95612 KB Output is correct
29 Correct 71 ms 95608 KB Output is correct
30 Correct 4070 ms 300300 KB Output is correct
31 Correct 4262 ms 299936 KB Output is correct
32 Correct 4026 ms 301052 KB Output is correct
33 Correct 3649 ms 301856 KB Output is correct
34 Correct 3833 ms 304940 KB Output is correct
35 Correct 4580 ms 305336 KB Output is correct
36 Correct 4107 ms 304576 KB Output is correct
37 Correct 3969 ms 304996 KB Output is correct
38 Correct 3799 ms 304304 KB Output is correct
39 Correct 3996 ms 304848 KB Output is correct
40 Correct 3502 ms 466520 KB Output is correct
41 Correct 4030 ms 484380 KB Output is correct
42 Correct 3464 ms 414108 KB Output is correct
43 Correct 3442 ms 449916 KB Output is correct
44 Correct 3464 ms 454188 KB Output is correct
45 Correct 3672 ms 464252 KB Output is correct
46 Correct 3552 ms 492820 KB Output is correct
47 Correct 3361 ms 444212 KB Output is correct
48 Correct 3627 ms 464228 KB Output is correct
49 Correct 3522 ms 475124 KB Output is correct
50 Correct 68 ms 95528 KB Output is correct
51 Correct 68 ms 95608 KB Output is correct
52 Correct 67 ms 95608 KB Output is correct