結果

問題 No.2875 What is My Rank?
ユーザー hongrock
提出日時 2024-09-06 23:16:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,329 bytes
コンパイル時間 2,100 ms
コンパイル使用メモリ 208,368 KB
最終ジャッジ日時 2025-02-24 04:56:29
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 WA * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define pb emplace_back
#define mp make_pair
 
using ll = long long;
using pii = pair<int,int>;
 
constexpr int mod = 998244353;
constexpr int inf = 0x3f3f3f3f;
constexpr int N = 2e5 + 10;

int n, l[N], r[N], m, b[N << 1];

ll pow_mod(ll x, ll p){
  ll s = 1;
  while(p){
    if(p & 1) s = s * x % mod;
    x = x * x % mod;
    p >>= 1;
  }
  return s;
}

void _main(){
  cin >> n;
  m = 0;
  unordered_map<int, vector<pii>> g;
  for(int i=1; i<=n; ++i){
    cin >> l[i] >> r[i];
    b[m++] = l[i];
    b[m++] = r[i] + 1;
    if(i > 1){
      g[l[i]].pb(r[i] - l[i] + 1, -1);
      g[r[i] + 1].pb(r[i] - l[i] + 1, 1);
    }
  }
  sort(b, b+m);
  m = unique(b, b+m) - b;
  ll ans = 0;
  ll S = 0;
  ll ext = 0;
  for(int i=m-1; i>=0; --i){
    if(i < m - 1){
      int L = b[i], R = b[i + 1];
      int len = R - L;
      ll q = 1ll * (len - 1) * len / 2 % mod;
      if(l[1] <= L && R - 1 <= r[1]){
        ans = (ans + ext + q * S) % mod;
      }
      ext = (ext + len * S % mod) % mod;
    }
    for(auto [len, f] : g[b[i]]){
      S = (S + pow_mod(len, mod - 2) * f % mod + mod) % mod;
    }
  }
  ll p = pow_mod(r[1] - l[1] + 1, mod - 2);
  cout << (ans % mod * p + 1) % mod << '\n';
}

int main(){
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  _main();
  return 0;
}
0