Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- const int N = 1e4;
- typedef long long int ll;
- ll arr[N], tree[N];
- void build(int l, int r, int idx){
- if(l == r){
- tree[idx] = arr[l];
- return;
- }
- ll mid = l + r >> 1;
- build(1, mid, 2 * idx);
- build(mid + 1, r, 2* idx + 1);
- tree[idx] = max(tree[2 * idx], tree[2 * idx + 1]);
- }
- void update(int l, int r, int pos, ll val, int idx){
- if(l == r && pos == l){
- tree[idx] = val;
- return ;
- }
- if(l > pos || pos > r)return;
- ll mid = (l + r) >> 1;
- update(l, mid, pos, val, 2 * idx);
- update(mid + 1, r, pos, val, 2 * idx + 1);
- tree[idx] = max(tree[2 * idx], tree[2 * idx + 1]);
- }
- ll query(int l, int r, int ql, int qr, int idx){
- if(ql > r || qr < l)return 0;
- if(ql <= l && r <= qr)return tree[idx];
- ll mid = l + r >> 1;
- ll x = query(l, mid, ql, qr, 2 * idx);
- ll y = query(mid + 1, r, ql , qr, 2 * idx + 1);
- return max(x, y);
- }
- int main(){
- int i, n, q;
- cin >> n >> q;
- for(i = 1; i <= n; i++)
- cin >> arr[i];
- build(1, n, 1);
- cout << tree[1] << endl;
- update(1, n, 2, 100, 1);
- cout << tree[1] << endl;
- while(q--){
- int x, y;
- cin >> x >> y;
- cout << query(1, n, x, y, 1) << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment