Can you answer these queries II

Source: Internet
Author: User
Tags define local

A very good question, thought for a long time. The first breakthrough is that the enumeration can be found on one side, assuming the tail y, and then quickly find the largest head x that satisfies the condition, and the continuous interval

It is easy to think of the part and the idea that if it is starting from Y to the front, then it is a suffix about y and.

If one side of the answer is in Y, then it is converted to the suffix and the RMQ.

The suffix of y is similar to the suffix of y-1 or y+1, just think about how to push it.

y = = 0 o'clock all suffixes are very easy to get, so consider using Y recursion y+1, this suffix and is from the right to the left to accumulate, repetition does not count,

For element A[y+1], it affects only the last occurrence of the suffix y+1 to a[y+1], and the interval update is fine.

Then on the card, after the mouse to remind the baby to think, in the process can be maintained a history of the most value, the history of the most value how to maintain a variety of entanglements ...

The easiest thing to think about is that when the segment tree interval is updated, the current max changes to update the historical maximum, but because the interval is updated, it needs to be marked,

The historical maximum value of the tagged child node is not actually updated, so the update to the historical maximum is also tagged.

/**********************************************************            ------------------                          ** Author Abyssalfish ***********************************************************/#include<cstdio>#include<iostream>#include<string>#include<cstring>#include<queue>#include<vector>#include<stack>#include<map>#include<Set>#include<algorithm>#include<cmath>#include<numeric>using namespaceStd;typedefLong Longll;Const intMax_n = 1e5+5;intdat[max_n*2];int*ConstPS = dat +Max_n;intA[max_n];Const intMax_m =max_n;template<typename t>structlist{intHd[max_n]; intNx[max_m];    T Val[max_m]; intsz; voidInitintN) {memset (HD+1,0,sizeof(int) *n); SZ =0; }    voidAddintUConstT &v) {nx[++SZ] =Hd[u]; VAL[SZ]=v; Hd[u]=sz; }}; LIST<int>LyX;intN;#definepara int o = 1, int l = 1, int r = N#defineLo (o<<1)#defineRO (o<<1|1)#defineTvar int md = (l+r) >>1;#defineLSN LO,L,MD#defineRSN Ro,md+1,r#defineINSD QL&LT;=L&AMP;&AMP;R&LT;=QRConst intSt_size =1<< -; ll dwn[st_size];ll mx[st_size];ll his_mx[st_size];ll his_dwn[st_size];inlinevoidSink_his (intO, ll D) {His_dwn[o]= Max (His_dwn[o], Dwn[o] + D);//Historical Most updated markers are also the largestHis_mx[o] = max (His_mx[o], Mx[o] +d);} InlinevoidSinkintO,ll D) {Mx[o]+=D; Dwn[o]+=D;} InlinevoidPush_down (into) {ll&d =His_dwn[o]; if(d) {//The child node has not been updated and the old value is savedsink_his (lo,d);        Sink_his (RO,D); D=0; } ll&AMP;D2 =Dwn[o]; if(D2) {sink (LO,D2);        Sink (RO,D2); D2=0; }}intql,qr;ll Q_DLT;voidModify (para) {if(INSD) {sink (O,Q_DLT); His_dwn[o]=Max (His_dwn[o], dwn[o]); His_mx[o]=Max (His_mx[o], mx[o]); }    Else{Tvar push_down (o); if(QL <=MD) Modify (LSN); if(QR >MD) Modify (RSN); Mx[o]=Max (Mx[lo],mx[ro]); His_mx[o]=Max (Mx[o], his_mx[o]); }}ll Q_HIS_MX (para) {if(INSD)returnHis_mx[o]; Else{Tvar push_down (o); ll re= -INFINITY; if(QL <= md) RE =Max (RE,Q_HIS_MX (LSN)); if(QR > md) RE =Max (RE,Q_HIS_MX (RSN)); returnre; }}ll Ans[max_n];voidsolve () {inti;  for(i =1; I <= N; i++) {scanf ("%d", A +i); }    intQ; scanf"%d",&p); intx, y;  for(i =1; I <= Q; i++) {scanf ("%d%d", &x, &y);    Lyx.add (Y,X); }     for(y =1; Y <= N; y++) {QL= ps[a[y]]+1; QR=y; Q_DLT=A[y]; Ps[a[y]]=y;        Modify ();  for(i = lyx.hd[y]; i; i =Lyx.nx[i]) {QL=Lyx.val[i]; Ans[i]=q_his_mx (); }    }     for(i =1; I <= Q; i++) {printf ("%lld\n", Ans[i]); }}//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif    //cout<< (((int.) ceil (LOG2 (max_n)) +1) <<endl;scanf"%d",&N);    Solve (); return 0;}

Can you answer these queries II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.