CodeForces 86 D Powerful array 莫队
Powerful array
题意:求区间[l, r] 内的数的出现次数的平方 * 该数字。
题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了。
这个题目中发现了一个很神奇的事情,本来数组开1e6大小就直接过了4100+ms, 想测试一下inline,顺手把空间砍成了刚好够用,然后跑的更慢了 4700+ms,删了inline之后T了,到现在也不知道发生了啥。
如果有大牛路过希望帮我看下, CF run id 38822420(4100+) 38822607(4700+)38822636(TLE)。然后就是我空间稍微开大一点就跑的更快了。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); 4 #define LL long long 5 #define ULL unsigned LL 6 #define fi first 7 #define se second 8 #define pb push_back 9 #define lson l,m,rt<<1 10 #define rson m+1,r,rt<<1|1 11 #define max3(a,b,c) max(a,max(b,c)) 12 #define min3(a,b,c) min(a,min(b,c)) 13 #define _S(X) cout << x << ' '; 14 #define __S(x) cout << x << endl; 15 typedef pair<int,int> pll; 16 const int INF = 0x3f3f3f3f; 17 const LL mod = (int)1e9+7; 18 const int N = 8e6+ 100; 19 int n, m, blo; 20 int cnt[N]; 21 int a[N]; 22 LL ans[N]; 23 LL tmp; 24 struct Node{ 25 int l, r; 26 int id; 27 }q[N]; 28 void Add(int p){ 29 tmp -= 1ll * a[p] * cnt[a[p]] * cnt[a[p]]; 30 cnt[a[p]]++; 31 tmp += 1ll * a[p] * cnt[a[p]] * cnt[a[p]]; 32 } 33 void Remove(int p){ 34 tmp -= 1ll * a[p] * cnt[a[p]] * cnt[a[p]]; 35 cnt[a[p]]--; 36 tmp += 1ll * a[p] * cnt[a[p]] * cnt[a[p]]; 37 } 38 bool cmp(Node x1, Node x2){ 39 if(x1.l/blo != x2.l/blo) 40 return x1.l/blo < x2.l / blo; 41 return x1.r < x2.r; 42 } 43 int main(){ 44 scanf("%d%d", &n, &m); 45 blo = sqrt(n); 46 for(int i = 1; i <= n; i++) 47 scanf("%d", &a[i]); 48 for(int i = 1; i <= m; i++){ 49 scanf("%d%d", &q[i].l, &q[i].r); 50 q[i].id = i; 51 } 52 sort(q+1, q+1+m, cmp); 53 int nL = 0, nR = 0, tL, tR; 54 for(int i = 1; i <= m; i++){ 55 tL = q[i].l, tR = q[i].r; 56 while(nL < tL) Remove(nL++); 57 while(nL > tL) Add(--nL); 58 while(nR <= tR) Add(nR++); 59 while(nR > tR+1) Remove(--nR); 60 ans[q[i].id] = tmp; 61 } 62 for(int i = 1; i <= m; i++){ 63 printf("%I64d\n", ans[i]); 64 } 65 return 0; 66 }
86D
转载于:https://www.cnblogs.com/MingSD/p/9122549.html
CodeForces 86 D Powerful array 莫队相关推荐
- CodeForces - 86D Powerful array(莫队)
题目链接:点击查看 题目大意:给出一个由n个数字组成的数列,再给出m次查询,每次查询要求输出[l,r]中的答案,这个题目的答案为: 假设x为区间[l,r]内的数,出现的次数记为cnt[x],则数x的贡 ...
- cf D. Powerful array 莫队算法
D. Powerful array 题意:给定一个序列>>每次查询一个区间>>查询该区间内 出现过的数字*出现的次数的平方 的和 思路:学习莫队的第一题或者说小z的袜子是第一题 ...
- Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队
题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...
- Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队算法
链接: http://codeforces.com/problemset/problem/86/D 题意: 给你一个数组,每次询问一个区间,求对于每个数,算出这个数在这个区间出现的个数的平方再*这个数 ...
- 【打CF,学算法——四星级】CodeForces 86D Powerful array (莫队算法)
[CF简介] 题目链接:CF 86D 题面: D. Powerful array time limit per test 5 seconds memory limit per test 256 meg ...
- Codeforces D. Powerful array(莫队)
题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...
- Powerful array CodeForces - 86D (莫队算法)
An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- CodeForces - 617E XOR and Favorite Number (莫队+前缀和)
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...
最新文章
- Nature:Gordon组采用甘露糖苷选择性抑制尿路致病性大肠杆菌
- Tomcat启动报错 Could not reserve enough space for object heap
- LIstview滑动时不加载图片,停止时加载!
- jQuery中 :first 和 :last 选择器诡异问题
- C++bidirectional dijkstra双向最短路径算法(附完整源码)
- 手把手引进门之 ASP.NET Core Entity Framework Core(官方教程翻译版 版本3.2.5)
- Android 识别图片二维码
- 编译cubieboard android 源码过程详解之(六):pack
- Forget Yourself
- 如何防范短信接口被恶意攻击
- ObjectC 与 C++ 混编时的编译器设置
- 什么是0day漏洞?如何预防0day攻击?
- Selenium WebDriver 常用API
- 已经出狱的李一男和即将出狱的王欣,还能赶上这个时代吗?
- 158、如何分辨出一台PoE交换机是否标准PoE供电
- 【C#】Excel舍入函数Round、RoundUp、RoundDown的C#版
- iOS 直播间礼物动画队列
- 在Windows10上编译SWASH模型
- SVM适合小数据量原因
- Ubuntu16.04 安装 CUDA、CUDNN、OpenCV 并用 Anaconda 配置 Tensorflow 和 Caffe 详细过程