Problem description
There are n squares, left-to-right in a row, numbered 1-n.
There are 3 types of operation for m operations:
1. Modify the weights of a lattice,
2. To seek a continuous lattice of weights and
3. Ask for the maximum value of a continuous lattice.
For each of the 2, 3 operation output you have to find the results.
Input format
The first row of 2 integers n,m.
The next line of n integers represents the initial weights of the n lattices.
Next m line, 3 integers per line p,x,y,p represents the type of operation, p=1 when the weight of the modified lattice X is y,p=2 when the weighted value of the interval [x, y] is calculated, and the p=3 indicates the maximum weight of the interval [x, y] lattice.
Output format
There are several lines, the number of rows equals the total number of p=2 or 3 operations.
1 integers per line, corresponding to the result of each p=2 or 3 operation.
Sample Input 4 3
1 2 3 4
2 1 3
1 4 3
3 1 4 Sample output 6
3 Data size and conventions
For 20% of data n <= 100,m <= 200.
For 50% of data n <= 5000,m <= 5000.
For 100% data 1 <= n <= 100000,m <= 100000,0 <= lattice weights <= 10000.
1#include <bits/stdc++.h>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <algorithm>6#include <vector>7 #definell __int648 #definePI ACOs (-1.0)9 #defineMoD 1000000007Ten using namespacestd; One intn,m; A structnode - { - intL,r; the intvalue; - intMAXN; -}tree[400005]; - intEXM; + voidBuildtree (intRoot,intLintR) - { +Tree[root].l=l; ATree[root].r=R; at if(l==R) - { -scanf"%d",&EXM); -tree[root].value=tree[root].maxn=EXM; - return; - } in intMid= (l+r) >>1; -Buildtree (root<<1, l,mid); toBuildtree (root<<1|1, mid+1, R); +Tree[root].maxn=max (tree[root<<1].maxn,tree[root<<1|1].MAXN); -tree[root].value=tree[root<<1].value+tree[root<<1|1].value; the } * voidUpdateintRoot,intWhat,intc) $ {Panax Notoginseng if(tree[root].l==tree[root].r&&tree[root].r==What ) - { theTree[root].maxn=tree[root].value=C; + return; A } the intMid= (TREE[ROOT].L+TREE[ROOT].R) >>1; + if(what<=mid) -Update (root<<1, what,c); $ Else $Update (root<<1|1, what,c); -Tree[root].maxn=max (tree[root<<1].maxn,tree[root<<1|1].MAXN); -tree[root].value=tree[root<<1].value+tree[root<<1|1].value; the } - intQuery1 (intRootintLintR)Wuyi { the if(tree[root].l==l&&tree[root].r==R) - { Wu returnTree[root].value; - } About intMid= (TREE[ROOT].L+TREE[ROOT].R) >>1; $ if(r<=mid) - returnQuery1 (root<<1, l,r); - Else - { A if(l>mid) + returnQuery1 (root<<1|1, l,r); the Else - returnQuery1 (root<<1, L,mid) +query1 (root<<1|1, mid+1, R); $ } the } the intQuery2 (intRootintLintR) the { the if(tree[root].l==l&&tree[root].r==R) - { in returnTREE[ROOT].MAXN; the } the intMid= (TREE[ROOT].L+TREE[ROOT].R) >>1; About if(r<=mid) the returnQuery2 (root<<1, l,r); the Else the { + if(l>mid) - returnQuery2 (root<<1|1, l,r); the ElseBayi returnMax (Query2 (root<<1, L,mid), Query2 (root<<1|1, mid+1, R)); the } the } - intMain () - { the intq,w,e; thescanf"%d%d",&n,&m); theBuildtree (1,1, n); the for(intI=1; i<=m;i++) - { thescanf" %d%d%d",&q,&w,&e); the if(q==1) theUpdate1, w,e);94 if(q==2) theprintf"%d\n", Query1 (1, W,e)); the if(q==3) theprintf"%d\n", Query2 (1, W,e));98 } About return 0; -}
Algorithm training operation lattice segment tree board problem