Cod sursa(job #424152)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 24 martie 2010 17:11:27
Problema Matrice 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.42 kb
using namespace std;

#include <set>
#include <map>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <utility>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define oo 1<<30
#define f first
#define s second
#define II inline
#define db double
#define ll long long
#define pb push_back
#define mp make_pair
#define Size(V) ((int)(V.size()))
#define all(v) v.begin() , v.end()
#define CC(v) memset((v),0,sizeof((v)))
#define CP(v,w) memcpy((v),(w),sizeof((w)))
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);++(i))
#define REP(i, N) for (int (i)=0;(i)<(int)(N);++(i))
#define FORit(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); ++it)

#define IN  "matrice2.in"
#define OUT "matrice2.out"
#define N_MAX (370)
#define Q_MAX (21000)
#define NxN_MAX (92000)
#define G(x,y) ( ((x)-1)*(N) + (y) )

typedef vector<int> VI;
typedef pair<int,int> pi;
typedef vector<string> VS;
template<class T> string toString(T n) {ostringstream ost;ost<<n;ost.flush();return ost.str();}

int last = -1,N,K,V[N_MAX][N_MAX],Nr[NxN_MAX],S[NxN_MAX],T[NxN_MAX];
vector< pair<pi,pi> > Q;
vector<pi> A,B,M;

const int xx[] = {-1,0,1,0};
const int yy[] = {0,1,0,-1};

II void scan()
{
	freopen(IN,"r",stdin);
	freopen(OUT,"w",stdout);
	
	scanf("%d%d",&N,&K);
	FOR(i,1,N)
	FOR(j,1,N)
	{
		scanf("%d",V[i]+j);
		B.pb( mp(i,j) );
	}
	
	int x1,y1,x2,y2;	
	FOR(i,1,K)
	{
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		Q.pb( mp(mp(x1,y1),mp(x2,y2)) );
	}
}

II bool ok(int x,int y)
{
	if(x < 1 || x > N || y < 1 || y > N)
		return false;
	return true;
}

II int rep(int x)
{
	return (T[x] == x) ? x : T[x] = rep(T[x]); 
}

II void add(int x,int y)
{
	if(Nr[x] > Nr[y])
		Nr[x] += Nr[y],
		T[y] = x;
	else
		Nr[y] += Nr[x],
		T[x] = y;
}

II bool comp(const pi x,const pi y)
{
	return V[x.f][x.s] > V[y.f][y.s];
}

II bool comp2(const int x,const int y)
{
	return x > y;
}

II void solve()
{
	sort(B.begin(),B.end(),comp);
	
	FOR(i,1,N)
	FOR(j,1,N)
		S[++S[0]] = V[i][j];
	sort(S+1,S+S[0]+1,comp2);
	
	int aux = S[0];
	S[0] = 1;
	FOR(i,2,aux)
		if(S[i] != S[i-1])
			S[++S[0]] = S[i];
	
	REP(i,K)
		M.pb( mp(1,i) );
	
	int step;
	for(step = 1;step <= S[0];step <<= 1);
	for(;step;step >>= 1)
	{
		FOR(i,1,N)
		FOR(j,1,N)
		{
			T[ G(i,j) ] = G(i,j);
			Nr[ G(i,j) ] = 1;
		}
		vector<pi>::iterator Bi = B.begin();
		
		REP(i,Size(M))
		{
			if(M[i].f + step > S[0])
				continue;
			for(;Bi != B.end() && V[Bi->f][Bi->s] >= S[ M[i].f + step - 1];++Bi)
				for(int k = 0;k < 4;++k)
				{
					if( !ok(Bi->f + xx[k],Bi->s + yy[k]) )
						continue;
					if(V[Bi->f + xx[k] ][Bi->s + yy[k]] < V[Bi->f][Bi->s])
						continue;
					
					int V1 = G(Bi->f + xx[k],Bi->s + yy[k]);
					int V2 = G(Bi->f,Bi->s);
					if( rep(V1) != rep(V2) )
						add( rep(V1),rep(V2) );
				}
			
			int V1 = G(Q[ M[i].s ].f.f,Q[ M[i].s ].f.s);
			int V2 = G(Q[ M[i].s ].s.f,Q[ M[i].s ].s.s);
			if( rep(V1) != rep(V2) )
				M[i].f += step;
		}
		
		sort( all(M) );
	}
	
	vector<pi> Sol;
	REP(i,K)
		Sol.pb( mp(M[i].s,S[ M[i].f ]) );
	sort( all(Sol) );
	FORit(it,Sol)
		printf("%d\n",it->s);
}

int main()
{
	scan();
	solve();
	return 0;
}