40992023-03-14 14:34:33Szin AttilaHegyi levegőcpp14Time limit exceeded 90/1003.105s514180 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 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,m,q;
   cin >> n >> m >> q;
   map<int, vector<int> > ma;
   for(int i = 0; i < n*m; i++) {
      int x;
      cin >> x;
      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,b,c,d;
      cin >> a >> b >> c >> d;
      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
1Accepted3ms1824 KiB
2Accepted3ms2080 KiB
subtask219/19
3Accepted8ms4640 KiB
4Accepted8ms4596 KiB
5Accepted8ms4588 KiB
6Accepted8ms4416 KiB
7Accepted8ms4524 KiB
8Accepted8ms4820 KiB
9Accepted8ms5296 KiB
10Accepted8ms5384 KiB
subtask320/20
11Accepted3ms3184 KiB
12Accepted4ms4156 KiB
13Accepted41ms15108 KiB
14Accepted550ms145356 KiB
15Accepted1.791s413676 KiB
subtask420/20
16Accepted1.034s203128 KiB
17Accepted1.077s203288 KiB
18Accepted1.087s239952 KiB
19Accepted929ms191572 KiB
20Accepted913ms179832 KiB
subtask531/31
21Accepted1.269s255536 KiB
22Accepted944ms184736 KiB
23Accepted805ms147144 KiB
24Accepted850ms147564 KiB
25Accepted1.223s256068 KiB
26Accepted926ms174216 KiB
27Accepted1.133s336084 KiB
28Accepted1.203s354760 KiB
29Accepted1.202s346936 KiB
subtask60/10
30Time limit exceeded3.072s345088 KiB
31Time limit exceeded3.055s299208 KiB
32Time limit exceeded3.065s242372 KiB
33Time limit exceeded3.059s226680 KiB
34Time limit exceeded3.088s383468 KiB
35Time limit exceeded3.072s306352 KiB
36Accepted2.388s514180 KiB
37Time limit exceeded3.105s480660 KiB
38Time limit exceeded3.095s503044 KiB
39Time limit exceeded3.098s446228 KiB