Mod segma problem

https://atcoder.jp/contests/abc378/tasks/abc378_e

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

#define int long long
#define lowbit(x) (x&(-x))
#define pii pair<int, int>
#define mkp make_pair

const int N = 2e5 + 10, mod = 998244353;

int n,pre[N],s[N],t[N],m;
void upd(int x,int k){
    if(x==0)return;
    while(x<N){
        t[x]+=k;
        x+=lowbit(x);
    }
}
inline int que(int x){
    int res=0;
    while(x>0){
        res+=t[x];
        x-=lowbit(x);
    }
    return res;
}
void solve()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int x;cin>>x;pre[i]=(pre[i-1]+x)%m;
    }
    for(int i=1;i<=n;i++)s[i]=s[i-1]+pre[i];
    int ans=0;
    for(int i=1;i<=n;i++){
        ans+=(pre[i]*i-s[i-1]+(que(N-1)-que(pre[i]))*m);
        upd(pre[i],1);
    }
    cout<<ans<<endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    T = 1;
    while (T--)
        solve();
}
/*
1
3
1 2 3
1 2 3
2 3 1
*/
posted @ 2024-12-13 18:37  lyrrr  阅读(1)  评论(0编辑  收藏  举报