775. [SOJ 1136] Shan Hai jing ★ ¡ï input file:
hill.in
Output file:
hill.out
Simple comparison
Time limit: 1 s memory limit: MB
"Problem description"
"The first day of Nanshan Mountain." The first day of the mountain, near the western sea, more than a GUI, more than Jinyu. There is Grass Yan, its shape such as Leek and Qinghua, its name day Shanyu, Food not hungry ... East 300, Ji Dong Mountain, more Tyli wood, more white ape, more water jade, more gold.
East 380, itinerary Wing Mountain, which many monsters, water and more strange fish, more white jade, more deinagkistrodon insects, more strange snakes, strange wood, not above. ......”
"Shan Hai Jing" is a mountain as the outline, with the sea as the line to record the ancient rivers, plants, animals and minerals, and so on, and every record route will not have a duplicate mountain appearance. One day, your geography teacher Haidong want to revisit the route in the Shan Hai Jing, in order to simplify the problem, Haidong teacher has each mountain with an integer to indicate his degree of joy to the mountain, he would like to know the mountain to block B to a section of the middle of the road (I,J). Can make him feel most satisfied, namely (I,J) this road all mountains of the joy and Evil degree is (c,d) (a≤c≤d≤b) maximum value. So the teacher will ask you, can you help him? It is noteworthy that in "Shan Hai Jing", the mountain of block I can only reach the I+1 mountain.
Input
Input format (enter the file name Hill. In
Enter the 1th line is two number, N,m,2≤n≤100000,1≤m≤l00000,n said there is a total of N mountain, m indicates the number of teachers want to inquire.
The 2nd line is n integers, representing the N-Mount of the mountain, the absolute value is less than 10000.
The following M line has a A, a, a and a two number, 1≤a≤j≤b≤m, which represents block a mountain to block B mountain.
Output
Output format (output file name Hill.out)
There are altogether m-lines, each with 3 numbers of i,j,s, indicating that the total degree of happiness from Block I to Mount J is S. Obviously, for each query, there are a≤i≤j≤b, if there are multiple sets of solutions, then the output I is the smallest, if I is also equal, then the output J smallest solution.
"Input and Output sample"
Input (Hill. In
5 3
5-6 3-1 4
1 3
1 5
5 5
Output (Hill. Out
1 1 5
3 5 6
5 5 4
Recommend a URL that speaks very well-click to open the link.
In a nutshell, it is the maximum value of LM and its right endpoint LR that is stored from the left to the right, the value of RM and its left endpoint RL, the interval sum and the interval left end L,r, and the maxn of the interval and its left-end ml,mr. Note the order when passing, because the left endpoint needs to be as far forward as possible.
Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #define root 1,1,n#define lchild rt<<1,l,mid#define rchild rt<<1|1,mid+1, Rusing namespace Std;struct point{int l,r,sum,lr,rl,lm,rm,maxn,ml,mr;} Tree[800010];int N,m,a[100010];inline int in () {int x=0; char ch=getchar (); bool F=true;while (ch< ' 0 ' | | ch> ' 9 ') {if ( ch== '-') F=false;ch=getchar ();} while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); if (!f) X=-x;return x;} inline void push_up (int rt,int l,int r) {//l,r,sumtree[rt].l=l,tree[rt].r=r;tree[rt].sum=tree[rt<<1].sum+tree[ Rt<<1|1].sum;//lmaxn,lrtree[rt].lm=tree[rt<<1].lm,tree[rt].lr=tree[rt<<1].lr;if (tree[rt< <1].SUM+TREE[RT<<1|1].LM>TREE[RT].LM) Tree[rt].lm=tree[rt<<1].sum+tree[rt<<1|1].lm,tree [rt].lr=tree[rt<<1|1].lr;//rmax,rltree[rt].rm=tree[rt<<1|1].rm,tree[rt].rl=tree[rt<<1|1].rl; if (tree[rt<< 1|1].sum+tree[rt<<1].rm>=tree[rt].rm) tree[rt].rm=tree[rt<<1|1].sum+tree[rt<<1].rm,tree[ rt].rl=tree[rt<<1].rl;//maxntree[rt].maxn=tree[rt<<1].maxn;tree[rt].ml=tree[rt<<1].ml,tree[ Rt].mr=tree[rt<<1].mr;if (TREE[RT<<1].RM+TREE[RT<<1|1].LM>TREE[RT].MAXN) {tree[rt].maxn= TREE[RT<<1].RM+TREE[RT<<1|1].LM;TREE[RT].ML=TREE[RT<<1].RL,TREE[RT].MR=TREE[RT<<1|1]. LR;} if (TREE[RT<<1|1].MAXN>TREE[RT].MAXN) {tree[rt].maxn=tree[rt<<1|1].maxn;tree[rt].ml=tree[rt< <1|1].ml,tree[rt].mr=tree[rt<<1|1].mr;}} inline void build (int rt,int l,int R) {if (l==r) {int x=in (); tree[rt].sum=tree[rt].lm=tree[rt].rm=tree[rt].maxn=x;tree[ Rt].l=tree[rt].r=tree[rt].lr=tree[rt].rl=tree[rt].ml=tree[rt].mr=l;return;} int mid= (L+R) >>1;build (lchild); Build (Rchild);p ush_up (rt,l,r);} Inline point query (int rt,int l,int r,int ll,int rr) {if (ll<=l && r<=rr) return (point) (Tree[rt]); int mid= (l +r) >>1;if (rr<=MID) return query (LCHILD,LL,RR), else if (ll>mid) return query (RCHILD,LL,RR), else {point s1,s2,s;s1=query (lchild , Ll,mid), S2=query (RCHILD,MID+1,RR);//l,r,sums.sum=s1.sum+s2.sum; S.l=s1.l,s.r=s2.r;//lmax,lrs.lm=s1.lm,s.lr=s1.lr;if (S1.SUM+S2.LM>S.LM) S.lm=s1.sum+s2.lm,s.lr=s2.lr;//rmax, Rls.rm=s2.rm,s.rl=s2.rl;if (S2.SUM+S1.RM>=S.RM) s.rm=s2.sum+s1.rm,s.rl=s1.rl;//maxn,ml,mrs.maxn=s1.maxn,s.ml= S1.ml,s.mr=s1.mr;if (S1.RM+S2.LM>S.MAXN) s.maxn=s1.rm+s2.lm,s.ml=s1.rl,s.mr=s2.lr;if (S2.MAXN>S.MAXN) S.MAXN =s2.maxn,s.ml=s2.ml,s.mr=s2.mr;return s;}} int main () {freopen ("hill.in", "R", stdin), Freopen ("Hill.out", "w", stdout), N=in (), M=in (), build (Root), while (m--) {int X=in (), Y=in ();p oint k=query (root,x,y);p rintf ("%d%d%d\n", K.ML,K.MR,K.MAXN);} return 0;}
"Segment Tree" "SOJ1136" "cogs775" Shan Hai Jing