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前缀和为某数的个数并增加或减 ...
随机推荐
- Database: coursera assignment 1
q.1: Find the titles of all movies directed by Steven Spielberg. select title from moviewhere direct ...
- Java多线程系列 基础篇07 synchronized底层优化
转载 http://www.cnblogs.com/paddix/ 作者:liuxiaopeng http://www.infoq.com/cn/articles/java-se-16-synchro ...
- 虚拟化技术及ip netns简介
虚拟化技术: Iass:infrastructure as a server 直接启动一个虚拟机,需要什么程序自己安装 Paas:platform as a servicce 启动一个虚拟机,并安装了 ...
- 让loadrunner走下神坛(全)
作者: sunshinelius(转载请注明作者) Loadrunner无疑是一个强大有力的压力测试工具.它的脚本可以录制生成,自动关联:测试场景可以面向指标,多方监控:测试结果图表显示,拆分组合.相 ...
- Mockito @BeforeClass @BeforeMethod @BeforeTest 的生命周期
@BeforeClass---@AfterClass 类实例化前, 被执行, 主要用于设置环境变量等, 与SpringTestContext结合用的时候要注意, 这种情况下@autowire的bean ...
- Hadoop- HDFS的Safemode
Hadoop- HDFS的Safemode hadoop启动时,NameNode启动完后就开始进入安全模式,等待DataNode向NameNode发送block report ,当datanode b ...
- JAVA- continue与break与return关键字
continue与break关键字 for(int i=0;i<10;i++){ if(i%2==0){ continue; //跳过当前循环执行下一次循环 } System.out.print ...
- 分享知识-快乐自己:Spring中的(三种)异常处理机制
案例目录结构: Web.xml 配置: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application ...
- HihoCoder1653 : 公平分队([Offer收割]编程练习赛39)(贪心)
描述 小Hi和小Ho在玩一个战争游戏.游戏中2N个战斗单位,其中第i个单位的战斗力是Ai. 现在小Hi和小Ho要各选N个单位组成队伍,当然他们都希望自己队伍的总战斗力越大越好. 为了使分队更加公平,经 ...
- iOS UINavgationController、 UINavigationBar、 UINavigationItem关系分析
一般导航控制器含有4个对象,UINavigationController.UINavigationBar.UIViewController.UINavigationItem. 1:UINavigati ...