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
;
}
查看全文
相关阅读:
请求的详细资料级别没有事实表
BIEE汇总数据如何放在后面
Biee仪表盘中提示空值如何去掉
UFT测试本地应用程序登陆小实例(描述性编程)
Mysql找回丢失密码
linux下Mysql多实例实现
如何从零安装Mysql
Linux系统下yum源配置(Centos 6)
Linux系统管理常用命令用法总结(2)
Linux系统管理常用命令用法总结(1)
原文地址:https://www.cnblogs.com/SQL/p/926666.html
最新文章
C# Socket编程(3)编码和解码
C# Socket编程(2)识别网络主机
C# Socket编程(1)基本的术语和概念
Holer一款局域网服务器代理到公网的内网映射工具
有登陆认证的情况下如何使用Wisdom RESTClient?
使用Wisdom RESTClient进行自动化测试,如何取消对返回的body内容的校验?对排除的JSON属性字段不做校验?
Java用Gson按照键值key排序json所有节点
Java用Gson遍历json所有节点
Java用Jackson遍历json所有节点
如何通过命令行使用Wisdom RESTClient?
热门文章
使用Wisdom RESTClient自动化测试REST API,如何取消对返回的body内容的校验?
使用Wisdom RESTClient如何在Linux和Mac上获取测试报告和API文档?
如何使用Wisdom RESTClient定制满足您个性化需求的API文档?
oracle sqlldr数据导入错误Field in data file exceeds maximum length解决
R语言
LINUX下文件编码转换
EBS主机文件的开发(转)
app_query.append用法(转)
WIN7没有了兼容模式
WIN7 64位EBS R12打开报错FRM-92095:Oracle JInitiator版本太旧,请安装版本1.1.8.2或更高版本
Copyright © 2011-2022 走看看