Sona, Maven of the Strings. Of cause, she can play the zither.

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 are 109 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
This problem contains several cases. And this problem provides 2 seconds to run.
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
For each part, you should output the energy of that part.

Sample Input
8
1 1 3 1 3 1 3 3
4
1 8
3 8
5 6
5 5

Sample Output
128
72
2
1

题意: 给定N个数和m次查询,查询给定范围内不同种类颜色出现次数的立方和。

题解:莫队算法加离散化

代码如下

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
long long a[200010],b[200010],c[200010];
long long sum,k,d[200010];
struct z{int l,r,id,p;
} s[200010];
bool cmp(z a,z b)   //对查询进行排序
{if(a.p==b.p)return a.r<b.r;elsereturn a.p<b.p;
}
void add(int x,int y)  //随着范围移动数据加减
{if(x==0)return ;sum=sum-c[a[x]]*c[a[x]]*c[a[x]];  //减去原来的c[a[x]]=c[a[x]]+y;sum=sum+c[a[x]]*c[a[x]]*c[a[x]];  //加上现在的
}
int main()
{int n,m;while(~scanf("%d",&n)&&n){int i,j,l=0,r=0;for(i=1;i<=n;i++){scanf("%lld",&a[i]);d[i]=a[i];c[i]=0;}scanf("%d",&m);sort(d+1,d+1+n);k=unique(d+1,d+n+1)-(d+1);for(i=1;i<=n;i++)a[i]=lower_bound(d+1,d+1+k,a[i])-d;   //离散化进行优化 int x=sqrt(n);for(i=0;i<m;i++){scanf("%d%d",&s[i].l,&s[i].r);s[i].id=i;s[i].p=s[i].l/x;}sort(s,s+m,cmp);sum=0;for(i=0;i<m;i++)          //莫队 {while(l<s[i].l) add(l++,(-1));while(l>s[i].l) add(--l,1);while(r>s[i].r) add(r--,(-1));while(r<s[i].r) add(++r,1);b[s[i].id]=sum;}for(i=0;i<m;i++)printf("%lld\n",b[i]);}return 0;
}

Sona(莫队+离散化)相关推荐

  1. (NBUT - 1457)Sona(莫队+离散化)

    题目链接:Sona - NBUT 1457 - Virtual Judge (ppsucxtt.cn) 题意比较简单:有N个数,有M个询问,求每次询问的区间[L,R]中,每种数字出现次数的立方和. 在 ...

  2. NBUT 1457 Sona (莫队算法)

    [1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...

  3. NBUT 1457 Sona 莫队算法 分块处理

    [1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...

  4. NBUT1457 Sona 莫队算法

    由于10^9很大,所以先离散化一下,把给你的这一段数哈希 时间复杂度O(nlogn) 然后就是分块莫队 已知[L,R],由于事先的离散化,可以在O(1)的的时间更新[l+1,r],[l,r+1],[l ...

  5. 带修莫队 ---- 离散化 + 暴力 + 带修莫队 F. Machine Learning

    题目链接 题目大意: 给出nnn个数字,qqq个询问: 每次询问有两种类型,一种是询问区间,一种是单体修改: 询问区间是询问区间内最小的没用到的大于0的整数: 比如我有一串数字是 1 1 2 2 2 ...

  6. NBUT - 1457 Sona (莫队算法)

    点我看题 题意:求某段区间内数字出现次数的立方和. 分析:莫队的模板题,不得不说这题神坑,vj说好的操作系统为Linux呢???结果%I64d才能过== 当然这题不用输入挂其实也能过- #includ ...

  7. NBUT - 1457 Sona 莫队

    NBUT - 1457 Sona 题意: 计算一个区间内每种数出现次数的立方和,那么转移的话,假设一个区间是[L,R],如果向左转移,用ans保存区间的答案,cnt[i]记录第 i 种数出现的次数,那 ...

  8. HDU 4417 Super Mario(莫队 + 树状数组 + 离散化)

    Super Mario 思路 区间查找问题,容易想到离线莫队,确实这题就是莫队,接下来我们考虑如何维护区间高度值问题. 既然是离线嘛,我们容易想到离散化和他挂钩,想想这题是否需要离散化,高度的最大值是 ...

  9. 分治 —— 莫队算法

    [概述] 莫队算法(mo's algorithm)是用来解决离线区间不修改询问问题,可以将复杂度优化到 O(n^1.5),除去普通的莫队算法外,还有带修改的莫队.树上莫队等等. 莫队常用于维护区间答案 ...

最新文章

  1. Unique Paths II
  2. 黑入苹果、特斯拉竟如此容易!这位鬼才的攻击方法火了!
  3. Linux下MySql出现#1036 – Table ‘ ‘ is read only 错误解决方法
  4. Java程序员必看!java值类型和引用类型的区别
  5. [LOJ 6288]猫咪[CF 700E]Cool Slogans
  6. 如何进入、退出docker的container
  7. JavaScript高级程序设计58.pdf
  8. 如何创建sequence
  9. P4777-[模板]扩展中国剩余定理(EXCRT)
  10. LeetCode 103. 二叉树的锯齿形层次遍历(BFS / 双栈)
  11. GC算法-标记清除算法
  12. spring Boot报错 之五种(不打包运行)
  13. 互联网晚报 | 1月15日 星期六 | 娃哈哈董事长称准备6亿发年终奖;河南省消协对辛巴提起公益诉讼;支付宝上线消息“刷子”功能...
  14. Python os模块文件操作(一)
  15. 搜集访问网页历史记录软件_微软宣布OneDrive版本历史记录功能抵达Win10和macOS客户端版...
  16. Ext3.4.0中EditorGridPanel可以复制文本
  17. 暴雪插件占用服务器,你还在用这款插件吗?暴雪公布《魔兽世界》怀旧服卡顿原因...
  18. 超实用的 Python 技巧,异步操作数据库!
  19. python3卸载_python3卸载
  20. 玩转WSL 2(一)——WSL的介绍和安装

热门文章

  1. 机械员培训建筑八大员培训机械设备液压系统故障诊断技术现状
  2. html列表序号为圆点的,HTML列表
  3. 考研计算机学科专业基础综合考试大纲
  4. CI2451/CI2454/CSM2433灯控遥控方案国产8位RISC内核无线MCU单片机芯片
  5. Python 文件数据批量录入数据库(MongoDB)
  6. 【历史上的今天】8 月 20 日:传奇程序员诞生日;谷歌发布 Pixel 4a
  7. 企业—Docker容器之仓库简介及公共仓库的搭建及其使用
  8. 【在线研讨-现场文字】《敏捷开发用户故事分类与组织结构(一期-5)》2012-06-26...
  9. 浙江工业大学线下宣讲开始了,线上投递同步持续进行
  10. [人生哲理] 父子骑驴的故事--人生经典