【BZOJ】3781: 小B的询问(莫队算法)
http://www.lydsy.com/JudgeOnline/problem.php?id=3781
还能不能再裸点。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=50005;
struct dat { int l, r, id; }q[N];
int a[N], n, m, k, pos[N];
ll sum, ans[N], s[N]; inline bool cmp(const dat &a, const dat &b) { return pos[a.l]==pos[b.l]?a.r<b.r:a.l<b.l; }
inline void fix(const int &x, const int &f) {
sum-=s[x]*s[x];
s[x]+=f;
sum+=s[x]*s[x];
}
void init() {
int sz=sqrt(0.5+n);
for1(i, 1, n) pos[i]=i/sz;
sort(q+1, q+1+m, cmp);
}
int main() {
read(n); read(m); read(k);
for1(i, 1, n) read(a[i]);
for1(i, 1, m) read(q[i].l), read(q[i].r), q[i].id=i;
init();
int l=1, r=0;
for1(i, 1, m) {
int xl=q[i].l, xr=q[i].r, id=q[i].id;
while(l<xl) fix(a[l++], -1);
while(l>xl) fix(a[--l], 1);
while(r<xr) fix(a[++r], 1);
while(r>xr) fix(a[r--], -1);
ans[id]=sum;
}
for1(i, 1, m) printf("%lld\n", ans[i]);
return 0;
}
Description
Input
Output
Sample Input
1 3 2 1 1 3
1 4
2 6
3 5
5 6
Sample Output
9
5
2
HINT
对于全部的数据,1<=N、M、K<=50000
Source
【BZOJ】3781: 小B的询问(莫队算法)的更多相关文章
- 【模板】BZOJ 3781: 小B的询问 莫队算法
http://www.lydsy.com/JudgeOnline/problem.php?id=3781 N个数的序列,每次询问区间中每种数字出现次数的平方和,可以离线. 丢模板: #include ...
- Bzoj 3781: 小B的询问 莫队,分块,暴力
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 426 Solved: 284[Submit][Status][Discuss ...
- bzoj 3781 小B的询问 —— 莫队
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3781 就是莫队,左端点分块排序,块内按右端点排序,然后直接做即可. 代码如下: #inclu ...
- BZOJ 3781: 小B的询问 [莫队]
求区间每种颜色出现次数平方和 写裸题练手 #include <iostream> #include <cstdio> #include <algorithm> #i ...
- 【bzoj3781】小B的询问 莫队算法
原文地址:http://www.cnblogs.com/GXZlegend/p/6803821.html 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L ...
- bzoj 2308 小Z的袜子(莫队算法)
小Z的袜子 [题目链接]小Z的袜子 [题目类型]莫队算法 &题解: 莫队算法第一题吧,建议先看这个理解算法,之后在参考这个就可以写出简洁的代码 我的比第2个少了一次sort,他的跑了1600m ...
- bzoj 2038 小Z的袜子 莫队算法
题意 给你一个长度序列,有多组询问,每次询问(l,r)任选两个数相同的概率.n <= 50000,数小于等于n. 莫队算法裸题. 莫队算法:将序列分为根号n段,将询问排序,以L所在的块为第一关键 ...
- BZOJ 2038 小z的袜子 & 莫队算法(不就是个暴力么..)
题意: 给一段序列,询问一个区间,求出区间中.....woc! 贴原题! 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过 ...
- bzoj 3781 小B的询问(莫队算法)
[题意] 若干个询问sigma{ cnt[i]^2 } cnt[i]表示i在[l,r]内的出现次数. [思路] 莫队算法,裸题. 一个cnt数组即可维护插入与删除. [代码] #include< ...
- BZOJ 3781: 小B的询问
3781: 小B的询问 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 643 Solved: 435[Submit][Status][Discuss ...
随机推荐
- WINDOWS和Linux上安装php7 alpha 并安装 yaf
WINDOWS和Linux上安装php7 alpha 并安装 yaf PHP技术 widuu 2个月前 (06-15) 126浏览 0评论 windows 1.windows上安装 php7 ...
- 【Hadoop】史上最全 Hadoop 生态 全景图
- Greedy:Stall Reservations(POJ 3190)
牛挤奶 题目大意:一群牛很挑剔,他们仅在一个时间段内挤奶,而且只能在一个棚里面挤,不能与其他牛共享地方,现在给你一群牛,问你如果要全部牛都挤奶,至少需要多少牛棚? 这一题如果把时间区间去掉,那就变成装 ...
- google maps js v3 api教程(2) -- 在地图上添加标记
原文链接 google maps javascript官方文档:https://developers.google.com/maps/documentation/javascript/ 我们在创建地图 ...
- Excel VBA Dir
内容来自ExcelHome网站 一.题目: 要求编写一段代码,运用Dir函数返回一个文件夹的文件列表.二.代码:Sub 示例_1_12() Dim wjm wjm = Di ...
- 网络流-最大流 模板(poj 1273)
#include<cstdio> #include<iostream> #include<cstring> #include<queue> #defin ...
- C++复制(拷贝)构造函数详解
本文转载自 http://blog.csdn.net/lwbeyond/article/details/6202256,在此感谢作者 CTemp B(A); //复制构造函数,C++风格的 ...
- SpringMVC详细示例实战
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
- [译]SQL Server 之 查询计划的简单参数化
SQL Server能把一些常量自动转化为参数,以重用这些部分的查询计划. SELECT FirstName, LastName, Title FROM Employees WHERE Employe ...
- Android__Context
Context字面意思上下文,位于framework package的android.content.Context中, 其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过Co ...