結果

問題 No.2197 Same Dish
ユーザー umezoumezo
提出日時 2023-01-21 04:11:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 2,465 bytes
コンパイル時間 2,340 ms
コンパイル使用メモリ 215,436 KB
実行使用メモリ 17,300 KB
最終ジャッジ日時 2023-09-05 19:54:24
合計ジャッジ時間 5,089 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
15,464 KB
testcase_01 AC 7 ms
15,948 KB
testcase_02 AC 6 ms
15,932 KB
testcase_03 AC 6 ms
15,532 KB
testcase_04 AC 6 ms
15,404 KB
testcase_05 AC 7 ms
15,528 KB
testcase_06 AC 5 ms
15,584 KB
testcase_07 AC 7 ms
15,468 KB
testcase_08 AC 5 ms
15,800 KB
testcase_09 AC 6 ms
15,452 KB
testcase_10 AC 7 ms
15,400 KB
testcase_11 AC 7 ms
15,400 KB
testcase_12 AC 6 ms
15,772 KB
testcase_13 AC 35 ms
15,716 KB
testcase_14 AC 179 ms
16,988 KB
testcase_15 AC 196 ms
17,100 KB
testcase_16 AC 134 ms
16,504 KB
testcase_17 AC 122 ms
16,508 KB
testcase_18 AC 199 ms
17,248 KB
testcase_19 AC 202 ms
17,180 KB
testcase_20 AC 199 ms
17,036 KB
testcase_21 AC 200 ms
17,028 KB
testcase_22 AC 91 ms
17,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

template <typename X,typename M>
struct SegTreeLazy {
  using FX=function<X(X,X)>;
  using FA=function<X(X,M)>;
  using FM=function<M(M,M)>;
  int n;
  FX fx;
  FA fa;
  FM fm;
  const X ex;
  const M em;
  vector<X> dat;
  vector<M> lazy;
  SegTreeLazy(int n_,FX fx_,FA fa_,FM fm_,X ex_,M em_)
    : n(),fx(fx_),fa(fa_),fm(fm_),ex(ex_),em(em_),dat(n_*4,ex),lazy(n_*4,em) {
    int x=1;
    while (n_ > x) x *= 2;
    n=x;
  }
  void set(int i,X x) { dat[i+n-1]=x;}
  void build() {
    for (int k=n-2;k >= 0;k--) dat[k]=fx(dat[2*k+1],dat[2*k+2]);
  }

  void eval(int k) {
    if(lazy[k] == em) return; 
    if(k<n-1) {           
      lazy[k*2+1]=fm(lazy[k*2+1],lazy[k]);
      lazy[k*2+2]=fm(lazy[k*2+2],lazy[k]);
    }
    dat[k]=fa(dat[k],lazy[k]);
    lazy[k]=em;
  }
  void update(int a,int b,M x,int k,int l,int r){
    eval(k);
    if(a<=l && r<=b){
      lazy[k]=fm(lazy[k],x);
      eval(k);
    }else if(a<r && l<b){      
      update(a,b,x,k*2+1,l,(l+r)/2); 
      update(a,b,x,k*2+2,(l+r)/2,r);
      dat[k]=fx(dat[k*2+1],dat[k*2+2]);
    }
  }
  void update(int a,int b,M x){update(a,b,x,0,0,n);}
  X query_sub(int a,int b,int k,int l,int r){
    eval(k);
    if(r<=a || b<=l){
      return ex;
    }else if(a <= l && r <= b){
      return dat[k];
    }else{
      X vl=query_sub(a,b,k*2+1,l,(l+r)/2);
      X vr=query_sub(a,b,k*2+2,(l+r)/2,r);
      return fx(vl,vr);
    }
  }
  X query(int a,int b){return query_sub(a,b,0,0,n);}
};

const int MOD=998244353;
ll modpow(ll x,ll n){
  x%=MOD;
  ll ans=1;
  while(n){
    if(n&1) ans=ans*x%MOD;
    x=x*x%MOD;
    n/=2;
  }
  return ans;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  ll n,k;
  cin>>n>>k;
  vector<int> L(n),R(n);
  rep(i,n) cin>>L[i]>>R[i];
  vector<pair<int,int>> P(n);
  rep(i,n) P[i]={L[i],R[i]};
  sort(ALL(P));
  
  using X=ll;
  using M=ll;
  auto fx=[](X x1,X x2) -> X { return max(x1,x2);};
  auto fa=[](X x,M m) -> X { return x+m;};
  auto fm=[](M m1,M m2) -> M { return m1+m2;};
  X ex=0;
  M em=0;
  SegTreeLazy<X,M> lseg(200200,fx,fa,fm,ex,em);
  
  ll tmp=k;
  rep(i,n){
    int l=P[i].first,r=P[i].second;
    if(i>0){
      auto t=lseg.query(l,r);
      if(t>=k) tmp=0;
      else tmp=tmp*(k-t)%MOD;
    }
    lseg.update(l,r,1);
  }
  cout<<(modpow(k,n)-tmp+MOD)%MOD<<endl;

  return 0;
}
0