# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1048207 |
2024-08-08T05:14:51 Z |
phong |
Golf (JOI17_golf) |
C++17 |
|
5 ms |
41564 KB |
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
//#define int long long
const int nmax = 1e6 + 5, N = 1e6;
const ll oo = 1e9 + 5, base = 311;
const int lg = 17, tx = 26;
const ll mod = 998244353;
#define pii pair<int, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;
struct node{
int x1, x2, y1, y2;
}a[nmax];
int n, rt, S, T, U, V;
vector<int> nen;
int gg(int u){
return lower_bound(nen.begin(), nen.end(), u) - nen.begin() + 1;
}
struct holder{
int x, l, r;
};
vector<holder> A, B;
int tree[nmax << 2], lz[nmax << 2];
void build(int idx){
if(!idx) lz[1] = gg(-oo);
else lz[1] = gg(oo);
}
void fix(int id, int val){
if(!val) return;
tree[id] = val;
lz[id] = val;
}
void down(int id){
fix(id << 1, lz[id]);
fix(id << 1 | 1, lz[id]);
lz[id] = 0;
}
void update(int id, int l, int r, int u, int v, int val){
if(l > v || r < u || u > v) return;
if(l >= u && r <= v) return fix(id, val);
down(id);
int mid = r + l >> 1;
update(id << 1, l, mid, u, v,val);
update(id << 1 | 1, mid + 1, r, u, v, val);
}
#define update(l, r, val) update(1, 1, rt, l, r, val)
int get(int id, int l, int r, int u){
if(l == r) return tree[id];
down(id);
int mid = r + l >> 1;
if(u <= mid)return get(id << 1, l, mid, u);
return get(id << 1 | 1, mid + 1, r, u);
}
#define get(x) get(1, 1, rt, x)
int L[nmax], R[nmax];
void add(int x1, int y1, int x2, int y2){
if(x1 > x2) swap(x1, x2);
if(y1 > y2) swap(y1, y2);
A.push_back({y1, x1, x2});
A.push_back({y2, x1, x2});
B.push_back({x1, y1, y2});
B.push_back({x2, y1, y2});
}
struct hos{
int x, l, r, id;
};
vector<holder> one,two;
vector<int> adj[nmax];
//A doc
//B ngang
int ans[nmax][2];
queue<pii> q;
void solve(){
//tao canh
S = gg(S), T = gg(T);
U = gg(U), V = gg(V);
// cout << nen[U - 1]<< ' ' << nen[V - 1] << ' ';
add(gg(-oo), gg(-oo), gg(oo), gg(oo));
for(int i = 1; i <= n; ++i){
A.push_back({a[i].y1, a[i].x1, a[i].x2});
A.push_back({a[i].y2, a[i].x1, a[i].x2});
B.push_back({a[i].x1, a[i].y1, a[i].y2});
B.push_back({a[i].x2, a[i].y1, a[i].y2});
}
one.push_back({gg(-oo), gg(-oo), gg(oo)});
one.push_back({gg(oo), gg(-oo), gg(oo)});
two.push_back({gg(-oo), gg(-oo), gg(oo)});
two.push_back({gg(oo), gg(-oo), gg(oo)});
sort(A.begin(), A.end(), [](holder A, holder B){
return A.x < B.x;
});
vector<pii> tmp;
tmp.push_back({S, T});
tmp.push_back({U, V});
for(int i = 1; i <= n; ++i){
tmp.push_back({a[i].x1 - 1, a[i].y1 - 1});
tmp.push_back({a[i].x1 - 1, a[i].y2 + 1});
tmp.push_back({a[i].x2 + 1, a[i].y1 - 1});
tmp.push_back({a[i].x2 + 1, a[i].y2 + 1});
}
//#####
sort(tmp.begin(), tmp.end(), [](pii a, pii b){
return a.se < b.se;
});
build(0);
int pos = 0;
for(int i = 0; i < tmp.size(); ++i){
int x = tmp[i].fi, y = tmp[i].se;
while(pos < A.size() && A[pos].x <= y){
update(A[pos].l, A[pos].r, A[pos].x);
++pos;
}
L[i] = get(x);
}
build(1);
pos = 0;
reverse(A.begin(), A.end());
for(int i = tmp.size() - 1; i >= 0; --i){
int x = tmp[i].fi, y = tmp[i].se;
while(pos < A.size() && A[pos].x >= y){
update(A[pos].l, A[pos].r, A[pos].x);
++pos;
}
R[i] = get(x);
}
for(int i = 0; i < tmp.size(); ++i){
if(L[i] + 1 <= R[i] - 1){
one.push_back({tmp[i].fi, L[i] + 1, R[i] - 1});
}
}
//######
sort(B.begin(), B.end(), [](holder A, holder B){
return A.x < B.x;
});
sort(tmp.begin(), tmp.end(), [](pii a, pii b){
return a.fi < b.fi;
});
build(0); pos = 0;
for(int i = 0; i < tmp.size(); ++i){
int x = tmp[i].fi, y = tmp[i].se;
while(pos < B.size() && B[pos].x <= x){
update(B[pos].l, B[pos].r, B[pos].x);
++pos;
}
L[i] = get(y);
}
build(1); pos = 0;
reverse(B.begin(), B.end());
for(int i = tmp.size() - 1; i >= 0; --i){
int x = tmp[i].fi, y = tmp[i].se;
while(pos < B.size() && B[pos].x >= x){
update(B[pos].l, B[pos].r, B[pos].x);
++pos;
}
R[i] = get(y);
}
for(int i = 0; i < tmp.size(); ++i){
if(L[i] + 1 <= R[i] - 1)
two.push_back({tmp[i].se, L[i] + 1, R[i] - 1});
}
//#####
memset(ans, 0x3f, sizeof ans);
for(int i = 0; i < one.size(); ++i){
if(S == one[i].x && one[i].l <= T && one[i].r >= T ){
q.push({i, 0});
ans[i][0] = 1;
}
}
for(int i = 0; i < two.size(); ++i){
if(T == two[i].x && two[i].l <= S && one[i].r >= S){
q.push({i, 1});
ans[i][1] = 1;
}
}
while(q.size()){
pii tmp = q.front();q.pop();
if(tmp.se == 0){
for(int j = 0; j < two.size(); ++j){
int x = one[tmp.fi].x, l = one[tmp.fi].l, r = one[tmp.fi].r;
int y = two[j].x, le = two[j].l, ri = two[j].r;
if(le <= x && x <= ri && l <= y && y <= r){
if(ans[j][1] > oo){
ans[j][1] = ans[tmp.fi][0] + 1;
q.push({j, 1});
}
}
}
}
else{
for(int j = 0; j < one.size(); ++j){
int x = two[tmp.fi].x, l = two[tmp.fi].l, r = two[tmp.fi].r;
int y = one[j].x, le = one[j].l, ri = one[j].r;
if(le <= x && x <= ri && l <= y && y <= r){
if(ans[j][0] > oo){
ans[j][0] = ans[tmp.fi][1] + 1;
q.push({j, 0});
}
}
}
}
}
int mi = oo;
for(int i = 0; i < one.size(); ++i){
if(U == one[i].x && one[i].l <= V && one[i].r >= V ){
// cout << nen[one[i].l - 1]<< ' ' << nen[one[i].r - 1] << endl;
mi = min(ans[i][0], mi);
}
// cout << nen[one[i].x - 1]<< ' ' << nen[one[i].l - 1] << ' ' << nen[one[i].r - 1] << ' ' << ans[i][0] << endl;
}
// cout <<"###\n";
for(int i = 0; i < two.size(); ++i){
if(V == two[i].x && two[i].l <= U && one[i].r >= U){
mi = min(mi, ans[i][1]);
}
// cout << nen[two[i].x - 1] << ' ' << nen[two[i].l - 1] << ' ' << nen[two[i].r - 1] << ' ' << ans[i][1] << endl;
}
cout << mi;
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("CANDY.inp", "r", stdin);
// freopen("CANDY.out", "w", stdout);
cin >> S >> T >> U >> V >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i].x1 >> a[i].x2 >> a[i].y1 >> a[i].y2;
++a[i].x1;--a[i].x2;
++a[i].y1;--a[i].y2;
nen.push_back(a[i].x1);
nen.push_back(a[i].x2);
nen.push_back(a[i].y1);
nen.push_back(a[i].y2);
nen.push_back(a[i].x1 - 1);
nen.push_back(a[i].x2 + 1);
nen.push_back(a[i].y1 - 1);
nen.push_back(a[i].y2 + 1);
}
for(int x = -1; x <= 1; ++x){
nen.push_back(S + x); nen.push_back(U + x);
nen.push_back(T + x); nen.push_back(V + x);
nen.push_back(-oo + x); nen.push_back(oo + x);
}
sort(nen.begin(), nen.end());
nen.erase(unique(nen.begin(), nen.end()), nen.end());
rt = nen.size();
for(int i = 1; i <= n; ++i){
a[i].x1 = gg(a[i].x1);
a[i].x2 = gg(a[i].x2);
a[i].y1 = gg(a[i].y1);
a[i].y2 = gg(a[i].y2);
}
//tao canh
solve();
}
/*
0 0 5 5
1
2 3 2 4
3 5 8 6
1
5 6 2 8
*/
Compilation message
golf.cpp: In function 'void update(int, int, int, int, int, int)':
golf.cpp:51:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int mid = r + l >> 1;
| ~~^~~
golf.cpp: In function 'int get(int, int, int, int)':
golf.cpp:60:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | int mid = r + l >> 1;
| ~~^~~
golf.cpp: In function 'void solve()':
golf.cpp:119:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for(int i = 0; i < tmp.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:121:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | while(pos < A.size() && A[pos].x <= y){
| ~~~~^~~~~~~~~~
golf.cpp:132:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | while(pos < A.size() && A[pos].x >= y){
| ~~~~^~~~~~~~~~
golf.cpp:138:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(int i = 0; i < tmp.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:151:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
151 | for(int i = 0; i < tmp.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:153:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
153 | while(pos < B.size() && B[pos].x <= x){
| ~~~~^~~~~~~~~~
golf.cpp:163:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | while(pos < B.size() && B[pos].x >= x){
| ~~~~^~~~~~~~~~
golf.cpp:169:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
169 | for(int i = 0; i < tmp.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:176:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
176 | for(int i = 0; i < one.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:182:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
182 | for(int i = 0; i < two.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:191:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
191 | for(int j = 0; j < two.size(); ++j){
| ~~^~~~~~~~~~~~
golf.cpp:203:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
203 | for(int j = 0; j < one.size(); ++j){
| ~~^~~~~~~~~~~~
golf.cpp:216:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
216 | for(int i = 0; i < one.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp:224:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<holder>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
224 | for(int i = 0; i < two.size(); ++i){
| ~~^~~~~~~~~~~~
golf.cpp: At global scope:
golf.cpp:233:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
233 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
41560 KB |
Output is correct |
2 |
Correct |
5 ms |
41564 KB |
Output is correct |
3 |
Incorrect |
5 ms |
41564 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
41560 KB |
Output is correct |
2 |
Correct |
5 ms |
41564 KB |
Output is correct |
3 |
Incorrect |
5 ms |
41564 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
41560 KB |
Output is correct |
2 |
Correct |
5 ms |
41564 KB |
Output is correct |
3 |
Incorrect |
5 ms |
41564 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |