博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NBUT 1457 Sona(莫队算法+离散化)
阅读量:5148 次
发布时间:2019-06-13

本文共 2933 字,大约阅读时间需要 9 分钟。

 

  • [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.
  • 样例输入
  • 81 1 3 1 3 1 3 341 83 85 65 5
  • 样例输出
  • 1287221

 

 

题目链接:

莫队算法就是能用n*sqrt(n)的时间来解决无修改区间查询的一种办法……,主要处理就是把询问暂时不按照读入顺序而按照所在块和右端点值为关键字进行排序,然后对这样的一个暂时非正常的询问顺序进行计算回答,然后可以再次按照输入顺序排序或者用数组记录答案后输出。

可以参考这篇博客:

这题写出立方公式就很容易发现a^3和(a+1)^3以及(a-1)^3的区别,然后就可以很方便地进行O(1)转化,网上一些题解代码都是暴力减掉当前的再加回更新的,即减掉a*a*a再加上(a+1)*(a+1)*(a+1),感觉这样写不太好……,反正如果能快速得到[L-1,R]或[L,R+1]这样的区间信息的话莫队算法值得一试。这题用了输入输出外挂搞到2100+MS,一些人直接用立方加减的3300+MS

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3f#define CLR(x,y) memset(x,y,sizeof(x))#define LC(x) (x<<1)#define RC(x) ((x<<1)+1)#define MID(x,y) ((x+y)>>1)typedef pair
pii;typedef __int64 LL;const double PI=acos(-1.0);const int N=100010;struct info{ int l; int r; LL energy; int id;};info node[N];LL arr[N];int unit,b[N];LL cnt[N];LL ans[N];inline bool cmp(const info &a,const info &b){ if(a.l/unit!=b.l/unit) return a.l/unit
='0'&&ch<='9') res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; return flag?-res:res;}void Out(LL a){ if(a>9) Out(a/10LL); putchar(a%10LL+'0');}int main(void){ int n,i,j,k,L,R; while (~scanf("%d",&n)) { for (i=1; i<=n; ++i) { arr[i]=Scan(); b[i]=arr[i]; } sort(b+1,b+1+n); int m=unique(b+1,b+1+n)-b-1; for (i=1; i<=n; ++i) arr[i]=lower_bound(b+1,b+1+m,arr[i])-b; unit=(int)sqrt(1.0*n); scanf("%d",&k); for (i=0; i
node[i].l) update(--L,1,temp); while (R
node[i].r) update(R--,-1,temp); ans[node[i].id]=temp; } for (i=0; i

转载于:https://www.cnblogs.com/Blackops/p/5766255.html

你可能感兴趣的文章
oracle 创建暂时表
查看>>
201421410014蒋佳奇
查看>>
Xcode5和ObjC新特性
查看>>
LibSVM for Python 使用
查看>>
Centos 7.0 安装Mono 3.4 和 Jexus 5.6
查看>>
CSS属性值currentColor
查看>>
java可重入锁reentrantlock
查看>>
浅谈卷积神经网络及matlab实现
查看>>
解决ajax请求cors跨域问题
查看>>
《收获,不止Oracle》pdf
查看>>
Real-Time Rendering 笔记
查看>>
如何理解HTML结构的语义化
查看>>
Activity之间的跳转:
查看>>
实验四2
查看>>
多路复用
查看>>
Python数据可视化之Pygal(雷达图)
查看>>
Java学习笔记--字符串和文件IO
查看>>
转 Silverlight开发历程—(画刷与着色之线性渐变画刷)
查看>>
SQL语法(3)
查看>>
在js在添版本号
查看>>