Codeforces86D【莫队算法】
题意:
给一个序列和一些区间,每次询问对区间所有不同的数,求每个不同的出现的个数的平方*其值的总和
2*2*1+1*1*2
思路:
裸的莫队算法。
补:
1.cmp写错。
2.LL运算不会进行转化。
3.莫队的起始应该直接先处理好L,R。
卡了将近2.5h,水题让自己很生气。以及不会查错误真的撒比!
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=1e6+10;
LL num[N],res[N],c[N];
struct asd
{
int id,left,right;
}e[200010];
int pos[200010];
int n,m; bool cmp(asd x,asd y)
{
if(pos[x.left]==pos[y.left])
return x.right<y.right;
return pos[x.left]<pos[y.left];
} LL ans;
void solve()
{
ans=0;
sort(e,e+m,cmp);
for(int i=e[0].left;i<=e[0].right;i++)
{
ans=ans+(2*num[c[i]]+1)*c[i];
num[c[i]]++;
}
res[e[0].id]=ans;
int L=e[0].left,R=e[0].right;
for(int i=1; i<m; i++)
{
while(R<e[i].right)
{
R++;
ans=ans+((num[c[R]]<<1)+1)*c[R];
num[c[R]]++;
}
while(R>e[i].right)
{
num[c[R]]--;
ans=ans-((num[c[R]]<<1)+1)*c[R];
R--;
}
while(L<e[i].left)
{
num[c[L]]--;
ans=ans-((num[c[L]]<<1)+1)*c[L];
L++;
}
while(L>e[i].left)
{
L--;
ans=ans+((num[c[L]]<<1)+1)*c[L];
num[c[L]]++;
}
res[e[i].id]=ans;
}
for(int i=0; i<m; i++)
printf("%I64d\n",res[i]);
} int main()
{
scanf("%d%d",&n,&m);
int block=(int)sqrt(n);
for(int i=1;i<=n;i++)
{
scanf("%I64d",&c[i]);
pos[i]=(i-1)/block+1;
}
for(int i=0;i<m;i++)
{
scanf("%d%d",&e[i].left,&e[i].right);
e[i].id=i;
}
solve();
return 0;
}
Codeforces86D【莫队算法】的更多相关文章
- [Codeforces86D]Powerful array(莫队算法)
题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- NPY and girls-HDU5145莫队算法
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
随机推荐
- SP1437 Longest path in a tree(树的直径)
应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...
- 微信小程序配置详解
在之前已经通过微信公众平台的官方网站https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html,注册好小程序并且登录成功后(这里主 ...
- [容易]在O(1)时间复杂度删除链表节点
题目来源:http://www.lintcode.com/zh-cn/problem/delete-node-in-the-middle-of-singly-linked-list/
- com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '88888888' for key 'PRIMARY'
严重: Servlet.service() for servlet jsp threw exceptioncom.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityC ...
- python的join()函数
def join(self, iterable): # real signature unknown; restored from __doc__ """ S.join( ...
- 常见的25个python面试问答
常见的25个python面试问答 说到好用简洁的大数据技术,除了Hadoop.R等等,Python也是其中熠熠生辉的一员,因而广受企业和商家的青睐.求职季,不少应聘者在面试相关职业时都被要求掌握Pyt ...
- [acm]HDOJ 2673 shǎ崽 OrOrOrOrz
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2673 拍两次序,交替输出 #include<iostream> #include< ...
- Nginx HTTP Server相关
一.Nginx安装: 采取手动编译安装 对多种重要的选项进行配置 安装前提:常用工具和库,GCC PCRE(Rewrite模块需要) pcre-devel(源码) zlib zlib-devel(源码 ...
- 集训Day13
我这个肥宅一点都不快乐嘤嘤嘤 bzoj3456 求n个点的无向简单连通图的个数 我们可以用容斥推出递推式 首先我们令为 于是有 这样就是可以用CDQ分治解决的一类递推式了 不是那么明显的变形一下得到 ...
- 【Lintcode】096.Partition List
题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...