HDOJ 4417-Super Mario line segment tree or tree array offline processing ..

Source: Internet
Author: User
Tags acos

Question:

Same as above

Question:

Hold on to this question as a dead man ~~ This is because of a question in today's exercise competition. SPOJ
KQUERY. until I passed the HDOJ question using the last tree array .. the SPOJ request does not time out .. view rankings... time can be exceeded to 11 .. it's a bit embarrassing... check the time efficiency .. bottom-up: Tree division, line segment Tree, tree array, and Optimized Tree array...

Tree partitioning has the lowest efficiency... it seems that the application scope of tree division is still very limited... only when seeking the kth number .. it's hard to solve the problem...

Line Segment tree and tree Array Processing .. you need to save all the inquiries... sort the number of inquiries in ascending order .. save each number and its serial number .. sort by number in ascending order... first, the number of columns is empty (all 0 ).. then the side is placed with the number side statistical result...

Program: Line Segment tree 171 MS

#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<queue>#include<stack>#include<set>#include<map>#include<algorithm>#define ll long long#define eps 1e-5#define oo 1000000007#define pi acos(-1.0)#define MAXN 100005using namespace std;struct node{       int x,w;  }p[MAXN];struct question{       int l,r,x,id; }q[200005];int sum[MAXN<<2],ans[200005];void update(int p,int x,int l,int r,int now){       if (l==r) { sum[now]=x; return; }       int mid=l+r>>1;       if (p<=mid) update(p,x,l,mid,now<<1);       if (p>mid)  update(p,x,mid+1,r,now<<1|1);       sum[now]=sum[now<<1]+sum[now<<1|1];       return; }int query(int L,int R,int l,int r,int now){       if (L<=l && R>=r) return sum[now];       int mid=l+r>>1,ans=0;       if (L<=mid) ans+=query(L,R,l,mid,now<<1);       if (R>mid)  ans+=query(L,R,mid+1,r,now<<1|1);              return ans;}bool cmp1(node a,node b) { return a.x<b.x; }bool cmp2(question a,question b) { return a.x<b.x; } int main(){                   int n,m,i,x,t,h,T,cases;         scanf("%d",&T);       for (cases=1;cases<=T;cases++)       {               printf("Case %d:\n",cases);               scanf("%d%d",&n,&m);               for (i=1;i<=n;i++) scanf("%d",&p[i].x),p[i].w=i;               sort(p+1,p+1+n,cmp1);               p[n+1].x=oo;                for (i=1;i<=m;i++)                {                     scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].x);                     q[i].l++,q[i].r++;                     q[i].id=i;               }               sort(q+1,q+1+m,cmp2);               memset(sum,0,sizeof(sum));               h=x=1;               while (x<=n && h<=m)               {                     t=p[x].x;                     while (p[x].x==t) update(p[x].w,1,1,n,1),x++;                     while (h<=m && q[h].x<t) ans[q[h].id]=0,h++;                     while (h<=m && q[h].x<p[x].x)                      ans[q[h].id]=query(q[h].l,q[h].r,1,n,1),h++;               }               for (i=1;i<=m;i++) printf("%d\n",ans[i]);       }       return 0;}

Program: Tree array 125 MS

#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<queue>#include<stack>#include<set>#include<map>#include<algorithm>#define ll long long#define eps 1e-5#define oo 1000000007#define pi acos(-1.0)#define MAXN 100005using namespace std;struct node{       int x,w;  }p[MAXN];struct question{       int l,r,x,id; }q[200005];int sum[MAXN],ans[200005],n; void insert(int x,int k){       while (k<=n)       {             sum[k]+=x;             k+=k&(-k);       }       return;}int query(int k){       int ans=0;       while (k)       {               ans+=sum[k];               k-=k&(-k);       }       return ans;}bool cmp1(node a,node b) { return a.x<b.x; }bool cmp2(question a,question b) { return a.x<b.x; } int main(){                   int m,i,x,t,h,T,cases;        scanf("%d",&T);       for (cases=1;cases<=T;cases++)       {               printf("Case %d:\n",cases);               scanf("%d%d",&n,&m);               for (i=1;i<=n;i++) scanf("%d",&p[i].x),p[i].w=i;               sort(p+1,p+1+n,cmp1);               p[n+1].x=oo;                for (i=1;i<=m;i++)                {                     scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].x);                     q[i].l++,q[i].r++;                     q[i].id=i;               }               sort(q+1,q+1+m,cmp2);               memset(sum,0,sizeof(sum));               h=x=1;               while (x<=n && h<=m)               {                     t=p[x].x;                     while (p[x].x==t) insert(1,p[x].w),x++;                     while (h<=m && q[h].x<t) ans[q[h].id]=0,h++;                     while (h<=m && q[h].x<p[x].x)                          ans[q[h].id]=query(q[h].r)-query(q[h].l-1),h++;               }               for (i=1;i<=m;i++) printf("%d\n",ans[i]);       }       return 0;}

Program: Tree array 78 MS

#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<queue>#include<stack>#include<set>#include<map>#include<algorithm>#define ll long long#define eps 1e-5#define oo 1000000007#define pi acos(-1.0)#define MAXN 100005#define MAXM 200005using namespace std;struct node{       int x,w;  }p[MAXN]; int sum[MAXN],ans[200005],n,ql[MAXM],qr[MAXM],qx[MAXM],qid[MAXM]; void insert(int x,int k){       while (k<=n)       {             sum[k]+=x;             k+=k&(-k);       }       return;}int query(int k){       int ans=0;       while (k)       {               ans+=sum[k];               k-=k&(-k);       }       return ans;}int input(){       char c;       do       {             c=getchar();       }while (c<'0' || c>'9');       int d=0;       while (c>='0' && c<='9')       {               d=d*10+c-'0';               c=getchar();       }       return d;}bool cmp1(node a,node b) { return a.x<b.x; }bool cmp2(int a,int b) { return qx[a]<qx[b]; } int main(){                   int m,i,x,t,h,T,cases;         scanf("%d",&T);       for (cases=1;cases<=T;cases++)       {              printf("Case %d:\n",cases);              scanf("%d%d",&n,&m);              for (i=1;i<=n;i++) p[i].x=input(),p[i].w=i;              sort(p+1,p+1+n,cmp1);              p[n+1].x=oo;                for (i=1;i<=m;i++)               {                   ql[i]=input()+1,qr[i]=input()+1,qx[i]=input();                   qid[i]=i;               }              sort(qid+1,qid+1+m,cmp2);              memset(sum,0,sizeof(sum));              h=x=1;                while (x<=n && h<=m)              {                   t=p[x].x;                   while (p[x].x==t) insert(1,p[x].w),x++;                   while (h<=m && qx[qid[h]]<t) ans[qid[h]]=0,h++;                   while (h<=m && qx[qid[h]]<p[x].x)                    ans[qid[h]]=query(qr[qid[h]])-query(ql[qid[h]]-1),h++;              }              for (i=1;i<=m;i++) printf("%d\n",ans[i]);       }       return 0;}

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.