【Codeforces193D】Two Segments
来源:互联网 发布:osi的七层网络结构图 编辑:程序博客网 时间:2024/06/07 06:29
枚举线段树lazy标记维护即可。(似乎有点太久远了)
#include <bits/stdc++.h>#define N 300009#define gc getchar()#define ll long long#define mid (l+r>>1)using namespace std;int n,a[N],pos[N];ll ans;int dl[N],dr[N],now[N],cnt,root;struct node{ int ls,rs,sum,min,len,bz;}seg[5000000];int read(){ int x=1; char ch; while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1; int s=ch-48; while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-48; return s*x;}void build(int l,int r,int &cur){ if (!cur) cur=++cnt; if (l==r) seg[cur].len=1; if (l==r) return; build(l,mid,seg[cur].ls); build(mid+1,r,seg[cur].rs);}void down(int cur){ if (seg[cur].bz) { seg[seg[cur].ls].min+=seg[cur].bz; seg[seg[cur].ls].bz+=seg[cur].bz; seg[seg[cur].rs].min+=seg[cur].bz; seg[seg[cur].rs].bz+=seg[cur].bz; seg[cur].bz=0; }}void up(int cur){ seg[cur].min=min(seg[seg[cur].ls].min,seg[seg[cur].rs].min); seg[cur].len=seg[seg[cur].ls].len*(seg[cur].min==seg[seg[cur].ls].min); seg[cur].len+=seg[seg[cur].rs].len*(seg[cur].min==seg[seg[cur].rs].min); seg[cur].sum=seg[seg[cur].ls].sum*(seg[cur].min==seg[seg[cur].ls].min); seg[cur].sum+=seg[seg[cur].rs].sum*(seg[cur].min==seg[seg[cur].rs].min); seg[cur].sum+=seg[seg[cur].ls].len*(seg[cur].min+1==seg[seg[cur].ls].min); seg[cur].sum+=seg[seg[cur].rs].len*(seg[cur].min+1==seg[seg[cur].rs].min);}void ins(int l,int r,int R,int k,int cur){ if (r<=R) { seg[cur].min+=k; seg[cur].bz+=k; return; } down(cur); ins(l,mid,R,k,seg[cur].ls); if (mid<R) ins(mid+1,r,R,k,seg[cur].rs); up(cur);}int qry(int l,int r,int R,int cur){ if (r<=R) return seg[cur].sum*(seg[cur].min<2)+seg[cur].len*(seg[cur].min<3); down(cur); int ans=qry(l,mid,R,seg[cur].ls); if (mid<R) ans+=qry(mid+1,r,R,seg[cur].rs); return ans;}int main(){ n=read(); build(1,n,root); for (int i=1;i<=n;i++) a[i]=read(),pos[a[i]]=i; for (int i=1;i<=n;i++) { ins(1,n,i,1,root); if (pos[i]-1>0&&a[pos[i]-1]<i) ins(1,n,a[pos[i]-1],-1,root); if (pos[i]+1<=n&&a[pos[i]+1]<i) ins(1,n,a[pos[i]+1],-1,root); if (i>1) ans+=qry(1,n,i-1,root); } printf("%lld\n",ans); return 0;}
阅读全文
0 0
- 【Codeforces193D】Two Segments
- Codeforces 193D Two Segments (线段树)
- Segments
- Segments
- Segments
- Segments
- Segments
- CF 121D Lucky Segments (two points)
- Codeforces 193 D. Two Segments(线段树)
- 【Codeforces Round #122 (Div. 1)】Codeforces 193D Two Segments
- two scripts - monitoring the usage of rollback segments of Oracle Database
- 1991Segments
- using segments
- Gstreamer Segments
- Gstreamer Segments
- Predefined Segments
- POJ3304 Segments
- poj3304 - Segments
- 关于全栈工程师的一点想法
- 带你了解面向对象的设计原则
- iOS中语音合成技术
- 为什么苹果的利润不会受HTML5影响?
- cannot resolve corresponding JNI function的解决方法
- 【Codeforces193D】Two Segments
- ORACLE 各类博客
- 完美解决OTA提示apply_patch_space 错误!(更新)
- 链表的基本操作
- 在linux下安装java环境方法
- 软件程序运行的原理,运行是什么意思
- C#进阶系列——WebApi 路由机制剖析:你准备好了吗?
- linux 下 进程和线程的区别
- mysql -- 递归查询所有子节点