给你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 = , INF = 0x7fffffff;
int n, m, pos[maxn], s[maxn*], 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]]-)*(s[c[x]]-));
} void del(LL x)
{
s[c[x]]--;
ans -= c[x]*((s[c[x]]+)*(s[c[x]]+) - (s[c[x]])*(s[c[x]]));
} int main()
{
ans = ;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++)
scanf("%d", &c[i]);
int block = sqrt(n);
for(int i=; i<=n; i++)
pos[i] = (i-)/block + ;
for(int i=; i<=m; i++)
{
scanf("%d%d", &Node[i].l, &Node[i].r);
Node[i].id = i;
}
sort(Node+, Node++m, cmp);
for(int i=, l=, r=; i<=m; i++)
{
for(; r < Node[i].r; r++)
add(r+);
for(; r > Node[i].r; r--)
del(r);
for(; l < Node[i].l; l++)
del(l);
for(; l > Node[i].l; l--)
add(l-);
Node[i].res = ans;
}
sort(Node+, Node++m, cmp_id);
for(int i=; i<=m; i++)
printf("%I64d\n", Node[i].res); return ;
}

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

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

    An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...

  2. Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)

    vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...

  3. CF 86D 莫队(卡常数)

    CF 86D 莫队(卡常数) D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes i ...

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

    CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! =  j) ...

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

    莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...

  6. Codeforces 86D Powerful array (莫队算法)

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

  7. Codeforces 86D Powerful array (莫队)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  8. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  9. Codeforces 86D - Powerful array(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...

随机推荐

  1. 12只超酷机器人,在家用3D打印搞定!

    3D打印最吸引人的地方在于它完全无极限!现在的3D打印已经广范应用在我们的生活.以及工业上的各个领域.最棒的是,DIY玩家可以真正从中受益.我们现在已经可以应用3D打印,在家制作自己的机器人了.如果你 ...

  2. 【日常训练】数据中心(CSP 201812-4)

    分析 题目实际上是在要在给定的边上构建出一个树,使得这个树的最长边尽可能小. 这实际上是最小生成树的性质(反证法).问题从而得到解决. 代码 /* * Code name => csp20181 ...

  3. 传递JSON数据有没有必要用RequestBody?

    在使用SpringMVC的时候自己一直避免使用RequestBody,因为觉的它在参数处理的时候不够方便.理由如下:1.不使用RequestBody时是这样的:前端参数可以直接使用JSON对象: // ...

  4. 自动分配ip的方法- 【Linux】

    1.  查看本机无线网络使用的网卡 2.  设置vbox的网络连接为桥接,并选择本机无线网络对应的网卡 3.  进入系统,输入ifconfig命令,记录下系统的HWaddr 4.  修改系统ip配置文 ...

  5. python虚拟环境管理之virtualenv,virtualenvwrapper,pipenv,conda

    虚拟环境的作用 使python环境拥有独立的包,避免污染原本的python环境.为不同的项目创建不同的环境可以避免安装的库过于庞大和相互干扰. 例如你想在同一台机器上开发用python2和python ...

  6. 六大iT公司的组织结构

  7. Django_rest_framework_基础

    简介 为什么要使用REST framework? Django REST framework 是一个强大且灵活的工具包,用以构建Web APIs. - 在线可视的API,对于赢得你的开发者们十分有用  ...

  8. 图解Raid5数据存储的原理

  9. sprint3(第一天)

    1.今天计划了sprint3要做的内容: 整合前台和后台,然后发布让用户使用,然后给我们反馈再进行改进 2.backlog表格: ID Name Est How to demo 1 实现用户登录与权限 ...

  10. 在虚拟机下安装Ubuntu

    目录: 1.安装虚拟机 2.在虚拟下安装Ubuntu 本文将按照目录分两步来讲一下在虚拟机下安装Ubuntu.第一步是安装虚拟机,第二步是在虚拟机下安装Ubuntu. 安装虚拟机 下载虚拟机链接以及激 ...