zoukankan      html  css  js  c++  java
  • 区间求和

    基准时间限制:1 秒 空间限制:131072 KB 分值: 80
    LYK在研究一个有趣的东西。
    假如有一个长度为n的序列,那么这个序列的权值将是所有有序二元组i,j的 Σajai 其中1<=i<j<=n。
    但是这个问题似乎太简单了。
    于是LYK想在所有有序二元组k,l中若ak=al其中1<=k<l<=n,则将 a{k},a{k+1},...,a{l}  提出当做一个序列,计算它的权值。
    并统计所有这样的区间的权值和。
    由于答案可能很大,你只需要将答案对2^32取模即可。
    建议使用读入优化。
    Input
    第一行一个整数n(1<=n<=1000000),接下来一行n个数ai(1<=ai<=1000000)表示LYK的序列。
    Output
    一行表示答案。
    Input示例
    5
    3 4 5 5 3
    Output示例
    2
    分析:区间[l,r]对x(l<=x<=r)的贡献次数为2*x-l-r;
       所以维护前缀和,后缀和,然后对每个数算贡献即可;
       注意取模2^32等价于unsigned long long的溢出;

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define intxt freopen("in.txt","r",stdin)
    const int maxn=1e6+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll 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*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,a[maxn];
    ll suml[maxn],sumr[maxn],numl[maxn],numr[maxn],pos[maxn],pre[maxn],f[maxn],fl[maxn],fr[maxn];
    unsigned ll ans;
    int main()
    {
        int i,j;
        scanf("%d",&n);
        rep(i,1,n)a[i]=read();
        rep(i,1,n)
        {
            numl[i]=numl[pos[a[i]]]+1;
            suml[i]=suml[pos[a[i]]]+i;
            pos[a[i]]=i;
        }
        memset(pos,0,sizeof(pos));
        for(i=n;i>=1;i--)
        {
            numr[i]=numr[pos[a[i]]]+1;
            sumr[i]=sumr[pos[a[i]]]+i;
            pos[a[i]]=i;
        }
        rep(i,1,n)
        {
            fl[i]=fl[i-1];
            fl[i]+=i*numr[i];
            fl[i]-=suml[i-1];
            f[i]=f[i-1]+numr[i]-numl[i-1];
        }
        for(i=n;i>=1;i--)
        {
            fr[i]=fr[i+1];
            fr[i]+=i*numl[i];
            fr[i]-=sumr[i+1];
            ans=ans+a[i]*(2*i*f[i]-fl[i]-fr[i]);
        }
        printf("%u
    ",ans);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    python的继承、封装、多态 --面向对象的特征
    ab压测工具简介
    dotnet core Console事件处理机制
    屹立千年,只为你一个回眸
    Derivative of the Sigmoid function
    Merge Overlapping Intervals
    Array of products
    Longest Peak
    javascript事件的注册方式总结
    纯css实现圆柱体-超简单!
  • 原文地址:https://www.cnblogs.com/dyzll/p/6128549.html
Copyright © 2011-2022 走看看