莫队算法+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. Openlayers 3 的 imagelayer

    <body> <div id="map"></div> <script> var extent = [0, 0, 1024, 968 ...

  2. oracle递归查询子节点

    通过子节点向根节点追朔. select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid 通过根节 ...

  3. 求n!末尾0的个数

    题目连接 /* £:离散数学. £:n!中2的个数>5的个数. £:2*5=10: */ #include<cstdio> #include<cstring> #incl ...

  4. 转载 Deep learning:三(Multivariance Linear Regression练习)

    前言: 本文主要是来练习多变量线性回归问题(其实本文也就3个变量),参考资料见网页:http://openclassroom.stanford.edu/MainFolder/DocumentPage. ...

  5. HDU1865--More is better(统计并查集的秩(元素个数))

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  6. Nape实现坐标旋转角度回弹

    乒乓球以一个向量运动,碰到障碍后反弹以一个新的向量运动,如下图: 要实现回弹只需要求出向量v1,把向量v0取反,再旋转(a+b)度就可以得到向量v1. 向量取反: var v:vec2 = new V ...

  7. 转:透析QTP自动化测试框架SAFFRON

    1.为什么要使用框架? 框架是一组自动化测试的规范.测试脚本的基础代码,以及测试思想.惯例的集合.可用于减少冗余代码.提高代码生产率.提高代码重用性和可维护性.例如QTestWare就是QTP自动化测 ...

  8. HDU 3265 Posters ——(线段树+扫描线)

    第一次做扫描线,然后使我对线段树的理解发生了动摇= =..这个pushup写的有点神奇.代码如下: #include <stdio.h> #include <algorithm> ...

  9. asp 随机生成字符

    <%function rndNumLetters(num) randomize dim num0,num1,num2,num3,pass if num = "" or not ...

  10. PAT乙1003

    这次终于觉得智商不够用了,特么的. 总结给你的经验,对于这样字符串的题目,经常会出现一种叫做递归定义的东西. 还有一种叫做,相同的字母表示相同的字符串. 这道题目一共有三个条件. 1. 字符串中必须仅 ...