题目大意:

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



思路分析:

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

不能用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. Python9-day4 作业

    #!/usr/bin/env python# -*- coding:utf-8 -*-# Author:Timli = ["alex", "eric", &qu ...

  2. Hi3519V101 SDK安装以及开发环境搭建

    Hi3519V101 Linux开发环境 1.安装Hi3519V101 SDKHi3519V101 SDK是基于Hi3519V101 DMEB的软件开发包,包含了在Linux相关应用开发时使用的各种工 ...

  3. Flash生成HTML5动画方法

      方法一:利用“swiffy”将Flash转换成HTML5动画. 首先,我们需要下载一款基于“Flash”程序的插件,名称为“swiffy”,这是一款由谷歌推出的一个Flash扩展,可以通过“Fla ...

  4. 2. vsCode 安装GoCode

    1)安装gocode 打开命令提示符(以管理员身份打开),输入: go get -u -v github.com/nsf/gocode 开始下载: 下载完毕: 下载完成,查看D:\GoWorks目录, ...

  5. vs进行C#编程中常用的几个快捷键

    (1)输入svm然后按Tab键会生成Main函数: (2)Ctrl +k+s 三个键一起按,会调出代码段:选中多行后,然后按以上三个快捷键,输入需要使用的代码段,按下Tab,代码段会自动包括选中代码. ...

  6. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...

  7. Axure:从单一评价方式到用户自由选择

    导读: 亲,还记得淘宝对货物的评价方式吗?还记得对快递哥的评价方式吗? 1,经典五星评:                                                         ...

  8. PTA 09-排序2 Insert or Merge (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge   (25分) According to ...

  9. resetlogs报错 ORA-00392

      alter database open resetlogs或者 alter database open resetlogs upgrade报错:ORA-00392     在rman restor ...

  10. POJ 2409 Let it Bead ——Burnside引理

    [题目分析] 裸题直接做. 一个长度为n,颜色为m的环,本质不同的染色方案是多少. 数据范围比较小,直接做就好了. [代码] #include <cstdio> #include < ...