結果
| 問題 |
No.1675 Strange Minimum Query
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2021-09-10 21:29:12 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 196 ms / 2,000 ms |
| コード長 | 3,547 bytes |
| コンパイル時間 | 2,068 ms |
| コンパイル使用メモリ | 201,632 KB |
| 最終ジャッジ日時 | 2025-01-24 09:42:00 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
const char newl = '\n';
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
vector<T> ts(n);
for(size_t i=0;i<n;i++) cin>>ts[i];
return ts;
}
namespace ushi{
template <typename T>
struct SegmentTree{
using F = function<T(T,T)>;
Int n;
F f;
T ti;
vector<T> dat;
SegmentTree(F f,T ti):f(f),ti(ti){}
void init(Int n_){
n=1;
while(n<n_) n<<=1;
dat.assign(n<<1,ti);
}
void build(const vector<T> &v){
Int n_=v.size();
init(n_);
for(Int i=0;i<n_;i++) dat[n+i]=v[i];
for(Int i=n-1;i;i--)
dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
}
void set_val(Int k,T x){
dat[k+=n]=x;
while(k>>=1)
dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
}
T query(Int a,Int b){
if(a>=b) return ti;
T vl=ti,vr=ti;
for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
if(l&1) vl=f(vl,dat[l++]);
if(r&1) vr=f(dat[--r],vr);
}
return f(vl,vr);
}
template<typename C>
Int max_right(Int s,C &check,T &acc,Int k,Int l,Int r){
if(l+1==r){
acc=f(acc,dat[k]);
return check(acc)?-1:k-n;
}
Int m=(l+r)>>1;
if(m<=s) return max_right(s,check,acc,(k<<1)|1,m,r);
if(s<=l and check(f(acc,dat[k]))){
acc=f(acc,dat[k]);
return -1;
}
Int vl=max_right(s,check,acc,(k<<1)|0,l,m);
if(~vl) return vl;
return max_right(s,check,acc,(k<<1)|1,m,r);
}
// max t s.t. check(query(s,t))
// O(\log N)
template<typename C>
Int max_right(Int s,C &check){
assert(s<n and check(ti) and not check(query(s,n)));
T acc=ti;
return max_right(s,check,acc,1,0,n);
}
};
}
namespace dual{
template <typename E>
struct SegmentTree{
using H = function<E(E,E)>;
Int n,height;
H h;
E ei;
vector<E> laz;
SegmentTree(H h,E ei):h(h),ei(ei){}
void init(Int n_){
n=1;height=0;
while(n<n_) n<<=1,height++;
laz.assign(2*n,ei);
}
inline void propagate(Int k){
if(laz[k]==ei) return;
laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
laz[k]=ei;
}
inline void thrust(Int k){
for(Int i=height;i;i--) propagate(k>>i);
}
void update(Int a,Int b,E x){
if(a>=b) return;
thrust(a+=n);
thrust(b+=n-1);
for(Int l=a,r=b+1;l<r;l>>=1,r>>=1){
if(l&1) laz[l]=h(laz[l],x),l++;
if(r&1) --r,laz[r]=h(laz[r],x);
}
}
E get_val(Int a){
thrust(a+=n);
return laz[a];
}
void set_val(Int a,E x){
thrust(a+=n);
laz[a]=x;
}
};
}
template<typename T>
void space(const vector<T> &vs){
for(size_t i=0;i<vs.size();i++){
if(i) cout<<' ';
cout<<vs[i];
}
cout<<'\n';
}
//INSERT ABOVE HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
Int n,q;
cin>>n>>q;
vector<Int> ls(q),rs(q),bs(q);
for(Int i=0;i<q;i++) cin>>ls[i]>>rs[i]>>bs[i],ls[i]--;
auto f=[&](Int a,Int b){return min(a,b);};
auto h=[&](Int a,Int b){return max(a,b);};
vector<Int> as(n,1);
{
dual::SegmentTree<Int> seg(h,1);
seg.init(n);
for(Int i=0;i<q;i++){
seg.update(ls[i],rs[i],bs[i]);
}
for(Int i=0;i<n;i++) as[i]=seg.get_val(i);
}
{
ushi::SegmentTree<Int> seg(f,1e9+7);
seg.build(as);
for(Int i=0;i<q;i++)
if(seg.query(ls[i],rs[i])!=bs[i]) drop(-1);
}
space(as);
return 0;
}
beet