• Scalar Queries CodeForces


    You are given an array $a_1,a_2,…,a_n$. All $a_i$ are pairwise distinct.

    Let's define function $f(l,r)$ as follows:

    • let's define array $b_1,b_2,…,b_{r-l+1}$, where $b_i=a_{l-1+i}$;
    • sort array $b$ in increasing order;
    • result of the function $f(l,r)$ is $sumlimits_{i=1}^{r-l+1}b_icdot i$.

    Calculate $Bigg(sumlimits_{1le lle rle n}f(l,r)Bigg )mod(10^9+7)$, i.e. total sum of $f$ for all subsegments of $a$ modulo $10^9+7$.

    可以得到$a_x$的贡献为

    $sumlimits_{substack{a_i<a_x\ i<x}} icdot (n-x+1)+sumlimits_{substack{a_i<a_x\ i>x}}xcdot (n-i+1)+xcdot (n-x+1)$

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #define REP(i,a,n) for(int i=a;i<=n;++i)
    #define PER(i,a,n) for(int i=n;i>=a;--i)
    using namespace std;
    typedef long long ll;
    
    const int N = 1e6+10, P = 1e9+7;
    int n, a[N], b[N];
    ll c[N], cnt[N];
    void add(int x, int v) {
    	for (; x<=n; x+=x&-x) c[x]+=v;
    }
    ll query(int x) {
    	ll r = 0;
    	for (; x; x^=x&-x) r+=c[x];
    	return r%P;
    }
    
    int main() {
    	scanf("%d", &n);
    	REP(i,1,n) scanf("%d",a+i),b[i]=a[i];
    	sort(b+1,b+1+n);
    	REP(i,1,n) a[i]=lower_bound(b+1,b+1+n,a[i])-b;
    	REP(i,1,n) {
    		cnt[i] += query(a[i])*(n-i+1)%P;
    		add(a[i], i);
    	}
    	REP(i,1,n) cnt[i] += (ll)i*(n-i+1)%P, c[i] = 0;
    	PER(i,1,n) {
    		cnt[i] += query(a[i])*i%P;
    		add(a[i], (n-i+1));
    	}
    	ll ans = 0;
    	REP(i,1,n) ans+=cnt[i]*b[a[i]]%P;
    	printf("%lld
    ", ans%P);
    }
    
  • 相关阅读:
    关于makefile
    代理服务器简要
    配置ftp服务
    Nginx服务器的Web请求处理机制
    Nginx模块
    linux的开机网络设置
    mysql数据库优化的常见方法
    笔试题【二维矩阵操作,文件存取】
    水仙花数的求解
    9x9乘法表输出[Java]
  • 原文地址:https://www.cnblogs.com/uid001/p/10882397.html
Copyright © 2020-2023  润新知