• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest May 25th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. void build_tree(int m[],int v,int tl,int tr) {
2.     if (tl==tr) tree[v]=make_pair(m[tl],tl);
3.     else {
4.         int tm=(tl+tr)/2;
5.         build_tree(m,2*v,tl,tm);
6.         build_tree(m,2*v+1,tm+1,tr);
7.     if (tree[2*v].first>tree[2*v+1].first)
8.         tree[v]=make_pair(tree[2*v].first,tree[2*v].second);
9.
10.     else tree[v]=make_pair(tree[2*v+1].first,tree[2*v+1].second);
11.     }
12. }
13.
14.
15.
16. int sum(int v,int tl,int tr,int l,int r) {
17.     if (l>r) return 0;
18.     if (l==tl && r==tr) return tree[v];
19.     int tm=(tl+tr)/2;
20.     return sum(2*v,tl,tm,l,min(r,tm))+sum(2*v+1,tm+1,tr,max(l,tm+1),r);
21. }
22.
23. void update (int v, int tl, int tr, int pos, int new_val) {
24.     if (tl == tr)
25.         t[v] = new_val;
26.     else {
27.         int tm = (tl + tr) / 2;
28.         if (pos <= tm)
29.             update (v*2, tl, tm, pos, new_val);
30.         else
31.             update (v*2+1, tm+1, tr, pos, new_val);
32.         t[v] = t[v*2] + t[v*2+1];
33.     }
34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top