HDU 1754: I Hate It

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1754: I Hate It相关的知识,希望对你有一定的参考价值。


///@author Sycamore
///@date 9/17/2017
///@link http://acm.hdu.edu.cn/showproblem.php?pid=1754
#include<bits/stdc++.h>

using namespace std;
const int MAXN = 200000 + 5;
int val[MAXN];
struct node
{
int l, r, max_val;
} ST[MAXN << 2];
void Build(int index, int l, int r)
{

ST[index].l = l;
ST[index].r = r;
if (l == r) ST[index].max_val = val[l];
else
{
int m = (l + r) >> 1;
Build(index << 1, l, m);
Build((index << 1) + 1, m + 1, r);
ST[index].max_val = max(ST[index << 1].max_val, ST[(index << 1) + 1].max_val);
}

}

void Update(int n, int index, int id)//top-down
{
if (id < ST[index].l || id > ST[index].r)
return;
if (ST[index].l == id && ST[index].r == id)
{
ST[index].max_val = n;
return;
}

Update(n,index << 1, id);
Update(n,(index << 1) + 1, id);
ST[index].max_val = max(ST[index << 1].max_val, ST[(index << 1) + 1].max_val);
}

int Query(int l, int r, int index)//top-down
{
if (ST[index].l > r || ST[index].r <l)return 0;
if (ST[index].l >= l && ST[index].r <= r)return ST[index].max_val;
int m = (ST[index].l + ST[index].r) >> 1;
if (r <= m)return Query(l, r, index << 1);//in the left interval
if (l > m)return Query(l, r, (index << 1) + 1);
return max(Query(l, m, index << 1), Query(m + 1, r, (index << 1) + 1));
}

#define endl ‘\n‘

int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
while (cin >> n >> m)
{


for (int i = 1; i <= n; i++)cin >> val[i];
Build(1, 1, n);
char cmd;
while (m--)
{
cin >> cmd;
if (cmd == ‘Q‘)
{
int l, r;
cin >> l >> r;
cout << Query(l, r, 1) << endl;
}
else
{
int a, id;
cin >> id >> a;
Update(a, 1, id);
}

}
}
return 0;
}

 

以上是关于HDU 1754: I Hate It的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1754 I Hate It 线段树

I Hate it-HDU1754

HDU 1754: I Hate It

I Hate It HDU - 1754 (线段树)

hdu 1754 I Hate It(分块做法)

HDU 1754 I Hate It