QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#743415#9631. Median Replacementpmt2018WA 2ms8000kbC++176.1kb2024-11-13 19:09:562024-11-13 19:10:04

Judging History

你现在查看的是最新测评结果

  • [2024-11-13 19:10:04]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:8000kb
  • [2024-11-13 19:09:56]
  • 提交

answer

/*
Problem : M. Median Replacement
Url : https://qoj.ac/contest/1840/problem/9631
Date : 2024/11/12 16:19:41
Solver : pmt2018
*/

#include<bits/stdc++.h>

#define y0 pmtx
#define y1 pmtxx
#define x0 pmtxxx
#define x1 pmtxxxx
#define pb push_back
#define mp make_pair
#define fi first 
#define se second
#define lowbit(x) (x&(-x))

#define DEBUG printf("Passing Line %d in function [%s].\n",__LINE__,__FUNCTION__)

using namespace std;

typedef pair<int ,int > pii;
typedef vector<int > vi;
typedef long long ll;
typedef unsigned long long ull;

namespace FastIO{
    const int SIZE=(1<<20);
    char in[SIZE],*inS=in,*inT=in+SIZE;
    inline char Getchar(){
        if(inS==inT){inT=(inS=in)+fread(in,1,SIZE,stdin);if(inS==inT)return EOF;}
        return *inS++;
    }
    char out[SIZE],*outS=out,*outT=out+SIZE;
    inline void Flush(){fwrite(out,1,outS-out,stdout);outS=out;}
    inline void Putchar(char c){*outS++=c;if(outS==outT)Flush();}
    struct NTR{~NTR(){Flush();}}ZTR;
}

#ifndef LOCAL
    #define getchar FastIO::Getchar
    #define putchar FastIO::Putchar 
#endif

template<typename T> inline void read(T &x){
    x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    x*=f;
}

inline void readstr(char* str){
    int len=0;char c=getchar();
    while(c==' '||c=='\n')c=getchar();
    while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();
    str[len] = '\0';
}

template<typename T>inline void write(T x){
    if(!x)putchar('0');
    if(x<0)x=-x,putchar('-');
    static int sta[20];int tot=0;
    while(x)sta[tot++]=x%10,x/=10;
    while(tot)putchar(sta[--tot]+48);
}

inline void writestr(char* str){int cur=0;while(str[cur])putchar(str[cur++]);}

const int maxn=200007,INF=0x3f3f3f3f;
const ll MOD=1'000'000'007;
const ll LINF=0x3f3f3f3f3f3f3f3fll;
const ll P=19260817;

template<typename T>inline void ckmax(T &x,T y){x=(y>x?y:x);}
template<typename T>inline void ckmin(T &x,T y){x=(y<x?y:x);}
template<typename T>inline T my_abs(T x){if(x<0)x=-x;return x;}
inline int mod1(int x){return x<MOD?x:x-MOD;}
inline int mod2(int x){return x<0?x+MOD:x;}
inline void add(int &x,int y){x=mod1(x+y);}
inline void sub(int &x,int y){x=mod2(x-y);}

int n;
int l[maxn], r[maxn];

int o[maxn<<1], tot;

int dp[maxn][4];
int res[maxn];

int ge(int l, int r, int x){
    if(l>=x)return r-l+1;
    if(x>r)return 0;
    return r-x+1;
}

int lt(int l, int r, int x){
    return r-l+1-ge(l,r,x);
}

int all = 1;

int frac[maxn], ifrac[maxn], inv[maxn];

int qpow(int a, int b) {
    int ret = 1;
    while(b) {
        if(b&1)ret=1ll*ret*a%MOD;
        b>>=1;
        a=1ll*a*a%MOD;
    }
    return ret;
}
void init(){
    int N = 200;
    frac[0] = 1;
    for (int i = 1; i <= N; i++) frac[i] = 1ll * frac[i-1] * i % MOD;
    ifrac[N] = qpow(frac[N], MOD - 2);
    for (int i = N; i >= 1; i--) {
        ifrac[i - 1] = 1ll * i * ifrac[i] % MOD;
        inv[i] = 1ll * ifrac[i] * frac[i - 1] % MOD;
    }
    // cout<<1ll*frac[N]*ifrac[N]%MOD<<endl;
}

