给你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. 北美KubeCon新风,正把K8S魔力带向边缘计算

    作者:DJ 审校:Kevin·Wang 1. 容器生态圈新的创新方向 2018年容器技术圈的年终盛典北美KubeCon终于在西雅图落下了帷幕.这次北美KubeCon总共吸引了8000多观众参会,创下历 ...

  2. JUC——延迟队列

    所谓的延迟队列最大的特征是它可以自动通过队列进行脱离,例如:现在有一些对象被临时保存着,但是有可能该集合对象是一个公共对象,那么里面的某些数据如果不在使用的时候就希望其可以在指定的时间达到后自动的消失 ...

  3. JUC——线程同步锁(ReentrantLock)

    ReentrantLock简介 ReentrantLock是一个可重复的互斥锁,又被称为独占锁,可重入的意思是:ReentrantLock锁可以被单个线程多次获取.但是在同一个时间点只能被一个线程锁持 ...

  4. JDK 和 JRE 有什么区别

    JDK是Java开发工具包(Java Development Kit),JRE是Java运行环境(Java Runtime Environment),JDK包含了JRE,搭建Java环境的时候,安装J ...

  5. Python 3 利用机器学习模型 进行手写体数字检测

    0.引言 介绍了如何生成手写体数字的数据,提取特征,借助 sklearn 机器学习模型建模,进行识别手写体数字 1-9 模型的建立和测试. 用到的几种模型: 1. LR,Logistic Regres ...

  6. Linux 文件搜索命令:find、which、whereis 和 locate

    Linux 提供了许多用于文件搜索的命令,这些命令都很强大,但是也有一些不同之处,这里分别介绍一下. 一.find 命令 find 是最常见和最强大的一个文件搜索命令.使用 find 命令可以在指定目 ...

  7. 为什么使用React Native

    React Native使你能够在Javascript和React的基础上获得完全一致的开发体验,构建世界一流的原生APP. React Native着力于提高多平台开发的开发效率 —— 仅需学习一次 ...

  8. eos源码编译

    编译源码 运行代码 在阿里云 纽约服务器上运行没有出现任何问题. 在其他电脑上出现很多问题. 搜集到的问题如下: 随着EOSIO软件越来越成熟,后来的开发者也越来越幸福.EOS相关源码的编译和运行变得 ...

  9. Linux虚拟机安装教程

    必备组件: vmware(程序主题) 链接:https://pan.baidu.com/s/14OplOGOQTVAnf0iDqgDhDQ 提取码:jape centos(Linux系统) 链接:ht ...

  10. PHP 包含文件

    1.require test123.php <?php $a=1; 运行文件: <?php require('test123.php'); echo 'Hello!'; echo '< ...