题意:

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

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;
}

最新文章

  1. js字符串方法
  2. 利用Spring的@Async异步处理改善web应用中耗时操作的用户体验
  3. html5 离线存储 worker
  4. 谈EntityFramework数据更新之技巧
  5. Linux 面试题总结
  6. 深入理解block
  7. hdu-2586-How far away ?(离线LCA)
  8. [Database] Deadlock avoidance protocol
  9. Debug程序无法运行解决
  10. leetcode Longest Common Prefix python
  11. windows7股票的,win8残疾人,安装Han澳大利亚sinoxn个时间,sinox它支持大多数windows软体
  12. Shell实例----------从文件夹里面多个文件里面查找指定内容
  13. 向php提交数据及json
  14. MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.2 Static Map with Two Layers
  15. 2019-4-22 linux学习
  16. Neo4j 第三篇:Cypher查询入门
  17. Java的大内存分页支持
  18. 发布上线前,先小秀一把俺的64位浏览器,速度那觉对是杠杠滴,上youtube,上google不费劲
  19. PureFtpd 连接数据库错误
  20. lua -- io.pathinfo

热门文章

  1. 辛星跟您玩转vim第四节之操作文本内容
  2. iOS Dev (53) 修复UIImagePickerController偷换StatusBar颜色的问题
  3. 【linux】ubuntu16.04安装vncserver实现远程访问图形界面
  4. 【linux】自动删除7天前的文件
  5. [egret+pomelo]实时游戏杂记(4)
  6. 在Linux下搭建我的世界(Minecraft)服务器
  7. 使用diff制作补丁【学习笔记】
  8. Cocos2d-x中判断点击是否在触摸屏区域
  9. PHP5.3之后的新特性【转】
  10. SENet(Squeeze-and-Excitation Networks)算法笔记---通过学习的方式来自动获取到每个特征通道的重要程度,然后依照这个重要程度去提升有用的特征并抑制对当前任务用处不大的特征