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

     

    题意:

      给你一个序列an 。t次询问,问在[L, R] 的区间里面的值是多少。公式是:ai的个数的平方 * ai 的求和。具体看Note。

    题解:

      这一题用莫队算法。如果做过小Z的袜子的话,这一题很简单,小Z的袜子是 ai个数的平方求和,这里就乘多了一个ai。

      巨坑:num[] 数组要用int 开,用long long开的话会被卡TLE。(很绝望,被卡了10次罚时,最后用输入挂才过的)

     

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <string>
      5 #include <algorithm>
      6 #include <cmath>
      7 #include <vector>
      8 #include <queue>
      9 #include <map>
     10 #include <stack>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 #define ms(a, b) memset(a, b, sizeof(a))
     15 #define pb push_back
     16 #define mp make_pair
     17 const LL INF = 0x7fffffff;
     18 const int inf = 0x3f3f3f3f;
     19 const int mod = 1e9+7;
     20 const int maxn = 1000000+10;
     21 
     22 template<class T>
     23 inline bool scan_d(T &ret){
     24     char c;int sgn;
     25     if(c=getchar(), c==EOF) return 0;
     26     while(c!='-'&&(c<'0'||c>'9'))   c = getchar();
     27     sgn=(c=='-')?-1:1;
     28     ret=(c=='-')?0:(c-'0');
     29     while(c=getchar(), c>='0'&&c<='9')  ret = ret*10+(c-'0');
     30     ret*=sgn;
     31     return 1;
     32 }
     33 inline void out(LL x)
     34 {
     35     if(x>9) out(x/10);
     36     putchar(x%10+'0');
     37 }
     38 
     39 int a[maxn];
     40 int unit, n, t;
     41 LL ans[maxn];
     42 int num[maxn];
     43 
     44 
     45 
     46 struct node
     47 {
     48     int l, r, id;
     49 }que[maxn];
     50 void init() {
     51     ms(num, 0);
     52 }
     53 bool cmp(node x1, node x2)
     54 {
     55     if(x1.l/unit != x2.l/unit){
     56         return x1.l/unit < x2.l/unit;
     57     }
     58     else{
     59         return x1.r < x2.r;
     60     }
     61 }
     62 void work()
     63 {
     64     sort(que, que+t, cmp);
     65     LL temp = 0;
     66     int l, r;
     67     l = 0, r = 0;
     68     for(int i =0;i<t;i++){
     69         while(r<que[i].r){
     70             r++;
     71             temp -= 1LL*num[a[r]]*num[a[r]]*a[r];
     72             num[a[r]]++;
     73             temp += 1LL*num[a[r]]*num[a[r]]*a[r];
     74         }
     75         while(r>que[i].r){
     76             temp -= 1LL*num[a[r]]*num[a[r]]*a[r];
     77             num[a[r]]--;
     78             temp += 1LL*num[a[r]]*num[a[r]]*a[r];
     79             r--;
     80         }
     81         while(l>que[i].l){
     82             l--;
     83             temp -= 1LL*num[a[l]]*num[a[l]]*a[l];
     84             num[a[l]]++;
     85             temp += 1LL*num[a[l]]*num[a[l]]*a[l];
     86         }
     87         while(l<que[i].l){
     88             temp -= 1LL*num[a[l]]*num[a[l]]*a[l];
     89             num[a[l]]--;
     90             temp += 1LL*num[a[l]]*num[a[l]]*a[l];
     91             l++;
     92         }
     93         ans[que[i].id] = temp;
     94     }
     95 }
     96 void solve() {
     97 //    scanf("%d%d", &n, &t);
     98     scan_d(n);scan_d(t);
     99     for(int i = 1;i<=n;i++)
    100         scan_d(a[i]);
    101     unit = (int)sqrt(n);
    102     for(int i = 0;i<t;i++){
    103         que[i].id = i;
    104 //        scanf("%d%d", &que[i].l, &que[i].r);
    105         scan_d(que[i].l);
    106         scan_d(que[i].r);
    107     }
    108     work();
    109     for(int i = 0;i<t;i++){
    110 //        cout << ans[i] << endl;
    111 //        printf("%I64d
    ", ans[i]);
    112         out(ans[i]);
    113         puts("");
    114     }
    115 }
    116 int main() {
    117 #ifdef LOCAL
    118     freopen("input.txt", "r", stdin);
    119 //        freopen("output.txt", "w", stdout);
    120 #endif
    121 //    ios::sync_with_stdio(0);
    122 //    cin.tie(0);
    123     init();
    124     solve();
    125     return 0;
    126 }
    View Code
  • 相关阅读:
    UEmacs/PK Keybindings
    ubunut install golang
    vyos ipsec l2tp simple experiment
    Lynx 命令行操作
    Linux开启路由转发功能
    PROTEUS的元器件及模型制作
    5G模拟网自动化测试的需求和挑战
    vyos site-to-site ipsec simple experiment
    kubeadm 搭建 k8s 时用到的常用命令汇总
    html 特殊符号标记
  • 原文地址:https://www.cnblogs.com/denghaiquan/p/7226432.html
Copyright © 2011-2022 走看看