本文主要是介绍NBUT 1457 Sona(莫队算法+离散化),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:点击打开链接
[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
题意:
右N个数字,M个询问,求每个询问区间[L,R]中,每种数字出现次数的立方和
分析:
莫队算法能够很好处理该题,只不过将原数组离散化一下就ok。
注意long long 和sqrt() 宁波学院有毒
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
const int maxn=1000010;
typedef long long ll;
int n,m,k;
struct node
{int l,r,id;
}Q[maxn]; ///保存询问值
int pos[maxn];///保存所在块
bool cmp(const node &a,const node &b)
{return pos[a.l]<pos[b.l]||(pos[a.l]==pos[b.l]&&(pos[a.l]&1?a.r<b.r:a.r>b.r));///奇偶排序//if(pos[a.l]==pos[b.l]) return a.r<b.r;//先按l所在的块排,如果相等就按r排//else return pos[a.l]<pos[b.l];
}
ll num[maxn*2];///保存区间的个数
///num要定义成ll,wa了好久
ll a[maxn],b[maxn];ll ans[maxn],ans2[maxn];
int L=0,R=0;
ll Ans=0;
void add(int x)
{Ans-=num[a[x]]*num[a[x]]*num[a[x]];num[a[x]]++;Ans+=num[a[x]]*num[a[x]]*num[a[x]];
}
void del(int x)
{Ans-=num[a[x]]*num[a[x]]*num[a[x]];num[a[x]]--;Ans+=num[a[x]]*num[a[x]]*num[a[x]];
}
int main()
{ while(~scanf("%d",&n)){memset(num,0,sizeof(num));//scanf("%d",&n);//int sz=n/sqrt(m*2/3);int sz=sqrt(n*1.0); ///不加1.0尽然编译错误for(int i=1;i<=n;i++){scanf("%lld",&a[i]);b[i]=a[i];pos[i]=i/sz;}sort(b+1,b+n+1);int num1=unique(b+1,b+n+1)-b-1;for(int i=1;i<=n;i++){a[i]=lower_bound(b+1,b+num1+1,a[i])-b;}scanf("%d",&m);for(int i=1;i<=m;i++){scanf("%d%d",&Q[i].l,&Q[i].r);Q[i].id=i;}sort(Q+1,Q+1+m,cmp);num[0]=0;L=1;R=0;Ans=0;for(int i=1;i<=m;i++) {while(R<Q[i].r){R++;add(R);}while(L>Q[i].l) ///前缀和L+1>Q[i].l{L--;add(L);}while(L<Q[i].l)///前缀和L+1<Q[i].l{del(L);L++;}while(R>Q[i].r){del(R);R--;}ans[Q[i].id]=Ans;}// printf("Case %d:\n",cas);for(int i=1;i<=m;i++)printf("%I64d\n",ans[i]);}return 0;
}
这篇关于NBUT 1457 Sona(莫队算法+离散化)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!