• [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
    
  • 提示
  • 来源
  • XadillaX

莫队算法,参考我上一篇博客吧。或者搜索我的“莫队算法”的文章。

对于每个询问 [l,r]按l分块,每sqrt(n)一块,同一块内按R递增排序。

LL,RR指针标记处理的区间,转移是O(1)的。先离散化数字,用一个数组统计这个数字出现的次数

如果次数加1  那么 ans = ans + num[i]*num[i] - (num[i])-1*(num[i]-1)

加一个输入外挂快些900+ms,不用也行。1250ms。挺快的 了。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<math.h>
using namespace std;
#define maxn 100007
#define ll long longint num[maxn],color[maxn],ord[maxn];
ll ans[maxn];
struct Node{int l,r,id;ll ans;
};
Node query[maxn];
int squ;
inline int comp(const Node &a,const Node &b){if(a.l / squ == b.l / squ)return a.r < b.r;return a.l/squ < b.l/squ;
}int getn(){int a = 0;char x;while(1){x = getchar();if(x==' ' || x == '\n') return a;a = a*10+x-'0';}
}
ll po3[maxn];
inline int compid(const Node&a,const Node &b){return a.id < b.id;
}
int main(){int n,m,q;for(int i = 0;i < maxn; i++)po3[i] = 1ll*i*i*i;while(scanf("%d",&n)!=EOF){getchar();for(int i = 1;i <= n; i++){//scanf("%d",&color[i]);color[i] = getn();ord[i] = color[i];}sort(ord+1,ord+n+1);int m = unique(ord+1,ord+1+n)-(ord+1);for(int i = 1;i <= n; i++)color[i] = lower_bound(ord+1,ord+1+m,color[i])-(ord+1);for(int i = 0;i <= m; i++)num[i] = 0;scanf("%d",&q);getchar();for(int i = 0;i < q; i++){//scanf("%d%d",&query[i].l,&query[i].r);query[i].l = getn();query[i].r = getn();query[i].id = i;}squ = sqrt(n*1.0);sort(query,query+q,comp);int LL=query[0].l,RR=query[0].l-1;ll res = 0;int x;for(int i = 0;i < q; i++){int id = query[i].id;while(RR < query[i].r){RR++;x = num[color[RR]]++;res -= po3[x];x++;res += po3[x];}while(RR > query[i].r){x = num[color[RR]]--;res -= po3[x];x--;res += po3[x];RR--;}while(LL > query[i].l){LL--;x = num[color[LL]]++;res -= po3[x];x++;res += po3[x];}while(LL < query[i].l){x = num[color[LL]]--;res -= po3[x];x--;res += po3[x];LL++;}query[i].ans = res;}sort(query,query+q,compid);for(int i = 0;i < q; i++){printf("%I64d\n",query[i].ans);}}return 0;
}

NBUT 1457 Sona 莫队算法 分块处理相关推荐

  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 (莫队算法)

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

  3. NBUT - 1457 Sona 莫队

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

  4. NBUT1457 Sona 莫队算法

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

  5. Sona NBUT - 1457 (莫队+hash)

    题目:点击此处 题意:求一个区间内出现的数的次数的立方的和 思路:主要不同就是add和remove里的不同 add:当前某个数的出现次数是cnt,如果遇见了这个数,那么先让ans-=cnt*cnt*c ...

  6. Sona - NBUT 1457 莫队算法

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

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

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

  8. 【BZOJ】3052: [wc2013]糖果公园 树分块+带修改莫队算法

    [题目]#58. [WC2013]糖果公园 [题意]给定n个点的树,m种糖果,每个点有糖果ci.给定n个数wi和m个数vi,第i颗糖果第j次品尝的价值是v(i)*w(j).q次询问一条链上每个点价值的 ...

  9. 分治 —— 莫队算法

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

最新文章

  1. 关于ES6的10个最佳特性
  2. 转正老板让你谈谈你的看法_让我们谈谈逻辑回归
  3. 周至四中计算机老师,周至县第四中学顺利承办周至县 “教育信息化技术与教学课堂深度融合交流研讨会”...
  4. LeetCode 1066. 校园自行车分配 II(状态压缩DP)
  5. Shell脚本之七 选择、循环结构
  6. libevent:信号、超时、回调
  7. 机器学习算法基础8-Nagel-Schreckenberg交通流模型-公路堵车概率模型
  8. iOS编程高性能之路-基于pthread的线程池
  9. gitbash PHP执行输出中文乱编解决方式
  10. 一些嵌入式开发有用的github上的开源代码库
  11. python 下载 M3U8 视频
  12. 利用计算机教室教师培训记录表,新学期教师计算机培训方案
  13. Java多线程--概述-转自Kyrie lrving
  14. 使用微PE安装U盘windows系统
  15. java根据某天获取当前周的日期
  16. Android 仿朋友圈单张图片限定宽高超出时按比例缩放效果实现
  17. 手机运行android虚拟机,手机也能装虚拟机?实测虚拟机APP坑爹or真有料
  18. 北航计算机科学与技术课表,北航计算机科学与技术五年课程参考
  19. 微信小程序生成普通网页的二维码
  20. LeetCode简单题之是否所有 1 都至少相隔 k 个元素

热门文章

  1. 神经网络中梯度下降的参数更新公式理解
  2. happy work, happy life
  3. 2022年个人学习年度计划
  4. PVDF电荷放大器的设计实践( 1 )
  5. 安科瑞电动机保护器具有过载反时限、过载定时限、接地、起动超时、漏电、欠载、断相、堵转等功能
  6. 霍金质疑超光速粒子发现:需要更多实验确认
  7. 企业法务管理-中顾企业法律风险管控中心
  8. 从0.1开始学Python——[17]
  9. LightOJ 1340 Story of Tomisu Ghost
  10. 国产CPU深度研究报告(110页)