HDU 3308 LCIS (线段树区间合并)
2015-09-29 23:20
309 查看
题意: 查询区间连续最长上升子串的长度 有单点修改操作
分析: 之前的风格草这种题太容易了 - - 随手写完就A了
代码:
//
// Created by TaoSama on 2015-09-29
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, q, a
;
typedef pair<int, int> Interval;
struct Node {
Node() {}
Node(int l, int r): l(l), r(r) {sz = r - l + 1;}
int l, r, sz;
int preR, sufL;
Interval sub;
} dat[N << 2];
Interval getMax(Interval x, Interval y) {
return x.second - x.first > y.second - y.first ? x : y;
}
Node push_up(Node x, Node y) {
Node ret(x.l, y.r);
ret.preR = x.preR;
ret.sufL = y.sufL;
ret.sub = getMax(x.sub, y.sub);
if(a[x.r] < a[x.r + 1]) {
if(ret.preR - ret.l + 1 == x.sz) ret.preR = y.preR;
if(ret.r - ret.sufL + 1 == y.sz) ret.sufL = x.sufL;
ret.sub = getMax(ret.sub, Interval(x.sufL, y.preR));
}
return ret;
}
void build(int l, int r, int rt) {
if(l == r) {
dat[rt] = Node(l, r);
dat[rt].preR = dat[rt].sufL = l;
dat[rt].sub = Interval(l, l);
scanf("%d", a + l);
return;
}
int m = l + r >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
dat[rt] = push_up(dat[rt << 1], dat[rt << 1 | 1]);
}
void update(int o, int v, int rt) {
if(dat[rt].l == dat[rt].r) {
a[dat[rt].l] = v;
return;
}
int m = dat[rt].l + dat[rt].r >> 1;
if(o <= m) update(o, v, rt << 1);
else update(o, v, rt << 1 | 1);
dat[rt] = push_up(dat[rt << 1], dat[rt << 1 | 1]);
}
Node query(int L, int R, int rt) {
if(L <= dat[rt].l && dat[rt].r <= R) return dat[rt];
int m = dat[rt].l + dat[rt].r >> 1;
if(L <= m && R > m) return push_up(query(L, R, rt << 1), query(L, R,
rt << 1 | 1));
else if(L <= m) return query(L, R, rt << 1);
else return query(L, R, rt << 1 | 1);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &q);
build(0, n - 1, 1);
while(q--) {
char op[2]; int x, y;
scanf("%s%d%d", op, &x, &y);
if(*op == 'U') update(x, y, 1);
else {
Interval ans = query(x, y, 1).sub;
printf("%d\n", ans.second - ans.first + 1);
}
}
}
return 0;
}
分析: 之前的风格草这种题太容易了 - - 随手写完就A了
代码:
//
// Created by TaoSama on 2015-09-29
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, q, a
;
typedef pair<int, int> Interval;
struct Node {
Node() {}
Node(int l, int r): l(l), r(r) {sz = r - l + 1;}
int l, r, sz;
int preR, sufL;
Interval sub;
} dat[N << 2];
Interval getMax(Interval x, Interval y) {
return x.second - x.first > y.second - y.first ? x : y;
}
Node push_up(Node x, Node y) {
Node ret(x.l, y.r);
ret.preR = x.preR;
ret.sufL = y.sufL;
ret.sub = getMax(x.sub, y.sub);
if(a[x.r] < a[x.r + 1]) {
if(ret.preR - ret.l + 1 == x.sz) ret.preR = y.preR;
if(ret.r - ret.sufL + 1 == y.sz) ret.sufL = x.sufL;
ret.sub = getMax(ret.sub, Interval(x.sufL, y.preR));
}
return ret;
}
void build(int l, int r, int rt) {
if(l == r) {
dat[rt] = Node(l, r);
dat[rt].preR = dat[rt].sufL = l;
dat[rt].sub = Interval(l, l);
scanf("%d", a + l);
return;
}
int m = l + r >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
dat[rt] = push_up(dat[rt << 1], dat[rt << 1 | 1]);
}
void update(int o, int v, int rt) {
if(dat[rt].l == dat[rt].r) {
a[dat[rt].l] = v;
return;
}
int m = dat[rt].l + dat[rt].r >> 1;
if(o <= m) update(o, v, rt << 1);
else update(o, v, rt << 1 | 1);
dat[rt] = push_up(dat[rt << 1], dat[rt << 1 | 1]);
}
Node query(int L, int R, int rt) {
if(L <= dat[rt].l && dat[rt].r <= R) return dat[rt];
int m = dat[rt].l + dat[rt].r >> 1;
if(L <= m && R > m) return push_up(query(L, R, rt << 1), query(L, R,
rt << 1 | 1));
else if(L <= m) return query(L, R, rt << 1);
else return query(L, R, rt << 1 | 1);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &q);
build(0, n - 1, 1);
while(q--) {
char op[2]; int x, y;
scanf("%s%d%d", op, &x, &y);
if(*op == 'U') update(x, y, 1);
else {
Interval ans = query(x, y, 1).sub;
printf("%d\n", ans.second - ans.first + 1);
}
}
}
return 0;
}
相关文章推荐
- java学习笔记--线程同步
- Android之Activity之间的数据通信方式大全(一)
- 初识KMP算法
- POJ 1741 Tree (树分治入门)
- 同伴个人代码复审结果
- nginx+tomcat
- 安装潮流组合--LAMP+NFS(编译php-fpm模块与apache2.4结合)
- UNIX环境高级编程——第四章
- css学习
- UIView属性的使用
- Hibernate中HQL函数汇总及其说明
- 统计图表可视化适用规则
- go struct结构的实例
- 乱七八糟
- 如何编写让别人能读懂的代码?
- 华丽转身——英语总结
- 传感器的使用:加速度传感器
- UIScrollView的偏移量
- 第 十 八 天 : 构 建 一 个 基 础 的 discuz 论坛
- 链表的颠倒算法