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] ...
随机推荐
- Codeforces Round #243 (Div. 2) A~C
题目链接 A. Sereja and Mugs time limit per test:1 secondmemory limit per test:256 megabytesinput:standar ...
- POJ 3393 Lucky and Good Months by Gregorian Calendar
http://poj.org/problem?id=3393 题意 : 对于这篇长长的英语阅读,表示无语无语再无语,花了好长时间,终于读完了.题目中规定每周的周六日为假日,其他为工作日,若是一个月的第 ...
- http://www.cnblogs.com/amboyna/archive/2008/03/08/1096024.html
http://www.cnblogs.com/amboyna/archive/2008/03/08/1096024.html
- Ubuntu 12.04安装字体
http://www.2cto.com/os/201210/160645.html 安装方法,终端输入: $ sudo thunar /usr/share/fonts/truetype 待ture ...
- 手机金属外壳加工工艺:铸造、锻造、冲压、CNC
现如今金属手机成为行业的热点,在消费电子产品中应用越来越广,本文详细介绍几种金属加工工艺及相关产品应用. 1.CNC+阳极:iPhone 5/6, HTC M7 2.锻造+CNC:华为P8,HTC M ...
- QT UAC问题汇总贴
http://www.qtcn.org/bbs/read-htm-tid-47983.html http://www.cnblogs.com/bombless/archive/2010/12/29/h ...
- 208. Implement Trie (Prefix Tree)
题目: Implement a trie with insert, search, and startsWith methods. 链接: http://leetcode.com/problems/i ...
- 188. Best Time to Buy and Sell Stock IV
题目: 链接: 题解: 测试: Reference:
- log4j:ERROR A "org.jboss.logging.appender.FileAppender" object is not assignable to a "org.apache.lo .
log4j:ERROR A "org.jboss.logging.appender.FileAppender" object is not assignable to a &quo ...
- word文档左侧显示目录
word2007 选择word的视图,然后选择文档结构图