[线段树] [洛谷] P1531 I Hate It
程序员文章站
2022-03-22 13:12:38
...
线段树的区间查询和单点更新
复习敲一下
//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
int arr[MAXN] ;
int tree[MAXN];
int N, M;
void init(int now, int l, int r)
{
if(l == r)
{
tree[now] = arr[l];
return ;
}
int mid = l + (r - l) / 2;
init(now * 2, l, mid);
init(now * 2 + 1, mid + 1, r);
tree[now] = max(tree[now * 2], tree[now * 2 + 1]);
}
void change(int now, int l, int r, int find, int key)
{
if(r < find || l > find)
return ;
if(l == r)
{
if(tree[now] < key)
tree[now] = key;
return ;
}
int mid = l + (r - l) / 2;
change(now * 2, l, mid, find, key);
change(now * 2 + 1, mid + 1, r, find, key);
tree[now] = max(tree[now * 2], tree[now * 2 + 1]);
}
int qury(int now, int l, int r, int x, int y)
{
if(l > y || r < x)
return 0;
if(x <= l && y >= r)
{
return tree[now];
}
int ans = 0;
int mid = l + (r - l) / 2;
ans = max(ans, qury(now * 2, l, mid, x, y) );
ans = max(ans, qury(now * 2 + 1, mid + 1, r, x, y) );
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>N>>M;
for(int i = 1; i <= N; i++)
cin>>arr[i];
init(1, 1, N);
while(M--)
{
char oprt;
int x, y;
cin>>oprt>>x>>y;
if(oprt == 'Q')
{
cout<<qury(1, 1, N, x, y)<<endl;
}
else
{
change(1, 1, N, x, y);
}
}
return 0;
}
上一篇: JIRA 集群软件Scarlet 发布
下一篇: LinkedList中的链表结构
推荐阅读
-
洛谷P4588 [TJOI2018]数学计算(线段树)
-
洛谷P3120 [USACO15FEB]牛跳房子(动态开节点线段树)
-
洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)
-
洛谷P3960 列队(动态开节点线段树)
-
洛谷P3722 [AH2017/HNOI2017]影魔(线段树 set spaly)
-
洛谷P4069 [SDOI2016]游戏(李超线段树)
-
洛谷P3586 [POI2015]LOG(贪心 权值线段树)
-
洛谷P4425 [HNOI/AHOI2018]转盘(线段树)
-
洛谷P3722 [AH2017/HNOI2017]影魔(线段树)
-
洛谷-P3372-线段树 1(区间修改,区间求和)