CSU 1515 Sequence (莫队算法)
题意:给n个数,m个询问。每个询问是一个区间,求区间内差的绝对值为1的数对数。
题解:先离散化,然后莫队算法。莫队是离线算法,先按按询问左端点排序,在按右端点排序。
ps:第一次写莫队,表示挺简单的,不过这题之前乱搞一气一直TLE,莫队还是很强大的。
代码:
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll; struct Node {
int val;
int pos;
bool operator < (const Node x) const {
return val < x.val;
}
} a[];
int b[], c[]; int tmp[];
ll so[]; struct query {
int l, r, id;
bool operator < (const query x) const {
if (l == x.l) return r < x.r;
return l < x.l;
}
} q[]; int update(int x, int d)
{
int ans = d * ((tmp[x+] + tmp[x-]));
if (d < ) tmp[x]--; else tmp[x]++;
return ans;
}
// 我好菜啊
int main()
{
//freopen("in.txt", "r", stdin);
int n, m;
while (~scanf("%d%d", &n, &m)) {
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i].val);
a[i].pos = i;
}
sort(a+, a++n);
b[] = ;for (int i = ; i <= n; ++i) {
if (a[i].val == a[i-].val) b[i] = b[i-];
else if (a[i].val == a[i-].val + ) b[i] = b[i-]+;
else b[i] = b[i-] + ;
}
for (int i = ; i <= n; ++i) {
c[ a[i].pos ] = b[i];
}
memset(tmp, , sizeof tmp); for (int i = ; i < m; ++i) {
scanf("%d%d", &q[i].l,&q[i].r);
q[i].id = i;
}
sort(q, q+m); int pl = , pr = ;
ll ans = ;
for (int i = ; i < m; ++i) {
int id = q[i].id;
int l = q[i].l;
int r = q[i].r;
if (pr < r) for (int i = pr+; i <= r; ++i) ans += update(c[i], );
else for (int i = pr; i > r; --i) ans += update(c[i], -);
if (pl < l) for (int i = pl; i < l; ++i) ans += update(c[i], -);
pr = r, pl = l;
so[id] = ans;
}
for (int i = ; i < m; ++i)
printf("%lld\n", so[i]);
}
return ;
}
CSU 1515 Sequence (莫队算法)的更多相关文章
- SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)
DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...
- 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]
题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述 Given a sequence of integers a1, a2, ..., an a ...
- HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)
链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...
- 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] ...
随机推荐
- static函数与普通函数
转自http://blog.163.com/sunshine_linting/blog/static/44893323201191294825184/ 全局变量(外部变量)的说明之前再冠以static ...
- C++: 单例模式和缺陷
C++: 单例模式和缺陷 实现一个单例模式 1 class Singleton { 2 private: 3 Singleton() { cout << " ...
- php的redis 操作类,适用于单台或多台、多组redis服务器操作
redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...
- php获取类的实例变量
<?php class Page {private $title; //构造函数固定名称为__construct,这样能将php的类构造函数独立于类名,以后修改类名就无需修改构造函数名称 fun ...
- 影响pogo pin连接器使用寿命的因素
精细化.安装简易化及使用寿命长是现在数码电子产品的趋势发展,pogo pin连接器体积小而且弹簧伸缩式设计,可以更好的缩小数码电子产品的尺寸并且连接安装更加的简单方便,因此pogo pin连接器得到了 ...
- Android安全问题 钓鱼程序
导读:文本介绍一种钓鱼应用,讲述如何骗取用户的用户名和密码,无须root 这个话题是继续android安全问题(二) 程序锁延伸的 之前我已经展示了如何制作程序锁.当打开指定应用的时候,弹出一个密码页 ...
- ArcGIS Engine 连接SQL Server并建立关联
IWorkspaceFactory pWFactory=new OLEDBWorkspaceFactory(); IPropertySet pPropertySet=new PropertySe ...
- Redpine Signals RS9110-N-11-02 Wi-Fi解决方案
Redpine Signals公司的RS9110-N-11-02是兼容IEEE 802.11bgn的Wi-Fi客户端模块,集成了MAC,基带处理器,RF收发器和功率放大器.和RS9110-LI MAC ...
- git同步远端的分支
如果用命令行,运行 git fetch,可以将远程分支信息获取到本地, 再运行 git checkout -b local-branchname origin/remote_branchname 就 ...
- [转] Android自动化测试之使用java调用monkeyrunner(五)
Android自动化测试之使用java调用monkeyrunner 众所周知,一般情况下我们使用android中的monkeyrunner进行自动化测试时,使用的是python语言来写测试脚本.不过, ...