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

至此,题目也就解完了.

 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #define LL long long
 using namespace std;
 ];
 LL cnt[],ans;
 struct query{
     int L,R,index; LL ans;
 }a[];
 struct data{
     int x,index;
 }c[];
 inline int read(){
     ; char ch=getchar();
     ') ch=getchar();
     +ch-',ch=getchar();
     return x;
 }
 bool cmp_x(data u,data v){return u.x<v.x;}
 bool cmp_index(data u,data v){return u.index<v.index;}
 bool cmp_blocks(query u,query v){return u.L/blocks==v.L/blocks?u.R<v.R:u.L<v.L;}
 bool cmp_id(query u,query v){return u.index<v.index;}
 *cnt[c[p].x]*cnt[c[p].x]-(LL)*cnt[c[p].x]+,cnt[c[p].x]--;}
 *cnt[c[p].x]*cnt[c[p].x]+(LL)*cnt[c[p].x]+,cnt[c[p].x]++;}
 int main(){
     ){
         ; i<=n; i++) ; break;}
         ; i<=n; i++) c[i].x=read(),c[i].index=i;
         sort(c+,c++n,cmp_x);
         ;
         memset(cc,,]=;
         ; i<=n; i++) ].x) cc[i]=cntnew; else cc[i]=++cntnew;
         ; i<=n; i++) c[i].x=cc[i];
         sort(c+,c++n,cmp_index);
         Q=read();
         ; i<=Q; i++) a[i].L=read(),a[i].R=read(),a[i].index=i;
         sort(a+,a++Q,cmp_blocks);
         ,curR=;
         memset(cnt,,;
         ; i<=Q; i++){
             while (curL<a[i].L) remove(curL++);
             while (curR>a[i].R) remove(curR--);
             while (curL>a[i].L) add(--curL);
             while (curR<a[i].R) add(++curR);
             a[i].ans=ans;
         }
         sort(a+,a++Q,cmp_id);
         ; i<=Q; i++) printf("%I64d\n",a[i].ans);
     }
     ;
 }

Sona的更多相关文章

  1. NBUT 1457 Sona(莫队算法+离散化)

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

  2. SONA Topology

    N多年以前就有有人设计传了一种类似“房子”状结构的拓扑图,在Cisco的文档中可以查到这种叫SONA.这是个非常神奇的设计,适合用于中小型网络,之所以这么讲,是因为在这个结构下,但凡任何一台接入层或者 ...

  3. Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)

    vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...

  4. NBUT1457 Sona 莫队算法

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

  5. NBUT 1457 Sona

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

  6. NBUT 1457 Sona (莫队算法)

    题目大意: 求一段区间内 出现的数字的次数的三次方的和 思路分析: 这要水过去的题目真是难,各种优化. 不能用map , 要离散化之后 先处理lowerbound. 优化输入. . . 时间卡的非常紧 ...

  7. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  8. 基础才是重中之重~方法override详解

    回到 目录 之所以写这篇文章,完全是因为这次代码审核,这次代码审核过程当中,出现了很多我认为基础知识不够扎实的问题,所以,打算把它们记录下来,共大家分享. 方法的override,即方法的覆写或者重写 ...

  9. Visual Studio 技能GET

    常用快捷键 自动生成头部注释 代码片段 NuGet Team Foundation 常用的VS快捷键 查看与设置快捷键 一般在菜单里面我们直接就可以看到一些功能的快捷键.另外,可以依次通过 菜单栏-工 ...

随机推荐

  1. .NET下使用 Seq结构化日志系统

    前言 我们公司在日志管理方面一直没有统一,主要痛点有: 每个开发人员都是各用各的,存储日志的形式也是五花八门,如:本地文件,数据库,Redis,MongoDB 由于公司访问服务器要通过堡垒机,所以本机 ...

  2. 【Mysql】【环境配置】Mac

    参看:http://www.cnblogs.com/chenmo-xpw/p/6102933.html     一.下载dmg包安装 1.下载MySQL dmg 包, 从官网 : http://www ...

  3. React native中的组建通知通信:

    有这么一个需求,在B页面pop()回到A页面,需要A页面执行刷新,那么我们可以采用以下方法: 1:在A页面Push到B页面中,加上一个A页面中的刷新函数做为参数,然后在B页面中在pop()函数封装后通 ...

  4. MVC后台获取数据和插入数据的三种方式【二】

    MVC模式下,从前端获取数据返回后台,总共有三种形式.下面的代码示例将演示如何将数据返回到后端. 一.首先我们看看表单代码,注意input标签中name的值. <html> <hea ...

  5. VC.时间(网页内容收集)

    1.VC++获得当前系统时间的几种方案_记忆53秒_新浪博客.html(http://blog.sina.com.cn/s/blog_676271a60101i0hb.html) 1.1.内容保存: ...

  6. STL——set

    (转) 1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用 ...

  7. CentOS/redhat使用光盘镜像源

    1,首先进行光盘的挂载,注意光盘挂载时不会自动建立目录的,    所以需要自己建立目录.    mkdir /mnt/cdrom    mount /dev/cdrom /mnt/cdrom  #de ...

  8. 第 8 章 容器网络 - 050 - 创建 overlay 网络

    在 host1 中创建 overlay 网络 ov_net1: docker network create -d overlay ov_net1 -d overlay 指定 driver 为 over ...

  9. seo中的竞价排名是什么

    seo中的竞价排名是什么 一.总结 一句话总结:竞价排名的基本特点是按点击付费,推广信息出现在搜索结果中(一般是靠前的位置),如果没有被用户点击,则不收取推广费. 搜索引擎的一种推广广告的方式 1.竞 ...

  10. Lasso linear model实例 | Proliferation index | 评估单细胞的增殖指数

    背景:We developed a cell-cycle scoring approach that uses expression data to compute an index for ever ...