Bzoj 3781: 小B的询问 莫队,分块,暴力
2024-08-22 22:31:44
3781: 小B的询问
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 426 Solved: 284
[Submit][Status][Discuss]
Description
小B有一个序列,包含N个1~K之间的整数。他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数。小B请你帮助他回答询问。
Input
第一行,三个整数N、M、K。
第二行,N个整数,表示小B的序列。
接下来的M行,每行两个整数L、R。
Output
M行,每行一个整数,其中第i行的整数表示第i个询问的答案。
Sample Input
6 4 3
1 3 2 1 1 3
1 4
2 6
3 5
5 6
1 3 2 1 1 3
1 4
2 6
3 5
5 6
Sample Output
6
9
5
2
9
5
2
HINT
对于全部的数据,1<=N、M、K<=50000
Source
这道题同 小Z的袜子 。。。
只需要把 加和减 改为 加减 个数的平方 即可。。。
代码:
#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 ;
}
最新文章
- gulp + webpack + sass 学习
- iOS-多线程基础
- java 22 - 4 多线程的代码实现的方式1
- Java集合系列:-----------01集合的整体框架
- ubuntu --- shortcut key
- shell 脚本编程概述
- cellery ImportError &; AttributeError
- HDU 1180 (BFS搜索)
- [转]编译Android源代码常见错误解决办法
- HDU 5422 Rikka with Graph
- 手游:cocos2d-x3.0 移植 wp8 开发 各种 “蛋疼”问题的汇总
- Top命令查看内存
- CodeForces 689A -Mike and Cellphone
- Adobe Flash Builder 4.7 新功能详解
- CPU-Z五大主要功能及使用方法初步了解
- linux中一些常用的目录简要说明
- git安装以及初始化
- recv() failed (104: Connection reset by peer) while reading response header from upstream
- 主题模型之潜在语义分析(Latent Semantic Analysis)
- sencha touch 在线实战培训 第一期 第五节