1493: [NOI2007]项链工厂
线段树。
真还就是个线段树。。
除去操作1,2的话,线段树很容易就处理了,问题在于如何处理操作1和2。(这点没想到)。。
我们用一个delta维护操作1,如果没有旋转就+k,不然就-k。
每次读入i和j的时候用trans处理一下,就成功在O(1)的时间解决了操作1和2。
细节很重要。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 2000000 + 10; struct Node {
int lc,rc,cnt;
}; struct segtree {
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1) int l[maxn],r[maxn],tag[maxn];
Node a[maxn];
int rev,delta,n,tmp; void trans(int &x,int &y) {
if(rev) { //反旋
x=(n-x+2-delta+n)%n;
y=(n-y+2-delta+n)%n;
swap(x,y);
}
else { //正旋
x=(x-delta+n)%n;
y=(y-delta+n)%n;
}
x=x?x:n;
y=y?y:n;
} void push(int x) {
if(!tag[x]) return;
a[lc(x)].lc=a[rc(x)].lc=tag[x];
a[lc(x)].rc=a[rc(x)].rc=tag[x];
tag[lc(x)]=tag[rc(x)]=tag[x];
a[lc(x)].cnt=a[rc(x)].cnt=1;
tag[x]=0;
} Node update(Node a,Node b) {
Node res;
res.lc=a.lc;
res.rc=b.rc;
res.cnt=a.cnt+b.cnt-(a.rc==b.lc);
return res;
} void update(int x) {
a[x]=update(a[lc(x)],a[rc(x)]);
} void build(int x,int L,int R) {
l[x]=L; r[x]=R;
if(L==R) {
scanf("%d",&tmp);
a[x].lc=a[x].rc=tmp;
a[x].cnt=1;
return;
}
int mid=(L+R)>>1;
build(lc(x),L,mid);
build(rc(x),mid+1,R);
update(x);
} void color(int x,int L,int R,int v) {
if(L>r[x] || R<l[x]) return;
if(L<=l[x] && r[x]<=R) {
tag[x]=v;
a[x].lc=a[x].rc=v;
a[x].cnt=1;
return;
}
push(x);
color(lc(x),L,R,v);
color(rc(x),L,R,v);
update(x);
} Node query(int x,int L,int R) {
if(L<=l[x] && r[x]<=R) return a[x];
int mid=(l[x]+r[x])>>1;
push(x);
if(R<=mid) return query(lc(x),L,R);
else if(L>mid) return query(rc(x),L,R);
else return update(query(lc(x),L,R),query(rc(x),L,R));
} void paws(int i,int j) {
trans(i,j);
Node cur;int ci,cj;
if(i<=j) {
cur=query(1,i,j);
ci=cur.lc; cj=cur.rc;
}
else {
cur=query(1,j,i);
cj=cur.lc; ci=cur.rc;
}
color(1,i,i,cj);
color(1,j,j,ci);
} void rotate() {
int k;
scanf("%d",&k);
if(rev) delta=(delta-k+n)%n; // 反旋
else delta=(delta+k)%n; // 正旋
} void paint(int i,int j,int v) {
trans(i,j);
if(i<=j) color(1,i,j,v);
else {
color(1,i,n,v);
color(1,1,j,v);
}
} void c1() {
Node cur;
cur=query(1,1,n);
printf("%d\n",max(cur.cnt-(cur.lc==cur.rc),1));
} void c2(int i,int j) {
trans(i,j);
Node cur;
if(i<=j) cur=query(1,i,j);
else cur=update(query(1,i,n),query(1,1,j));
printf("%d\n",cur.cnt);
} void init() {
int c;
scanf("%d%d",&n,&c);
rev=delta=0;
build(1,1,n);
}
}seg; char op[20];
int k,i,j,x,q; int main() {
seg.init();
scanf("%d",&q);
while(q--) {
scanf("%s",op);
if(op[0]=='R') seg.rotate();
else if(op[0]=='F') seg.rev^=1;
else if(op[0]=='S') {
scanf("%d%d",&i,&j);
seg.paws(i,j);
}
else if(op[0]=='P') {
scanf("%d%d%d",&i,&j,&x);
seg.paint(i,j,x);
}
else if(op[0]=='C' && op[1]=='S') {
scanf("%d%d",&i,&j);
seg.c2(i,j);
}
else seg.c1();
}
return 0;
}
1493: [NOI2007]项链工厂的更多相关文章
- bzoj 1493: [NOI2007]项链工厂(线段树)
1493: [NOI2007]项链工厂 Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 1256 Solved: 545[Submit][Status] ...
- BZOJ1493 [NOI2007]项链工厂
未完待续... 终于改对了 热泪盈眶.jpg 错误原因:pushdown的时候没有判断是否有左右儿子,也没当x=0 return,于是出现一些奇怪的错误 #include<bits/stdc++ ...
- bzoj1493[NOI2007]项链工厂 线段树
1493: [NOI2007]项链工厂 Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 1712 Solved: 723[Submit][Status] ...
- 数据结构(Splay平衡树): [NOI2007] 项链工厂
[NOI2007] 项链工厂 ★★★ 输入文件:necklace.in 输出文件:necklace.out 简单对比 时间限制:4 s 内存限制:512 MB [问题描述] T公司是一 ...
- BZOJ_1493_[NOI2007]项链工厂_Splay
BZOJ_1493_[NOI2007]项链工厂_Splay Description T公司是一家专门生产彩色珠子项链的公司,其生产的项链设计新颖.款式多样.价格适中,广受青年人的喜爱. 最近T公司打算 ...
- 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] ...
随机推荐
- android中设置Animation 动画效果
在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...
- android studio 不能在线更新android SDK Manager问题解决办法
Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: Connecti ...
- ADO.NET- 基础总结及实例介绍
最近闲暇时间写的一些小程序中,访问数据库比较多:下面主要介绍下ADO.NET方面知识,有不足之处,希望大神们不吝赐教: 提到ADO.NET,经常会和ASP.NET进行混淆,两者的区别很大,没有可比性, ...
- 机器学习中的数学-线性判别分析(LDA), 主成分分析(PCA)
转:http://www.cnblogs.com/LeftNotEasy/archive/2011/01/08/lda-and-pca-machine-learning.html 版权声明: 本文由L ...
- C# File
http://msdn.microsoft.com/zh-cn/library/system.io.file(v=vs.110).aspx using System; using System.IO; ...
- C# 获取属性字段上DescriptionAttribute的值
var ent = new Ent(); foreach (var item in ent.GetType().GetProperties()) { var v = (DescriptionAttri ...
- Java多线程——<一>概述、定义任务
一.概述 为什么使用线程?从c开始,任何一门高级语言的默认执行顺序是“按照编写的代码的顺序执行”,日常开发过程中写的业务逻辑,但凡不涉及并发的,都是让一个任务顺序执行以确保得到想要的结果.但是,当你的 ...
- linux源代码阅读笔记 linux文件系统(三)
当系统申请一个新的inode时.系统并不会对磁盘进行读写.它会在存储在内存的inode表(inode_table)中寻找一个空闲的位置. 如果找到了,直接返回该inode.否则要等待一个空闲的位置. ...
- JavaScript 隐式转换
javascript 中的怪癖,js运算符隐式类型转换 x + "" //等价于 String(x) + x //等价于 Number(x),也可以写成x-0 !!x //等价于 ...
- Struts2 常用的常量配置
在struts2-core-2.1.8.1.jar的org.apache.struts2包下面的default.properties资源文件里可以查到常用的常量配置,这些不用刻意的记住:忘记的时候可以 ...