• [1457] Sona

  • 时间限制: 5000 ms 内存限制: 65535 K
  • 问题描述
  • 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 has105 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.

  • 输入
  • 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.
  • 输出
  • For each part, you should output the energy of that part.
  • 样例输入
  • 8
    1 1 3 1 3 1 3 3
    4
    1 8
    3 8
    5 6
    5 5
    
  • 样例输出
  • 128
    72
    2
    1

题意:求出给定区间内各个数字的出现次数的立方和。

思路:运用莫队算法分块的思想解决。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
struct node
{int l,r,id;ll ans;
}arr[100010];
int n,m,pos[100010],c[100010];
ll num[100010];
ll ans;
map<int,int> match;
bool cmp(node a,node b)
{return pos[a.l]<pos[b.l] || (pos[a.l]==pos[b.l] && a.r<b.r);
}
bool cmp_id(node a,node b)
{return a.id<b.id;
}
void update(int p,int add)
{ans-=num[c[p]]*num[c[p]]*num[c[p]];num[c[p]]+=add;ans+=num[c[p]]*num[c[p]]*num[c[p]];
}
void solve()
{int i,j,k,l,r;ans=0;l=1;r=0;memset(num,0,sizeof(num));for(i=1;i<=m;i++){//printf("l=%d r=%d\n",arr[i].l,arr[i].r);for(;r<arr[i].r;r++)update(r+1,1);for(;r>arr[i].r;r--)update(r,-1);for(;l<arr[i].l;l++)update(l,-1);for(;l>arr[i].l;l--)update(l-1,1);arr[i].ans=ans;}
}
int main()
{int i,j,k,tot;while(~scanf("%d",&n)){for(i=1;i<=n;i++)scanf("%d",&c[i]);k=(int)sqrt((double)n);for(i=1;i<=n;i++)pos[i]=(i-1)/k+1;match.clear();tot=0;for(i=1;i<=n;i++){if(match[c[i]]>0)c[i]=match[c[i]];else{match[c[i]]=++tot;c[i]=tot;}}scanf("%d",&m);for(i=1;i<=m;i++){scanf("%d%d",&arr[i].l,&arr[i].r);arr[i].id=i;}sort(arr+1,arr+1+m,cmp);solve();sort(arr+1,arr+1+m,cmp_id);for(i=1;i<=m;i++)printf("%I64d\n",arr[i].ans);}
}

Sona - NBUT 1457 莫队算法相关推荐

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

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

  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. 分治 —— 莫队算法

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

  5. XOR and Favorite Number CF340E 莫队算法

    题目链接 题意:求给定询问区间[L,R]问有多少连续区间异或值等k,多次询问可以离线. a[i]^a[i+1]^a[i+2]^a[n]=(a[1]^a[2]^a[3]^...^a[i-1])^(a[1 ...

  6. Hdu 6534 Chika and Friendly Pairs 莫队算法+树状数组

    题目链接 题意求给区间[L,R]中有少对(i,j)满足i<j且abs(a[i]-a[j])<=k. 首先来说暴力的方法就是离散化,然后用树状数组来维护,但是m次询问,m很大,所以说一定会t ...

  7. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MB Submit: 9894  Solved: 4561 [Su ...

  8. 数据结构(莫队算法):国家集训队2010 小Z的袜子

    [题目描述] 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命-- 具体来说,小Z把这N只袜子从1到 ...

  9. C++ 莫队算法(转)

    胡小兔的良心莫队教程:莫队.带修改莫队.树上莫队 在开始学习莫队之前,照例先甩一道例题:BZOJ 1878 HH的项链. 题意:求区间内数的个数,相同的数只算一次. 在我关于这道题的上一篇题解中,我使 ...

最新文章

  1. java中Array和ArrayList区别 可以将 ArrayList想象成一种会自动扩增容量的Array
  2. 使用 Intel HAXM 为 Android 模拟器加速,媲美真机(转)
  3. OpenCASCADE:函数机制的使用
  4. Go web framework
  5. mysql导入表结构命令是_mysql,命令导入\导出表结构或数据
  6. 干货!Redis集群工作原理解析
  7. 55个好习惯可令世界更美好(2)
  8. 北京大学Cousera学习笔记--7-计算导论与C语言基础--基本数据类型变量常量
  9. read, readline, readlines
  10. 12个职场信条让你的工作不可替代
  11. 几何基础,多种矩阵的学习,世界坐标到屏幕坐标的两种转换方法,三种绘制方框的原理,hookd3d,hookopengl,骨骼透视,主播的秘密,FPS各种BT功能的原理 和检测对抗原理,UE4引擎,U3D
  12. Linux 操作系统镜像下载
  13. 【原创】【2021 android逆向系列】番外:oppo A57手机刷官方纯净版ROM且ROOT
  14. 删除流氓软件进程的方法
  15. ipa文件反编译_苹果ipa文件中信息的提取与加密文件的反编译
  16. [Swift]修改NSLayoutConstraint的multiplier
  17. 极简试用期转正述职报告PPT模板
  18. esp8266保存html文件,八,ESP8266 文件保存数据(基于Lua脚本语言)
  19. Access 密码破解原理- -
  20. linux下编译isl,CentOS 6 编译GCC 4.8.1简明流程

热门文章

  1. 出现<empty string>的可能原因
  2. 串口的SetCommTimeouts
  3. kafka笔记4--安装kafka ui
  4. iphone用计算机显示器,设计经验:修正iPhone和计算机显示器之间的色差
  5. 2013计算机等级考试三级教程网络技术,全国计算机等级考试三级教程——网络技术(2013年版)...
  6. Deep learning for integrated analys of breast cancer subtype specific multi-omics data
  7. (转载收藏)值得网页设计师前端收藏的40个实用工具
  8. LINQ To XML 入门(3)
  9. GMV从5w日增到1000w,夏日经济下它是如何成为直播间的新宠?
  10. chatgpt赋能python:Python怎么接单子平台有哪些