• Powerful array CodeForces


    给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和。1<=n,m<=200000.1<=s<=10^6

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 200050, INF = 0x7fffffff;
    int n, m, pos[maxn], s[maxn*10], c[maxn];
    LL ans;
    
    struct node
    {
        int l, r, id;
        LL res;
    }Node[maxn];
    
    bool cmp(node a, node b)
    {
        return pos[a.l] == pos[b.l] ? (a.r < b.r) : (a.l < b.l);
    }
    
    bool cmp_id(node a, node b)
    {
        return a.id < b.id;
    }
    
    
    
    void add(LL x)
    {
        s[c[x]]++;
        ans += c[x]*(s[c[x]]*s[c[x]] - (s[c[x]]-1)*(s[c[x]]-1));
    }
    
    void del(LL x)
    {
        s[c[x]]--;
        ans -= c[x]*((s[c[x]]+1)*(s[c[x]]+1) - (s[c[x]])*(s[c[x]]));
    }
    
    
    int main()
    {
        ans = 0;
        scanf("%d%d", &n, &m);
        for(int i=1; i<=n; i++)
            scanf("%d", &c[i]);
        int block = sqrt(n);
        for(int i=1; i<=n; i++)
            pos[i] = (i-1)/block + 1;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d", &Node[i].l, &Node[i].r);
            Node[i].id = i;
        }
        sort(Node+1, Node+1+m, cmp);
        for(int i=1, l=1, r=0; i<=m; i++)
        {
            for(; r < Node[i].r; r++)
                add(r+1);
            for(; r > Node[i].r; r--)
                del(r);
            for(; l < Node[i].l; l++)
                del(l);
            for(; l > Node[i].l; l--)
                add(l-1);
            Node[i].res = ans;
        }
        sort(Node+1, Node+1+m, cmp_id);
        for(int i=1; i<=m; i++)
            printf("%I64d
    ", Node[i].res);
    
    
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    Python学习——初识类与对象
    用Python代码实现贪心问题:最优合并问题、程序存储问题、最优服务次序问题
    爬虫解析库:XPath
    python基础学习之 模块导入
    python 数据结构
    深入理解vue中的slot与slot-scope
    BeanFactory和FactoryBean的区别简介
    Spring Boot 使用Caffeine缓存
    冒泡算法和递归算法demo
    AJPFX总结方法的特点
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9347681.html
Copyright © 2020-2023  润新知