Bzoj 3781: 小B的询问 莫队,分块,暴力
3781: 小B的询问
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 426 Solved: 284
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 3 2 1 1 3
1 4
2 6
3 5
5 6
Sample Output
9
5
2
HINT
对于全部的数据,1<=N、M、K<=50000
Source
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 50010
struct node
{
int l,r,id;
}q[MAXN];
LL ans[MAXN];
int C[MAXN],pos[MAXN],tot[MAXN];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
bool cmp(node a,node b)
{
if(pos[a.l]==pos[b.l])return a.r<b.r;
return a.l<b.l;
}
int main()
{
int n,m,k,block,i,L,R;
LL res;
n=read();m=read();k=read();
for(i=;i<=n;i++)C[i]=read();
block=(int)sqrt(n);
for(i=;i<=n;i++)pos[i]=(i-)/block+;
for(i=;i<=m;i++)
{
q[i].l=read();q[i].r=read();
q[i].id=i;
}
sort(q+,q+m+,cmp);
L=;R=;res=;
memset(tot,,sizeof(tot));
for(i=;i<=m;i++)
{
while(L<q[i].l)
{
res-=tot[C[L]]*tot[C[L]];
tot[C[L]]--;
res+=tot[C[L]]*tot[C[L]];
L++;
}
while(L>q[i].l)
{
L--;
res-=tot[C[L]]*tot[C[L]];
tot[C[L]]++;
res+=tot[C[L]]*tot[C[L]];
}
while(R<q[i].r)
{
R++;
res-=tot[C[R]]*tot[C[R]];
tot[C[R]]++;
res+=tot[C[R]]*tot[C[R]];
}
while(R>q[i].r)
{
res-=tot[C[R]]*tot[C[R]];
tot[C[R]]--;
res+=tot[C[R]]*tot[C[R]];
R--;
}
ans[q[i].id]=res;
}
for(i=;i<=m;i++)printf("%lld\n",ans[i]);
fclose(stdin);
fclose(stdout);
return ;
}
Bzoj 3781: 小B的询问 莫队,分块,暴力的更多相关文章
- bzoj 3781 小B的询问 —— 莫队
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3781 就是莫队,左端点分块排序,块内按右端点排序,然后直接做即可. 代码如下: #inclu ...
- 【模板】BZOJ 3781: 小B的询问 莫队算法
http://www.lydsy.com/JudgeOnline/problem.php?id=3781 N个数的序列,每次询问区间中每种数字出现次数的平方和,可以离线. 丢模板: #include ...
- BZOJ 3781: 小B的询问 [莫队]
求区间每种颜色出现次数平方和 写裸题练手 #include <iostream> #include <cstdio> #include <algorithm> #i ...
- 小B的询问 莫队分块
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...
- BZOJ 3781: 小B的询问
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 643 Solved: 435[Submit][Status][Discuss ...
- bzoj 3781: 小B的询问 分块
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 196 Solved: 135[Submit][Status] Descrip ...
- 洛谷P2709 BZOJ 3781 小B的询问 (莫队)
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...
- bzoj 3781 小B的询问(莫队算法)
[题意] 若干个询问sigma{ cnt[i]^2 } cnt[i]表示i在[l,r]内的出现次数. [思路] 莫队算法,裸题. 一个cnt数组即可维护插入与删除. [代码] #include< ...
- 洛谷 P2709 BZOJ 3781 小B的询问
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求$\sum_1^Kc_i^2$的值,其中$c_i$表示数字i在[L..R]中的重复次数.小B请 ...
随机推荐
- javascript类继承系列二(原型链)
原型链是采用最主要的继承方式,原理:每一个类(构造器,js中的function)都有一个原型属性(prototype)指向一个原型对象,原型对象有一个构造器(constructor),它又指回到fun ...
- insert一句话实现插入一条数据并且返回这条数据的某列
insert into [table] output inserted.columnName values();
- 案例:latch: cache buffers chains event tuning
前两天对oracle数据库(single instance)进行了迁移升级从10.2.0.4 升到11.2.0.3,有一个项目迁完后第二天,cpu负载升到了130更高(16cpus). 向用户询问后使 ...
- C++自定义异常处理
自定义异常类 class MyException { public: MyException() { } MyException(char* str) { msg = str; } MyExcepti ...
- eclipse和cygwin搭建C++环境的修正版本
最近嫌弃切换系统麻烦.用了cygwin作为netbeans下C++的环境继续学习.我学的很渣,就不卖弄了. 网络上有很多这样的文章.经过对比和实验发现一个深坑.教程部分还是跟其他的一样,重点部分红字做 ...
- MVVM模式应用 之为ApplicationBarIconButton 添加Command操作属性
在学习MVVM的过程中,总是会遇到挫折,一碰到就是花费好长时间去解决..唉,不求量,只求质. 第一种(已经实践成功): (1)http://bindableapplicationb.codeplex. ...
- 《wc》-linux命令五分钟系列之十七
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- jquery学习之旅
在jQuery中,css()方法的功能是设置或获取元素的某项样式属性. $<"div">.css("font-weight","bold& ...
- Gherkin学习笔记
前言 由于项目准备使用BDD模式开发,所以最近在学习BDD,同时也记录下自己的学习点滴. 参考原文:https://github.com/cucumber/cucumber/wiki/Gherkin ...
- node初步一:HTTP请求
一. 创建pathtest.js文件 var http= require('http' ); var url= require('url' ); function start (){ function ...