2019.2.25考试T3, 离线+线段树
\(\color{#0066ff}{题解}\)
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 2e5 + 10;
const int inf = 0x7fffffff;
struct Tree {
protected:
struct node {
node *ch[2];
int l, r, min, tag;
node(int l = 0, int r = 0, int min = 0, int tag = inf): l(l), r(r), min(min), tag(tag) {}
void upd() { min = std::min(ch[0]->min, ch[1]->min); }
void trn(int val) { tag = std::min(tag, val), min = std::min(min, val); }
void dwn() {
if(tag == inf) return;
ch[0]->trn(tag), ch[1]->trn(tag);
tag = inf;
}
int mid() { return (l + r) >> 1; }
}*root;
void build(node *&o, int l, int r, int *a) {
o = new node(l, r, 0);
if(l == r) return(void)(o->min = a[l]);
int mid = (l + r) >> 1;
build(o->ch[0], l, mid, a);
build(o->ch[1], mid + 1, r, a);
o->upd();
}
void lazy(node *o, int l, int r, int val) {
if(l <= o->l && o->r <= r) return (void)(o->trn(val));
o->dwn();
if(l <= o->mid()) lazy(o->ch[0], l, r, val);
if(r > o->mid()) lazy(o->ch[1], l, r, val);
o->upd();
}
int query(node *o, int pos) {
if(o->l == o->r) return o->min;
o->dwn();
if(pos <= o->mid()) return query(o->ch[0], pos);
else return query(o->ch[1], pos);
}
public:
void build(int l, int r, int *a) { build(root, l, r, a); }
void lazy(int l, int r, int val) { lazy(root, l, r, val); }
int query(int pos) { return query(root, pos); }
}s;
int n, m, q, k;
int nxt[maxn][25];
int pre[maxn], ans[maxn];
struct node {
int l, r, id;
node(int l = 0, int r = 0): l(l), r(r) {}
friend bool operator < (const node &a, const node &b) { return a.l < b.l; }
}e[maxn];
int a[maxn], ls[maxn];
bool vis[maxn];
int id(int x) { return x + 10; }
bool judge(int l, int r) {
for(int i = l; i <= r; i++) if(vis[i] || i > n) return false;
return true;
}
void predoit() {
int now = 1;
for(int i = 1; i <= m; i++) {
vis[a[i]] = true;
while(now <= n && !judge(now, now + k - 1)) now++;
ls[i] = now;
}
s.build(1, m, ls);
#ifdef olinr
for(int i = 1; i <= m; i++) printf("%d%c", ls[i], i == m? '\n' : ' ');
#endif
for(int i = 1; i <= n; i++) pre[i] = m + 1;
for(int i = m; i >= 1; i--) {
for(int j = -k + 1; j <= k - 1; j++) {
if(a[i] + j >= 1 && a[i] + j <= n)
nxt[i][id(j)] = pre[a[i] + j];
}
pre[a[i]] = i;
}
#ifdef olinr
for(int i = 1; i <= m; i++)
for(int j = -k + 1; j <= k - 1; j++)
if(a[i] + j >= 1 && a[i] + j <= n) printf("after %d, near %d is %d\n", i, a[i] + j, nxt[i][id(j)]);
#endif
}
void work() {
std::sort(e + 1, e + q + 1);
int now = 1;
while(now <= q && e[now].l == 1) ans[e[now].id] = s.query(e[now].r), now++;
for(int i = 1; i <= m; i++) {
for(int j = -k + 1; j <= 0; j++) {
if(a[i] + j < 1 || a[i] + j + k - 1 > n) continue;
int min = m + 1;
for(int v = 0; v < k; v++) min = std::min(min, nxt[i][id(j + v)]);
s.lazy(i, min - 1, a[i] + j);
}
while(now <= q && e[now].l <= i + 1) ans[e[now].id] = s.query(e[now].r), now++;
}
}
int main() {
freopen("stall.in", "r", stdin);
freopen("stall.out", "w", stdout);
n = in(), m = in(), q = in(), k = in();
for(int i = 1; i <= m; i++) a[i] = in();
for(int i = 1; i <= q; i++) e[i].l = in(), e[i].r = in(), e[i].id = i;
predoit();
work();
for(int i = 1; i <= q; i++) printf("%d\n", ans[i] == n + 1? -1 : ans[i]);
return 0;
}
/*
10 5 15 2
3 1 6 7 5
1 1
1 2
1 3
1 4
1 5
2 2
2 3
2 4
2 5
3 3
3 4
3 5
4 4
4 5
5 5
*/
2019.2.25考试T3, 离线+线段树的更多相关文章
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- bzoj2333 离线 + 线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...
- HDU 5700 区间交 离线线段树
区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...
- BZOJ 3626 [LNOI2014]LCA 树剖+(离线+线段树 // 在线+主席树)
BZOJ 4012 [HNOI2015]开店 的弱化版,离线了,而且没有边权(长度). 两种做法 1 树剖+离线+线段树 这道题求的是一个点zzz与[l,r][l,r][l,r]内所有点的lcalca ...
- HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...
- HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...
- 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)
2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...
- HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一棵二叉树,每个结点孩子数目为0或者2. ...
随机推荐
- Shell脚本把文件从GBK转为UTF-8编码
http://www.jb51.net/article/51308.htm 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...
- Excel开发学习笔记:发布VSTO下的Excel开发项目
遇到一个数据处理自动化的问题,于是打算开发一个基于excel的小工具.在业余时间一边自学一边实践,抽空把一些知识写下来以备今后参考,因为走的是盲人摸象的野路子,幼稚与错误请多包涵. 开发环境基于VST ...
- arm开发板6410/2440上mjpg-streamer网络视频服务器移植
摄像头移植 一.环境 主机环境 :ubuntu 10.10 目标板 :FS-S5PC100 主机工具链 :gcc-4.4.5 交叉工具链 :arm-unknown-li ...
- sql---left join;right join;inner join---区别
sql---left join;right join;inner join---区别 分为以下几类: 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自 ...
- oracle 远程tns配置
BYRUIY = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = rui-oracle11g)(PORT = )) (CONNECT_DATA = ...
- python 继承进阶
继承进阶 面向对象 1.类:具有相同属性和方法 的一类事物 类名可以实例化一个对象 类名可以调用类属性,(静态属性 和(方法)动态属性) 2.对象:也就是实例 对象名:调用对象属性 调用方法 3 ...
- Solaris10 如何设置空闲ssh连接超时断开
在ssh的配置文件中有2个参数可以控制空闲连接超时断开.这2个参数是ClientAliveCountMax和ClientAliveInterval. Solaris10上设置空闲ssh连接超时断开的方 ...
- 问题:Custom tool error: Failed to generate code for the service reference 'AppVot;结果:添加Service Reference, 无法为服务生成代码错误的解决办法
添加Service Reference, 无法为服务生成代码错误的解决办法 我的解决方案是Silverlight+WCF的应用,Done Cretiria定义了需要在做完Service端的代码后首先运 ...
- oracle 基础 执行sql文件
Oracle执行外部文件: c:>sqlplus user/pwd@db sql>@new.sql 执行多个sql文件: 1.把所有的文件都放在同一个目录下,然后在命令行里执行命令: ...
- getParameter的用法及含义
equest.getparameter用来获取页面输入框输入的数据例如:jsp页面学员账户:<input type="text" name="username&qu ...