Powerful array CodeForces - 86D(莫队)

给你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\n", Node[i].res);

    return 0;
}

原文地址:https://www.cnblogs.com/WTSRUVF/p/9347681.html

时间: 2024-08-30 11:03:31

Powerful array CodeForces - 86D(莫队)的相关文章

Powerful array CodeForces - 86D (莫队算法)

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 th

CodeForces - 86D 莫队算法

http://codeforces.com/problemset/problem/86/D 莫队算法就是调整查询的顺序,然后暴力求解. 每回可以通过现有区间解ans(l,r)得到区间(l+1,r),(l-1,r),(l,r+1),(l,r-1)的区间解. 调整方式http://blog.csdn.net/bossup/article/details/39236275 这题比那个还要简单,查询的是K^2*Z,很清楚就是莫队算法,然而做的时候没有学过,回来补题补到 关键是我一直没明白为什么重载小于号

XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和

CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! =  j)维护一个前缀异或值就可以了.要注意的是 区间[l, r], 我们需要将pre[l-1]......pre[r]都加进去, pre[l-1]不能少. #include<bits/stdc++.h> using namespace std; #define maxn 1234567 #define l

D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array 典型的莫队算法题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include

codeforces 86D D. 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, wh

CodeForces - 86D Powerful array (莫队)

题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[pos]*cnt[a[pos]]*cnt[a[pos]],将cnt[a[pos]]+1,再将乘积加回.其实根据数学原理,K^2和(K+1)^2差值是2K+1,那么其实每次更新时只要加上或减去a[pos]*(2*cnt[pos]+1)即可,这样更高效. #include<bits/stdc++.h>

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?

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】

任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Bob has a favorite number k and ai of length n. Now he asks yo

codeforces 617 E. XOR and Favorite Number(莫队算法)

题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, r] 使得 al xor al+1 xor ··· ar 为 k. 题解: 本题只有区间查询没有区间修改,而且数据量不大(10w),所以可以用离线的方法解决. 使用莫队算法来解决,就需要O(1)的修改[L, R+1] .[L, R-1].[L+1, R].[L-1, R]. 详细的莫队可以百度学一