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] ...
随机推荐
- Cookie 获取
二级域名可以获取一级域名的Cookie值 二级域名下删除顶级域名下的Cookie,需要添加顶级域名的Cookie作用域 /// <summary> /// 根据cookie名称删除 /// ...
- 笔记本电脑连接wifi有时候会自动断网提示有限的访问权限解决办法
解决办法如下: [设备管理器],找到[网络适配器]第一项,右键属性
- Android journey 2 @Android系统框架
此刻,本人还是一个android菜鸟,刚刚开始起步学习android相关知识,想用blog记录自己学习的过程,一方面给他人提供帮助,另一方面给自己个复习的地方. 在一起学习Android之前,先跟大家 ...
- java 把URL中的中文转换成utf-8编码
private static final String QUERY = "餐饮"; String sr = URLEncoder.encode(QUERY); System.out ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- new Date()的数据类型的问题
function getServerNow(){ return new Date(new Date() + svrMinusLocal); } svrMinusLocal是服务器时间减本地时间的时间差 ...
- auto_ptr的设计动机
auto_ptr的设计动机 C++标准程序库提供的auto_ptr是一种智能型指针(smart pointer),帮助程序员防止“被异常抛出时发生资源泄露”. 函数的操作经常依以下模式进行: 1.获取 ...
- SDUT1500 Message Flood
以前做过的用的字典树,可是貌似现在再用超内存....求解释... 问了LYN用的map函数做的,又去小小的学了map函数.... http://wenku.baidu.com/view/0b08cec ...
- PKUSC 模拟赛 day1 下午总结
下午到了机房之后又困又饿,还要被强行摁着看英文题,简直差评 第一题是NOIP模拟赛的原题,随便模拟就好啦 本人模拟功力太渣不小心打错了个变量,居然调了40多分钟QAQ #include<cstd ...
- stringgird中使用TClientDataSet排序的问题
function TfrmMain.createIIReport(cdsBody: TClientDataSet; silent: Boolean): String;var s,sText: ...