4100 | 2023-03-14 14:46:21 | Szin Attila | Hegyi levegő | cpp14 | Time limit exceeded 90/100 | 3.085s | 514164 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1840 KiB | ||||
2 | Accepted | 3ms | 2200 KiB | ||||
subtask2 | 19/19 | ||||||
3 | Accepted | 8ms | 4692 KiB | ||||
4 | Accepted | 8ms | 4780 KiB | ||||
5 | Accepted | 7ms | 4796 KiB | ||||
6 | Accepted | 7ms | 4832 KiB | ||||
7 | Accepted | 7ms | 5128 KiB | ||||
8 | Accepted | 7ms | 5144 KiB | ||||
9 | Accepted | 8ms | 5728 KiB | ||||
10 | Accepted | 7ms | 5928 KiB | ||||
subtask3 | 20/20 | ||||||
11 | Accepted | 2ms | 3716 KiB | ||||
12 | Accepted | 4ms | 4712 KiB | ||||
13 | Accepted | 39ms | 15496 KiB | ||||
14 | Accepted | 603ms | 145836 KiB | ||||
15 | Accepted | 2.076s | 414284 KiB | ||||
subtask4 | 20/20 | ||||||
16 | Accepted | 987ms | 203276 KiB | ||||
17 | Accepted | 985ms | 203428 KiB | ||||
18 | Accepted | 1.22s | 240060 KiB | ||||
19 | Accepted | 825ms | 191720 KiB | ||||
20 | Accepted | 769ms | 180100 KiB | ||||
subtask5 | 31/31 | ||||||
21 | Accepted | 995ms | 255652 KiB | ||||
22 | Accepted | 890ms | 185080 KiB | ||||
23 | Accepted | 755ms | 147480 KiB | ||||
24 | Accepted | 801ms | 147548 KiB | ||||
25 | Accepted | 1.113s | 255940 KiB | ||||
26 | Accepted | 879ms | 174416 KiB | ||||
27 | Accepted | 1.123s | 336380 KiB | ||||
28 | Accepted | 1.238s | 355132 KiB | ||||
29 | Accepted | 1.217s | 347312 KiB | ||||
subtask6 | 0/10 | ||||||
30 | Time limit exceeded | 3.085s | 373564 KiB | ||||
31 | Time limit exceeded | 3.082s | 299388 KiB | ||||
32 | Time limit exceeded | 3.073s | 242420 KiB | ||||
33 | Accepted | 2.733s | 449516 KiB | ||||
34 | Time limit exceeded | 3.045s | 394440 KiB | ||||
35 | Time limit exceeded | 3.072s | 372192 KiB | ||||
36 | Accepted | 2.28s | 514164 KiB | ||||
37 | Time limit exceeded | 3.062s | 480692 KiB | ||||
38 | Time limit exceeded | 3.085s | 503072 KiB | ||||
39 | Time limit exceeded | 3.075s | 492444 KiB |