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

Sample Output

6
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 ;
}

Bzoj 3781: 小B的询问 莫队,分块,暴力的更多相关文章

  1. bzoj 3781 小B的询问 —— 莫队

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3781 就是莫队,左端点分块排序,块内按右端点排序,然后直接做即可. 代码如下: #inclu ...

  2. 【模板】BZOJ 3781: 小B的询问 莫队算法

    http://www.lydsy.com/JudgeOnline/problem.php?id=3781 N个数的序列,每次询问区间中每种数字出现次数的平方和,可以离线. 丢模板: #include ...

  3. BZOJ 3781: 小B的询问 [莫队]

    求区间每种颜色出现次数平方和 写裸题练手 #include <iostream> #include <cstdio> #include <algorithm> #i ...

  4. 小B的询问 莫队分块

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

  5. BZOJ 3781: 小B的询问

    3781: 小B的询问 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 643  Solved: 435[Submit][Status][Discuss ...

  6. bzoj 3781: 小B的询问 分块

    3781: 小B的询问 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 196  Solved: 135[Submit][Status] Descrip ...

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

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

  8. bzoj 3781 小B的询问(莫队算法)

    [题意] 若干个询问sigma{ cnt[i]^2 } cnt[i]表示i在[l,r]内的出现次数. [思路] 莫队算法,裸题. 一个cnt数组即可维护插入与删除. [代码] #include< ...

  9. 洛谷 P2709 BZOJ 3781 小B的询问

    题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求$\sum_1^Kc_i^2$的值,其中$c_i$表示数字i在[L..R]中的重复次数.小B请 ...

随机推荐

  1. jmeter压测app

    使用代理的方式,录制app端脚本,之后用jmeter压测就没啥好说的了 1.电脑端谷歌设置本地代理(端口号为8888) 2.jmeter设置HTTP代理服务器(端口号为8888) 3.手机端wifi设 ...

  2. hibernate - Initial SessionFactory creation failed.org.hibernate.HibernateException

    对于数据库字段映射, 被映射的字段除了提供 getter方法之外, 还需要提供setter方法. 这也是java bean的一些规范. 例如, 题目中的异常 Initial SessionFactor ...

  3. MyEclipse中配置自己的JRE和tomcat

    MyEclipse中配置自己的JRE:windows>Preference>java>Installed JREs>Add>Stantard VM>next> ...

  4. CSS 伪元素

    CSS伪元素是用来添加一些选择器的特殊效果. 语法 伪元素的语法: selector:pseudo-element {property:value;} CSS类也可以使用伪元素: selector.c ...

  5. getting “fatal: not a git repository: '.'” when using post-update hook to execute 'git pull' on another repo

    Here is the script that ultimately worked. I think the bit I was originally missing that prevented i ...

  6. awk 多分隔符

    #!/bin/bash log_path="./log/" dates=`date -d '-1 days' +'%Y%m%d'` cd $log_path; for i in ` ...

  7. c++构造函数谁先执行的问题

    看到网上一哥们的帖子 http://blog.csdn.net/maray/article/details/7761709 东西不多就转发了 1 #include <iostream> u ...

  8. 2016030203 - 首次将内容推送到github中

    参考网址:http://www.cnblogs.com/plinx/archive/2013/04/08/3009159.html 和当你在你的github上创建repository后的提示信息如下 ...

  9. CocoaPods ADD private Spec Repo

    Private Pods CocoaPods is a great tool not only for adding open source code to your project, but als ...

  10. uboot全局变量

    一.global_data(include/asm-arm/global_data.h) typedef struct global_data { bd_t *bd; unsigned long fl ...