小B的询问 莫队分块
题目描述
小B有一个序列,包含N个1~K之间的整数。他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数。小B请你帮助他回答询问。
输入输出格式
输入格式:
第一行,三个整数N、M、K。
第二行,N个整数,表示小B的序列。
接下来的M行,每行两个整数L、R。
输出格式:
M行,每行一个整数,其中第i行的整数表示第i个询问的答案。
输入输出样例
说明
对于全部的数据,1<=N、M、K<=50000
莫队即可解决;
可参考[国家集训队]小Z的袜子
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll ans;
int n, m;
int c[maxn];
int pos[maxn];
ll sum[maxn];
ll Ans[maxn];
struct node {
int l, r, id; }nd[maxn]; bool cmpid(node a, node b) {
return a.id < b.id;
} bool cmp(node a, node b) {
if (pos[a.l] == pos[b.l])return a.r < b.r;
return a.l < b.l;
} void init() {
rdint(n); rdint(m); int K; rdint(K);
for (int i = 1; i <= n; i++)rdint(c[i]);
int blok = sqrt(n);
for (int i = 1; i <= n; i++)pos[i] = (i - 1) / blok + 1;
for (int i = 1; i <= m; i++) {
rdint(nd[i].l); rdint(nd[i].r);
nd[i].id = i;
} } void add(int p, int val) {
ans -= sqr(sum[c[p]]);
sum[c[p]] += (ll)val;
ans += sqr(sum[c[p]]);
} void sol() {
for (int i = 1, l = 1, r = 0; i <= m; i++) {
for (; r < nd[i].r; r++)add(r + 1, 1);
for (; r > nd[i].r; r--)add(r, -1);
for (; l < nd[i].l; l++)add(l, -1);
for (; l > nd[i].l; l--)add(l - 1, 1);
Ans[nd[i].id] = ans;
}
} int main() {
//ios::sync_with_stdio(0);
init();
sort(nd + 1, nd + 1 + m, cmp);
sol();
sort(nd + 1, nd + 1 + m, cmpid);
for (int i = 1; i <= m; i++) {
printf("%lld\n", Ans[nd[i].id]);
}
return 0;
}
小B的询问 莫队分块的更多相关文章
- Bzoj 3781: 小B的询问 莫队,分块,暴力
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 426 Solved: 284[Submit][Status][Discuss ...
- BZOJ2038 [2009国家集训队]小Z的袜子 莫队+分块
作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只袜子从1到N编号,然后从 ...
- BZOJ3781:小B的询问(莫队)
Description 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L ...
- 【bzoj3781】小B的询问 莫队算法
原文地址:http://www.cnblogs.com/GXZlegend/p/6803821.html 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L ...
- luogu 2709 小B的询问 莫队
题目链接 Description 小B有一个序列,包含\(N\)个\(1-K\)之间的整数.他一共有\(M\)个询问,每个询问给定一个区间\([L..R]\),求\(\sum_{i=1}^{K}c_i ...
- luoguP2709 小B的询问 [莫队]
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...
- 洛谷P2709 小B的询问 莫队
小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小 ...
- 【luogu1709】小B的询问 - 莫队
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...
- luogu 2709小b的询问--莫队
https://www.luogu.org/problemnew/show/P2709 无修改的莫队几乎没有什么太高深的套路,比较模板吧,大多都是在那两个函数上动手脚. 这题询问每一种数字数量的平方和 ...
随机推荐
- ABP缓存
简介 缓存是做什么的? 简单的可以认为是一个键值对的数据存于内存中,高速读取.作用为了减少和数据库的交互 Abp中缓存的使用 public class InvoiceAppService : Appl ...
- struts1-mapping.getInputForward()与mapping.getInput
转自:https://www.cnblogs.com/azai/archive/2010/06/05/1752416.html 奇怪为什么登陆失败的时候 没有错误提示.这个问题困扰了N久 仔细看了下, ...
- UIBezierPath和CAShapeLayer配合肆意画图
一.CAShapeLayer CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形 使用CAShapeLayer 绘制一个矩形 let layer ...
- jumpserver跳板机的搭建
搭建的跳板机基于0.3.2,别问我为什么不用0.5版本的,我能说我没有搭建成功么,步骤贼多,功能不完善,不建议生产环境使用 步骤其实很简单: github wiki :https://github.c ...
- 什么是个CDN???CDN是干什么的??
1.什么是CDN??? CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络& ...
- MyBatis总结一:快速入门
简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 ...
- strcmp()比较函数和strcasecmp()和strnatcmp()
strcmp()的函数原型如下() int strcmp(string str1,string str2) 该函数需要两个进行比较的参数字符串,如果这两个字符串相等,该函数就返回0,如果按字典顺序st ...
- Ubuntu 添加用户到sudoers
ubuntu上的用户有时候需要用到管理员权限,可以通过修改 /etc/sudoers 文件内容添加用户权限. 操作方式 1. 首先以root进入系统打开文件 sudo vim /etc/sudoers ...
- Luogu 3172 [CQOI2015]选数
考虑枚举$k$的倍数$dk$,容易知道$\left \lceil \frac{L}{K} \right \rceil\leq d\leq \left \lfloor \frac{H}{k} \righ ...
- 3.3PCL已有点类型介绍和增加自定义的点类型
1.PCL中有哪些可用的PointT类型 这些point类型都位于point_types.hpp文件中,如果用户需要自己定义类型,需要对已有类型了解. 1)PointXYZ---成员变量:float ...