結果

問題 No.2197 Same Dish
ユーザー umezoumezo
提出日時 2023-01-21 04:00:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,368 bytes
コンパイル時間 2,485 ms
コンパイル使用メモリ 209,488 KB
実行使用メモリ 10,264 KB
最終ジャッジ日時 2023-09-05 19:47:28
合計ジャッジ時間 5,321 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,276 KB
testcase_01 AC 5 ms
9,288 KB
testcase_02 AC 5 ms
9,400 KB
testcase_03 AC 5 ms
9,380 KB
testcase_04 WA -
testcase_05 AC 5 ms
9,380 KB
testcase_06 AC 5 ms
9,376 KB
testcase_07 AC 5 ms
9,404 KB
testcase_08 WA -
testcase_09 AC 5 ms
9,316 KB
testcase_10 WA -
testcase_11 AC 5 ms
9,332 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 220 ms
10,264 KB
testcase_22 AC 99 ms
10,016 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];
  
  using X=int;
  using M=int;
  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){
    if(i>0){
      auto t=lseg.query(L[i],R[i]);
      if(t>=k) tmp=0;
      else tmp=tmp*(k-t)%MOD;
    }
    lseg.update(L[i],R[i],1);
  }
  cout<<(modpow(k,n)-tmp+MOD)%MOD<<endl;

  return 0;
}
0