NBUT1457 Sona 莫队算法
由于10^9很大,所以先离散化一下,把给你的这一段数哈希 时间复杂度O(nlogn)
然后就是分块莫队 已知[L,R],由于事先的离散化,可以在O(1)的的时间更新[l+1,r],[l,r+1],[l-1,r],[l,r-1]时间复杂度O(n*sqrt(n));
代码如下,速度并不是很快(我比较喜欢手动的去重,unique一直没怎么用过)
/*96655 's source code for B
Memory: 3744 KB Time: 2968 MS
Language: G++ Result: Accepted
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn=;
map<int,int>mp;
int a[maxn],pos[maxn],b[maxn];
LL sum;
struct node
{
int l,r,id;
LL ans;
} res[maxn];
bool cmp1(node a,node b)
{
if(pos[a.l]==pos[b.l])return a.r<b.r;
return a.l<b.l;
}
bool cmp2(node a,node b)
{
return a.id<b.id;
}
void change(int pos,int op)
{ LL temp=b[a[pos]];
sum-=temp*temp*temp;
b[a[pos]]+=op;
temp=b[a[pos]];
sum+=temp*temp*temp;
}
int main()
{
int n,q;
while(~scanf("%d",&n))
{
double cn=n;
int blk=(int)(sqrt(cn));
sum=,mp.clear();
for(int i=; i<=n; ++i)
scanf("%d",&a[i]),pos[i]=(i-)/blk+,b[i]=a[i];
sort(b+,b+n+);
int cnt=;
for(int i=; i<=n; ++i)
if(b[i]!=b[i-])b[cnt++]=b[i];
--cnt;
for(int i=; i<=n; ++i)
a[i]=lower_bound(b+,b++cnt,a[i])-(b+);
memset(b,,sizeof(b));
scanf("%d",&q);
for(int i=; i<=q; ++i)
scanf("%d%d",&res[i].l,&res[i].r),res[i].id=i;
sort(res+,res++q,cmp1);
for(int i=,l=,r=; i<=q; ++i)
{
for(; r<res[i].r; ++r)
change(r+,);
for(; r>res[i].r; --r)
change(r,-);
for(; l<res[i].l; ++l)
change(l,-);
for(; l>res[i].l; --l)
change(l-,);
res[i].ans=sum;
}
sort(res+,res+q+,cmp2);
for(int i=; i<=q; ++i)
printf("%I64d\n",res[i].ans);
}
return ;
}
NBUT1457 Sona 莫队算法的更多相关文章
- NBUT 1457 Sona(莫队算法+离散化)
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...
- 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前缀和为某数的个数并增加或减 ...
随机推荐
- C# - 高级方法参数
可选参数 -必须有个默认值,默认值必须是字面值,常量值,新对象实例或者默认值类型值. public List<string> GetWords( string sentence, bool ...
- csu 1305 Substring (后缀数组)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1305 1305: Substring Time Limit: 2 Sec Memory Limi ...
- 破解之寻找OEP[手动脱壳](1)
OEP:(Original Entry Point),程序的入口点,软件加壳就是隐藏了OEP(或者用了假的OEP), 只要我们找到程序真正的OEP,就可以立刻脱壳. PUSHAD (压栈) 代表程序的 ...
- ntpServer搭建用以进行时间同步
在试各种乱七八糟的集群中,突然发现了一个问题,假如在一个闭网环境下安装某些集群软件的时候服务器之间的时间不同步(如HBase),会导致启动失败.那么就需要进行时间同步.可是往常都是网络校准的,没网的集 ...
- python datetime笔记
python datetime笔记 http://mint-green.diandian.com/post/2011-09-09/4892024 获取当前时间,并通过字符串输出. 格式为:%Y-%m- ...
- Java Code Examples for org.springframework.http.HttpStatus
http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.http.HttpStatus
- SDUT 2527 斗地主
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2527 思路 :以前的结训比赛,当时不会做,比完 ...
- SSH hibernate 使用时最好添加访问数据库的编码
SSH hibernate 使用时最好添加访问数据库的编码 如下所示:第13行为设置hibernate访问数据库的编码(&是&的转义序列) <!DOCTYPE hibernate ...
- php析构函数
<?php class Page { function __destruct() { echo "__destruct methor invokes<br>"; ...
- MetadataType的使用
MetadataType的使用,MVC的Model层数据验证指定要与数据模型类关联的元数据类 using System.ComponentModel.DataAnnotations; //指定要与数据 ...