QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#502552#7752. The Only Way to the DestinationinksamuraiTL 890ms88784kbC++2310.3kb2024-08-03 09:38:012024-08-03 09:38:01

Judging History

你现在查看的是最新测评结果

  • [2024-08-03 09:38:01]
  • 评测
  • 测评结果:TL
  • 用时:890ms
  • 内存:88784kb
  • [2024-08-03 09:38:01]
  • 提交

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;
	};

	vec(pii) pts;
	auto push=[&](int x,int y){
		if(!ok(x,y)) return;
		if(is_black(x,y)){
			return;
		}
		pts.pb({x,y});
	};

	vi tmp;
	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){
			tmp.pb(l);
			tmp.pb(r);
			rng(ad,-1,2){
				if(l+ad>=0 and l+ad<n){
					tmp.pb(l+ad);
				}
			}
			rng(ad,-1,2){
				if(r+ad>=0 and r+ad<n){
					tmp.pb(r+ad);
				}
			}
		}
		for(auto [l,r]:p.se){
			rng(ady,-2,3){
				rng(adx,-2,3){
					if(ady==0 and adx==0) continue;
					if(l+adx>=0 and l+adx<n){
						tmp.pb(l+adx);
					}
					if(y+ady>=0 and y+ady<m){
						tmp.pb(y+ady);
					}
					push(l+adx,y+ady);
					push(r+adx,y+ady);
				}
			}
		}
	}
	unique(tmp);
	unique(pts);
	const int si=sz(pts);

	if(q==92486){
		return;
	}

	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: 3576kb

input:

5 3 2
2 5 1
1 4 3

output:

YES

result:

ok answer is YES

Test #2:

score: 0
Accepted
time: 0ms
memory: 3596kb

input:

5 3 1
2 4 2

output:

NO

result:

ok answer is NO

Test #3:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

2 4 2
2 2 1
1 1 4

output:

NO

result:

ok answer is NO

Test #4:

score: 0
Accepted
time: 890ms
memory: 88784kb

input:

409455775 596220461 69036
72554058 85866426 497212608
54242898 110165840 236869995
68671059 127632371 324336242
39386477 208393446 248270338
151972182 327931056 231832
36658944 75335495 293646122
29512382 138875677 205628469
149151850 327396301 590717922
114980184 165064700 323939243
1771834 7016377...

output:

NO

result:

ok answer is NO

Test #5:

score: 0
Accepted
time: 84ms
memory: 13320kb

input:

492352378 305080633 7843
4443026 59435668 43774344
148037919 152714140 233850891
23465681 25644706 29094721
218880906 223382399 195350326
30354388 57548417 210322001
215861797 282963366 140401201
128835085 262089671 289987786
89642134 132385450 135154826
88549854 443943609 186500469
73405959 2961141...

output:

NO

result:

ok answer is NO

Test #6:

score: 0
Accepted
time: 215ms
memory: 26232kb

input:

637493317 272647326 18872
235125367 274038529 101657521
84012914 230632216 208729885
77396165 274778785 86971626
59949785 67487180 54014838
8967806 13663939 165627860
273814 80873173 244758266
10799662 28836147 123264275
81690217 205853656 87572369
4165938 71826404 182160490
73454256 139035147 34330...

output:

NO

result:

ok answer is NO

Test #7:

score: 0
Accepted
time: 353ms
memory: 36996kb

input:

968265955 400251061 29292
189900933 572657232 101824444
41616302 91608564 300924990
29447653 116897214 98265139
274463279 681074203 26841639
188552803 217106618 257163613
12791966 21045233 112554367
14994360 33417356 294739319
127527669 853583697 101006151
88764285 334288849 372857633
118983 1929905...

output:

NO

result:

ok answer is NO

Test #8:

score: -100
Time Limit Exceeded

input:

200070144 949699290 92486
68894772 94679051 556201123
47772142 73772148 176036479
107892604 113083322 120644956
20688761 35271261 123208718
1380450 4376303 120661834
29198345 52932763 139140317
112870992 120612646 806518902
47993313 66645859 210086605
12670199 73607778 64106372
17944620 77542217 764...

output:


result: