Algorithm training operation lattice time limit: 1.0s memory Limit: 256.0MB 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.
#include <cstdio> #include <cstdlib> #include <cstring> #define MAX 100002#define Lson l,mid,rr< <1 #define Rson mid+1,r,rr<<1|1using namespace std;struct node{int max,sum;} Tree[max<<2];int mx (int a,int b) {return a>b?a:b;} int maxn,sumn;void Build (int l,int R,int RR) {if (l==r) {scanf ("%d", &tree[rr].max); Tree[rr].sum=tree[rr].max; return;} int mid= (L+R) >>1;build (Lson); build (Rson); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum;tree[ RR].MAX=MX (Tree[rr<<1].max,tree[rr<<1|1].max);} void update (int left,int right,int d,int l,int r,int rr) {if (left==l&&right==r) {tree[rr].max=d;tree[rr].sum=d; return;} int mid= (L+R) >>1;if (right<=mid) {update (Left,right,d,lson); TREE[RR].MAX=MX (tree[rr<<1].max,tree[ Rr<<1|1].max); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum;} else if (left>mid) {update (Left,right,d,rson); tree[rr].max=mx (Tree[rr<<1].max,tree[rr<<1|1].max); Tree[rr].sum=tree[rr<<1].sUm+tree[rr<<1|1].sum;} else {update (Left,mid,d,lson); update (Mid+1,right,d,rson); TREE[RR].MAX=MX (tree[rr<<1].max,tree[rr<<1 |1].max); tree[rr].sum=tree[rr<<1].sum+tree[rr<<1|1].sum;}} void Ma (int left,int right,int l,int r,int rr) {if (left==l&&right==r) {maxn=mx (TREE[RR].MAX,MAXN); return;} int mid= (L+R) >>1;if (right<=mid) {MA (Left,right,lson);} else if (left>mid) {MA (Left,right,rson);} else {MA (left,mid,lson); Ma (Mid+1,right,rson);}} void SS (int left,int right,int l,int r,int rr) {if (left==l&&right==r) {sumn+=tree[rr].sum; return;} int mid= (L+R) >>1;if (right<=mid) {SS (Left,right,lson);} else if (Left>mid) {SS (Left,right,rson);} else {SS (Left,mid,lson); SS (Mid+1,right,rson);}} int main () {int n,m,i,p,x,y;scanf ("%d%d", &n,&m), Build (1,n,1), for (I=1;i<=m;++i) {scanf ("%d", &p); Switch (p) {case 1:scanf ("%d%d", &x,&y); update (x,x,y,1,n,1); Break;case 2:sumn=0;scanf ("%d%d", &x,&y) ; SS (x,y,1,n,1);p rintf ("%d\n", sumn); Break;case 3:mAXN=0;SCANF ("%d%d", &x,&y), MA (x,y,1,n,1);p rintf ("%d\n", MAXN); break;}} return 0;}
Algorithm training operation Lattice