QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#502581 | #7752. The Only Way to the Destination | inksamurai | WA | 0ms | 3768kb | C++23 | 10.0kb | 2024-08-03 09:53:04 | 2024-08-03 09:53:05 |
Judging History
answer
#include <bits/stdc++.h>
// cut here
namespace atcoder {
namespace internal {
// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
} // namespace internal
} // namespace atcoder
namespace atcoder {
template <class S,
S (*op)(S, S),
S (*e)(),
class F,
S (*mapping)(F, S),
F (*composition)(F, F),
F (*id)()>
struct lazy_segtree {
public:
lazy_segtree() : lazy_segtree(0) {}
lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
log = internal::ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
lz = std::vector<F>(size, id());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
void set(int p, S x) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
S get(int p) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
return d[p];
}
S prod(int l, int r) {
assert(0 <= l && l <= r && r <= _n);
if (l == r) return e();
l += size;
r += size;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push(r >> i);
}
S sml = e(), smr = e();
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() { return d[1]; }
void apply(int p, F f) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
d[p] = mapping(f, d[p]);
for (int i = 1; i <= log; i++) update(p >> i);
}
void apply(int l, int r, F f) {
assert(0 <= l && l <= r && r <= _n);
if (l == r) return;
l += size;
r += size;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push((r - 1) >> i);
}
{
int l2 = l, r2 = r;
while (l < r) {
if (l & 1) all_apply(l++, f);
if (r & 1) all_apply(--r, f);
l >>= 1;
r >>= 1;
}
l = l2;
r = r2;
}
for (int i = 1; i <= log; i++) {
if (((l >> i) << i) != l) update(l >> i);
if (((r >> i) << i) != r) update((r - 1) >> i);
}
}
template <bool (*g)(S)> int max_right(int l) {
return max_right(l, [](S x) { return g(x); });
}
template <class G> int max_right(int l, G g) {
assert(0 <= l && l <= _n);
assert(g(e()));
if (l == _n) return _n;
l += size;
for (int i = log; i >= 1; i--) push(l >> i);
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!g(op(sm, d[l]))) {
while (l < size) {
push(l);
l = (2 * l);
if (g(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
template <bool (*g)(S)> int min_left(int r) {
return min_left(r, [](S x) { return g(x); });
}
template <class G> int min_left(int r, G g) {
assert(0 <= r && r <= _n);
assert(g(e()));
if (r == 0) return 0;
r += size;
for (int i = log; i >= 1; i--) push((r - 1) >> i);
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!g(op(d[r], sm))) {
while (r < size) {
push(r);
r = (2 * r + 1);
if (g(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
std::vector<S> d;
std::vector<F> lz;
void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
void all_apply(int k, F f) {
d[k] = mapping(f, d[k]);
if (k < size) lz[k] = composition(f, lz[k]);
}
void push(int k) {
all_apply(2 * k, lz[k]);
all_apply(2 * k + 1, lz[k]);
lz[k] = id();
}
};
} // namespace atcoder
// cut here
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) ((int) a.size())
#define all(a) a.begin(), a.end()
#define vec(...) vector<__VA_ARGS__>
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
#define unique(a) sort(all(a)); a.erase(unique(all(a)),a.end());
#define yare cout<<"YES\n"; exit(0);
#define nare cout<<"NO\n"; exit(0);
int op(int l,int r){
return l+r;
}
int e(){
return 0;
}
int mapping(int l,int r){
return l+r;
}
int composition(int l,int r){
return l+r;
}
int id(){
return 0;
}
void slv(){
int n,m,q;
cin>>n>>m>>q;
map<int,vec(pii)> mp;
rep(i,q){
int l,r,y;
cin>>l>>r>>y;
l-=1,r-=1,y-=1;
mp[y].pb({l,r});
}
auto ok=[&](int x,int y)->bool{
return min(x,y)>=0 and x<n and y<m;
};
auto is_black=[&](int x,int y)->bool{
auto it=lower_bound(all(mp[y]),pii{x+1,-1});
if(it==mp[y].begin()){
return 0;
}
it=prev(it);
if((*it).se>=x){
return 1;
}
return 0;
};
vi tmp;
vec(pii) pts;
auto push=[&](int x,int y){
if(!ok(x,y)) return;
if(is_black(x,y)){
return;
}
tmp.pb(x);
tmp.pb(y);
pts.pb({x,y});
};
for(auto p:mp){
int y=p.fi;
rng(ad,-1,2){
if(y+ad>=0 and y+ad<m){
tmp.pb(y+ad);
}
}
for(auto [l,r]:p.se){
push(r+1,y);
push(r,y-1);
push(r,y+1);
push(r+1,y-1);
push(r+1,y+1);
push(l-1,y);
push(l-1,y-1);
push(l-1,y+1);
push(l,y+1);
push(l,y-1);
}
}
unique(tmp);
unique(pts);
const int si=sz(pts);
auto check=[&](int x,int y){
if(!ok(x,y)) return;
bool pok=1;
rep(di,2){
rep(dj,2){
int nx=x+di;
int ny=y+dj;
if(!ok(nx,ny)) return;
if(is_black(nx,ny)){
pok=0;
}
}
}
if(pok){
nare;
}
};
for(auto p:pts){
rng(di,-1,2){
rng(dj,-1,2){
check(p.fi+di,p.se+dj);
}
}
}
map<int,vi> interesting_x,interesting_y;
rep(i,si){
interesting_x[pts[i].fi].pb(pts[i].se);
interesting_y[pts[i].se].pb(pts[i].fi);
}
map<pii,int> ids;
rep(i,si){
ids[pts[i]]=i;
}
vec(vi) adj(si);
auto add_edge=[&](int x,int y,int nx,int ny){
int u=ids[{x,y}];
int v=ids[{nx,ny}];
adj[u].pb(v);
adj[v].pb(u);
};
auto ask_y=[&](int y,int l,int r){
if(l>r){
swap(l,r);
}
auto it=lower_bound(all(mp[y]),pii{l,-1});
if(it==mp[y].end() or (*it).fi>r){
add_edge(l,y,r,y);
}
};
rep(i,si){
auto [x,y]=pts[i];
// chems magla da chems dabla
int pos=lower_bound(all(interesting_y[y]),x)-interesting_y[y].begin();
// if(pos-1>=0){
// ask_y(y,x,interesting_y[y][pos-1]);
// }
if(pos+1<sz(interesting_y[y])){
ask_y(y,x,interesting_y[y][pos+1]);
}
}
int qry=0;
vec(array<int,4>) qrys;
vec(vec(array<int,3>)) adqry(sz(tmp));
auto ask_x=[&](int x,int l,int r){
if(l>r){
swap(l,r);
}
int idx=lower_bound(all(tmp),x)-tmp.begin();
int id=lower_bound(all(tmp),r)-tmp.begin();
adqry[id].pb({1,idx,sz(qrys)});
id=lower_bound(all(tmp),l)-tmp.begin();
adqry[id].pb({0,idx,sz(qrys)});
qrys.pb({x,l,x,r});
};
rep(i,si){
auto [x,y]=pts[i];
// chems marcxniv da chems marjvniv
int pos=lower_bound(all(interesting_x[x]),y)-interesting_x[x].begin();
// if(pos-1>=0){
// ask_x(x,y,interesting_x[x][pos-1]);
// }
if(pos+1<sz(interesting_x[x])){
ask_x(x,y,interesting_x[x][pos+1]);
}
}
for(auto p:mp){
int id=lower_bound(all(tmp),p.fi)-tmp.begin();
for(auto [l,r]:p.se){
int idl=lower_bound(all(tmp),l)-tmp.begin();
int idr=lower_bound(all(tmp),r)-tmp.begin();
adqry[id].pb({2,idl,idr});
}
}
vi cnt(sz(qrys));
atcoder::lazy_segtree<int,op,e,int,mapping,composition,id> seg(sz(tmp));
per(i,sz(tmp)){
for(auto ar:adqry[i]){
if(ar[0]==2){
seg.apply(ar[1],ar[2]+1,1);
}else if(ar[0]==1){
cnt[ar[2]]-=seg.get(ar[1]);
}else{
cnt[ar[2]]+=seg.get(ar[1]);
}
}
}
rep(i,sz(qrys)){
if(cnt[i]==0){
add_edge(qrys[i][0],qrys[i][1],qrys[i][2],qrys[i][3]);
}
}
rep(i,si){
unique(adj[i]);
}
// if(si){
// vi usd(si);
// auto dfs=[&](auto self,int v,int par)->void{
// usd[v]=1;
// for(auto u:adj[v]){
// if(u==par) continue;
// if(usd[u]){
// nare;
// }
// self(self,u,v);
// }
// };
// dfs(dfs,0,-1);
// }
yare;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0);
slv();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3628kb
input:
5 3 2 2 5 1 1 4 3
output:
YES
result:
ok answer is YES
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3768kb
input:
5 3 1 2 4 2
output:
YES
result:
wrong answer expected NO, found YES