不知道哪里的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. 门店评级VS坏客户

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  2. 即将上线的Spark服务器面临的一系列填坑笔记

    即将上线的Spark服务器面临的一系列填坑笔记 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 把kafka和flume倒腾玩了,以为可以轻松一段时间了,没想到使用CDH部署的spa ...

  3. 网络编程基础【day10】:IO多路复用

    这些名词比较绕口,理解涵义就好.一个epoll场景:一个酒吧服务员(一个线程),前面趴了一群醉汉,突然一个吼一声“倒酒”(事件),你小跑过去给他倒一杯,然后随他去吧,突然又一个要倒酒,你又过去倒上,就 ...

  4. mysql命令备份和还原

    1.导出整个数据库 mysqldump -u用户名 -p密码  数据库名 > 导出的文件名  C:\Users\jack> mysqldump -uroot -pmysql sva_rec ...

  5. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

  6. PHP7 学习笔记(八)JetBrains PhpStorm 2017.1 x64 MySQL数据库管理工具的使用

    填写基本信息 这时候我们可以看到已经连接成功的数据库了 打开一个表,我们可以很清楚的看到数据库表的数据 切换到DDL模式

  7. indexOf与includes的比较

    indexOf和includes都代表检测数组或字符串中是否包含某一个元素 其中indexOf返回的是数值类型,而includes返回的是布尔类型 var ary = [,,]; console.lo ...

  8. 重定向printf

    #include "stdio.h" #ifdef __GNUC__#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)#else# ...

  9. Android studio 自动导入(全部)包 import

    http://blog.csdn.net/buaaroid/article/details/44979629 1 Android studio 只有import单个包的快捷键:Alt+Enter.没有 ...

  10. QWidget窗口类

    import sys from PyQt5.QtWidgets import QWidget, QApplication,QPushButton from PyQt5.QtGui import QIc ...