HDU 5200 pretty like a woman's solution.

Source: Internet
Author: User

Line tree, TLE, various. Alas.... I'm so stupid.

I use the line of the tree is the record left and right interval longest consecutive tree number ... Anyway, tle.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int n=50050;struct q{int Val,index;} Que[n];int segt[n*4],l[n*4],r[n*4],c[n*4];int treeh[n];int ans[n];bool cmp (Q a,q b) {if (a.val<b.val) return true; return false;} void build (int l,int R,int RT) {l[rt]=r[rt]=r-l+1; C[rt]=1;if (l==r) {Segt[rt]=treeh[l];return;} int m= (L+R) >>1;build (l,m,rt<<1); build (m+1,r,rt<<1|1); Segt[rt]=max (segt[rt<<1],segt[rt<<1|1]);} void Slove (int l,int r,int rt,int hv) {if (l==r) {return;} int m= (L+R) >>1;if (hv>=segt[rt<<1]) l[rt<<1]=r[rt<<1]=c[rt<<1]=0;elseslove (L,m, RT&LT;&LT;1,HV); if (hv>=segt[rt<<1|1]) L[rt<<1|1]=r[rt<<1|1]=c[rt<<1|1]=0;elseslove (m +1,R,RT&LT;&LT;1|1,HV); L[rt]=l[rt<<1],r[rt]=r[rt<<1|1];if (l[rt<<1]>=m-l+1) l[rt]+=l[rt<<1|1];if (R[rt< &LT;1|1]&GT;=R-M) r[rt]+=r[rt<<1]; C[rt]=c[rt<<1]+c[rt<<1|1];if (r[rt&LT;&LT;1]&AMP;&AMP;L[RT&LT;&LT;1|1]&AMP;&AMP;C[RT]) c[rt]--;}      void readint1 (int i) {treeh[i]=0;      Char ch;      Ch=getchar ();      while (!isdigit (CH)) Ch=getchar ();          while (IsDigit (ch)) {treeh[i]=treeh[i]*10+ch-' 0 ';      Ch=getchar ();      }}int Readint2 () {int x=0;      Char ch;      Ch=getchar ();      while (!isdigit (CH)) Ch=getchar ();          while (IsDigit (ch)) {x=x*10+ch-' 0 ';      Ch=getchar ();  } return x; }int Main () {int n,q;while (scanf ("%d%d", &n,&q)!=eof) {memset (ans,0,sizeof (int) *n); for (int i=1;i<=n;i++) Readint1 (i); build (1,n,1); for (int i=0;i<q;i++) {que[i].val=readint2 (); Que[i].index=i;} Sort (que,que+q,cmp); for (int i=0;i<q;i++) {if (que[i].val>=segt[1]) {ans[que[i].index]=0;} Else{slove (1,n,1,que[i].val); ans[que[i].index]=c[1];} for (int i =0;i<q;i++) printf ("%d\n", Ans[i]);} return 0;}

  

This is the solution after looking at others, beautiful ah ...

Sort the trees, sort the inquiries, and press the heights. Two pointer scan, for no higher than the height of the inquiry tree, its original position, if the left and right are not cut off, then the number of paragraphs +1, if all cut off, then the number of segments-1, except in addition, the number of segments unchanged.

Sigh again, beautiful ah .... t_t

#include <iostream> #include <cstdio> #include <algorithm>using namespace std;const int n=50050; struct Th{int height,index;} Tree[n],que[n];bool vis[n];int n,q,ans[n];bool cmp (TH a,th b) {if (a.height<b.height) return True;return false;} void work (int &c,int index) {if (index==0) {if (vis[index+1]) c--;} else if (index==n-1) {if (vis[index-1]) c--;} Else{if (!vis[index+1]&&!vis[index-1]) C++;else if (Vis[index+1]&&vis[index-1]) {c--;}}} int main () {while (scanf ("%d%d", &n,&q)!=eof) {for (int i=0;i<n;i++) {scanf ("%d", &tree[i].height); Tree[i].index=i;} Sort (tree,tree+n,cmp); for (int i=0;i<q;i++) {scanf ("%d", &que[i].height); Que[i].index=i;} Sort (que,que+q,cmp), memset (vis,false,sizeof (VIS)), int ct=0;int counts=1;for (int i=0;i<q;i++) {for (; ct<n;ct++ {if (tree[ct].height>que[i].height) break;vis[tree[ct].index]=true;work (Counts,tree[ct].index);} Ans[que[i].index]=counts;} for (int i=0;i<q;i++) printf ("%d\n", Ans[i]);} return 0;}

  

HDU 5200 pretty like a woman's solution.

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.