int solve_dp(int x){
    for(int i = 1; i <= n; i++) dp[i][0]=dp[i][1]=dp[i][2]=0;
    
    dp[2][0b00] = 1ll * lt(l[1], r[1], x) * lt(l[2], r[2], x) % MOD; 
    dp[2][0b01] = 1ll * ge(l[1], r[1], x) * lt(l[2], r[2], x) % MOD; 
    dp[2][0b10] = 1ll * lt(l[1], r[1], x) * ge(l[2], r[2], x) % MOD; 
    // dp[2][0b11] = 1ll * lt(l[1], r[1], x) * lt(l[2], r[2], x) % MOD; 

    for(int i = 3; i <= n; i++){
        //0 00
        //1 00
        add(dp[i][0b00], 1ll * dp[i-1][0b00] * lt(l[i], r[i], x) % MOD);    // 0 00
        add(dp[i][0b00], 1ll * dp[i-1][0b01] * lt(l[i], r[i], x) % MOD);    // 0 01

        add(dp[i][0b01], 1ll * dp[i-1][0b10] * lt(l[i], r[i], x) % MOD);    // 0 10

        add(dp[i][0b10], 1ll * dp[i-1][0b00] * ge(l[i], r[i], x) % MOD);    // 1 00
    }

    return mod2(all - mod1(dp[n][0] + mod1(dp[n][1] + dp[n][2])));
}

// F(x) = sum_{i=1..N} res[i] * (prod (x - j)) / (prod (i - j))
// prod (x-j) = prod / (x - i)
// prod (i-j) = (i-1)! * (-1)^(n-i) * (N-i)!

//F(5) = 1 * 3 * 2 / -1 / -2 + 1 * 4 * 2 / 1 / -1 + 1 * 4 * 3 / 1 / 2

int Lagrange(int N, int x) {
    int prod = 1;
    for(int i = 1; i <= N; i++) 
        prod = 1ll * prod * (x - i) % MOD;
    int ans = 0;
    for(int i = 1; i <= N; i++) {
        // cout<<res[i]<<endl;
        int prodij = 1ll * frac[i-1] * frac[N - i] % MOD;
        if((N-i)&1) prodij = MOD - prodij;
        // cout<< res[i] <<' '
        //     << 1ll * prod % MOD * qpow(x - i, MOD - 2) % MOD <<' '
        //     << prodij <<endl;
        add(ans, 1ll * res[i] % MOD
                     * prod % MOD * qpow(x - i, MOD - 2) % MOD
                     * qpow(prodij, MOD - 2) % MOD);
    }
    return ans;
}

int solve_seg(int l, int r){ // [l,r)
    r--;
    int ans = 0;
    if(r - l <= 200){
        for(int i = l; i <= r; i++){
            // cout<<solve_dp(i)<<endl;
            add(ans, solve_dp(i));
        }
        return ans;
    }
    for(int i = 1; i <= n + 3; i++){
        res[i] = solve_dp(i + l - 1);
        // cout<<i<<' '<<i+l-1<<' '<<res[i]<<endl;
        add(res[i], res[i - 1]);
    }
    ans = Lagrange(n + 3, r - l + 1);
    return ans;
}

void solve() {
    read(n);
    o[++tot]=1;
    for(int i=1;i<=n;i++)read(l[i]), o[++tot]=l[i];
    for(int i=1;i<=n;i++)read(r[i]), o[++tot]=r[i];

    o[++tot]=1e9+1;
    sort(o+1,o+tot+1);
    tot = unique(o + 1, o + tot + 1) - o - 1;

    for(int i = 1; i <= n; i++) all = 1ll * all * (r[i] - l[i] + 1) % MOD;

    int ans = 0;
    for(int i = 1; i < tot; i++){
        // cout<<o[i]<<' '<<o[i+1]<<endl;
        add(ans, solve_seg(o[i], o[i+1]));
        // cout<<endl;
    }
    printf("%d\n", ans);
}

int main(){
#ifdef LOCAL
    // freopen(".in","r",stdin);
    // freopen(".in","w",stdout);
#else
    // freopen(".in","r",stdin);
    // freopen(".out","w",stdout);
#endif
    init();
    int T;
    read(T);
    while(T--)solve();
    return 0;
}

//things to remember
//1.long long
//2.array length
//3.freopen
//4 boarder case

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 8000kb

input:

10
5
5 1 4 3 2
14 2 5 3 2
5
4 5 1 2 3
13 7 1 2 3
5
5 2 5 3 1
10 2 12 3 2
5
5 5 3 1 5
57 5 3 1 5
5
2 2 3 3 5
4 5 4 4 5
5
4 5 3 5 3
13 7 3 5 3
5
5 1 4 2 3
14 3 4 2 3
5
1 2 5 4 5
2 8 5 7 5
5
1 1 3 5 1
8 2 3 8 1
5
4 4 4 2 3
5 10 5 2 3

output:

180
999991987
999194929
957261443
948518939
455552817
666573259
328084453
997361460
926103098

result:

wrong answer 2nd lines differ - expected: '170', found: '999991987'