10332 | 2024-03-31 13:13:48 | 111 | Hegyi levegő | cpp17 | Accepted 100/100 | 1.208s | 747020 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
int ans[500000],mn;
struct DSU{
vector<int>v;
vector<vector<tuple<int,int,int>>>q;
DSU(int n):v(n,-1),q(n){
}
int find(int i){
return v[i]<0?i:v[i]=find(v[i]);
}
void unite(int i,int j){
int a=find(i),b=find(j);
if(a==b){
return;
}
if(q[a].size()<q[b].size()){
swap(a,b);
}
for(auto[ii,jj,k]:q[b]){
int aa=find(ii),bb=find(jj);
if(aa==a&&bb==b||aa==b&&bb==a){
ans[k]=mn;
}
else{
q[a].emplace_back(ii,jj,k);
}
}
v[a]+=v[b];
v[b]=a;
}
void add(int i,int j,int k){
q[find(i)].emplace_back(i,j,k);
q[find(j)].emplace_back(i,j,k);
}
};
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N,M,Q;
cin>>N>>M>>Q;
int m[N][M];
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
cin>>m[i][j];
}
}
auto C=[&](int i,int j)->int{
return i*M+j;
};
DSU dsu(N*M);
for(int i=0;i<Q;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
dsu.add(C(a-1,b-1),C(c-1,d-1),i);
}
vector<tuple<int,int,int>>e;
for(int i=0;i<N;i++){
for(int j=1;j<M;j++){
e.emplace_back(max(m[i][j],m[i][j-1]),C(i,j),C(i,j-1));
}
}
for(int i=1;i<N;i++){
for(int j=0;j<M;j++){
e.emplace_back(max(m[i][j],m[i-1][j]),C(i,j),C(i-1,j));
}
}
sort(e.begin(),e.end());
for(auto[x,i,j]:e){
mn=x;
dsu.unite(i,j);
}
for(int i=0;i<Q;i++){
cout<<ans[i]<<'\n';
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1832 KiB | ||||
2 | Accepted | 3ms | 2200 KiB | ||||
subtask2 | 19/19 | ||||||
3 | Accepted | 4ms | 3380 KiB | ||||
4 | Accepted | 6ms | 3768 KiB | ||||
5 | Accepted | 6ms | 3804 KiB | ||||
6 | Accepted | 6ms | 4120 KiB | ||||
7 | Accepted | 6ms | 4384 KiB | ||||
8 | Accepted | 6ms | 4536 KiB | ||||
9 | Accepted | 6ms | 4820 KiB | ||||
10 | Accepted | 4ms | 4784 KiB | ||||
subtask3 | 20/20 | ||||||
11 | Accepted | 2ms | 3568 KiB | ||||
12 | Accepted | 4ms | 4288 KiB | ||||
13 | Accepted | 17ms | 12676 KiB | ||||
14 | Accepted | 189ms | 112764 KiB | ||||
15 | Accepted | 620ms | 271304 KiB | ||||
subtask4 | 20/20 | ||||||
16 | Accepted | 514ms | 184932 KiB | ||||
17 | Accepted | 517ms | 184904 KiB | ||||
18 | Accepted | 657ms | 232796 KiB | ||||
19 | Accepted | 628ms | 198804 KiB | ||||
20 | Accepted | 617ms | 190560 KiB | ||||
subtask5 | 31/31 | ||||||
21 | Accepted | 321ms | 204744 KiB | ||||
22 | Accepted | 303ms | 155944 KiB | ||||
23 | Accepted | 282ms | 124732 KiB | ||||
24 | Accepted | 284ms | 124684 KiB | ||||
25 | Accepted | 323ms | 205044 KiB | ||||
26 | Accepted | 314ms | 143636 KiB | ||||
27 | Accepted | 301ms | 274936 KiB | ||||
28 | Accepted | 324ms | 307640 KiB | ||||
29 | Accepted | 298ms | 286124 KiB | ||||
subtask6 | 10/10 | ||||||
30 | Accepted | 1.185s | 579632 KiB | ||||
31 | Accepted | 1.167s | 451472 KiB | ||||
32 | Accepted | 1.098s | 349320 KiB | ||||
33 | Accepted | 1.078s | 320480 KiB | ||||
34 | Accepted | 1.179s | 580668 KiB | ||||
35 | Accepted | 1.208s | 548676 KiB | ||||
36 | Accepted | 1.072s | 406516 KiB | ||||
37 | Accepted | 1.036s | 692832 KiB | ||||
38 | Accepted | 1.074s | 747020 KiB | ||||
39 | Accepted | 1.001s | 743276 KiB |