hdu4417 (Offline operation + tree-like array)

Source: Internet
Author: User

Test instructions

Given an array of length n, a query of M times, how many of the numbers are smaller than h for each query [a, b] interval?

Since the values of N and M range from 0 to 10 for 5, the direct answer will time out, so consider reading all the query operations first, then answering the number in the [a, b] interval that is smaller than H.

Summation is similar to the method of finding positive ordinal numbers.

Writing can be inserted into the search, you can also look at the side of the plug, while the side of the simple;

But this problem from the beginning WA to the tail, because the Add function less dozen equals number, slap himself.

Side by side search:

#include <iostream>#include<cstdio>#include<time.h>#include<stdlib.h>#include<cstring>#include<algorithm>#defineMAXN 110000#defineLL intusing namespacestd;structnode{LL Value,id;};structquerynode{LL l,r,h=-1, CNT;}; Node A[maxn];querynode QUE[MAXN]; LL C[MAXN]; LL n,m; LL ANS[MAXN];voidinit () {memset (A,0,sizeof(a)); Memset (c,0,sizeof(c)); memset (ans,0,sizeof(ANS));}BOOLCMP1 (node A,node b) {returna.value<B.value; }BOOLCMP2 (Querynode a,querynode b) {returna.h<B.H;}intInline Lowbit (intx) {    returnX & (-x);}voidInline Add (intx) {     while(x <=N) {C[x]++; X+=lowbit (x); }}intInline sum (intx) {    ints =0;  while(X >0) {s+=C[x]; X-=lowbit (x); }    returns;}voidsolve () {intt=1;  for(intI=1; i<=n;i++)    {            if(Que[t]. h==-1)            {                 Break; }             while(Que[t]. H<a[i].value && Que[t]. h!=-1) {ans[que[t].cnt]=sum (Que[t]. R)-sum (Que[t]. L-1); T++;            } add (A[i].id);  while(a[i].value==a[i+1].value && i<=N) {i++;            Add (a[i].id); }             while(Que[t]. H==a[i].value && Que[t]. h!=-1) {ans[que[t].cnt]=sum (Que[t]. R)-sum (Que[t]. L-1); T++; }    }     for(inti=t;i<=m;i++) {ans[que[i].cnt]=sum (Que[i]. R)-sum (Que[i]. L-1); }}intMain () {//freopen ("Test.txt", "R", stdin);    intT; scanf ("%d",&t); intCase=1;  for(intCase=1; case<=t; case++) {scanf ("%d%d",&n,&M);      Init ();  for(intI=1; i<=n;i++) {scanf ("%d",&a[i].value); A[i].id=i; }       for(intI=1; i<=m;i++) {scanf ("%d%d%d", &que[i]. L,&que[i]. r,&Que[i].         H); Que[i]. L++; Que[i]. R++; Que[i].cnt=i; } sort (A+1, a+n+1, CMP1); Sort (que+1, que+m+1, CMP2);       Solve (); printf ("Case %d:\n", case);  for(inti =1; I <= M; ++i) {printf ("%d\n", Ans[i]); }    }    return 0;}

Side-by-side interpolation:

#include <iostream>#include<cstdio>#include<time.h>#include<stdlib.h>#include<cstring>#include<algorithm>#defineMAXN 110000#defineLL intusing namespacestd;structnode{LL Value,id;};structquerynode{LL l,r,h,cnt;}; Node A[maxn];querynode QUE[MAXN]; LL C[MAXN]; LL n,m; LL ANS[MAXN];voidinit () {memset (A,0,sizeof(a)); Memset (c,0,sizeof(c)); memset (ans,0,sizeof(ans));  for(intI=0; i<maxn;i++) {Que[i]. H=-1; }}BOOLCMP1 (node A,node b) {returna.value<B.value; }BOOLCMP2 (Querynode a,querynode b) {returna.h<B.H;}intInline Lowbit (intx) {    returnX & (-x);}voidInline Add (intx) {     while(x <=N) {C[x]++; X+=lowbit (x); }}intInline sum (intx) {    ints =0;  while(X >0) {s+=C[x]; X-=lowbit (x); }    returns;}voidsolve1 () { for(intAski =0, DITJ =0; Aski < M; ++Aski) {            while(Ditj < N && Que[aski]. H >=a[ditj].value)              {Add (a[ditj].id); DITJ++; } ans[que[aski].cnt]= SUM (Que[aski]. R)-sum (Que[aski]. L1); }}intMain () {//freopen ("Test.txt", "R", stdin);    intT; scanf ("%d",&t); intCase=1;  for(intCase=1; case<=t; case++) {scanf ("%d%d",&n,&l);      Init ();  for(intI=0; i<n;i++) {scanf ("%d",&a[i].value); A[i].id=i+1; }       for(intI=0; i<m;i++) {scanf ("%d%d%d", &que[i]. L,&que[i]. r,&Que[i].         H); Que[i]. L++; Que[i]. R++; Que[i].cnt=i+1; } sort (A,a+N,CMP1); Sort (Que,que+M,CMP2);       Solve1 (); printf ("Case %d:\n", case);  for(inti =1; I <= M; ++i) {printf ("%d\n", Ans[i]); }    }    return 0;}

hdu4417 (Offline operation + tree-like array)

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.