Submission #294123

# Submission time Handle Problem Language Result Execution time Memory
294123 2020-09-08T15:43:01 Z Gajowy Furniture (JOI20_furniture) C++14
0 / 100
5000 ms 640 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define e1 first
#define e2 second
#define int int_fast32_t
#define uint uint_fast64_t
#define ll int_fast64_t
#define ull uint_fast64_t
#define int16 int_fast16_t
#define uint16 uint_fast16_t
#define int32 int_fast32_t
#define uint32 uint_fast32_t
#define int64 int_fast64_t
#define uint64 uint_fast64_t
#define ld long double
#define float long double
#define size(x) (int)x.size()
#define satori int testCases; cin>>testCases; while(testCases--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define all(r) begin(r),end(r)
#define rall(r) rbegin(r),rend(r)
#define time chrono::high_resolution_clock().now().time_since_epoch().count()
#define elapsed(__begin,__end) (__end-__begin)/CLOCKS_PER_SEC
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
float pi=3.14159265358979323846;

const int MAXN=1e3+10;

bool bad[MAXN][MAXN],hi[MAXN][MAXN],lo[MAXN][MAXN];

void clean_hi(int x,int y)
{
	hi[x][y]=false;
	if(hi[x-1][y])
		clean_hi(x-1,y);
	else
	if(hi[x][y-1])
		clean_hi(x,y-1);
}

bool extend_hi(int x,int y)
{
	if(hi[x][y])
	{
		if(hi[x-1][y])
			clean_hi(x-1,y);
		return true;
	}
	if(!bad[x][y+1]&&extend_hi(x,y+1))
	{
		hi[x][y]=true;
		return true;
	}
	if(!bad[x+1][y]&&extend_hi(x+1,y))
	{
		hi[x][y]=true;
		return true;
	}
	return false;
}

void clean_lo(int x,int y)
{
	lo[x][y]=false;
	if(lo[x-1][y])
		clean_lo(x-1,y);
	else
	if(lo[x][y-1])
		clean_lo(x,y-1);
}

bool extend_lo(int x,int y)
{
	if(lo[x][y])
	{
		if(lo[x][y-1])
			clean_lo(x,y-1);
		return true;
	}
	if(!bad[x+1][y]&&extend_lo(x+1,y))
	{
		lo[x][y]=true;
		return true;
	}
	if(!bad[x][y+1]&&extend_lo(x,y+1))
	{
		lo[x][y]=true;
		return true;
	}
	return false;
}

int32_t main()
{
	fastio;
	int n,m,x,y,cx,cy,q;
	cin>>n>>m;
	for(int i=0;i<=m+1;i++)
		bad[0][i]=bad[n+1][i]=true;
	for(int i=1;i<=n;i++)
		bad[i][0]=bad[i][m+1]=true;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			cin>>x;
			if(x)
				bad[i][j]=true;
			if((i>1||j>1)&&bad[i-1][j]&&bad[i][j-1])
				bad[i][j]=true;
		}
	lo[n][m]=hi[n][m]=true;
	extend_hi(1,1);
	extend_lo(1,1);
	cin>>q;
	while(q--)
	{
		cin>>x>>y;
		if(lo[x][y]&&hi[x][y])
		{
			cout<<"0\n";
			continue;
		}
		cout<<"1\n";
		bad[x][y]=true;
		if(lo[x][y])
		{
			lo[x][y]=false;
			cx=x,cy=y;
			while(true)
			{
				if(lo[cx-1][cy])
					cx--;
				else
					cy--;
				lo[cx][cy]=false;
				if(extend_lo(cx,cy))
					break;
			}
		}
		if(hi[x][y])
		{
			hi[x][y]=false;
			cx=x,cy=y;
			while(true)
			{
				if(hi[cx-1][cy])
					cx--;
				else
					cy--;
				hi[cx][cy]=false;
				if(extend_hi(cx,cy))
					break;
			}
		}
	}
}

Compilation message

furniture.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("O3")
      | 
furniture.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      |
# Verdict Execution time Memory Grader output
1 Execution timed out 5030 ms 640 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5030 ms 640 KB Time limit exceeded
2 Halted 0 ms 0 KB -