D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;
- D. Powerful array
- time limit per test
- seconds
- memory limit per test
- megabytes
- input
- standard input
- output
- standard output
- An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + ..., ar, where ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of products Ks·Ks·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.
- You should calculate the power of t given subarrays.
- Input
- First line contains two integers n and t ( ≤ n, t ≤ ) — the array length and the number of queries correspondingly.
- Second line contains n positive integers ai ( ≤ ai ≤ ) — the elements of the array.
- Next t lines contain two positive integers l, r ( ≤ l ≤ r ≤ n) each — the indices of the left and the right ends of the corresponding subarray.
- Output
- Output t lines, the i-th line of the output should contain single positive integer — the power of the i-th query subarray.
- Please, do not use %lld specificator to read or write -bit integers in C++. It is preferred to use cout stream (also you may use %I64d).
- Examples
- Input
- Output
- Input
- Output
- Note
- Consider the following array (see the second sample) and its [, ] subarray (elements of the subarray are colored):
- Then K1 = , K2 = , K3 = , so the power is equal to · + · + · = .
- /**
- 题目:D. Powerful array
- 链接:http://codeforces.com/problemset/problem/86/D
- 题意:给定n个数,m次查询;每次查询[l,r]的权值;
- 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x;
- 所有贡献和即为该区间的值;
- 思路:由于静态区间,所以离线+莫队算法; 然后(cnt+1)*(cnt+1)*x-cnt*cnt*x=(2*cnt+1)*x;
- 来优化计算;常规暴力计算超时;
- */
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<algorithm>
- #include<cmath>
- #include<map>
- #include<set>
- #include<vector>
- #include<string>
- #include<queue>
- #include<bitset>
- using namespace std;
- typedef long long ll;
- const int inf = 0x3f3f3f3f;
- const double eps=1e-;
- const int maxn = 2e5+;
- const ll mod = 1e9+;
- ll num[], c[maxn], pos[maxn];
- ll ans;
- int n , m;
- struct node
- {
- ll l, r;
- ll ans;
- int id;
- }t[maxn];
- int cmp_id(node a,node b)
- {
- return a.id<b.id;
- }
- int cmp(node a,node b)
- {
- if(pos[a.l]==pos[b.l]) return a.r<b.r;
- return pos[a.l]<pos[b.l];
- }
- void update(int place,int add)
- {
- ll v = c[place];
- if(add==){
- ans += v*(*num[v]+);
- num[v]++;
- }else
- {
- num[v]--;
- ans -= v*(*num[v]+);
- }
- }
- void solve()
- {
- memset(num, , sizeof num);
- ans = ;
- for(int i = t[].l; i <= t[].r; i++){
- update(i,);
- }
- t[].ans = ans;
- for(int i = ; i <= m; i++){
- for(int j = t[i-].l; j < t[i].l; j++) update(j,-);//减得时候,自身开始;
- for(int j = t[i-].l; j > t[i].l; j--) update(j-,);//增的时候,不包括自身;
- for(int j = t[i-].r; j < t[i].r; j++) update(j+,);
- for(int j = t[i-].r; j > t[i].r; j--) update(j,-);
- t[i].ans = ans;
- }
- }
- int main()
- {
- while(scanf("%d%d",&n,&m)==)
- {
- for(int i = ; i <= n; i++) scanf("%I64d",&c[i]);
- for(int i = ; i <= m; i++){
- scanf("%I64d%I64d",&t[i].l,&t[i].r);
- t[i].id = i;
- }
- int N = int(sqrt(n));
- for(int i = ; i <= n; i++){
- pos[i] = i/N+;
- }
- sort(t+,t++m,cmp);
- solve();
- sort(t+,t++m,cmp_id);
- for(int i = ; i <= m; i++){
- printf("%I64d\n",t[i].ans);
- }
- }
- return ;
- }
D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;的更多相关文章
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=s< ...
- Codeforces 86D Powerful array(莫队算法)
和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces D. Powerful array(莫队)
题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...
- [BZOJ3781]:小B的询问(离线莫队算法)
题目传送门 题目描述 小B有一个序列,包含$N$个$1~K$之间的整数.他一共有$M$个询问,每个询问给定一个区间$[L...R]$,求$\sum \limits_{i=1}^{K}c(i)^2$的值 ...
- CodeForces 86D Powerful array(莫队+优化)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- C++ 莫队算法(转)
胡小兔的良心莫队教程:莫队.带修改莫队.树上莫队 在开始学习莫队之前,照例先甩一道例题:BZOJ 1878 HH的项链. 题意:求区间内数的个数,相同的数只算一次. 在我关于这道题的上一篇题解中, ...
- HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)
传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
随机推荐
- jdk8新特性
JDK8新特性(JDK8的新特性) * 接口中可以定义有方法体的方法,如果是非静态,必须用default修饰 * 如果是静态的就不用了 class Test { public void run() { ...
- Java中泛型得到T.class
例子: public class Test<T> { public Class<T> getTClass() { return (Class<T>) ((Param ...
- 关于DNS,你应该知道这些
在互联网时代中,如果要问哪个应用层协议最重要的话,我想答案无疑是DNS.虽然我们每天都享受着DNS服务带来的便利, 却对它往往知之甚少.因此本文就来介绍一下DNS协议的工作流程,真正认识一下这个支撑着 ...
- Instant Run 的操作影响到了代码,导致Android App启动闪退的问题
转自yuhc163原文android启动应用java.lang.NoClassDefFoundError: Class not found using the boot class loader; n ...
- 纯JS操作获取桌面路径方法
//active 控件获取当前用户的桌面的路径的方法 var wsh = new ActiveXObject("wscript.shell"); listall(wsh.Speci ...
- python获取系统信息,
1 操作系统系统综合信息cur_sysinfo=platform.uname()浏览器信息cur_browserinfo=self.driver.capabilities['version']浏览器新 ...
- python中list/tuple/dict/set的区别
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推.Python有6个序列的内置类型,但最常见的是列表list和元组t ...
- SQL注入之导出WebShell
已经听N个人过说有人已经发现SQL注入Access得到webshell的技术了,也只是听说而已,具体的细节还是不得而知. 最近在看的书中一章提到Jet的安全,然后灵光一闪,呵呵,发现了一种可以利用ac ...
- CentOS7.x 通过mail命令发,使用465端口(smtps协议)发送邮件
#创建证书mkdir -p /root/.certs/echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CE ...
- B3:状态模式 State
当一个对象内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.状态模式主要解决当控制一个对象状态转换条件表达式过于复杂时的情况,把状态判断逻辑移到表示不同状态的一系列类中.如果状态判断很简单, ...