Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
pdp-camp
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Public
pdp-camp
Commits
f558342f
Commit
f558342f
authored
May 09, 2017
by
Panagiotis Kostopanagiotis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SPOJ - GSS1, Segment Tree
parent
f28da6ee
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
0 deletions
+70
-0
gss1.cpp
2017/gss1.cpp
+70
-0
No files found.
2017/gss1.cpp
0 → 100644
View file @
f558342f
#include <cstdio>
#include <algorithm>
#define MAXN 50050
using
namespace
std
;
struct
node
{
int
ans
,
mpref
,
msuf
,
sum
;
};
node
segtree
[
4
*
MAXN
+
1
];
int
N
,
A
[
MAXN
+
1
];
void
init
(
int
n
,
int
l
,
int
r
)
{
if
(
l
==
r
)
{
segtree
[
n
].
sum
=
A
[
l
];
segtree
[
n
].
mpref
=
A
[
l
];
segtree
[
n
].
msuf
=
A
[
l
];
segtree
[
n
].
ans
=
A
[
l
];
}
else
{
int
mid
=
(
l
+
r
)
>>
1
;
init
(
2
*
n
,
l
,
mid
);
init
(
2
*
n
+
1
,
mid
+
1
,
r
);
segtree
[
n
].
sum
=
segtree
[
2
*
n
].
sum
+
segtree
[
2
*
n
+
1
].
sum
;
segtree
[
n
].
ans
=
max
(
max
(
segtree
[
2
*
n
].
ans
,
segtree
[
2
*
n
+
1
].
ans
),
segtree
[
2
*
n
].
msuf
+
segtree
[
2
*
n
+
1
].
mpref
);
segtree
[
n
].
mpref
=
max
(
segtree
[
2
*
n
].
mpref
,
segtree
[
2
*
n
].
sum
+
segtree
[
2
*
n
+
1
].
mpref
);
segtree
[
n
].
msuf
=
max
(
segtree
[
2
*
n
+
1
].
msuf
,
segtree
[
2
*
n
+
1
].
sum
+
segtree
[
2
*
n
].
msuf
);
}
}
node
query
(
int
n
,
int
l
,
int
r
,
int
i
,
int
j
)
{
if
(
l
==
i
&&
r
==
j
)
return
segtree
[
n
];
int
mid
=
(
l
+
r
)
>>
1
;
if
(
j
<=
mid
)
return
query
(
2
*
n
,
l
,
mid
,
i
,
j
);
else
if
(
i
>
mid
)
return
query
(
2
*
n
+
1
,
mid
+
1
,
r
,
i
,
j
);
node
A
=
query
(
2
*
n
,
l
,
mid
,
i
,
mid
);
node
B
=
query
(
2
*
n
+
1
,
mid
+
1
,
r
,
mid
+
1
,
j
);
node
C
;
C
.
sum
=
A
.
sum
+
B
.
sum
;
C
.
ans
=
max
(
max
(
A
.
ans
,
B
.
ans
),
A
.
msuf
+
B
.
mpref
);
C
.
mpref
=
max
(
A
.
mpref
,
A
.
sum
+
B
.
mpref
);
C
.
msuf
=
max
(
B
.
msuf
,
B
.
sum
+
A
.
msuf
);
return
C
;
}
int
main
(
void
)
{
scanf
(
"%d"
,
&
N
);
for
(
int
i
=
1
;
i
<=
N
;
i
++
)
{
scanf
(
"%d"
,
&
A
[
i
]
);
}
init
(
1
,
1
,
N
);
int
Q
;
scanf
(
"%d"
,
&
Q
);
while
(
Q
--
)
{
int
x
,
y
;
scanf
(
"%d %d"
,
&
x
,
&
y
);
node
A
=
query
(
1
,
1
,
N
,
x
,
y
);
printf
(
"%d
\n
"
,
A
.
ans
);
}
return
0
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment