NBUT 1457 Sona 莫队算法 分块处理
来源:互联网 发布:网站怎么连接数据库 编辑:程序博客网 时间:2024/05/05 05:01
[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
- 提示
无
- 来源
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 莫队算法 分块处理
- NBUT 1457 Sona (莫队算法)
- Sona - NBUT 1457 莫队算法
- NBUT 1457Sona(莫队算法)
- NBUT 1457 Sona (莫队算法)
- NBUT 1457 Sona 莫队算法
- NBUT 1457 Sona (莫队算法)
- NBUT 1457 Sona(莫队算法+离散化)
- NBUT 1457 Sona [简单莫队]
- NBUT 1457 Sona
- 莫队算法(NBUT1457 Sona)
- nbut1457 Sona 【莫队算法+离散化】
- NBUT 1457 分块法
- NBUT 1457 分块
- NBUT 1463 Sona
- NBUT 1457 (莫队算法 离散化)
- 分块、莫队算法
- 分块_莫队算法
- iOS开发:创建真机调试证书
- SPI
- android 完美自定义 checkbox 样式
- 饭卡 (01背包)
- 理解AngularJS中的Service类型
- NBUT 1457 Sona 莫队算法 分块处理
- 如何在github上托管项目
- 发送post请求(表单的方式)
- 揭开NFV的面纱
- PAT 1055. The World's Richest (25)
- hdu-1166-敌兵布阵
- centos 挂载iscsi 连接网络存储 vnxe
- HDU 1387 Team Queue(优先队列)
- 欢迎使用CSDN-markdown编辑器