NBUT 1457 Sona (莫队算法)
题目大意:
求一段区间内 出现的数字的次数的三次方的和
思路分析:
这要水过去的题目真是难,各种优化。
不能用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 (莫队算法)的更多相关文章
- NBUT1457 Sona 莫队算法
由于10^9很大,所以先离散化一下,把给你的这一段数哈希 时间复杂度O(nlogn) 然后就是分块莫队 已知[L,R],由于事先的离散化,可以在O(1)的的时间更新[l+1,r],[l,r+1],[l ...
- 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前缀和为某数的个数并增加或减 ...
随机推荐
- URL链接后面的参数解析,与decode编码解码;页面刷新回到顶部jquery
function request() { var urlStr = location.search; ) { theRequest = []; return; } urlStr = urlStr.su ...
- windows python MySQLdb 安装配置
一.环境 系统:windows10/7/8 软件: 1.python2.7.XX(https://www.python.org/downloads/或者https://www.python.org/f ...
- js 类型之间的相互转化
设置元素对象属性 var img = document.querySelector("img") img.setAttribute("src","ht ...
- iMX6QD How to Add 24-bit LVDS Support in Android
iMX6QD How to Add 24-bit LVDS Support in Android 版本 4 由 Ying Liu 于 2012-10-14 下午11:52创建,最后由 Jodi Pau ...
- uboot下include\autoconfig.mk分析
CONFIG_CMD_FAT=yCONFIG_HARD_I2C=yCONFIG_IMX_OTP=yCONFIG_CMD_ITEST=yCONFIG_ETH_PRIME=yCONFIG_CMD_BDI= ...
- opencv中相关的矩阵运算
一.矩阵Mat I,img,I1,I2,dst,A,B;double k,alpha;Scalar s;1.加法I=I1+I2;//等同add(I1,I2,I);add(I1,I2,dst,mask, ...
- 【HIHOCODER 1323】回文字符串(区间DP)
描述 给定一个字符串 S ,最少需要几次增删改操作可以把 S 变成一个回文字符串? 一次操作可以在任意位置插入一个字符,或者删除任意一个字符,或者把任意一个字符修改成任意其他字符. 输入 字符串 S. ...
- PAT Basic 1052
1052 卖个萌 萌萌哒表情符号通常由“手”.“眼”.“口”三个主要部分组成.简单起见,我们假设一个表情符号是按下列格式输出的: [左手]([左眼][口][右眼])[右手] 现给出可选用的符号集合,请 ...
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 167: illegal multibyte sequence
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 167: illegal multibyte sequence文件 ...
- centos 服务器配置
安装防火墙 安装Apache 安装MySQL 安装PHP 安装JDK 安装Tomcat 服务器上搭建Apache +MySQL+PHP +JDK +Tomcat环境,用的是Linux Centos7. ...