莫队算法+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的更多相关文章

  1. CSU 1515 Sequence (莫队算法)

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

  2. STL or 线段树 --- CSU 1555: Inversion Sequence

    Inversion Sequence Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1555 Mean: 给你一 ...

  3. Inversion Sequence(csu 1555)

    Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence w ...

  4. CSU 1060 Nearest Sequence

    题意:求三个序列的最长公共子序列. 思路:一开始以为只要求出前两个的LCS,然后和第三个再求一遍LCS就是答案了.但是样例就对我进行啪啪啪打脸了.实际上就跟两个序列的差不多,换成三维的就行了. 代码: ...

  5. CSU 2018年12月月赛 B 2214: Sequence Magic

    Description 有一个1到N的自然数序列1,2,3,...,N-1,N. 我们对它进行M次操作,每次操作将其中连续的一段区间 [Ai,Bi][Ai,Bi] (即第Ai个元素到第Bi个元素之间的 ...

  6. CSU 1809 Parenthesis(线段树+前缀和)

    Parenthesis Problem Description: Bobo has a balanced parenthesis sequence P=p1 p2-pn of length n and ...

  7. 课程五(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 ...

  8. CSU 1809 - Parenthesis - [前缀和+维护区间最小值][线段树/RMQ]

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1809 Bobo has a balanced parenthesis sequenc ...

  9. CSU 1809 Parenthesis(RMQ-ST+思考)

    1809: Parenthesis Submit Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n ...

随机推荐

  1. oracle一次删除多张表

    通过拼接sql语句来完成 例如有如下个表 想一次性删除,执行如下语句: select 'drop table '||table_name ||';' as dropsql from USER_TABL ...

  2. sqlserver 2008 查看表描述,和表结构

    sp_help sys_user sp_columns   sys_user --表结构 THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号 , c ...

  3. RxSwift 之官方文档

    RxSwift 官方文档结构 Introduction: Subjects Transforming Observables Filtering Observables Combining Obser ...

  4. Swift 常用字符串操作

    原文链接:http://www.jianshu.com/p/52e7580166ff 版本2:增加了Swift 2.0的语法,与Swift 1.2的语法相比,主要是:advance方法变成了advan ...

  5. oracle10g遇到ORA-16038日志无法归档问题

    SQL> shutdown immediate ORA-01109: 数据库未打开 已经卸载数据库. ORACLE 例程已经关闭. SQL> startup ORACLE 例程已经启动. ...

  6. JS-DOM操作应用高级(三)

    appendChild   1.先把元素从原有的父级上删除    2.添加到新的父级 <title>无标题文档</title> <script> window.on ...

  7. const与static的区别

    const就是只读的意思,只在声明中使用;const修饰的数据类型是指常类型,常类型的变量或对象的值是不能被更新的. const的作用: (1)可以定义const常量,具有不可变性. (2)便于进行类 ...

  8. 学习笔记——组合模式Composite

    组合模式,典型的层次结构. 与装饰器类图相似. 区别在于:装饰器模式是为了在接口中增加方法,而组合模式在于层次元素的叠加. ConcreteComponent就是中间结点,可以包含更多的Concret ...

  9. hdu_5691_Sitting in Line(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5691 题意:中文,不解释 题解:设dp[i][j]表示当前状态为i,以第j个数为末尾的最忧解,然后dp ...

  10. 格式化一个文件的大小(size),或者说是格式化一个app的大小(size)

    long number = 6243161; Formatter.formatFileSize(context, number): 需要导包,import android.text.format.Fo ...