CodeForces 86D(Yandex.Algorithm 2011 Round 2)

思路:莫队算法,离线操作,将所有询问的左端点进行分块(分成sqrt(n) 块每块sqrt(n)个),用左端点的块号进行排序小的在前,块号相等的,右端点小的在前面。 这样要是两个相邻的查询在同一块内左端点每次最多移动sqrt(n) n次的话效率为nsqrt(n) ,对于同一块内右端点为有序的那么最多移动 n次  。总的效率为nsqrt(n) 。 要是相邻的查询不同块 最坏效率为O(n) 因为块最多为sqrt(n)个那么坏效率也为nsqrt(n)。   总的效率就为nsqrt(n)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <iostream>
#define N 200010
#define LL __int64
using namespace std;
struct node {
	int l, r, id;
	int pid;
} s[N];
inline bool cmp(node a, node b) {
	return a.id < b.id || a.id == b.id && a.r < b.r;
}
int a[N];
LL ans[N];
int cnt[1000100];
LL sum = 0;
inline void add(LL x) {
	sum += x * (cnt[x] << 1 | 1);
	cnt[x]++;
}
inline void dec(LL x) {
	sum += x * (1 - (cnt[x] << 1));
	cnt[x]--;
}
int main() {
	int i, j, k, n, m, one;
	memset(cnt, 0, sizeof(cnt));
	scanf("%d%d", &n, &m);
	one = sqrt(n * 1.0);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	for (int i = 0; i < m; ++i) {
		scanf("%d%d", &s[i].l, &s[i].r);
		s[i].id = s[i].l / one;
		s[i].pid=i;
	}
	sort(s, s + m, cmp);
	int l, r;
	l = s[0].l;
	r = s[0].r;

	for (int i = s[0].l; i <= s[0].r; ++i)
		add(a[i]);
	ans[s[0].pid]=sum;
	for (i = 1; i < m; ++i) {
		while(r<s[i].r)add(a[++r]);
		while(r>s[i].r)dec(a[r--]);
		while(l<s[i].l)dec(a[l++]);
		while(l>s[i].l)add(a[--l]);
		ans[s[i].pid]=sum;
	}
	for(int i=0;i<m;++i)
		printf("%I64d\n",ans[i]);
	return 0;
}

  

CodeForces 86D(Yandex.Algorithm 2011 Round 2)

时间: 2024-07-31 06:06:18

CodeForces 86D(Yandex.Algorithm 2011 Round 2)的相关文章

Codeforces 558(C、D、E)总结

558C 题意:给你n个数,可对每个数进行操作(乘2或者除以2).求最少的操作使得所有的数都相等. 思路 : dp[ t ] 表示所有的数转化到 t 所需的最少操作, vis[ t ] 表示有多少数可以转化成 t . 对于一个数 num , 把它所能到达的数用上述的数组记录下就行了(具体看代码). 注意 : 输入: 3 5 4 4 输出 : 2  (5/2*2=4) #include <iostream> #include <cstdio> #include <cstring

CodeForces 400D (最短路+并查集) Dima and Bacteria

 Description Dima took up the biology of bacteria, as a result of his experiments, he invented k types of bacteria. Overall, there are n bacteria at his laboratory right now, and the number of bacteria of type i equals ci. For convenience, we will

YYH的球盒游戏(NOIP模拟赛Round 6)

原题传送门 这题的算法优化真是博大精深. 具体我们一个一个来讲. 我们先看这道题的算法,(这..裸的费用流啊...) 然后我们发现负边(好吧,其实没有什么用.spfa可以跑) 首先所有的球都要向源点连费用0,流量为球的数量的边 然后我们考虑每一个球都要到汇点,所以从盒子向汇点连费用0,流量inf的边. 对于每一个球向每一个盒子连费用为-c(c为收益),流量为1的边. 重点来了:怎么处理多放? 我们已知一个公式 n^2-(n-1)^2=n*2-1; 所以我们从每一个盒子向汇点连流量为1,费用为2*

魔法使的烟花(NOIP模拟赛Round 7)

[问题描述] 魔法森林里有很多蘑菇,魔法使常常采摘它们来制作魔法药水.为了在6月的那个奇妙的晚上用魔法绽放出最绚丽的烟花,魔法使决定对魔法森林进行一番彻底的勘探. 魔法森林分为n个区域,由n-1条长度相等的道路将它们连接为了无向连通图.每个区域都有一个等级,这n个区域的等级满足如下性质: ①1号区域的等级为1: ②由一条道路连接的两个区域(即相邻两个区域)等级相差为1: ③设两个区域x,y被一条道路连接,若x到1号区域的最短距离小于y到1号区域的最短距离,则有x到1号区域的最短路径上的所有区域的

Yandex.Algorithm 2011 A. Double Cola

1.题目描写叙述:点击打开链接 2.解题思路:本题是一道找规律的数学题,通过题意描写叙述不难知道,相当于有5棵二叉树构成了一个森林,须要你按层次遍历找到第n个人是谁. 观察后不难发现,如果最開始的一层为第0层,序号n所在层为l,那么0~l-1层之间有5*(2^l-1)个结点,令5*(2^l-1)=n,计算得.l=log(n/5+1)/log(2). 因为第l-1层最后一个人的序号是ed=5*(2^l-1),第l层每一个人有2^l个,设k每一个人的序号(从0開始),则k=(n-ed-1)/(2^l

Powerful array CodeForces - 86D(莫队)

给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和.1<=n,m<=200000.1<=s<=10^6 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #includ

Powerful array CodeForces - 86D (莫队算法)

An array of positive integers a1,?a2,?...,?an is given. Let us consider its arbitrary subarray al,?al?+?1...,?ar, where 1?≤?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 th

CodeForces 171F(千古神题。。)

 D - 乐 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 171F Description qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. Input j

UVA - 10559 Blocks 和 Vasya and Binary String CodeForces - 1107E (dp OR 记忆化搜索)

UVA - 10559 Blocks 题意:消消乐,每次连续相同的可以消除,分数加上长度的平方,问最多可以获得几分全部消完 题解: 区间dp + 记忆化搜索 dp[i][j][k] : (区间 [i,  j] 后面带上一段和 j 颜色相同的且长度为 k )的消消乐最大积分 1.消最后一段颜色和 j 颜色相同的 dp[i][j][k] <-- dp[i][j-1][0] + (k+1)^2 2.对于i <= l < j, 如果 l 和 j 的颜色相同, 那么可以把 [l+1, j-1]消掉