7.20 【数据结构】综合测试1

· · 个人记录

测试情况

T1 求m区间内的最小值 T2 [NICA #1] 序列 T3 [USACO23JAN] Air Cownditioning II B T4 【模板】康托展开
\color{#E74C3C}0 \color{#F39C11}40 \color{#E74C3C}0 \color{#E74C3C}0
我是fvv

测题总结

-P1440 求m区间内的最小值-

错误原因

未使用数量=(x-1)-1\sim x-1中已使用的数字个数

问题转化为:需要一个数据结构,支持

则本题变成标准的树状数组题目,用树状数组来实现即可

Code

50pts
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=998244353,LOG=60;
int n,ans;
int a[N],f[N];
bool vis[N];
signed main()
{
    cin>>n;
    f[0]=1;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        f[i]=i*f[i-1];
        f[i]%=mod; 
    }
    for(int i=1;i<=n;i++)
    {
        int tmp=0;
        for(int j=1;j<=a[i]-1;j++)
        {
            tmp+=(!vis[j]);  
        }
        ans+=(tmp)*(f[n-i]);
        ans%=mod; 
        vis[a[i]]=1;
    }
    cout<<ans+1;
    return 0;
}
100pts
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=998244353,LOG=60;
int n,ans;
int a[N],f[N];
bool vis[N];
struct Tree
{
    int c[N];
    int lowbit(int x)
    {
        return (x&(-x));
    }
    int query(int x)
    {
        int res=0;
        while(x)
        {
            res+=c[x];
            x-=lowbit(x);
        }
        return res;
    }
    void modify(int x,int y)
    {
        while(x<=n)
        {
            c[x]+=y;
            x+=lowbit(x);
        }
    }
}tr1;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n;
    f[0]=1;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        f[i]=i*f[i-1];
        f[i]%=mod;
    }
    for(int i=1;i<=n;i++)
    {
        int tmp=0;
        tmp=a[i]-1-tr1.query(a[i]-1);
        ans+=(tmp)*(f[n-i]);
        ans%=mod;
        tr1.modify(a[i],1);
    }
    cout<<ans+1;
    return 0;
}