• [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

题目链接:NBUT 1457

莫队算法就是能用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<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
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<int,int> 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<b.l/unit;return a.r<b.r;
}void init()
{CLR(arr,0);CLR(cnt,0);
}
inline void update(int pos,int n,LL &temp)
{LL &val=arr[pos];if(n==1)temp=temp+3*cnt[val]*cnt[val]+3*cnt[val]+1;elsetemp=temp-3*cnt[val]*cnt[val]+3*cnt[val]-1;cnt[val]+=n;
}
int Scan()
{int res=0,ch,flag=0;if((ch=getchar())=='-')flag=1;else if(ch>='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<k; ++i){node[i].l=Scan();node[i].r=Scan();node[i].id=i;}sort(node,node+k,cmp);L=node[0].l;R=node[0].l-1;LL temp=0;for (i=0; i<k; ++i){while (L>node[i].l)update(--L,1,temp);while (R<node[i].r)update(++R,1,temp);while (L<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<k; ++i){Out(ans[i]);putchar('\n');}init();}return 0;
}

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

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 莫队算法 分块处理

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

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

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

  4. NBUT - 1457 Sona 莫队

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

  5. NBUT1457 Sona 莫队算法

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

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

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

  7. NBUT 1457 Sona

    莫队算法+离散化 1.map会TLE,必须离散化做 2.long long会WA,__int64定义 %I64d输出输出能AC 3.注意输入的序列会爆int #include<cstdio> ...

  8. Sona - NBUT 1457 莫队算法

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

  9. Sona(莫队+离散化)

    Sona, Maven of the Strings. Of cause, she can play the zither. Sona can't speak but she can make fan ...

最新文章

  1. 网络安全工具:Wireshark
  2. 26个音序的正确写法和占格_部编语文汉语拼音音序表,示范朗读+视频教学
  3. redis一般缓存什么样数据_门户数据展示_Redis缓存数据
  4. [转帖]ASP.NET中常用的优化性能的方法
  5. Python大数据:jieba分词,词频统计
  6. mssql 计划怎每隔n秒_前端:调你一个接口6秒还配资深工程师?后端:有24部分需要处理!...
  7. 用十行代码快速创建权限管理系统
  8. 佳能g2810打印机扫描怎么用_办公用这款佳能彩色激光打印机无线MF643CDW就够了!...
  9. 论文笔记《Neural Machine Translation by Jointly Learning to Align and Translate》
  10. Python解析JSON对象
  11. 1.业务架构·应用架构·数据架构实战 --- 架构实践全景图
  12. php图书馆占座系统代码,PHP图书馆管理系统(源码+数据库脚本+截图)
  13. cdr圆形渐变填充怎么设置_适用于平面设计的软件cdr!
  14. 一文读懂 delete和delete[ ]
  15. 将一个数组分成2个数组,使得2个数组的差值最小
  16. 男人,如何洞悉女人的内心世界笔记
  17. 二元决策图(Binary Decision Diagrams - BDD) (一)
  18. conda\pip 安装pytorch
  19. c语言控制树莓派蓝牙,方法2-树莓派3B蓝牙rfcomm通信调试
  20. MessageBox.Show用法

热门文章

  1. docker 常用命令集合
  2. 第五周周记(国庆第一天)
  3. 【C/S语言】.net平台
  4. linux下使用source /etc/profile保存配置后,新的环境变量只能在一个终端里面有效
  5. LeetCode简单题之数组异或操作
  6. LeetCode中等题之煎饼排序
  7. LeetCode简单题之赎金信
  8. Nginx最新版安装教程(Windows+Linux)
  9. 高效Tensor张量生成
  10. php7安装redis6扩展