不知道哪里的oj。。做了交不上去。。

也是莫队的模板题

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 100005
#define ll long long
struct Query{
int L,R,id;
}q[MAXN];
ll ans[MAXN],cnt[MAXN];//这个颜色在当前区间出现的次数
int s,note[MAXN];
bool cmp(Query a,Query b){
if(a.L/s == b.L/s) return a.R<b.R;
return a.L/s < b.L/s;
}
ll cube(ll a){
return a*a*a;
}
int main(){
int n,m;
scanf("%d",&n);
s=(int)sqrt(n);
for(int i=;i<=n;i++) scanf("%d",&note[i]);
scanf("%d",&m);
for(int i=;i<m;i++) {
int a,b;
scanf("%d%d",&a,&b);
q[i]=(Query){a,b,i};
}
sort(q,q+m,cmp);
int L=,R=;
ll res=;
for(int i=;i<m;i++){
while(R<q[i].R){
R++;
res+=cube((cnt[note[R]]+))-cube(cnt[note[R]]);
cnt[note[R]]++;
}
while(R>q[i].R){
res-=cube(cnt[note[R]])-cube(cnt[note[R]]-);
cnt[note[R]]--;
R--;
}
while(L<q[i].L){
res-=cube(cnt[note[L]])-cube(cnt[note[L]]-);
cnt[note[L]]--;
L++;
}
while(L>q[i].L){
L--;
res+=cube(cnt[note[L]]+)-cube(cnt[note[L]]);
cnt[note[L]]++;
}
ans[q[i].id]=res;
}
for(int i=;i<m;i++){
printf("%d\n", ans[i]);
}
return ;
}

NBUT1457的更多相关文章

  1. NBUT1457 Sona 莫队算法

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

随机推荐

  1. How to get API key (APPID)

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  2. Git与GitHub的基本使用

    Git与GitHub的基本使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Git的基本使用 1.版本库创建 a>.什么是版本库呢 版本库又名仓库,英文名reposit ...

  3. CENTOS 升级Nodejs 到最新版本

    1.去官网下载和自己系统匹配的文件: 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通过  uname -a ...

  4. Linux 内核 hlist 详解

    在Linux内核中,hlist(哈希链表)使用非常广泛.本文将对其数据结构和核心函数进行分析. 和hlist相关的数据结构有两个:hlist_head 和 hlist_node //hash桶的头结点 ...

  5. Spark记录-Scala程序例子(函数/List/match/option/泛型/隐式转换)

    object func { def main(args:Array[String]):Unit={ //函数赋值给变量时, 必须在函数后面加上空格和下划线. def sayHello(name: St ...

  6. 使用Spring时web.xml中的配置

    使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...

  7. JS 互相调用iframe页面中js方法、VUE里 iframe 互调方法

    1,父 html 调用子 iframe 内方法: document.getElementById("iframe").contentWindow.func(data1,data2. ...

  8. android 简单文件操作

    1.布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...

  9. Redis 主从模式

    系统:Centos6.6x64安装目录:/usr/local/主:192.168.100.103从:192.168.100.104 ,下载安装: 安装依赖: # yum install gcc tcl ...

  10. pycharm永久激活(转)

    机器上安装的pycharm失效了,注册服务器也不管用了.网上找了一个比较满意的激活方法,推荐给大家: 第一步:下载jar包: 此jar包的目的就是让截获截止时间并骗过pycharm; 百度云下载地址  ...