41002023-03-14 14:46:21Szin AttilaHegyi levegőcpp14Time limit exceeded 90/1003.085s514164 KiB
#include <bits/stdc++.h>
using namespace std;
#define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using ll = long long;

const int maxN = 2e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;

vector<int> p, s, mo;
vector<set<int> > edges;
int curr;

int readInt() {
	bool minus = false;
	int result = 0;
	char ch;
	ch = getchar();
	while (true) {
		if (ch == '-') break;
		if (ch >= '0' && ch <= '9') break;
		ch = getchar();
	}
	if (ch == '-') minus = true; else result = ch-'0';
	while (true) {
		ch = getchar();
		if (ch < '0' || ch > '9') break;
		result = result*10 + (ch - '0');
	}
	if (minus)
		return -result;
	else
		return result;
}

int root(int x) {
   if(p[x] == x) return x;
   if(p[x] == -1) return -1;

   return (p[x] = root(p[x]));
}

void connect(int a, int b) {
   a = root(a), b = root(b);

   if(a == b || a == -1 || b == -1) return;

   if(s[a] < s[b]) swap(a, b);

   p[b] = a;
   s[a] += s[b];
   for(int i : edges[b]) {
      if(edges[a].find(i) == edges[a].end()) edges[a].insert(i);
      else {
         mo[i] = curr;
         edges[a].erase(i);
      }
   }
}

int main() {
   InTheNameOfGod;

   int n = readInt(),m = readInt(),q = readInt();
   map<int, vector<int> > ma;
   for(int i = 0; i < n*m; i++) {
      int x = readInt();
      ma[x].push_back(i);
   }


   p.resize(n*m+1, -1);
   s.resize(n*m+1, 0);
   edges.resize(n*m+1);
   mo.resize(q, -1);


   for(int i = 0; i < q; i++) {
      int a = readInt(),b = readInt(),c = readInt(),d = readInt();
      edges[m*(a-1) + b-1].insert(i);
      edges[m*(c-1) + d-1].insert(i);
   }

   for(auto i : ma) {
      curr = i.first;
      for(int x : i.second) {
         p[x] = x;
         s[x] = 1;
         if(x % m > 0) connect(x, x-1);
         if(x % m < m-1) connect(x, x+1);
         if(x > m-1) connect(x, x-m);
         if(x < (n-1)*m) connect(x, x+m);
      } 
   }

   for(int i : mo) cout << i << "\n";
   
   return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1840 KiB
2Accepted3ms2200 KiB
subtask219/19
3Accepted8ms4692 KiB
4Accepted8ms4780 KiB
5Accepted7ms4796 KiB
6Accepted7ms4832 KiB
7Accepted7ms5128 KiB
8Accepted7ms5144 KiB
9Accepted8ms5728 KiB
10Accepted7ms5928 KiB
subtask320/20
11Accepted2ms3716 KiB
12Accepted4ms4712 KiB
13Accepted39ms15496 KiB
14Accepted603ms145836 KiB
15Accepted2.076s414284 KiB
subtask420/20
16Accepted987ms203276 KiB
17Accepted985ms203428 KiB
18Accepted1.22s240060 KiB
19Accepted825ms191720 KiB
20Accepted769ms180100 KiB
subtask531/31
21Accepted995ms255652 KiB
22Accepted890ms185080 KiB
23Accepted755ms147480 KiB
24Accepted801ms147548 KiB
25Accepted1.113s255940 KiB
26Accepted879ms174416 KiB
27Accepted1.123s336380 KiB
28Accepted1.238s355132 KiB
29Accepted1.217s347312 KiB
subtask60/10
30Time limit exceeded3.085s373564 KiB
31Time limit exceeded3.082s299388 KiB
32Time limit exceeded3.073s242420 KiB
33Accepted2.733s449516 KiB
34Time limit exceeded3.045s394440 KiB
35Time limit exceeded3.072s372192 KiB
36Accepted2.28s514164 KiB
37Time limit exceeded3.062s480692 KiB
38Time limit exceeded3.085s503072 KiB
39Time limit exceeded3.075s492444 KiB