线段树维护动态连续子段HDU1540
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1540
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
int abss(int a);
int lowbit(int n);
int Del_bit_1(int n);
int maxx(int a,int b);
int minn(int a,int b);
double fabss(double a);
void swapp(int &a,int &b);
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
//const ll INF=(1LL<<60);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; struct node
{
int l,r;
int lmax,rmax,len;
}tr[N<<]; void up(int rt)
{
tr[rt].rmax=tr[rs].rmax;
if(tr[rs].rmax==tr[rs].len)tr[rt].rmax=tr[rs].len+tr[ls].rmax;
tr[rt].lmax=tr[ls].lmax;
if(tr[ls].lmax==tr[ls].len)tr[rt].lmax=tr[ls].len+tr[rs].lmax;
}
void Build(int l,int r,int rt)
{
tr[rt].l=l;
tr[rt].r=r;
tr[rt].len=r-l+;
tr[rt].lmax=tr[rt].rmax=r-l+;
if(l==r)return;
int mid=l+r>>;
Build(l,mid,ls);
Build(mid+,r,rs);
// up(rt);
}
void Delete(int pos,int l,int r,int rt)
{
if(l==r)
{
tr[rt].lmax=tr[rt].rmax=;
return;
}
int mid=l+r>>;
if(pos<=mid)Delete(pos,l,mid,ls);
else Delete(pos,mid+,r,rs);
up(rt);
}
void Rebuild(int pos,int l,int r,int rt)
{
if(l==r)
{
tr[rt].lmax=tr[rt].rmax=;
return;
}
int mid=l+r>>;
if(pos<=mid)Rebuild(pos,l,mid,ls);
else Rebuild(pos,mid+,r,rs);
up(rt);
}
int QnumL(int pos,int l,int r,int rt)
{
if(pos>=r)return tr[rt].rmax;
int mid=l+r>>;
if(pos>mid)
{
if(tr[rs].lmax<pos-tr[rs].l+)
{
return max(QnumL(pos,mid+,r,rs),tr[rs].rmax-(tr[rs].r-pos+)+);
}
else return pos-tr[rs].l++tr[ls].rmax;
}
else return QnumL(pos,l,mid,ls);
}
int QnumR(int pos,int l,int r,int rt)
{
if(pos<=l)return tr[rt].lmax;
int mid=l+r>>;
if(pos<=mid)
{
if(tr[ls].rmax<tr[ls].r-pos+)
{
return max(QnumR(pos,l,mid,ls),tr[ls].lmax-(pos-tr[ls].l+)+);
}
else return tr[ls].r-pos++tr[rs].lmax;
}
else return QnumR(pos,mid+,r,rs);
} int op[N];
bool D[N];
int main()
{
int n,m;
while(~sc("%d%d",&n,&m))
{
int cnt=;
Build(,n,);
for(int i=;i<=n;++i)
D[i]=;
for(int i=;i<=m;++i)
{
char j[];
int pos=;
sc("%s",j);
if(j[]!='R')sc("%d",&pos); if(j[]=='D')
Delete(pos,,n,),op[++cnt]=pos,D[op[cnt]]=;
else if(j[]=='Q')
{
if(D[pos]==)
{
pr("0\n");
continue;
}
int ansl=QnumL(pos,,n,);
int ansr=QnumR(pos,,n,);
pr("%d\n",ansl+ansr+(ansl||ansr?-:));
}
else Rebuild(op[cnt],,n,),D[op[cnt]]=,cnt--;
}
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
线段树维护动态连续子段HDU1540的更多相关文章
- Can you answer these queries V SPOJ - GSS5 (分类讨论+线段树维护区间最大子段和)
recursion有一个整数序列a[n].现在recursion有m次询问,每次她想知道Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 &l ...
- FJUT3568 中二病也要敲代码(线段树维护区间连续最值)题解
题意:有一个环,有1~N编号,m次操作,将a位置的值改为b,问你这个环当前最小连续和多少(不能全取也不能不取) 思路:用线段树维护一个区间最值连续和.我们设出两个变量Lmin,Rmin,Mmin表示区 ...
- SPOJ - GSS1-Can you answer these queries I 线段树维护区间连续和最大值
SPOJ - GSS1:https://vjudge.net/problem/SPOJ-GSS1 参考:http://www.cnblogs.com/shanyr/p/5710152.html?utm ...
- Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)
You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defi ...
- 【xsy3423】党² 线段树+李超线段树or动态半平面交
本来并不打算出原创题的,此题集CF542A和sk的灵感而成,算个半原创吧. 题目大意: 给定有$n$个元素的集合$P$,其中第$i$个元素中包含$L_i,R_i,V_i$三个值. 给定另一个有$n$个 ...
- 【bzoj4712】洪水 树链剖分+线段树维护树形动态dp
题目描述 给出一棵树,点有点权.多次增加某个点的点权,并在某一棵子树中询问:选出若干个节点,使得每个叶子节点到根节点的路径上至少有一个节点被选择,求选出的点的点权和的最小值. 输入 输入文件第一行包含 ...
- [动态dp]线段树维护转移矩阵
背景:czy上课讲了新知识,从未见到过,总结一下. 所谓动态dp,是在动态规划的基础上,需要维护一些修改操作的算法. 这类题目分为如下三个步骤:(都是对于常系数齐次递推问题) 1先不考虑修改,不考虑区 ...
- BZOJ2733:使用并查集维护连通性之后用线段树维护+线段树合并(动态开点)
可以说是线段树合并的裸题吧 题意就是给你两个操作 一个操作是合并两个集合,这两个集合都是用权值线段树维护的,便于查询第k小元素 另一个操作就是查询区间极值了 #include<cstdio> ...
- cf213E 线段树维护hash
链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...
随机推荐
- Cash Machine (POJ 1276)(多重背包——二进制优化)
链接:POJ - 1276 题意:给你一个最大金额m,现在有n种类型的纸票,这些纸票的个数各不相同,问能够用这些纸票再不超过m的前提下凑成最大的金额是多少? 题解:写了01背包直接暴力,结果T了,时间 ...
- 三个div并排
css: .div-inline{ display:inline} html: <div class="div-inline">第一个div盒子</div> ...
- elasticsearch _bulk api
https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.htmlbulk API 允许在单个步骤中进行多次 create . ...
- elasticsearch中文分词器(ik)配置
elasticsearch默认的分词:http://localhost:9200/userinfo/_analyze?analyzer=standard&pretty=true&tex ...
- golang入门time与string转换, time加减时间, 两个时间差
package main import ( "fmt" "time") var timeLayoutStr = "2006-01-02 15:04:0 ...
- 分布式系统的应用程序性能监视工具,专为微服务、云本机架构和基于容器(Docker、K8s、Mesos)架构而设计。 SkyWalking
Apache SkyWalking™ | SkyWalking Teamhttp://skywalking.apache.org/zh/ Application performance monitor ...
- PAT 甲级 1027 Colors in Mars (20 分)(简单,进制转换)
1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way ...
- CSS - clearfix清除浮动
首先,我们来解释一下为什么要使用 clearfix(清除浮动). 通常我们在写html+css的时候,如果一个父级元素内部的子元素是浮动的(float),那么常会发生父元素不能被子元素正常撑开的情况, ...
- 深入理解Android消息机制
在日常的开发中,Android 的消息机制作为系统运行的根本机制之一,显得十分的重要. 从 Handler 发送消息开始 查看源码,Handler的post.send方法最终都会走到 public f ...
- django 之(二) --- 源码分析
CBV类视图继承 CBV:继承自View:注册的时候使用的as_view() 入口 不能使用请求方法的名字作为参数的名字 只能接受已经存在的属性对应的参数 定义了一个view 创建了一个类视图对象 保 ...