好题一个,区间合并问题
大意就是问某个区间内最长连续上升序列的长度
跟POJ 3667 Hotel 差不多,貌似还更简单一些,因为只有单点更新
用到了lmx, rmx, mx,表示从左端点往右的连续长度,右端点往左的连续长度,区间内的最大连续长度
/*
ID: sdj22251
PROG: inflate
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 100005
#define INF 1000000000
#define L(x) x<<1
#define R(x) x<<1|1
#define PI acos(-1.0)
#define eps 1e-7
using namespace std;
int a[MAXN];
struct node
{
int left, right, mid;
int lmx, rmx, mx;
}tree[4 * MAXN];
void up(int C)
{
tree[C].lmx = tree[L(C)].lmx;
tree[C].rmx = tree[R(C)].rmx;
if(tree[L(C)].lmx == tree[L(C)].right - tree[L(C)].left + 1 && a[tree[R(C)].left] > a[tree[L(C)].right])
tree[C].lmx += tree[R(C)].lmx;
if(tree[R(C)].rmx == tree[R(C)].right - tree[R(C)].left + 1 && a[tree[R(C)].left] > a[tree[L(C)].right])
tree[C].rmx += tree[L(C)].rmx;
int tmp = 0;
if(a[tree[R(C)].left] > a[tree[L(C)].right]) tmp = tree[L(C)].rmx + tree[R(C)].lmx;
tree[C].mx = max(tmp, max(tree[L(C)].mx, tree[R(C)].mx));
tree[C].mx = max(tree[C].mx, max(tree[C].lmx, tree[C].rmx));
}
void make_tree(int s, int e, int C)
{
tree[C].left = s;
tree[C].right = e;
tree[C].mid = (s + e) >> 1;
tree[C].lmx = tree[C].rmx = tree[C].mx = 1;
if(s == e) return;
make_tree(s, tree[C].mid, L(C));
make_tree(tree[C].mid + 1, e, R(C));
up(C);
}
void update(int p, int C)
{
if(tree[C].left == tree[C].right)return;
if(tree[C].mid >= p) update(p, L(C));
else update(p, R(C));
up(C);
}
int query(int s, int e, int C)
{
if(tree[C].left >= s && tree[C].right <= e) return tree[C].mx;
int s1 = 0, s2 = 0;
if(tree[C].mid >= s) s1 = query(s, e, L(C));
if(tree[C].mid < e) s2 = query(s, e, R(C));
int t = max(s1, s2);
if(a[tree[C].mid] < a[tree[C].mid + 1])
t = max(t, min(tree[C].mid - s + 1, tree[L(C)].rmx) + min(e - tree[C].mid, tree[R(C)].lmx));
return t;
}
int main()
{
int T, n, m, x, y;
char s[5];
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
make_tree(0, n - 1, 1);
while(m--)
{
scanf("%s%d%d", s, &x, &y);
if(s[0] == 'Q') printf("%d\n", query(x, y, 1));
else {a[x] = y; update(x, 1);}
}
}
return 0;
}
分享到:
相关推荐
hdu 1166线段树代码
hdu 1166线段树
【hdu5306】Gorgeous Sequence 线段树区间最值操作-附件资源
线段树入门资料,有利于初学者学习,让他们详细了解线段树。
从简单入门到偏中等的几个题,线段树很灵活,主要懂了lazy操作,其他的自己yy吧。
大量线段树题目 zoj 1610 线段覆盖 poj 2777 线段覆盖 poj 2528 需要离散化,建树不同,需要处理不同->注意这组数据 3 1 10 1 3 6 10 the ans is 3. hdu 1754 求区间最大值 hdu 1166 求区间和 hdu 1698 成段更新 ...
NULL 博文链接:https://128kj.iteye.com/blog/1738772
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
题面 【题目描述】 有nnn个营地,已知每个营地的人数,有四条命令: (1)Add(1) Add(1)Add iii jjj,iii和jjj为正整数,表示第iii个营地增加jjj个人(jjj不超过303030) (2)Sub(2)Sub(2)Sub iii jjj ,iii和jjj为正...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
Hdu 3333解题报告 题意描述: 给你n个数现在要你求在k个区间上[ai, bi]的不相同的数之和各是多少. N,000; k,000; 显然,这题不能用暴力来做。 这题我们选择用线段数来做。