题目大意:

求一段区间内 出现的数字的次数的三次方的和



思路分析:

这要水过去的题目真是难,各种优化。

不能用map , 要离散化之后 先处理lowerbound。

优化输入。

时间卡的非常紧。

题目直接用莫队水过去。

假设你超时的话,最好还是试试上面三种优化。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#define maxn 100005 using namespace std;
typedef long long LL; int app[maxn];
int pos[maxn];
int save[maxn];
int x[maxn]; map<int,int>mymap; struct foo
{
int l,r,index;
LL ans;
bool operator < (const foo &cmp) const
{
if(pos[l]==pos[cmp.l])return r<cmp.r;
return pos[l]<pos[cmp.l];
}
}Q[maxn];
int n;
inline void scanf_(int &num){
char in;
bool neg=false;
while(((in=getchar()) > '9' || in<'0') && in!='-') ;
if(in=='-'){
neg=true;
while((in=getchar()) >'9' || in<'0');
}
num=in-'0';
while(in=getchar(),in>='0'&&in<='9')
num*=10,num+=in-'0';
if(neg)
num=0-num;
} bool cmp_id(const foo &a,const foo &b)
{
return a.index<b.index;
}
int sz;
void modify(int p,LL &ans,int add)
{
ans-=(LL)app[x[p]]*app[x[p]]*app[x[p]];
app[x[p]]+=add;
ans+=(LL)app[x[p]]*app[x[p]]*app[x[p]];
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(app,0,sizeof app); int SIZE = (int)sqrt(n*1.0); for(int i=1;i<=n;i++)
{
scanf_(save[i]);
x[i]=save[i];
pos[i]=(i-1)/SIZE+1;
}
int m;
scanf_(m);
for(int i=0;i<m;i++)
{
scanf_(Q[i].l);
scanf_(Q[i].r);
Q[i].index=i;
} sort(save+1,save+n+1);
sz=unique(save+1,save+n+1)-save; for(int i=1;i<=n;i++)
{
x[i] = lower_bound(save+1,save+sz,x[i])-save;
}
sort(Q,Q+m);
LL ans=0;
for(int i=0,l=1,r=0;i<m;i++)
{
if(r<Q[i].r)
{
for(r=r+1;r<=Q[i].r;r++)
modify(r,ans,1);
r--;
}
if(r>Q[i].r)
{
for(;r>Q[i].r;r--)
{
modify(r,ans,-1);
}
}
if(l<Q[i].l)
{
for(;l<Q[i].l;l++)
modify(l,ans,-1);
}
if(l>Q[i].l)
{
for(l=l-1;l>=Q[i].l;l--)
modify(l,ans,1);
l++;
} if(Q[i].l==Q[i].r)
{
Q[i].ans=1;
continue;
} Q[i].ans=ans;
} sort(Q,Q+m,cmp_id);
for(int i=0;i<m;i++)
printf("%I64d\n",Q[i].ans);
}
return 0;
}

NBUT 1457 Sona (莫队算法)的更多相关文章

  1. NBUT1457 Sona 莫队算法

    由于10^9很大,所以先离散化一下,把给你的这一段数哈希 时间复杂度O(nlogn) 然后就是分块莫队 已知[L,R],由于事先的离散化,可以在O(1)的的时间更新[l+1,r],[l,r+1],[l ...

  2. NBUT 1457 Sona(莫队算法+离散化)

    [1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...

  3. NBUT 1457 莫队算法 离散化

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

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

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

  5. NPY and girls-HDU5145莫队算法

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 【http】http协议的队首阻塞

    1 队首阻塞 就是需要排队,队首的事情没有处理完的时候,后面的人都要等着. 2 http1.0的队首阻塞 对于同一个tcp连接,所有的http1.0请求放入队列中,只有前一个请求的响应收到了,然后才能 ...

  2. 【css】背景图片填充

    background: url(../img/icon_img/blue_gou.png) 0 0 no-repeat; background-size: cover; border-color: # ...

  3. 浅谈Session与Cookie的关系

    一.概念理解: 首先cookie是服务端识别客户的唯一标识的依据,客户在访问网站时候,服务端为了记住这个客户,会在服务端按照它的规则制作一个cookie数据,会将这个cookie数据保留在服务端一段时 ...

  4. Django ORM操作及进阶

    一般操作 看专业的官网文档,做专业的程序员! 必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 ...

  5. nrf52810学习笔记——三

    在开发nRF52系列的蓝牙方案的时候,会用到IDE.SDK.softdevice.nrfgoStudio等开发软件,这里做一个小小的总结. 首先,下载SDK,里面有适合keil4号iar7(iar8也 ...

  6. 算法学习记录-排序——冒泡排序(Bubble Sort)

    冒泡排序应该是最常用的排序方法,我接触的第一个排序算法就是冒泡,老师也经常那这个做例子. 冒泡排序是一种交换排序, 基本思想: 通过两两比较相邻的记录,若反序则交换,知道没有反序的记录为止. 例子: ...

  7. 【UOJ#51】【UR #4】元旦三侠的游戏(博弈论)

    [UOJ#51][UR #4]元旦三侠的游戏(博弈论) 题面 UOJ 题解 考虑暴力,\(sg[a][b]\)记录\(sg\)函数值,显然可以从\(sg[a+1][b]\)和\(sg[a][b+1]\ ...

  8. java 移位操作

    http://blog.csdn.net/javazejian/article/details/51181320 java的移位操作

  9. OSPF 提升四 Network Types & FRAM-RELAY

    Network Types 1.loopback 2.point-to-point 3.broadcast 4.NBMA 5.POINT-TO-Multipoint 6.point-To-Multip ...

  10. Codeforces Round #362 (Div. 2)

    闲来无事一套CF啊,我觉得这几个题还是有套路的,但是很明显,这个题并不难 A. Pineapple Incident time limit per test 1 second memory limit ...