Algorithm training operation lattice time limit: 1.0s memory limit: 256.0MBProblem 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 <iostream>#include<cstdio>#defineMAXN 500004structnode{intLeft,right; intnum;} tree[3*MAXN];intmaxv[2*MAXN];int out=0;intMaxintXinty) { if(x>y)returnx; Else returny;}voidBuildintLeftintRightinti) {Tree[i].left=Left ; Tree[i].right=Right ; Tree[i].num=0; if(tree[i].left==tree[i].right)return ; intMid= (left+right)/2; Build (Left,mid,2*i); Build (Mid+1, right,2*i+1);}voidInsertintIdintIintj) { if(tree[id].left==i&&tree[id].right==i) {tree[id].num=J; Maxv[id]=J; } if(tree[id].left==tree[id].right)return ; if(i>tree[id].right)return; if(i<tree[id].left)return; intMid= (tree[id].left+tree[id].right)/2; if(i<=mid) Insert (ID*2, i,j); ElseInsert (ID*2+1, i,j); Tree[id].num=tree[2*id].num+tree[2*id+1].num; Maxv[id]=max (maxv[id*2],maxv[id*2+1]);}voidSumintIdintIintj) { intMid= (tree[id].left+tree[id].right)/2; if(tree[id].left==i&&tree[id].right==j) { out+=Tree[id].num; return ; } if(j<=mid) sum (ID*2, i,j); Else if(i>mid) sum (ID*2+1, i,j); Else{sum (id*2, I,mid); SUM (ID*2+1, mid+1, J); }}intQueryintIdintLintR) { intMid= (tree[id].left+tree[id].right)/2; intans=0; if(l==tree[id].left&&r==tree[id].right) { returnMaxv[id]; } if(r<=mid) ans=max (Ans,query (id*2, L,r)); Else if(l>mid) ans=max (Ans,query (id*2+1, L,r)); Else{ans=max (Ans,query (id*2, L,mid)); Ans=max (Ans,query (id*2+1, mid+1, R)); } returnans;}intMain () {intI,p,x,y; intN,m,u; while(~ scanf ("%d%d",&n,&m) {Build (1N1); for(i=1; i<=n;i++) {scanf ("%d",&u); Insert (1, I,u); } for(i=1; i<=m;i++) {scanf ("%d%d%d",&p,&x,&y); if(p==1) Insert (1, x, y); if(p==2) {sum (1, x, y); printf ("%d\n", out); out=0; } if(p==3) {printf ("%d\n", Query (1, x, y)); } } } return 0;}
Algorithm training operation Lattice