Sona

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 has 105 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.

InputThis 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.OutputFor each part, you should output the energy of that part.

Sample Input

8
1 1 3 1 3 1 3 3
4
1 8
3 8
5 6
5 5

Sample Output

128
72
2
1

Hint

题目的大意就是,给定一个序列,然后给出一些区间的询问,求出区间内每一类数字的数量的立方和.

显然,这是离线题,非常适合用莫队去做.主要的地方还是在remove和add两个函数内.

remove:

某种数字的数目从x变成了x-1,所以ans-=x^3-(x-1)^3=3x^2-3x+1

add:

某种数字的数目从x变成了x+1,所以ans+=(x+1)^3-x^3=3x^2+3x+1

至此,题目也就解完了.

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 int n,Q,K,blocks,cc[100005];
 7 LL cnt[100005],ans;
 8 struct query{
 9     int L,R,index; LL ans;
10 }a[100005];
11 struct data{
12     int x,index;
13 }c[100005];
14 inline int read(){
15     int x=0; char ch=getchar();
16     while (ch<‘0‘||ch>‘9‘) ch=getchar();
17     while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
18     return x;
19 }
20 bool cmp_x(data u,data v){return u.x<v.x;}
21 bool cmp_index(data u,data v){return u.index<v.index;}
22 bool cmp_blocks(query u,query v){return u.L/blocks==v.L/blocks?u.R<v.R:u.L<v.L;}
23 bool cmp_id(query u,query v){return u.index<v.index;}
24 void remove(int p){ans-=(LL)3*cnt[c[p].x]*cnt[c[p].x]-(LL)3*cnt[c[p].x]+1,cnt[c[p].x]--;}
25 void add(int p){ans+=(LL)3*cnt[c[p].x]*cnt[c[p].x]+(LL)3*cnt[c[p].x]+1,cnt[c[p].x]++;}
26 int main(){
27     while (scanf("%d",&n)==1){
28         for (int i=1; i<=n; i++) if (i*i>n){blocks=i-1; break;}
29         for (int i=1; i<=n; i++) c[i].x=read(),c[i].index=i;
30         sort(c+1,c+1+n,cmp_x);
31         int cntnew=1;
32         memset(cc,0,sizeof cc),cc[1]=1;
33         for (int i=2; i<=n; i++) if (c[i].x==c[i-1].x) cc[i]=cntnew; else cc[i]=++cntnew;
34         for (int i=1; i<=n; i++) c[i].x=cc[i];
35         sort(c+1,c+1+n,cmp_index);
36         Q=read();
37         for (int i=1; i<=Q; i++) a[i].L=read(),a[i].R=read(),a[i].index=i;
38         sort(a+1,a+1+Q,cmp_blocks);
39         int curL=1,curR=0;
40         memset(cnt,0,sizeof cnt); ans=0;
41         for (int i=1; i<=Q; i++){
42             while (curL<a[i].L) remove(curL++);
43             while (curR>a[i].R) remove(curR--);
44             while (curL>a[i].L) add(--curL);
45             while (curR<a[i].R) add(++curR);
46             a[i].ans=ans;
47         }
48         sort(a+1,a+1+Q,cmp_id);
49         for (int i=1; i<=Q; i++) printf("%I64d\n",a[i].ans);
50     }
51     return 0;
52 }

时间: 2024-08-08 22:10:01

Sona的相关文章

NBUT 1457 Sona (莫队算法)

题目大意: 求一段区间内 出现的数字的次数的三次方的和 思路分析: 这要水过去的题目真是难,各种优化. 不能用map , 要离散化之后 先处理lowerbound.优化输入... 时间卡的很紧.. 题目直接用莫队水过去. 如果你超时的话,不妨试试上面三种优化. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <map> #

NBUT1457 Sona 莫队算法

由于10^9很大,所以先离散化一下,把给你的这一段数哈希 时间复杂度O(nlogn) 然后就是分块莫队 已知[L,R],由于事先的离散化,可以在O(1)的的时间更新[l+1,r],[l,r+1],[l-1,r],[l,r-1]时间复杂度O(n*sqrt(n)): 代码如下,速度并不是很快(我比较喜欢手动的去重,unique一直没怎么用过) /*96655 's source code for B Memory: 3744 KB Time: 2968 MS Language: G++ Result

NBUT 1457 Sona

莫队算法+离散化 1.map会TLE,必须离散化做 2.long long会WA,__int64定义 %I64d输出输出能AC 3.注意输入的序列会爆int #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<map> #include<algorithm> using namespace std; const int maxn = 1

NBUT 1457 B - Sona

同小Z的袜子,只是需要离散化,平方变成了立方 //#include<bits/stdc++.h> #include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> #define N 100010 #define LL long long using namespace std; struct node { int l,r,id; }; int n,m,t; int

(莫队算法)两题莫队算法统计数量的入门题

因为这两题差不多,而且比较简单,就放一起,做了这题,这种题目就是巨水的题了.随便写都行. CodeForces - 86D  Powerful array 题意: 多次查询数列中从L到R每个数字出现次数的平方乘这个数字的和. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <map> 6 #includ

用Sonarqube检查和度量代码质量——安装sonarqube

以前关注点一直在怎么提高应用程序的质量,没太在意代码级别的质量.最近因为某些因素的推动,需要关注到代码级别的质量去,把质量工作尽量往前推,也符合质量控制的原则.  试用了一下sonarqube(老版本的叫sonar,ww.sonarqube.org),对代码的提升的确有很多的作用,sonarqube能从7个维度来对代码质量进行度量.多大的作用,大家实践下就很容易看出来.尤其是建议大家把rules里面的说明和例子都好好看看,对以后自己写代码的时候,质量提高有很大好处. Sonarqube安装:  

01 &nbsp; Cisco网络架构

智能信息网(Intelligent Information Network, IIN) 面向服务的网络架构(Service-Oriented Network Architecture,SONA) 融合网络数据流: 语音和视频数据流.语音应用数据流.关键任务数据流.交易数据流.路由协议数据流.网络管理数据流 Cisco IIN 3个阶段 阶段1--集成传输:将数据.语音和视频合并到一个IP网络中,从而实现安全的网络融合. 阶段2--集成服务:通过融合网络集成设施,可以集中和共享IT资源(这被称为虚

C#与Java多态方面的语法差异

C#与Java多态方面的语法差异 2016-11-29 Java代码: public static void main(String[] args) { Mother mother=new Mother(); mother.showName(); Mother human=new Son(); human.showName(); Son sona=new Son(); sona.showName(); } public class Mother { public void showName(){

成员变量的隐藏,方法的覆盖,super关键字

成员变量的隐藏:当父类和子类有相同的成员变量时,即定义了与父类相同的成员变量时,就会发生子类对父类变量的隐藏.对于子类的对象来说,父类中的同名成员变量被隐藏起来,子类就会优先使用自己的成员变量,父类成员隐藏. public class yincang {public static void main(String []args){ SonA son=new SonA(); son.showson(); FatherA father=new FatherA(); father.showfather