結果
| 問題 |
No.1864 Shortest Paths Counting
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-15 13:00:40 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 283 ms / 2,000 ms |
| コード長 | 7,513 bytes |
| コンパイル時間 | 1,837 ms |
| コンパイル使用メモリ | 137,932 KB |
| 最終ジャッジ日時 | 2025-02-24 08:42:45 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 23 |
ソースコード
/*
tenkei90-036
*/
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cassert>
#include<random>
#include<set>
#include<map>
#include<cassert>
#include<unordered_map>
#include<bitset>
#include<numeric>
#include<algorithm>
using namespace std;
using ll = long long;
const int inf=1<<30;
const ll INF=1LL<<62;
typedef pair<ll,ll> P;
typedef pair<int,P> PP;
const ll MOD=998244353;
const int dy[]={0,1,0,-1};
const int dx[]={1,0,-1,0};
const int mod = 998244353;
struct mint {
long long x; // typedef long long long long;
mint(long long x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}//後ろのconstはメンバxを変更しないことを示す
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
bool operator==(const mint a) const {return a.x==x;}
mint pow(unsigned long long int t) const {
assert(t>=0);
if (!t) return 1;
//aがtで割り切れない場合には t%=(mod-1)をして良い
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
std::istream& operator>>(std::istream& is, mint& a) { return is >> a.x;}
std::ostream& operator<<(std::ostream& os, const mint& a) { return os << a.x;}
struct combination {
std::vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
};
//座標圧縮
template <class T> struct compress{
std::set<T> st;
std::map<T,int> mp;
int sz;
compress(const std::vector<T>& a):sz(0){
for(auto x:a){
st.insert(x);
}
for(auto x:st){
mp[x]=sz++;
}
}
int val(const T& x){
//ベクトルaの要素xに対応した要素を取り出す
return mp[x];
}
int size(){
return sz;
}
};
mint op(mint x, mint y){
//(x,y)を比較する任意の演算子.今回はmaxとした
return (x+y);
}
mint e(){
//initialize value
return 0;
}
template<class Type,
Type (*op)(Type,Type),
Type (*e)()
> class segment_tree{
public:
std::vector<Type> dat;
int n=1;
segment_tree(int n_){
while(n < n_){
n*=2;
}
dat = std::vector<Type>(2*n-1,e());// 0,1,2,...,2*n-1,2*n-2
}
~segment_tree(){
std::vector<Type>().swap(dat);
}
void set(int k,Type a){
update(k,a);
}
void update(int k,Type a){
k+=n-1;
dat[k] = a;
while(k>0){
k = (k-1)/2;
dat[k]=op(dat[2*k+1],dat[2*k+2]);
}
}
//[a,b)
Type query(int a,int b,int k,int l,int r){
if(r<=a || b<=l) return e();
if(a<=l && r<=b) return dat[k];
else{
Type vl = query(a,b,2*k+1,l,(l+r)/2);
Type vr = query(a,b,2*k+2,(l+r)/2,r);
return op(vl,vr);
}
}
//[a,b)の範囲でのmax
Type query(int a,int b){
return query(a,b,0,0,n);
}
Type operator[](int index){
return get(index);
}
Type get(int index){
index += n-1;
return dat[index];
}
void add(int index,Type val){
update(index,op(get(index),val));
}
};
int main(){
int N;
cin>>N;
vector<ll> X(N),Y(N);
vector<P> XY(N);
for(int i=0;i<N;i++){
cin>>X[i]>>Y[i];
XY[i].first = (X[i]+Y[i]);
XY[i].second = (X[i]-Y[i]);
}
if(XY[0].first>XY[N-1].first){
for(int i=0;i<N;i++){
//x軸に対称移動
XY[i].first*=(-1);
}
}
//XY[0].first<=XY[N-1].first
if(XY[0].second>XY[N-1].second){
for(int i=0;i<N;i++){
//y軸に対称移動
XY[i].second*=(-1);
}
}
//XY[0].second<=XY[N-1].second
ll d=abs(XY[N-1].first-XY[0].first)+abs(XY[N-1].second-XY[0].second);
vector<P> points;
for(int i=0;i<N;i++){
if(XY[0].first<=XY[i].first && XY[i].first <=XY[N-1].first && XY[0].second<=XY[i].second && XY[i].second<=XY[N-1].second){
points.push_back({XY[i].first+d,XY[i].second});
}
}
int sz=points.size();
sort(points.begin(),points.end());//X座標の小さい順にみる
// {
// //O(N^2)の解法
// XY=points;
// vector<mint> dp(sz);
// dp[0]=1;
// for(int i=0;i<sz;i++){
// for(int j=0;j<sz;j++){
// //jからiにいく
// if(i==j) continue;
// if(XY[j].first<=XY[i].first && XY[j].second<=XY[i].second){
// dp[i]+=dp[j];
// }
// }
// }
// cout<<dp[sz-1]<<endl;
// }
{
XY=points;
vector<ll> py;
for(auto [_,y]:XY){
py.push_back(y);
}
compress<ll> cp(py);
for(int i=0;i<sz;i++){
XY[i].second = cp.val(XY[i].second);
}
vector<mint> dp(sz+5);
segment_tree<mint,op,e> seg(sz+5);
dp[0]=1;
seg.set(XY[0].second,dp[0]);
for(int i=1;i<sz;i++){
dp[i]=seg.query(0,XY[i].second+1);
seg.add(XY[i].second,dp[i]);
}
cout<<dp[sz-1]<<endl;
}
// vector<int> ord(N);
// iota(ord.begin(),ord.end(),0);
// sort(ord.begin(),ord.end(),
// [&](const int lhs,const int rhs){
// if(XY[lhs].first==XY[rhs].first) return XY[lhs].second<XY[rhs].second;
// return XY[lhs].first<XY[rhs].first;
// });//X座標が小さい方 -> Y座標が小さい方
// vector<ll> y(N);
// for(int i=0;i<N;i++){
// y[i]=XY[i].second;
// }
// compress<ll> cp(y);
// for(int i=0;i<N;i++){
// XY[i].second = cp.val(XY[i].second);
// }
// map<int,vector<int>> mp;
// for(int i=0;i<N;i++){
// mp[XY[ord[i]].second].push_back(ord[i]);
// }
// vector<mint> dp(N+1,0);
// dp[ord[0]]=1;
// segment_tree<mint,op,e> seg(N+10);
// seg.set(XY[ord[0]].second,dp[ord[0]]);//0番目のy座標にdp[0]を入れる
// for( auto [_,vec]:mp){
// //[0,XY[ord[i]].second)までの
// mint sum=0;
// for(auto idx:vec){
// sum += seg.query(0,XY[idx].second);
// }
// for(auto idx:vec){
// dp[idx] = sum;
// seg.add(XY[idx].second,dp[idx]);
// }
// }
// mint ans=dp[ord[N-1]];
// cout<<ans<<endl;
}