zoukankan      html  css  js  c++  java
  • CF 86D 莫队(卡常数)

    CF 86D 莫队(卡常数)

    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.

    题意: 
    一个数列,问[L,R]区间内(每个数字的个数的平方*数字的大小)的和。 

    思路: 
    莫队模板。 

    离线+分块

    将n个数分成sqrt(n)块。

    对所有询问进行排序,排序标准:

          1. Q[i].left /block_size < Q[j].left / block_size (块号优先排序)

          2. 如果1相同,则 Q[i].right < Q[j].right (按照查询的右边界排序)

    问题求解:

    从上一个查询后的结果推出当前查询的结果。(这个看程序中query的部分)

    如果一个数已经出现了x次,那么需要累加(2*x+1)*a[i],因为(x+1)^2*a[i] = (x^2 +2*x + 1)*a[i],x^2*a[i]是出现x次的结果,(x+1)^2 * a[i]是出现x+1次的结果。

    时间复杂度分析:

    排完序后,对于相邻的两个查询,left值之间的差最大为sqrt(n),则相邻两个查询左端点移动的次数<=sqrt(n),总共有t个查询,则复杂度为O(t*sqrt(n))。

    又对于相同块内的查询,right端点单调上升,每一块所有操作,右端点最多移动O(n)次,总块数位sqrt(n),则复杂度为O(sqrt(n)*n)。

    right和left的复杂度是独立的,因此总的时间复杂度为O(t*sqrt(n)  +  n*sqrt(n))。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cmath>
     6 using namespace std;
     7 #define N 200100
     8 typedef long long ll;
     9 ll a[N], cnt[N*5], ans[N], res;
    10 int L, R;
    11 
    12 struct node {
    13     int x, y, l, p;
    14 } q[N];
    15 bool cmp(const node &x, const node &y) {
    16     if (x.l == y.l) return x.y < y.y;
    17     return x.l < y.l;
    18 }
    19 void query(int x, int y, int flag) {
    20     if (flag) {
    21         for (int i=x; i<L; i++) {
    22             res += ((cnt[a[i]]<<1)+1)*a[i];
    23             cnt[a[i]]++;
    24         }
    25         for (int i=L; i<x; i++) {
    26             cnt[a[i]]--;
    27             res -= ((cnt[a[i]]<<1)+1)*a[i];
    28         }
    29         for (int i=y+1; i<=R; i++) {
    30             cnt[a[i]]--;
    31             res -= ((cnt[a[i]]<<1)+1)*a[i];
    32         }
    33         for (int i=R+1; i<=y; i++) {
    34             res += ((cnt[a[i]]<<1)+1)*a[i];
    35             cnt[a[i]]++;
    36         }
    37 
    38     } else {
    39         for (int i=x; i<=y; i++) {
    40             res += ((cnt[a[i]]<<1)+1)*a[i];
    41             cnt[a[i]]++;
    42         }
    43     }
    44     L = x, R = y;
    45 }
    46 int main() {
    47     int n, t;
    48 
    49     scanf("%d%d", &n, &t);
    50     for (int i=1; i<=n; i++) scanf("%I64d", &a[i]);
    51     int block_size = sqrt(n);
    52 
    53     for (int i=0; i<t; i++) {
    54         scanf("%d%d", &q[i].x, &q[i].y);
    55         q[i].l = q[i].x / block_size;
    56         q[i].p = i;
    57     }
    58 
    59     sort(q, q+t, cmp);
    60 
    61 
    62     memset(cnt, 0, sizeof(cnt));
    63 
    64     res = 0;
    65     for (int i=0; i<t; i++) {
    66         query(q[i].x, q[i].y, i);
    67         ans[q[i].p] = res;
    68     }
    69 
    70     for (int i=0; i<t; i++) printf("%I64d
    ", ans[i]);
    71 
    72     return 0;
    73 }
  • 相关阅读:
    禁止select下拉框的其中某个选择项不能被选择
    jQuery 增加 删除 修改select option
    jquery ajax后台向前台传list 前台用jquery $.each遍历list
    转载 java枚举类型enum的使用 (原文地址:http://blog.csdn.net/wgw335363240/article/details/6359614)
    C#性能优化实践 资料整理
    MySql 优化 网上资料
    第06组 Alpha冲刺(4/6)
    第06组 Alpha冲刺(3/6)
    第06组 Alpha冲刺(2/6)
    第06组 Alpha冲刺(1/6)
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/8185412.html
Copyright © 2011-2022 走看看