NBUT1457
不知道哪里的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",¬e[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的更多相关文章
- NBUT1457 Sona 莫队算法
由于10^9很大,所以先离散化一下,把给你的这一段数哈希 时间复杂度O(nlogn) 然后就是分块莫队 已知[L,R],由于事先的离散化,可以在O(1)的的时间更新[l+1,r],[l,r+1],[l ...
随机推荐
- Python复习笔记(三)函数进阶
问题 1:在函数内部,针对参数使用 赋值语句,会不会影响调用函数时传递的 实参变量? —— 不会! 无论传递的参数是 可变 还是 不可变 只要 针对参数 使用 赋值语句,会在 函数内部 修改 局部变量 ...
- This page is about building Firefox Desktop
This page is about building Firefox Desktop The Mozilla build system, like the rest of the Mozilla c ...
- xml的解析方式的简介
xml的解析的简介(写到java代码) *xml是一个标记型文档 *js使用dom解析标记型文档? -根据html的层级结构,在内存中分配一个树形结构,把html的标签,属性和文本都封装成对象 -do ...
- C# 面向对象的new关键字的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- JavaScript之this,call,apply
this:被调用的上下文对象: apply与call:切换被调用的上下文对象,即 调用时,this被临时性地切换 //demo 1 [call] function forEach(list,callb ...
- NOIP2018ty记
前置传送门:noip2018前流水账 Day-inf~Day-3 写流水账里了 懒得再写了 Day-2~Day-1 做了些noip的原题 真是奇怪,我天天爱跑步和逛公园都是1A的,结果反而有些普及组的 ...
- 【IT界的厨子】酱香鲈鱼
食材: 前世曾经回眸的鲈鱼一条(主要选刺少的鱼,适合孩子吃,大人吃随意,草鱼比较大) 五花肉少许(肥一些的) 豆腐 辅料: 葱姜 蒜(选) 大料 香菜 调味: 啤酒(两罐) 黄豆酱或豆瓣酱(选) 老抽 ...
- 第16月第17天 contentMode
1. self.contentMode = UIViewContentModeScaleAspectFill; self.clipsToBounds = YES; http://blog.csdn.n ...
- POJ 2503 Babelfish (STL)
题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...
- adb环境变量配置
针对win10系统: 搜索“高级系统设置”,点击“环境变量”按钮: 找到“path”双击: 双击“path”,在弹出的环境变量列表中新建,填入adb的文件路径 检查配置是否成功,运行命令adb,出现如 ...