CSU 1515 Sequence
莫队算法+map
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std; const int maxn=+;
int n,t,a[maxn],cnt[maxn*],pos[maxn];
struct X
{
int l,r,id;
} s[maxn];
int L,R;
int Ans,f[maxn]; bool cmp(const X&a,const X&b)
{
if(pos[a.l]==pos[b.l]) return a.r<b.r;
return pos[a.l]<pos[b.l];
} int main()
{
while(~scanf("%d",&n))
{
scanf("%d",&t);
int sz=sqrt(n);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
pos[i]=i/sz;
}
for(int i=; i<=t; i++)
{
scanf("%d%d",&s[i].l,&s[i].r);
s[i].id=i;
} sort(s+,s++t,cmp); map<int,int>m;
Ans=;
for(int i=s[].l; i<=s[].r; i++)
{
Ans=Ans+m[a[i]-]+m[a[i]+];
m[a[i]]++;
} f[s[].id]=Ans;
L=s[].l;
R=s[].r; for(int i=; i<=t; i++)
{
while(L<s[i].l)
{
Ans=Ans-m[a[L]-]-m[a[L]+];
m[a[L]]--;
L++;
} while(L>s[i].l)
{
L--;
Ans=Ans+m[a[L]-]+m[a[L]+];
m[a[L]]++;
} while(R>s[i].r)
{
Ans=Ans-m[a[R]-]-m[a[R]+];
m[a[R]]--;
R--;
} while(R<s[i].r)
{
R++;
Ans=Ans+m[a[R]-]+m[a[R]+];
m[a[R]]++;
} f[s[i].id]=Ans;
} for(int i=; i<=t; i++)
printf("%d\n",f[i]);
} return ;
}
CSU 1515 Sequence的更多相关文章
- CSU 1515 Sequence (莫队算法)
题意:给n个数,m个询问.每个询问是一个区间,求区间内差的绝对值为1的数对数. 题解:先离散化,然后莫队算法.莫队是离线算法,先按按询问左端点排序,在按右端点排序. ps:第一次写莫队,表示挺简单的, ...
- STL or 线段树 --- CSU 1555: Inversion Sequence
Inversion Sequence Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1555 Mean: 给你一 ...
- Inversion Sequence(csu 1555)
Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence w ...
- CSU 1060 Nearest Sequence
题意:求三个序列的最长公共子序列. 思路:一开始以为只要求出前两个的LCS,然后和第三个再求一遍LCS就是答案了.但是样例就对我进行啪啪啪打脸了.实际上就跟两个序列的差不多,换成三维的就行了. 代码: ...
- CSU 2018年12月月赛 B 2214: Sequence Magic
Description 有一个1到N的自然数序列1,2,3,...,N-1,N. 我们对它进行M次操作,每次操作将其中连续的一段区间 [Ai,Bi][Ai,Bi] (即第Ai个元素到第Bi个元素之间的 ...
- CSU 1809 Parenthesis(线段树+前缀和)
Parenthesis Problem Description: Bobo has a balanced parenthesis sequence P=p1 p2-pn of length n and ...
- 课程五(Sequence Models),第一 周(Recurrent Neural Networks) —— 3.Programming assignments:Jazz improvisation with LSTM
Improvise a Jazz Solo with an LSTM Network Welcome to your final programming assignment of this week ...
- CSU 1809 - Parenthesis - [前缀和+维护区间最小值][线段树/RMQ]
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1809 Bobo has a balanced parenthesis sequenc ...
- CSU 1809 Parenthesis(RMQ-ST+思考)
1809: Parenthesis Submit Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n ...
随机推荐
- iOS 判断奇偶数
if (_bigUrlArray.count%2==0) {//如果是偶数 a = i*(_bigUrlArray.count/count);//每个线程图片初始数 b = (i+1)*(_bigUr ...
- thrift概述
Apache Thrift 是FaceBook实现的一种跨平台的远程服务调用(RPC)的框架.它采用接口描述语言(IDL)定义并创建服务,传输数据采用二进制格式,相对于XML和Json等常用数据传输方 ...
- HDU/5499/模拟
题目链接 模拟题,直接看代码. £:分数的计算方法,要用double; #include <set> #include <map> #include <cmath> ...
- Block 实现 浅析
前言 这里 有关于 block 的 5 道测试题,建议你阅读本文之前先做一下测试. 先介绍一下什么是闭包.在 wikipedia 上,闭包的定义) 是: In programming language ...
- FZU 2091 播放器(栈应用)
栈基础应用 #include<iostream> #include<cstdio> #include<cstring> #include<vector> ...
- as3 公式
AS3缓动公式:sprite.x += (targetX - sprite.x) * easing;//easing为缓动系数变量sprite.y += (targetY - sprite.y) * ...
- Chapter 1 First Sight——22
The girls were opposites. The tall one was statuesque. 女生却相反,高的像雕像一般 She had a beautiful figure, the ...
- 分类算法-----KNN
摘要: 所谓K最近邻,就是k个最近的邻居的意思,说的是每个样本都可以用她最接近的k个邻居来代表.kNN算法的核心思想是如果一个样本在特征空间中的k个最相似的样本中的大多数属于某一个类别,则该样本也属于 ...
- Html基础详解之(CSS)
css选择器 CSS选择器用于选择你想要的元素的样式的模式. “CSS”列表示在CSS版本的属性定义(CSS1,CSS2,CSS3). CSS id和class选择器 <!DOCTYPE htm ...
- mac编译openssl扩展报错 openssl.c:44:10: fatal error: 'openssl/evp.h' file not found
解决办法 brew link openssl --force 然后 ./configure --with-openssl --with-php-config=/usr/local/php/bin/ph ...