BZOJ_3781_小B的询问_莫队

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

Sample Output

6
9
5
2

莫队水题,只是当时忘了在BZ上交....
早期代码欣赏?
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define N 50050
#define LL long long
int n, q, c[N], h[N], maxn ,block, pos[N];
LL now;
struct A {
int s, t, id;
LL ans;
}a[N];
bool cmp1(const A &x,const A &y) {
if(pos[x.s] == pos[y.s]) return x.t < y.t;
return pos[x.s] < pos[y.s];
}
bool cmp2(const A &x,const A &y) {return x.id < y.id; }
void update(int x,int sig) {
now -= 1ll * h[c[x]] * h[c[x]];
h[c[x]] += sig;
now += 1ll * h[c[x]] * h[c[x]];
}
int main() {
scanf("%d%d%d",&n,&q,&maxn);
int i, j, block = sqrt(n), l, r = 0;
for(i = 1;i <= n; ++ i) scanf("%d", &c[i]);
for(i = 1;i <=block; ++ i) {
l = r + 1;
r = i * block;
for(j = l;j <= r; ++ j) {
pos[j] = i;
}
}
if(r != n) {
++ block;
l = r + 1;
r = n;
for(i = l;i <= r; ++ i) pos[i] = block;
}
for(i = 1;i <= q; ++ i) scanf("%d%d",&a[i].s,&a[i].t),a[i].id = i;
sort(a + 1, a + q + 1, cmp1);
for(l = 1, r = 0, i = 1;i <= q; ++ i) {
while(l < a[i].s) update(l, -1), ++ l;
while(l > a[i].s) update(l - 1, 1), -- l;
while(r < a[i].t) update(r + 1, 1), ++ r;
while(r > a[i].t) update(r, -1), -- r;
a[i].ans = now;
}
sort(a + 1, a + q + 1, cmp2);
for(i = 1;i <= q; ++ i) printf("%d\n",a[i].ans);
}

BZOJ_3781_小B的询问_莫队的更多相关文章

  1. [bzoj3781]小B的询问_莫队

    小B的询问 bzoj-3781 题目大意:给定一个n个数的序列,m次询问.每次询问一段区间内数的种类的平方和. 注释:$1\le n\,m\le 5\cdot 10^4$. 想法:莫队练习题. 我们考 ...

  2. 小B的询问(题解)(莫队)

    小B的询问(题解)(莫队) Junlier良心莫队 题目 luoguP2709 小B的询问 code #include<bits/stdc++.h> #define lst long lo ...

  3. 洛谷2709 小B的询问(莫队)

    题面 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R] ...

  4. 【Luogu P2709 小B的询问】莫队

    题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...

  5. P2709 小B的询问(莫队)

    P2709 小B的询问 莫队模板 资磁离线询问 维护两个跳来跳去的指针 先分块,蓝后询问按块排序. 蓝后每次指针左右横跳更新答案 #include<iostream> #include&l ...

  6. 2018.07.01 洛谷小B的询问(莫队)

    P2709 小B的询问 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数 ...

  7. 小B的询问(莫队)

    题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...

  8. 洛谷P2709 BZOJ 3781 小B的询问 (莫队)

    题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...

  9. P2709 小B的询问——普通莫队&&模板

    普通莫队概念 莫队:莫涛队长发明的算法,尊称莫队.其实就是优化的暴力. 普通莫队只兹磁询问不支持修改,是离线的. 莫队的基本思想:就是假定我得到了一个询问区间[l,r]的答案,那么我可以在极短(通常是 ...

随机推荐

  1. GDI+ ColorMatrix的完全揭秘

    无论是用何种语言,只要使用过Windows的GDI+的人对ColorMatrix都不陌生,我的BLOG文章中也多次提到过,并在<GDI+ for VCL基础 -- 颜色调整矩阵ColorMatr ...

  2. Android Camera探究之路——起步

    Android Camera探究之路--起步 Camera在手机中有着举足轻重的地位,无论是二维码还是照片.识别.都离不开摄像头,本文将对Android中的Camera进行全面解析. 权限镇楼: &l ...

  3. ggplot2-为图形加入直线

    本文更新地址:http://blog.csdn.net/tanzuozhev/article/details/51112057 本文在 http://www.cookbook-r.com/Graphs ...

  4. smali语法(一)

    一.什么是Smali? Smali,Baksmali分别是指安卓系统里的Java虚拟机(Dalvik)所使用的一种dex格式文件的汇编器,反汇编器.其语法是一种宽松式的Jasmin/dedexer语法 ...

  5. css实现轮播效果图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. java 最长回文字串

      package string.string1_6; public class LongestPalidrome { /** * 使用常规方法, 以字符串的每一个字符作为中心进行判断, 包括奇数和偶 ...

  7. 系统函数C字符串的实现(11):strchr

    字符查找函数strchr char *mystrchr(const char *str, const char c) { char *p = NULL; for (char*newp = str; * ...

  8. ZeroMQ Distributed Messaging

    ZeroMQ \zero-em-queue\, \ØMQ\: Ø  Connect your code in any language, on any platform. Ø  Carries mes ...

  9. send data to Flume client-sdk flume使用之httpSource

    https://flume.apache.org/FlumeDeveloperGuide.html#client-sdk flume使用之httpSource - CSDN博客 https://blo ...

  10. mysql-test-run.pl

    wget https://raw.githubusercontent.com/mysql/mysql-server/5.7/mysql-test/mysql-test-run.pl