Sona - NBUT 1457 莫队算法
来源:互联网 发布:简述单片机的开发过程 编辑:程序博客网 时间:2024/05/23 19:16
[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
思路:运用莫队算法分块的思想解决。
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); }}
0 0
- 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 [简单莫队]
- NBUT 1457 Sona
- 莫队算法(NBUT1457 Sona)
- nbut1457 Sona 【莫队算法+离散化】
- NBUT 1463 Sona
- NBUT 1457 (莫队算法 离散化)
- Sona
- NBUT 1641 错误的算法
- NBUT 1457 分块法
- NBUT 1457 分块
- 黑马程序员——JAVA集合
- Django数据库操作(python)
- 一步步将vim改造成C/C++开发环境(IDE)
- 学习七十一雾央MFC游戏心得,初体验(八)
- JMS基本概念
- Sona - NBUT 1457 莫队算法
- Apache部署Django环境
- 从内存的角度观察 堆、栈、全局区(静态区)(static)、文字常量区、程序代码区
- linux下nohup命令——让程序在远程主机后台运行
- 内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区
- Myeclipse中怎样搭建SSH框架
- AndroidADT如何签名打包问题详解
- TCP/IP协议族-----1、引言
- 在静态HTML页面之间传值