bzoj 3781 小B的询问——分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3781
非常经典的分块套路。于是时间空间比大家的莫队差了好多……
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const int N=5e4+,M=;
int n,m,w,base,a[N],cnt[M][N],ct[N];
ll sm[M][M],ans;
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return fx?ret:-ret;
}
int bh(int a){return (a-)/base+;}
void init()
{
int R=bh(n);
for(int i=;i<=R;i++)
{
int t=i*base,j=i;
if(t>n)
{
t=(i-)*base;
for(int k=t+;k<=n;k++)
sm[i][i]+=(cnt[i][a[k]]<<)+,cnt[i][a[k]]++;
j=i-;
}
for(;t;j--)
{
sm[i][j]+=sm[i][j+];//
for(int k=;k<=base;k++,t--)
{
sm[i][j]+=(cnt[i][a[t]]<<)+;
cnt[i][a[t]]++;
}
}
}
}
int main()
{
n=rdn(); m=rdn(); w=rdn();
base=sqrt(n);
for(int i=;i<=n;i++)a[i]=rdn();
init();
for(int i=,l,r,u,v;i<=m;i++)
{
l=rdn(); r=rdn(); u=bh(l); v=bh(r);
if(v-u<=)
{
ans=;
for(int j=l;j<=r;j++) ct[a[j]]=;
for(int j=l;j<=r;j++)
ans+=(ct[a[j]]<<)+,ct[a[j]]++;
printf("%lld\n",ans);continue;
}
ans=sm[v-][u+];
int L=u*base,R=(v-)*base;
for(int j=l;j<=L;j++)
{
ans+=((cnt[v-][a[j]]-cnt[u][a[j]])<<)+;
cnt[v-][a[j]]++;
}
for(int j=R+;j<=r;j++)
{
ans+=((cnt[v-][a[j]]-cnt[u][a[j]])<<)+;
cnt[v-][a[j]]++;
}
for(int j=l;j<=L;j++) cnt[v-][a[j]]--;
for(int j=R+;j<=r;j++) cnt[v-][a[j]]--;
printf("%lld\n",ans);
}
return ;
}
bzoj 3781 小B的询问——分块的更多相关文章
- bzoj 3781: 小B的询问 分块
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 196 Solved: 135[Submit][Status] Descrip ...
- Bzoj 3781: 小B的询问 莫队,分块,暴力
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 426 Solved: 284[Submit][Status][Discuss ...
- BZOJ 3781: 小B的询问
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 643 Solved: 435[Submit][Status][Discuss ...
- 洛谷P2709 BZOJ 3781 小B的询问 (莫队)
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...
- 洛谷 P2709 BZOJ 3781 小B的询问
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求$\sum_1^Kc_i^2$的值,其中$c_i$表示数字i在[L..R]中的重复次数.小B请 ...
- 【模板】BZOJ 3781: 小B的询问 莫队算法
http://www.lydsy.com/JudgeOnline/problem.php?id=3781 N个数的序列,每次询问区间中每种数字出现次数的平方和,可以离线. 丢模板: #include ...
- bzoj 3781 小B的询问 —— 莫队
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3781 就是莫队,左端点分块排序,块内按右端点排序,然后直接做即可. 代码如下: #inclu ...
- bzoj 3781 小B的询问(莫队算法)
[题意] 若干个询问sigma{ cnt[i]^2 } cnt[i]表示i在[l,r]内的出现次数. [思路] 莫队算法,裸题. 一个cnt数组即可维护插入与删除. [代码] #include< ...
- BZOJ 3781: 小B的询问 [莫队]
求区间每种颜色出现次数平方和 写裸题练手 #include <iostream> #include <cstdio> #include <algorithm> #i ...
随机推荐
- What is love ? and how to do?
1.匹配度(matched-degree): 灵性 文化(东西方.南北方) 智力 审美 性 2.对待差异的原则(The principle of difference): 抓大放小 求同存异 心脑并用 ...
- Android 学习之逐帧动画(Frame)
帧动画就是将一些列图片.依次播放. 利用肉眼的"视觉暂留"的原理,给用户的感觉是动画的错觉,逐帧动画的原理和早期的电影原理是一样的. a:须要定义逐帧动画,能够通过代码定义.也能够 ...
- poj Kindergarten
Kindergarten 又是一道自己没思考出来的题 !!!!! 还是老样子,题目去我拉的专题里有. 题目: 给出G给女孩,B给男孩.女孩之间是相互认识的,男孩之间也是相互认识的.如今题目中给出M对男 ...
- Something about cache
http://www.tyut.edu.cn/kecheng1/2008/site04/courseware/chapter5/5.5.htm 5.5 高速缓冲存储器cache 随着CPU时钟速率的不 ...
- spring核心包功能解析
- 解压 boot.img
./split_bootimg.pl boot.img Page size: 2048 (0x00000800) Kernel size: 7062084 (0x006bc244) Ramdisk s ...
- UVA 11888 - Abnormal 89's(Manachar)
UVA 11888 - Abnormal 89's option=com_onlinejudge&Itemid=8&page=show_problem&category=524 ...
- IP和归属地
ip: http://www.ip.cn/index.php?ip=10.132.98.143 归属地: http://www.ip138.com:8080/search.asp?action=mob ...
- codeforces Looksery Cup 2015 H Degenerate Matrix
The determinant of a matrix 2 × 2 is defined as follows: A matrix is called degenerate if its determ ...
- POJ 1195 Mobile phones (二维树状数组)
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...