zoukankan      html  css  js  c++  java
  • CodeForces 86D Powerful array(莫队+优化)

    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 productsKs·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 lr (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 = 3K2 = 2K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.

    题目链接:CF 86D

    用自定义函数来排序会超时的一道题超时N次看了大牛的博客发现把块写到结构体里面速度比较快;把add和del函数写到while里面也可以加快速度,再加上位运算会快一点(注意一下优先级)

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    #include<sstream>
    #include<cstring>
    #include<bitset>
    #include<cstdio>
    #include<string>
    #include<deque>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define CLR(x,y) memset(x,y,sizeof(x))
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    typedef pair<int,int> pii;
    typedef long long LL;
    const double PI=acos(-1.0);
    const int N=200010;
    const int M=1000010;
    LL cnt[M];
    int arr[N];
    LL ans[N];
    int unit;
    struct info
    {
    	int l,r;
    	int d;
    	int b;
    	bool operator<(const info &t)const
    	{
    		if(b==t.b)
    			return r<t.r;
    		return b<t.b;
    	}
    };
    info node[N];
    int main(void)
    {
    	int n,m,i,j;
    	scanf("%d%d",&n,&m);
    	unit=(int)sqrt(n+0.5);
    	for (i=1; i<=n; ++i)
    		scanf("%d",&arr[i]);
    	for (i=0; i<m; ++i)
    	{
    		scanf("%d%d",&node[i].l,&node[i].r);
    		node[i].d=i;
    		node[i].b=node[i].l/unit;
    	}
    	sort(node,node+m);
    	int L=node[0].l;
    	int R=L-1;
    	LL power=0;
    	for (i=0; i<m; ++i)
    	{
    		while (L>node[i].l)
    		{
    			--L;
    			power=power+arr[L]*((cnt[arr[L]]<<1)+1);
    			++cnt[arr[L]];
    		}
    		while (L<node[i].l)
    		{
    			--cnt[arr[L]];
    			power=power-arr[L]*((cnt[arr[L]]<<1)+1);			
    			++L;
    		}
    		while (R>node[i].r)
    		{
    			--cnt[arr[R]];
    			power=power-arr[R]*((cnt[arr[R]]<<1)+1);			
    			--R;
    		}	
    		while (R<node[i].r)
    		{
    			++R;
    			power=power+arr[R]*((cnt[arr[R]]<<1)+1);
    			++cnt[arr[R]];
    		}
    		ans[node[i].d]=power;
    	}
    	for (i=0; i<m; ++i)
    		printf("%I64d
    ",ans[i]);
    	return 0;
    }
  • 相关阅读:
    《楞严经四种清净明诲》 (转自学佛网:http://www.xuefo.net/nr/article56/559965.html)
    忏悔言情小说带来的意淫以及对治方法 (转自学佛网:http://www.xuefo.net/nr/article55/554935.html)
    一个80后妈妈的邪淫忏悔(转自学佛网:http://www.xuefo.net/nr/article55/551761.html)
    邪淫真正的可怕危害 (转自学佛网:http://www.xuefo.net/nr/article54/544414.html)
    净空法师详解《般若波罗蜜多心经》,转给有缘人
    净空法师主讲:净业三福【全2集】
    《阿含经》的思想
    佛教最早时期的根本经典 《阿含经》为何如此重要?
    弘一法师妙解《心经》
    读唯识三十颂讲话日记 原创: 李美君 那兰陀广场
  • 原文地址:https://www.cnblogs.com/Blackops/p/5792404.html
Copyright © 2011-2022 走看看