题意:

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

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. 我的Java开发学习之旅------>求N内所有的素数

    一.素数的概念 质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能被其他自然数(质数)整除,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数 ...

  2. Scipy.sparse矩阵的存储,读取和转化为稠密矩阵

    import numpy as np import scipy.sparse as sp m = sp.lil_matrix((7329,7329)) np.save(path,m) #用numpy的 ...

  3. Android适配API23之后权限的动态申请

    一.权限介绍 对于6.0以下的权限及在安装的时候,根据权限声明产生一个权限列表,用户只有在同意之后才能完成app的安装,造成了我们想要使用某个app,就要默默忍受其一些不必要的权限(比如是个app都要 ...

  4. Spring Boot2.0之 jar打包方式

    Jar类型打包方式 1.使用mvn celan  package 打包 2.使用java –jar 包名 war类型打包方式 1.使用mvn celan package 打包 2.使用java –ja ...

  5. Strom 消息处理机制 中英对照翻译 (Storm如何保证消息被完全处理)

    官方链接: http://storm.incubator.apache.org/documentation/Guaranteeing-message-processing.html What does ...

  6. java 创建 HMAC 签名

    ava 创建 HMAC 签名 psd素材 1. []ComputopTest.java package com.javaonly.hmac.test; import java.io.IOExcepti ...

  7. laravel 在apache或nginx的配置

    laravel 下载后,如何运行起来呢,根据自己的应用,记录了几个关键点: 1.apache 配置: 打开http.conf文件,将mod_rewrite前面的#去掉(启用重写模块): 2.nginx ...

  8. Linux_服务器_05_CentOS 7安装完成后初始化的方法_Linux

    参考资料 1.CentOS 7安装完成后初始化的方法_Linux

  9. 使用js获取当前页面的url网址信息。

    1.设置或获取整个 URL 为字符串: window.location.href 2.设置或获取与 URL 关联的端口号码: window.location.port 3.设置或获取 URL 的协议部 ...

  10. AOP和IOC的作用(转)

    AOP和IOC的作用 转载▼     IOC:控制反转,是一种设计模式.一层含义是控制权的转移:由传统的在程序中控制依赖转移到由容器来控制:第二层是依赖注入:将相互依赖的对象分离,在spring配置文 ...