BZOJ_1493_[NOI2007]项链工厂_Splay
BZOJ_1493_[NOI2007]项链工厂_Splay
Description
.png)

Input
Output
对于每一个 C 和 CS 命令,应输出一个整数代表相应的答案。
Sample Input
1 2 3 2 1
4
C
R 2
P 5 5 2
CS 4 1
Sample Output
1
HINT

#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
#define RR register
int check[500];
void init() {
check['R']=check['F']=check['S']=check['P']=check['C']=1;
}
inline char nc() {
static char buf[100000],*p1,*p2;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd() {
RR int x=0; RR char s=nc();
while(s<'0'||s>'9') s=nc();
while(s>='0'&&s<='9') x=(x<<3)+(x<<1)+s-'0',s=nc();
return x;
}
inline char rc() {
char s=nc();
while(!check[s]) s=nc();
return s;
}
#define N 500050
#define ls ch[p][0]
#define rs ch[p][1]
#define get(x) (ch[f[x]][1]==x)
int f[N],ch[N][2],val[N],cover[N],la[N],ra[N],as[N],rt,siz[N],rev[N],n,a[N];
void pushup(int p) {
if(!p) return ;
siz[p]=1;
if(ls) siz[p]+=siz[ls];
if(rs) siz[p]+=siz[rs];
la[p]=ra[p]=val[p];
if(ls) la[p]=la[ls];
if(rs) ra[p]=ra[rs];
as[p]=as[ls]+as[rs]+1;
if(ls&&val[p]==ra[ls]) as[p]--;
if(rs&&val[p]==la[rs]) as[p]--;
}
void pushdown(int p) {
if(!p) return ;
if(rev[p]) {
swap(ch[ls][0],ch[ls][1]); swap(ch[rs][0],ch[rs][1]);
swap(la[ls],ra[ls]); swap(la[rs],ra[rs]);
rev[ls]^=1; rev[rs]^=1; rev[p]=0;
}
if(cover[p]) {
int d=cover[p];
if(ls) la[ls]=ra[ls]=val[ls]=d,as[ls]=1,cover[ls]=d;
if(rs) la[rs]=ra[rs]=val[rs]=d,as[rs]=1,cover[rs]=d;
cover[p]=0;
}
}
void rotate(int x) {
int y=f[x],z=f[y],k=get(x);
ch[y][k]=ch[x][!k]; f[ch[y][k]]=y;
ch[x][!k]=y; f[y]=x; f[x]=z;
if(z) ch[z][ch[z][1]==y]=x;
pushup(y); pushup(x);
if(y==rt) rt=x;
}
void splay(int x,int y) {
for(int d;(d=f[x])!=y;rotate(x)) if(f[d]!=y) rotate(get(x)==get(d)?d:x);
}
int find(int x) {
int p=rt;
while(1) {
pushdown(p);
if(siz[ls]>=x) p=ls;
else {
x-=siz[ls]+1;
if(!x) return p;
p=rs;
}
}
}
void reverse(int x,int p) {
x=find(x); p=find(p+2);
splay(x,0); splay(p,rt);
rev[ls]^=1; swap(ch[ls][0],ch[ls][1]); swap(la[ls],ra[ls]);
if(ls) la[p]=ra[ls]; pushup(p); pushup(x);
}
void print() {
int i,p;
for(i=1;i<=n+2;i++) {
p=find(i);
printf("i=%d,p=%d,val[p]=%d,siz[p]=%d,f[p]=%d\n",i,p,val[p],siz[p],f[p]);
}
}
void build(int fa,int l,int r) {
if(l>r) return ;
int mid=(l+r)>>1;
val[mid]=a[mid-1];
siz[mid]=1;f[mid]=fa;
ch[fa][mid>fa]=mid;
build(mid,l,mid-1); build(mid,mid+1,r);
pushup(mid);
}
int get_ans(int x,int p) {
x=find(x); p=find(p+2);
splay(x,0); splay(p,rt);
return as[ls]?as[ls]:1;
}
void cover_col(int x,int y,int c) {
x=find(x); y=find(y+2); splay(x,0); splay(y,rt); int s=ch[y][0]; //print(); puts("FUCK");
cover[s]=c; val[s]=la[s]=ra[s]=c; as[s]=1; pushup(y); pushup(x);
}
void swapp(int x,int y) {
if(x==y) return ;
int cx=val[find(x+1)],cy=val[find(y+1)];
cover_col(x,x,cy); cover_col(y,y,cx);
}
int main() {
init();
int T;
n=rd(); T=rd();
int i,k,x,y;
for(i=1;i<=n;i++) a[i]=rd();
build(0,1,n+2); rt=(n+3)>>1;
char opt[10];
//print();
T=rd();
while(T--) {
opt[0]=rc();
if(opt[0]=='R') {
k=rd();
reverse(1,n); reverse(1,k); reverse(k+1,n); //print();
}else if(opt[0]=='F') {
reverse(2,n);
//print();
}else if(opt[0]=='S') {
x=rd(); y=rd();
swapp(x,y);
}else if(opt[0]=='P') {
x=rd(); y=rd(); k=rd();
if(x<=y) {
cover_col(x,y,k);
}else {
cover_col(x,n,k); cover_col(1,y,k);
}
//print();
}else if(opt[0]=='C') {
if(nc()=='S') {
x=rd(); y=rd();
if(x>y) {
int c1=val[find(2)],c2=val[find(n+1)];
int ans1=get_ans(x,n),ans2=get_ans(1,y);
printf("%d\n",max(ans1+ans2-(c1==c2),1));
}else {
printf("%d\n",max(get_ans(x,y),1));
}
}else {
int c1=val[find(2)],cn=val[find(n+1)];
printf("%d\n",max(get_ans(1,n)-(c1==cn),1));
}
}
}
}
BZOJ_1493_[NOI2007]项链工厂_Splay的更多相关文章
- BZOJ1493 [NOI2007]项链工厂
未完待续... 终于改对了 热泪盈眶.jpg 错误原因:pushdown的时候没有判断是否有左右儿子,也没当x=0 return,于是出现一些奇怪的错误 #include<bits/stdc++ ...
- bzoj 1493: [NOI2007]项链工厂(线段树)
1493: [NOI2007]项链工厂 Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 1256 Solved: 545[Submit][Status] ...
- 数据结构(Splay平衡树): [NOI2007] 项链工厂
[NOI2007] 项链工厂 ★★★ 输入文件:necklace.in 输出文件:necklace.out 简单对比 时间限制:4 s 内存限制:512 MB [问题描述] T公司是一 ...
- bzoj1493[NOI2007]项链工厂 线段树
1493: [NOI2007]项链工厂 Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 1712 Solved: 723[Submit][Status] ...
- 1493: [NOI2007]项链工厂
线段树. 真还就是个线段树.. 除去操作1,2的话,线段树很容易就处理了,问题在于如何处理操作1和2.(这点没想到).. 我们用一个delta维护操作1,如果没有旋转就+k,不然就-k. 每次读入i和 ...
- BZOJ1493 NOI2007 项链工厂 线段树模拟
提交地址:http://www.lydsy.com/JudgeOnline/problem.php?id=1493 题目大意:给一个数列,进行一系列操作.包括旋转,翻转,改变等操作,以及查询颜色段数. ...
- NOI2007项链工厂——sbTreap代码
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...
- NOI2007 项链工厂
题目链接:戳我 60pts 有一点容易写错的小细节: 比如说求全局的段数的时候,如果只有一种颜色,那么当左右端点相等时,就不要ans--了. 注意右端点小于左端点的情况. #include<io ...
- 【BZOJ-1493】项链工厂 Splay
1493: [NOI2007]项链工厂 Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 1440 Solved: 626[Submit][Status] ...
随机推荐
- wepy.request 请求成功但是不进入success和fail方法,及请求传参问题
1.根据wepy官方给的文档如下,用then拿后台返回的数据,如果用then报错,请先在app.wpy中配置promise. 没有success,fail,complete方法,如若用了也是不会进入方 ...
- Win2008 Server MySql安装包详细安装教程
首先去官网下载 下载MySql 下载地址:http://downloads.mysql.com/archives/community/ 我这里选择MSI的32位安装包安装,服务器系统32位的. 安装M ...
- Spider-scrapy断点续爬
scrapy的每一个爬虫,暂停时可以记录暂停状态以及爬取了哪些url,重启时可以从暂停状态开始爬取过的URL不在爬取 实现暂停与重启记录状态 方法一: 1.首先cd进入到scrapy项目里(当然你也可 ...
- python 调用 C 动态库
首先是 C 的头文件和源文件, #ifndef POINT_H #define POINT_H struct point { int x; int y; }; void point_print(str ...
- 集训第六周 O题
Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...
- Maximun product
Given a sequence of integers S = {S1, S2, ..., Sn}, you shoulddetermine what is the value of the max ...
- String类的概述和构造方法
StringDemo.java /* * String:字符串类 * 由多个字符组成的一串数据 * 字符串其本质就是一个字符数组 * * 构造方法: * String(String original) ...
- 在linux服务器上搭建Struts2项目运行环境
服务器上工作: 首先要先装java https://www.cnblogs.com/lamp01/p/8932740.html 然后装好tomcat https://www.cnblogs.com/y ...
- 添物不花钱学JavaEE(基础篇) --HTML
HTML是什么? HTML – Hyper Text Markup Language HTML官方网址 http://www.w3.org/TR/2014/REC-html5-20141028/ 其实 ...
- linux & command line & console & logs
linux & command line & console & logs how to get the logs form linux command console htt ...