• [1457] Sona

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

    Sona can't speak but she can make fancy music. Her music can attack, heal, encourage and enchant.

    There're an ancient score(乐谱). But because it's too long, Sona can't play it in a short moment. So Sona decide to just play a part of it and revise it.

    A score is composed of notes. There are 109 kinds of notes and a score has105 notes at most.

    To diversify Sona's own score, she have to select several parts of it. The energy of each part is calculated like that:

    Count the number of times that each notes appear. Sum each of the number of times' cube together. And the sum is the energy.

    You should help Sona to calculate out the energy of each part.

  • 输入
  • This problem contains several cases. And this problem provides 2 seconds to run.
    The first line of each case is an integer N (1 ≤ N ≤ 10^5), indicates the number of notes.
    Then N numbers followed. Each number is a kind of note. (1 ≤ NOTE ≤ 10^9)
    Next line is an integer Q (1 ≤ Q ≤ 10^5), indicates the number of parts.
    Next Q parts followed. Each part contains 2 integers Li and Ri, indicates the left side of the part and the right side of the part.
  • 输出
  • For each part, you should output the energy of that part.
  • 样例输入
  • 8
    1 1 3 1 3 1 3 3
    4
    1 8
    3 8
    5 6
    5 5
  • 样例输出
  • 128
    72
    2
    1

题目链接:NBUT 1457

莫队算法就是能用n*sqrt(n)的时间来解决无修改区间查询的一种办法……,主要处理就是把询问暂时不按照读入顺序而按照所在块和右端点值为关键字进行排序,然后对这样的一个暂时非正常的询问顺序进行计算回答,然后可以再次按照输入顺序排序或者用数组记录答案后输出。

可以参考这篇博客:莫队算法经典例题

这题写出立方公式就很容易发现a^3和(a+1)^3以及(a-1)^3的区别,然后就可以很方便地进行O(1)转化,网上一些题解代码都是暴力减掉当前的再加回更新的,即减掉a*a*a再加上(a+1)*(a+1)*(a+1),感觉这样写不太好……,反正如果能快速得到[L-1,R]或[L,R+1]这样的区间信息的话莫队算法值得一试。这题用了输入输出外挂搞到2100+MS,一些人直接用立方加减的3300+MS

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef __int64 LL;
const double PI=acos(-1.0);
const int N=100010; struct info
{
int l;
int r;
LL energy;
int id;
};
info node[N];
LL arr[N];
int unit,b[N];
LL cnt[N];
LL ans[N];
inline bool cmp(const info &a,const info &b)
{
if(a.l/unit!=b.l/unit)
return a.l/unit<b.l/unit;
return a.r<b.r;
} void init()
{
CLR(arr,0);
CLR(cnt,0);
}
inline void update(int pos,int n,LL &temp)
{
LL &val=arr[pos];
if(n==1)
temp=temp+3*cnt[val]*cnt[val]+3*cnt[val]+1;
else
temp=temp-3*cnt[val]*cnt[val]+3*cnt[val]-1;
cnt[val]+=n;
}
int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
void Out(LL a)
{
if(a>9)
Out(a/10LL);
putchar(a%10LL+'0');
}
int main(void)
{
int n,i,j,k,L,R;
while (~scanf("%d",&n))
{
for (i=1; i<=n; ++i)
{
arr[i]=Scan();
b[i]=arr[i];
}
sort(b+1,b+1+n); int m=unique(b+1,b+1+n)-b-1;
for (i=1; i<=n; ++i)
arr[i]=lower_bound(b+1,b+1+m,arr[i])-b; unit=(int)sqrt(1.0*n);
scanf("%d",&k);
for (i=0; i<k; ++i)
{
node[i].l=Scan();
node[i].r=Scan();
node[i].id=i;
}
sort(node,node+k,cmp);
L=node[0].l;
R=node[0].l-1;
LL temp=0;
for (i=0; i<k; ++i)
{
while (L>node[i].l)
update(--L,1,temp);
while (R<node[i].r)
update(++R,1,temp);
while (L<node[i].l)
update(L++,-1,temp);
while (R>node[i].r)
update(R--,-1,temp);
ans[node[i].id]=temp;
}
for (i=0; i<k; ++i)
{
Out(ans[i]);
putchar('\n');
}
init();
}
return 0;
}

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

  1. NBUT 1457 莫队算法 离散化

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

  2. NBUT1457 Sona 莫队算法

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

  3. NBUT 1457 Sona

    莫队算法+离散化 1.map会TLE,必须离散化做 2.long long会WA,__int64定义 %I64d输出输出能AC 3.注意输入的序列会爆int #include<cstdio> ...

  4. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  5. 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...

  6. CodeForces - 220B 离散化+莫队算法

    莫队算法链接:传送门 题意: 有n个数,m个区间.问区间内有多少个x,x满足x的个数等于x的值的个数(如果x是3,区间内要存在3个3). 题解: 因为a[i]太大,所以要离散化一下,但是不能用map容 ...

  7. HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)

    链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...

  8. BZOJ3289 Mato的文件管理(莫队算法+树状数组)

    题目是区间逆序数查询. 莫队算法..左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了.询问个数和数字个数都记为n,数字范围不确定所以离散化,这 ...

  9. CSU 1515 Sequence (莫队算法)

    题意:给n个数,m个询问.每个询问是一个区间,求区间内差的绝对值为1的数对数. 题解:先离散化,然后莫队算法.莫队是离线算法,先按按询问左端点排序,在按右端点排序. ps:第一次写莫队,表示挺简单的, ...

随机推荐

  1. Ubuntu 用户安装 MATE

      MATE 是经典桌面 Gnome 2 的分支,该桌面按照 Windows 用户操作习惯设计,适合于 Windows 转投 Linux 的初级用户,MATE 做了功能改进和新增功能.如:增加窗口管理 ...

  2. HDU 2082 母函数模板题

    找单词 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  3. RecyclerView拖拽排序和滑动删除实现

    效果图 如何实现 那么是如何实现的呢?主要就要使用到ItemTouchHelper ,ItemTouchHelper 一个帮助开发人员处理拖拽和滑动删除的实现类,它能够让你非常容易实现侧滑删除.拖拽的 ...

  4. mysql 超级管理员

    mysql> grant all privileges on *.* to 'master'@'%' identified by '3306' with grant option; Query ...

  5. 编辑并列DIV

    html编辑控件,编写3个div并行,先看一下效果,如下图: 我需要将整个白色部分作为一个整体,编辑控件使用,其实使用到的就是div中的float:left属性,并且定义好宽度.css 部分代码如下: ...

  6. Mongodb安装(Mac平台)

    1安装: sudo brew install mongodb 2 启动MongoDb sudo mongod —config /usr/local/etc/mongod.conf 3.登录mongo ...

  7. android 获取路径目录方法以及判断目录是否存在,创建目录

    Environment 常用方法: * 方法:getDataDirectory()解释:返回 File ,获取 Android 数据目录.* 方法:getDownloadCacheDirectory( ...

  8. CodeForces 433C Ryouko's Memory Note-暴力

                                             Ryouko's Memory Note Time Limit:1000MS     Memory Limit:262 ...

  9. js上传和预览图片

    [1].[代码] [HTML]代码 跳至 [1] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  10. ember.js:使用笔记6 子项目的前进与后退

    如下代码会根据model产生不同的table项,在进行其他设置后,一般是根据id来跳转到相应项目子项中: {{#each}} {{#link-to "tabls" this}}{{ ...