不知道哪里的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. Linux命令之grep

    grep命令 用处:文本搜索工具 用法:grep + ‘查找关键字的名字’ + 文件名 示例: 还在profile里面查找then 干净利落强大,有关then的信息显示

  2. js鼠标移入移出效果【原】

    <HTML> <HEAD> <!-- meta 解释 : http://www.haorooms.com/post/html_meta_ds --> <met ...

  3. authentication failed for xxx错误

    现象: 公司windows定期修改过密码后 一直报错.push的时候显示“Authentication Failed for http://x.x.x.x/x/git” 猜想: 发现可能是账号问题. ...

  4. mysql alter 用法,修改表,字段等信息

    一: 修改表信息 1.修改表名 alter table test_a rename to sys_app; 2.修改表注释 alter table sys_application comment '系 ...

  5. Java Web 自定义标签

    1.   自定义标签 由于在JSP页面中直接嵌入Java代码会导致页面开起来非常混乱,不方便和美工等配合工作,为此,JSP提供了自定义标签技术,可以代替直接嵌入Java代码的方式提供动态逻辑,但自定义 ...

  6. 21. Spring Boot Druid 数据源配置解析

    1.数据源配置属性类源码 package org.springframework.boot.autoconfigure.jdbc; @ConfigurationProperties( prefix = ...

  7. deepin下codeblocks更改调试终端

    codeblocks建立控制台程序生成完毕后,发现自带的调试终端xterm不能进行复制粘贴操作参考了Ubuntu的更换调试终端的方法,就是把deepin下的deepin-terminal 用作调试终端 ...

  8. MHA-手动Failover流程(传统复制&GTID复制)

    本文仅梳理手动Failover流程.MHA的介绍详见:MySQL高可用架构之MHA 一.基本环境 1.1.复制结构 VMware10.0+CentOS6.9+MySQL5.7.21 ROLE HOST ...

  9. snmp 发送类型ASN_OBJECT_ID

    参考链接: http://blog.csdn.net/yin138/article/details/50388878 ,,,,,,,,,}; int ret = snmp_set_var_typed_ ...

  10. spfa算法----最短路

    题目链接:https://cn.vjudge.net/contest/66569#problem/A 代码: vis数组代表是否还有用,首先初始化为0,首先第一个点入队列,标记为1,然后刚入队列的时候 ...