作者: smileandyxu 时间: 2016-3-11 13:46
#include <iostream>
#define INF 200000000
using namespace std;
int F[400001];
int A[100001];
int M, N;
inline int father(int x)
{
return (x >> 1);
}
inline int lchild(int x)
{
return (x << 1);
}
inline int rchild(int x)
{
return (x << 1) + 1;
}
int index(int x)
{
int l = 1;
int r = M;
int m = (l + r) / 2;
int i = 1;
while (r - l > 1) {
if (x <= m) {
i = lchild(i);
r = m;
}
else {
i = rchild(i);
l = m;
}
m = (l + r) / 2;
}
return i + x - m;
}
int minimum(int l, int r, int ql, int qr, int i)
{
int ans = INF;
if (ql <= l && r <= qr)
return F[i];
else {
int m = (l + r) / 2;
if (ql <= m) {
ans = min(ans, minimum(l, m, ql, qr, lchild(i)));
}
if (qr >= m + 1) {
ans = min(ans, minimum(m + 1, r, ql, qr, rchild(i)));
}
}
return ans;
}
void initialize()
{
for (int i = 1; i <= M * 2; ++i)
F[i] = INF;
}
void build(int l, int r, int i)
{
if (l == r)
F[i] = A[l];
else {
int m = (l + r) / 2;
build(l, m, lchild(i));
build(m + 1, r, rchild(i));
F[i] = min(F[lchild(i)], F[rchild(i)]);
}
}
void update(int p, int x)
{
int i = index(p);
bool flag = true;
F[i] = x;
i = father(i);
while (i >= 1 && flag) {
if (F[i] > x)
F[i] = x;
else
flag = false;
i = father(i);
}
}
int main()
{
cin >> M >> N;
for (int i = 1; i <= M; ++i) {
cin >> A[i];
}
initialize();
build(1, M, 1);
for (int i = 1; i <= N; ++i) {
int p, x, y;
cin >> p >> x >> y;
if (p == 1) {
cout << minimum(1, M, x, y, 1) << " ";
}
if (p == 2) {
update(x, y);
}
}
return 0;
} 作者: smileandyxu 时间: 2016-3-11 13:56
#include <iostream>
#define INF 200000000
using namespace std;
int F[400001];
int A[100001];
int P[100001];
int M, N;
inline int father(int x)
{
return (x >> 1);
}
inline int lchild(int x)
{
return (x << 1);
}
inline int rchild(int x)
{
return (x << 1) + 1;
}
int minimum(int l, int r, int ql, int qr, int i)
{
int ans = INF;
if (ql <= l && r <= qr)
return F[i];
else {
int m = (l + r) / 2;
if (ql <= m) {
ans = min(ans, minimum(l, m, ql, qr, lchild(i)));
}
if (qr >= m + 1) {
ans = min(ans, minimum(m + 1, r, ql, qr, rchild(i)));
}
}
return ans;
}
void initialize()
{
for (int i = 1; i <= M * 2; ++i)
F[i] = INF;
}
void build(int l, int r, int i)
{
if (l == r) {
F[i] = A[l];
P[l] = i;
}
else {
int m = (l + r) / 2;
build(l, m, lchild(i));
build(m + 1, r, rchild(i));
F[i] = min(F[lchild(i)], F[rchild(i)]);
}
}
void update(int p, int x)
{
int i = P[p];
bool flag = true;
F[i] = x;
i = father(i);
while (i >= 1 && flag) {
if (F[i] > x)
F[i] = x;
else
flag = false;
i = father(i);
}
}
int main()
{
cin >> M >> N;
for (int i = 1; i <= M; ++i) {
cin >> A[i];
}
initialize();
build(1, M, 1);
for (int i = 1; i <= N; ++i) {
int p, x, y;
cin >> p >> x >> y;
if (p == 1) {
cout << minimum(1, M, x, y, 1) << " ";
}
if (p == 2) {
update(x, y);
}
}
for (int i = 1; i <= M; ++i)
cout << P[i] << " ";
return 0;
} 作者: smileandyxu 时间: 2016-3-22 13:52