NBUT 1457 莫队算法 离散化
Description
Sona can't speak but she can make fancy music. Her music can attack, heal, encourage and enchant.
There're an ancient score(乐谱). But because it's too long, Sona can't play it in a short moment. So Sona decide to just play a part of it and revise it.
A score is composed of notes. There are109 kinds of notes and a score has 105 notes at most.
To diversify Sona's own score, she have to select several parts of it. The energy of each part is calculated like that:
Count the number of times that each notes appear. Sum each of the number of times' cube together. And the sum is the energy.
You should help Sona to calculate out the energy of each part.
Input
The first line of each case is an integer N (1 ≤ N ≤ 10^5), indicates the number of notes.
Then N numbers followed. Each number is a kind of note. (1 ≤ NOTE ≤ 10^9)
Next line is an integer Q (1 ≤ Q ≤ 10^5), indicates the number of parts.
Next Q parts followed. Each part contains 2 integers Li and Ri, indicates the left side of the part and the right side of the part.
Output
Sample Input
8
1 1 3 1 3 1 3 3
4
1 8
3 8
5 6
5 5
Sample Output
72
2
1
/*
* Author: sweat123
* Created Time: 2016/7/15 8:25:26
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int l,r,id;
}q[MAXN];
int a[MAXN],n,m,pos[MAXN],b[MAXN],k,p[MAXN];
ll ret,ans[MAXN];
int mp[MAXN];
bool cmp(node a,node b){
if(pos[a.l] == pos[b.l])return a.r < b.r;
return pos[a.l] < pos[b.l];
}
ll power(int x){
return 1LL * x * x * x;
}
int getkey(int x){
int l,r,m,ans;
l = ,r = k - ;
while(l <= r){
m = (l + r) >> ;
if(b[m] == x)return m;
else if(b[m] > x) r = m - ;
else l = m + ;
}
}
void init(){
sort(b+,b+n+);
k = ;
for(int i = ; i <= n; i++){
if(b[i] != b[i-]){
b[k++] = b[i];
}
}
for(int i = ; i <= n; i++){
int tp = getkey(a[i]);
p[i] = tp;
}
}
void updata(int x,int val){
ret -= power(mp[p[x]]);
mp[p[x]] += val;
ret += power(mp[p[x]]);
}
int main(){
while(~scanf("%d",&n)){
int tp = (int)ceil(sqrt(n * 1.0));
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
b[i] = a[i];
pos[i] = (i - ) / tp;
}
init();
memset(mp,,sizeof(mp));
scanf("%d",&m);
for(int i = ; i <= m; i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id = i;
}
sort(q+,q+m+,cmp);
int pl,pr;
pl = ;
pr = ;
ret = ;
for(int i = ; i <= m; i++){
int id = q[i].id;
if(q[i].l == q[i].r){
ans[id] = ;
continue;
} else {
if(pr <= q[i].r){
for(int j = pr + ; j <= q[i].r; j++){
updata(j,);
}
} else{
for(int j = pr; j > q[i].r; j--){
updata(j,-);
}
}
pr = q[i].r;
if(pl < q[i].l){
for(int j = pl; j < q[i].l; j++){
updata(j,-);
}
} else{
for(int j = pl - ; j >= q[i].l; j--){
updata(j,);
}
}
pl = q[i].l;
ans[id] = ret;
}
}
for(int i = ; i <= m; i++){
printf("%I64d\n",ans[i]);
}
}
return ;
}
NBUT 1457 莫队算法 离散化的更多相关文章
- NBUT 1457 Sona(莫队算法+离散化)
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...
- HDU 4358 莫队算法+dfs序+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)T ...
- 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...
- CodeForces - 220B 离散化+莫队算法
莫队算法链接:传送门 题意: 有n个数,m个区间.问区间内有多少个x,x满足x的个数等于x的值的个数(如果x是3,区间内要存在3个3). 题解: 因为a[i]太大,所以要离散化一下,但是不能用map容 ...
- HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)
链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...
- BZOJ3289 Mato的文件管理(莫队算法+树状数组)
题目是区间逆序数查询. 莫队算法..左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了.询问个数和数字个数都记为n,数字范围不确定所以离散化,这 ...
- CSU 1515 Sequence (莫队算法)
题意:给n个数,m个询问.每个询问是一个区间,求区间内差的绝对值为1的数对数. 题解:先离散化,然后莫队算法.莫队是离线算法,先按按询问左端点排序,在按右端点排序. ps:第一次写莫队,表示挺简单的, ...
- 【莫队算法】【权值分块】bzoj3920 Yuuna的礼物
[算法一] 暴力. 可以通过第0.1号测试点. 预计得分:20分. [算法二] 经典问题:区间众数,数据范围也不是很大,因此我们可以: ①分块,离散化,预处理出: <1>前i块中x出现的次 ...
- 【bzoj4542】[Hnoi2016]大数 莫队算法
题目描述 给出一个数字串,多次询问一段区间有多少个子区间对应的数为P的倍数.其中P为质数. 输入 第一行一个整数:P.第二行一个串:S.第三行一个整数:M.接下来M行,每行两个整数 fr,to,表示对 ...
随机推荐
- spring源码:核心组件(li)
一.AOP实现 Spring代理对象的产生:代理的目的是调用目标方法时我们可以转而执行InvocationHandler类的invoke方法,所以如何在InvocationHandler上做文章就是S ...
- redis的安装配置
主要讲下redis的安装配置,以及以服务的方式启动redis 1.下载最新版本的redis-3.0.7 到http://redis.io/download中下载最新版的redis-3.0.7 下载后 ...
- javascript代码 调试方法
你的代码可能包含语法错误,逻辑错误,如果没有调试工具,这些错误比较难于发现. 通常,如果 JavaScript 出现错误,是不会有提示信息,这样你就无法找到代码错误的位置. 在程序代码中寻找错误叫做代 ...
- 关于 Xcode8打印JSON的时候,NSLog控制台显示不完整
看到打印结果之后 为什么只打印了一部分???为什么呢??? 我也不知道为什么,可能是xcode8的原因. 借鉴了下国外人说了,printf可以打印全,自己写了个输出宏 #ifdef DEBUG #d ...
- showSetPwdDialog--自定义对话框
样式: 布局: layout dialog_set_pwd.xml <?xml version="1.0" encoding="utf-8"?> & ...
- 软件工程随笔(1)--jetbrain在软件工程中的应用
接下来几天我要写半年的软件工程学习后的感想,今天从介绍IDE开始.首先,本人至今为止全部项目都是在mypclise上完成的.本人采用myeclipse唯一的原因就是它使用方便.但是,我也承认myecl ...
- java编码原理,java编码和解码问题
java的编码方式原理 java的JVM的缺省编码方式由系统的“本地语言环境”设置确定,和操作系统的类型无关 . 在JAVA源文件-->JAVAC-->Class-->Java--& ...
- 高级数据过滤(like)
单字符过滤 '_' select * from T_Employee where FName like '_erry' 多字符过滤 '%' select * from T_Employee wher ...
- Spring profile配置应用
spring配置文件中可以配置多套不同环境配置,如下: <beans xml.....> <beans profile="dev"> < ...
- Oracle数据库的 增、删、改、查
有时候数据库的查询语句一时想不起来,或不确定是不是语句写的正确,现在整理了一下标准的基本查询语句,便于以后牢记: .数据操作语言 DML:添加(insert into).修改(update set ...