Submission #1152175

#TimeUsernameProblemLanguageResultExecution timeMemory
1152175jakubmz2Road Service 2 (JOI24_ho_t5)C++20
27 / 100
349 ms160148 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pi pair<int,int> struct d{ int mi = 1e9; int ma = 0; }; vector<d> przedzialy; const int MAXN = 1e6 + 5; int ile[22]; int koszt[MAXN]; int nast[MAXN][22]; bool comp(const pair<int,int> &p1, const pair<int,int> &p2){ if(p1.second != p2.second){ return p1.second < p2.second; } if(p1.first != p2.first){ return p1.first < p2.first; } return false; } void dfs(int a, int b, int nr, vector<vector<int>> &kolorek, vector<vector<vector<pair<int,int>>>> &graf){ if(kolorek[a][b] != -1) return; kolorek[a][b] = nr; przedzialy[nr].mi = min(przedzialy.back().mi, a); przedzialy[nr].ma = max(przedzialy.back().ma, a); for(auto u : graf[a][b]){ if(kolorek[u.first][u.second] == -1) dfs(u.first, u.second, nr, kolorek, graf); } return; } int polacz(int a, int b){ ll w = 0; for(ll l = 21; l >= 0; --l){ cout << a << " " << l << " " << nast[a][l] << " " << b << " skok\n"; if(nast[a][l] >= b or nast[a][l] == -1) continue; w += (1 << l); //cout << w << " w\n"; a = nast[a][l]; } if(w == (1 << 22) - 1) return -1; else return w + 2; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<int>> kolorek; vector<vector<vector<pair<int,int>>>> graf; kolorek.resize(n + 1); graf.resize(n + 1); for(int i = 0; i <= n; ++i){ kolorek[i].resize(m + 1); graf[i].resize(m + 1); } for(int i = 0; i <= n; ++i){ for(int j = 0; j <= m; ++j){ kolorek[i][j] = -1; graf[i][j].clear(); } } char x; string s; for(int i = 1; i <= n; ++i){ cin >> s; for(int j = 1; j < m; ++j){ x = s[j-1]; //cout << x << " x\n"; if(x == '1') { graf[i][j].push_back({i,j+1}); graf[i][j+1].push_back({i,j}); } } } //cout << "#\n"; for(int i = 1; i < n; ++i){ cin >> s; for(int j = 1; j <= m; ++j){ x = s[j-1]; if(x == '1') { graf[i][j].push_back({i + 1, j}); graf[i+1][j].push_back({i,j}); } } } //cout << "$\n"; int nr = 1; przedzialy.push_back((d){1000000000, 0}); for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ if(kolorek[i][j] == -1){ przedzialy.push_back((d){i, i}); dfs(i, j, nr, kolorek, graf); nr++; } } } for(int i = 1; i <= n; ++i){ for(int l = 1; l <= 21; ++l){ nast[i][l] = 1e9; } } for(int i = 1; i <= n; ++i){ nast[i][0] = i; for(int j = 1; j <= m; ++j){ nast[i][0] = max(nast[i][0], przedzialy[kolorek[i][j]].ma); } } for(int l = 1; l <= 21; ++l){ for(int i = 1; i <= n; ++i){ nast[i][l] = nast[nast[i][l - 1]][l - 1]; } } for(int i = 1; i <= n; ++i){ cin >> koszt[i]; if(koszt[i] != 1) exit(0); } // for(int i = 1; i <= n; ++i){ // for(int j = 1; j <= m; ++j){ // cout << i << " " << j << " " << "ij\n"; // cout << kolorek[i][j] << "\n"; // } // } // for(int i = 1; i < przedzialy.size(); ++i){ // cout << i << " " << przedzialy[i].mi << " " << przedzialy[i].ma << " p\n"; // } pi a, b; int r; for(int i = 0; i < q; ++i){ cin >> r; //cout << r << " r\n"; if(r == 2){ cin >> a.first >> a.second >> b.first >> b.second; //cout << a.first << " " << a.second << " " << b.first << " " << b.second << " ab\n"; if(a.first >= b.first){ swap(a.first, b.first); swap(a.second, b.second); } if(kolorek[a.first][a.second] == kolorek[b.first][b.second]){ cout << 0 << "\n"; continue; } a.first = przedzialy[kolorek[a.first][a.second]].ma; b.first = przedzialy[kolorek[b.first][b.second]].mi; //cout << a.first << " " << b.first << " first\n"; if(a.first >= b.first){ cout << 1 << "\n"; continue; } ll w = 0; for(ll l = 21; l >= 0; --l){ if(nast[a.first][l] >= b.first or nast[a.first][l] == -1) continue; w += (1 << l); //cout << w << " w\n"; a.first = nast[a.first][l]; } if(w == (1 << 22) - 1) cout << -1 << "\n"; else cout << w + 2 << "\n"; } else{ set<int> num; for(int j = 0; j < r; ++j){ cin >> a.first >> a.second; num.insert(kolorek[a.first][a.second]); } vector<pair<int,int>> v; for(auto u : num){ //cout << u << " " << przedzialy[u].mi << " " << przedzialy[u].ma << " ud\n"; v.push_back({przedzialy[u].mi, przedzialy[u].ma}); } sort(v.begin(),v.end(), comp); // for(auto u : v){ // cout << u.first << " " << u.second << " u\n"; // } if(v.size() == 1){ cout << 0 << "\n"; continue; } ll w = 1; ll curr = v[0].second; int it = 0; while(it < v.size()){ while(it < v.size() and v[it].first <= curr and curr <= v[it].second){ ++it; } if(it >= v.size()) break; int nxt = nast[curr][0]; //cout << curr << " " << nxt << " cn\n"; if(nxt == curr){ cout << -1 << "\n"; w = -1; break; } while(it < v.size() and v[it].second > curr and v[it].second < nxt){ //cout << v[it].first << " " << v[it].second << " v\n"; int gdzie = v[it].second; w++; while(it < v.size() and v[it].first <= gdzie and v[it].second >= gdzie){ //cout << v[it].first << " " << v[it].second << " v2\n"; ++it; } if(it >= v.size()) { //cout << "#\n"; break; } } if(it >= v.size()) break; w++; curr = nxt; } if(w != -1) cout << w << "\n"; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...