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 ...
随机推荐
- C# 垃圾回收机制(转)
摘要:今天我们漫谈C#中的垃圾回收机制,本文将从垃圾回收机制的原理讲起,希望对大家有所帮助. GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由 ...
- 【PHP伪静态】时获取不规则的URL参数
$url = explode('/', '/article/category-5/status-2/page-3'); $params = array(); foreach ($url as $v) ...
- mysql 修改 添加 删除 表字段
添加表的字段 alter table 表名 add 字段名 字段的类型 例子: alter table table1 add transactor varchar(10) n ...
- Android简单逐帧动画Frame的实现(二)
Android简单逐帧动画Frame的实现 Android简单逐帧动画Frame的实现 1.逐帧动画 即是通过播放预先排序好的图片来实现动态的画面,感觉像是放电影. 2.实现步骤: 1. 在工程里 ...
- java 继承与多态
Example5_11.java class 动物 { void cry() { } } class 狗 extends 动物 { void cry() { System.out.println(&q ...
- World Finals 2003 UVA - 1025 A Spy in the Metro(动态规划)
分析:时间是一个天然的序,这个题目中应该决策的只有时间和车站,使用dp[i][j]表示到达i时间,j车站在地上已经等待的最小时间,决策方式有三种,第一种:等待一秒钟转移到dp[i+1][j]的状态,代 ...
- oracle xe client 如何设置 tnsnames.ora(解决无法使用pl/sql developer的问题)
10.2版本xe的服务器和客户端安装都很方便,由于xe的服务器只允许建立一个实例,实例名字会直接默认为xe,客户端默认安装在C:\XEClient目录下,使用sqlplus连接服务器: sqlplus ...
- 善用log日志
#-*- coding:utf-8 -*- import logging logger = logging.getLogger() #定义一个log日志对象 hdlr = logging.FileHa ...
- ratingbar设置不可调节星星数量
<RatingBar android:id="@+id/rb_bar" android:layout_width="wrap_content" andro ...
- php源码分析之php_info输出中css样式是怎么来的
我们经常使用echo phpinfo();查看php的配置信息,但是大家知道里面的css样式是怎么来的吗? 我们查看源码(php源码/ext/standard/css.c) PHPAPI void p ...