zoukankan
html css js c++ java
线段树521
最近线段树狂写中 争取以后啥都不看就能写出来
#include
<
stdio.h
>
#include
<
string
.h
>
struct
node
{
node
*
pl,
*
pr;
int
left, right;
int
color;
int
colCount;
int
cl, cr;
}
mem[
200020
];
int
memCount;
int
Col[
31
];
int
ColCnt;
int
result;
node
*
newNode()
{
node
*
pt
=
&
mem[memCount
++
];
pt
->
color
=
pt
->
colCount
=
pt
->
cr
=
pt
->
cl
=
1
;
return
pt;
}
node
*
buildTree(
int
l,
int
r)
{
node
*
root
=
newNode();
root
->
left
=
l;
root
->
right
=
r;
if
(r
-
l
>
1
)
{
int
mid
=
(l
+
r)
/
2
;
root
->
pl
=
buildTree(l, mid);
root
->
pr
=
buildTree(mid, r);
}
return
root;
}
void
color(node
*
root,
int
c)
{
root
->
colCount
=
1
;
root
->
color
=
root
->
cl
=
root
->
cr
=
c;
}
void
update(node
*
root,
int
l,
int
r,
int
c)
{
if
(root
->
left
==
l
&&
root
->
right
==
r)
{
color(root, c);
return
;
}
if
(root
->
colCount
==
1
)
{
color(root
->
pl,root
->
color);
color(root
->
pr,root
->
color);
}
if
(root
->
colCount
==
1
&&
root
->
color
==
c)
return
;
root
->
colCount
=
2
;
if
(root
->
pl
->
right
>
l)
{
if
(root
->
pl
->
right
>=
r)
update(root
->
pl,l,r,c);
else
{
int
mid
=
(root
->
left
+
root
->
right)
/
2
;
update(root
->
pl,l,mid,c);
update(root
->
pr,mid,r,c);
}
}
else
{
update(root
->
pr,l,r,c);
}
root
->
cl
=
root
->
pl
->
cl;
root
->
cr
=
root
->
pr
->
cr;
}
void
ques(node
*
root,
int
l,
int
r)
{
if
(root
->
colCount
==
1
)
{
Col[ColCnt
++
]
=
root
->
color;
return
;
}
if
(root
->
right
-
root
->
left
<=
1
)
return
;
if
(root
->
pl
->
right
>
l)
{
if
(root
->
pl
->
right
>=
r)
ques(root
->
pl,l,r);
else
{
int
mid
=
(root
->
left
+
root
->
right)
/
2
;
ques(root
->
pl,l,mid);
ques(root
->
pr,mid,r);
}
}
else
{
ques(root
->
pr,l,r);
}
}
void
clean()
{
int
i, j;
for
(i
=
1
; i
<=
30
; i
++
)
for
(j
=
0
; j
<
ColCnt; j
++
)
{
if
(i
==
Col[j])
{
result
++
;
break
;
}
}
}
int
main()
{
int
l, t, o;
while
(scanf(
"
%d%d%d
"
,
&
l,
&
t,
&
o)
==
3
)
{
memCount
=
0
;
node
*
root
=
buildTree(
0
, l);
int
i;
char
op;
int
l, r, c;
for
(i
=
0
; i
<
o; i
++
)
{
scanf(
"
%s
"
,
&
op);
if
(op
==
'
C
'
)
{
scanf(
"
%d%d%d
"
,
&
l,
&
r,
&
c);
if
(l
<=
r)
update(root,l
-
1
,r,c);
else
update(root,r
-
1
,l,c);
}
else
{
ColCnt
=
0
;
result
=
0
;
memset(Col,
0
,
sizeof
(Col));
scanf(
"
%d%d
"
,
&
l,
&
r);
if
(l
<=
r)
ques(root,l
-
1
, r);
else
ques(root,r
-
1
,l);
clean();
printf(
"
%d\n
"
, result);
}
}
}
return
0
;
}
查看全文
相关阅读:
如何快速部署Oracle Database
有关Oracle统计信息的知识点
索引的重建
利用Openfiler配置基于文件系统的网络存储
Linux命令(007) -- systemctl
使用mysqldump导出数据库(表)
文件处理.Windows.Fastcopy.3.50.x64.文件复制简体中文破解版(验证版)
oracle 修改数据 保险方法
oracle存储过程、声明变量、for循环--后期添加游标 if 、decode、 case when
Oracle 游标使用全解
原文地址:https://www.cnblogs.com/SQL/p/926666.html
最新文章
Leetcode 429. N-ary Tree Level Order Traversal
Leetcode 590. N-ary Tree Postorder Traversal
WebUploader 设置单个文件上传
在Thinkphp3.2 中使用PHPMailer 发送邮件
单行文字溢出和多行文字溢出省略号显示的CSS样式
GeeTest 极验验证
tracert的应用
Velocity资源
Failed to load unit 'PATM' (VERR_SSM_FIELD_NOT_CONSECUTIVE)
Java&MySQL Type Mapping
热门文章
Velocity介绍
JS中Date.parse方法返回NaN解决方案
Ubuntu下添加Eclipse快捷方式
Curl之Post Json
Maven之debug技巧
Android之parseSDKContent failed
mysqlbinlog(日志管理工具)
mysqladmin(MySQL管理工具)
mysql(MySQL客户端连接工具)
Linux命令(008) -- yum
Copyright © 2011-2022 走看看