zoukankan      html  css  js  c++  java
  • D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;

    D. Powerful array
    time limit per test
    5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output
    
    An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of products Ks·Ks·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.
    
    You should calculate the power of t given subarrays.
    Input
    
    First line contains two integers n and t (1 ≤ n, t ≤ 200000) — the array length and the number of queries correspondingly.
    
    Second line contains n positive integers ai (1 ≤ ai ≤ 106) — the elements of the array.
    
    Next t lines contain two positive integers l, r (1 ≤ l ≤ r ≤ n) each — the indices of the left and the right ends of the corresponding subarray.
    Output
    
    Output t lines, the i-th line of the output should contain single positive integer — the power of the i-th query subarray.
    
    Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).
    Examples
    Input
    
    3 2
    1 2 1
    1 2
    1 3
    
    Output
    
    3
    6
    
    Input
    
    8 3
    1 1 2 2 1 3 1 1
    2 7
    1 6
    2 7
    
    Output
    
    20
    20
    20
    
    Note
    
    Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):
    Then K1 = 3, K2 = 2, K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.
    
    
    /**
    题目:D. Powerful array
    链接:http://codeforces.com/problemset/problem/86/D
    题意:给定n个数,m次查询;每次查询[l,r]的权值;
    权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x;
    所有贡献和即为该区间的值;
    思路:由于静态区间,所以离线+莫队算法;   然后(cnt+1)*(cnt+1)*x-cnt*cnt*x=(2*cnt+1)*x;
    来优化计算;常规暴力计算超时;
    
    */
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<string>
    #include<queue>
    #include<bitset>
    using namespace std;
    typedef long long ll;
    const int inf = 0x3f3f3f3f;
    const double eps=1e-8;
    const int maxn = 2e5+100;
    const ll mod = 1e9+7;
    ll num[1000005], c[maxn], pos[maxn];
    ll ans;
    int n , m;
    struct node
    {
        ll l, r;
        ll ans;
        int id;
    
    }t[maxn];
    int cmp_id(node a,node b)
    {
        return a.id<b.id;
    }
    int cmp(node a,node b)
    {
        if(pos[a.l]==pos[b.l]) return a.r<b.r;
        return pos[a.l]<pos[b.l];
    }
    void update(int place,int add)
    {
        ll v = c[place];
        if(add==1){
            ans += v*(2*num[v]+1);
            num[v]++;
        }else
        {
            num[v]--;
            ans -= v*(2*num[v]+1);
        }
    }
    void solve()
    {
        memset(num, 0, sizeof num);
        ans = 0;
        for(int i = t[1].l; i <= t[1].r; i++){
            update(i,1);
        }
        t[1].ans = ans;
        for(int i = 2; i <= m; i++){
            for(int j = t[i-1].l; j < t[i].l; j++) update(j,-1);//减得时候,自身开始;
            for(int j = t[i-1].l; j > t[i].l; j--) update(j-1,1);//增的时候,不包括自身;
            for(int j = t[i-1].r; j < t[i].r; j++) update(j+1,1);
            for(int j = t[i-1].r; j > t[i].r; j--) update(j,-1);
            t[i].ans = ans;
        }
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)==2)
        {
            for(int i = 1; i <= n; i++) scanf("%I64d",&c[i]);
    
            for(int i = 1; i <= m; i++){
                scanf("%I64d%I64d",&t[i].l,&t[i].r);
                t[i].id = i;
            }
    
            int N = int(sqrt(n));
            for(int i = 1; i <= n; i++){
                pos[i] = i/N+1;
            }
    
            sort(t+1,t+1+m,cmp);
            solve();
            sort(t+1,t+1+m,cmp_id);
            for(int i = 1; i <= m; i++){
                printf("%I64d
    ",t[i].ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    一本通1018
    并查集&MST
    hdu 1875 畅通工程再续
    hdu 1811 Rank of Tetris(拓扑排序+并查集)
    hdu 1325 is it a tree?
    hdu1285拓扑排序
    hdu2063 过山车(最大二分匹配)
    最小生成树二·Kruscal算法
    hiho一下 第二十一周(线段树 离散化)
    hiho一下 第二十周(线段树模板)
  • 原文地址:https://www.cnblogs.com/xiaochaoqun/p/6845631.html
Copyright © 2011-2022 走看看