题意:

给一个序列和一些区间,每次询问对区间所有不同的数,求每个不同的出现的个数的平方*其值的总和

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【莫队算法】的更多相关文章

  1. [Codeforces86D]Powerful array(莫队算法)

    题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...

  2. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  3. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  4. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  5. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  6. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  7. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  8. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  9. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

随机推荐

  1. reduce python 的用法

    1.查看reduce 的用法 在python 命令查看 import functools help(functools) help(functools.reduce) 或者 from functool ...

  2. Pentaho BIServer Community Edtion 6.1 使用教程 第一篇 软件安装

    一.简介: Pentaho BI Server 分为企业版和社区版两个版本.其中 社区版 CE(community edtion) 为免费版本. 二.下载CE版(CentOS): 后台下载命令: no ...

  3. [容易] A + B 问题

    题目来源:http://www.lintcode.com/zh-cn/problem/a-b-problem/

  4. mvc Bundling 学习记录

    因为现在的项目JS引用很多,无意中看到了MVC4的Bundling,开始的时候感觉很不错,将所有的CSS,js文件压缩成一个文件处理,画面调用也很简单 于是,花了一个下午的时候研究了一下,并且通过各种 ...

  5. 算法(Algorithms)第4版 练习 1.3.18

    1.3.18 Deletes from the list the node immediately following x.

  6. python做简易记事本

    以下内容参考<辛星tkinter教程第二版>: from tkinter import * from tkinter.filedialog import * from tkinter.me ...

  7. Proftpd mysql认证配置文档

    Proftpd mysql认证配置文档 ver1.0, 2012/09/25 一.下载安装proftp mysql 下载 wget http://cloud.github.com/downloads/ ...

  8. python 之深浅拷贝

    浅拷贝: 深拷贝:

  9. 51nod_1236_序列求和 V3 _组合数学

    51nod_1236_序列求和 V3 _组合数学 Fib(n)表示斐波那契数列的第n项,Fib(n) = Fib(n-1) + Fib(n-2).Fib(0) = 0, Fib(1) = 1. (1, ...

  10. configured to save RDB snapshots, but is currently not able to persist o...

    Redis问题 MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on d ...