41092023-03-14 17:31:52Szin AttilaHegyi levegőcpp14Time limit exceeded 90/1003.095s517560 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;
   vector<pair<int, int> > ma;
   for(int i = 0; i < n*m; i++) {
      int x;
      cin >> x;
      ma.push_back({x, i});
   }

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

   sort(ma.begin(), ma.end());

   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;
      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
2Accepted3ms2200 KiB
subtask219/19
3Accepted6ms3568 KiB
4Accepted6ms3688 KiB
5Accepted6ms3528 KiB
6Accepted6ms3504 KiB
7Accepted6ms3672 KiB
8Accepted6ms3844 KiB
9Accepted6ms4320 KiB
10Accepted7ms4568 KiB
subtask320/20
11Accepted3ms3356 KiB
12Accepted4ms4452 KiB
13Accepted37ms13640 KiB
14Accepted533ms125584 KiB
15Accepted1.44s312444 KiB
subtask420/20
16Accepted1.268s205700 KiB
17Accepted1.281s205724 KiB
18Accepted1.552s242248 KiB
19Accepted1.284s193980 KiB
20Accepted1.259s182404 KiB
subtask531/31
21Accepted1.317s235280 KiB
22Accepted975ms164788 KiB
23Accepted940ms126916 KiB
24Accepted940ms126916 KiB
25Accepted1.105s235396 KiB
26Accepted904ms174680 KiB
27Accepted1.358s337064 KiB
28Accepted1.493s355880 KiB
29Accepted1.338s337444 KiB
subtask60/10
30Time limit exceeded3.078s343616 KiB
31Time limit exceeded3.061s248424 KiB
32Time limit exceeded3.055s191532 KiB
33Accepted2.825s347820 KiB
34Time limit exceeded3.065s343548 KiB
35Time limit exceeded3.069s321644 KiB
36Accepted2.772s517560 KiB
37Time limit exceeded3.073s435008 KiB
38Time limit exceeded3.095s505416 KiB
39Time limit exceeded3.088s457832 KiB