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

小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

【BZOJ】3781: 小B的询问(莫队算法)的更多相关文章

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

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

  2. Bzoj 3781: 小B的询问 莫队,分块,暴力

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

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

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

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

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

  5. 【bzoj3781】小B的询问 莫队算法

    原文地址:http://www.cnblogs.com/GXZlegend/p/6803821.html 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L ...

  6. bzoj 2308 小Z的袜子(莫队算法)

    小Z的袜子 [题目链接]小Z的袜子 [题目类型]莫队算法 &题解: 莫队算法第一题吧,建议先看这个理解算法,之后在参考这个就可以写出简洁的代码 我的比第2个少了一次sort,他的跑了1600m ...

  7. bzoj 2038 小Z的袜子 莫队算法

    题意 给你一个长度序列,有多组询问,每次询问(l,r)任选两个数相同的概率.n <= 50000,数小于等于n. 莫队算法裸题. 莫队算法:将序列分为根号n段,将询问排序,以L所在的块为第一关键 ...

  8. BZOJ 2038 小z的袜子 & 莫队算法(不就是个暴力么..)

    题意: 给一段序列,询问一个区间,求出区间中.....woc! 贴原题! 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过 ...

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

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

  10. BZOJ 3781: 小B的询问

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

随机推荐

  1. django signal 浅析

    默认的signals极其参数 (django 1.6.5) 模型的(django/db/models/signal.py): from django.dispatch import Signal cl ...

  2. 《ASP.NET MVC4 WEB编程》学习笔记------Web API 续

    目录 ASP.NET WEB API的出现缘由 ASP.NET WEB API的强大功能 ASP.NET WEB API的出现缘由 随着UI AJAX 请求适量的增加,ASP.NET MVC基于Jso ...

  3. outlook.office365.com传参

    string m_html = string.Empty; m_html += "<script>"; m_html += string.Format("wi ...

  4. Java for LeetCode 153 Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  5. Java for LeetCode 065 Valid Number

    Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...

  6. springJDBC实现查询方法二

    无废话,看代码: @Override public List<Sites> queryAllSites(Pager pager) { String sql = "select * ...

  7. 读取STL模型

    读取二进制格式的STL模型文件 std::ifstream fin;fin.open(stlFilePath, std::ios::in | std::ios::binary);bool isBina ...

  8. Ubuntu 12.10 安装 jdk-7u10-linux-x64.tar.gz(转载)

    在Ubuntu 12.10下安装 jdk-7u10-linux-x64.tar.gz 总的原则:将jdk-7u10-linux-x64.tar.gz压缩包解压至/usr/lib/jdk,设置jdk环境 ...

  9. android 获取资源文件 r.drawable中的图片转换为drawable、bitmap

    1.R-Drawable Resources resources = mContext.getResources(); Drawable drawable = resources.getDrawabl ...

  10. php封装练习

    <body> <?php class jisuan { private $a; private $b; function __construct($a,$b) { $this-> ...