Use heap operations to constantly add points to find the number of values corresponding to each point

Source: Internet
Author: User

First, you need to understand the code of heap operations. After all, when the line segment tree is too large, it will always be close to the line segment tree.

First, build a heap:

D = 1;
For (; D <maxn + 2; D <= 1 );

Then assign the heap value.

Search for the sum of range segments:
Int query (int s, int T)
{
Int I = d + s-1, j = d + t + 1, ANS = 0;
For (; I ^ J ^ 1; I >>= 1, J >>= 1 ){
If (~ I & 1) ans + = sum [I ^ 1];
If (J & 1) ans + = sum [J ^ 1];
}
Return ans;
}

Update operation:

Void Update (int I)
{
For (; I ^ 1; I >>= 1)
Sum [I> 1] = sum [I] + sum [I ^ 1];
}

Previously, I wrote a star level question to my blog. Here I will continue to write it again, that is, to ask how many points are there for a star position, which is less than or equal to X and less than or equal to y.

What we adopt here is to first sort on one dimension, and then sort the order, and then use another dimension to build the result.

For example, if the questions of the stars have been sorted by the size of Y, we will save time.

We are constantly adding the first node to find several numbers smaller than X. Each time we add an X, we need to tree [d + x] ++ and then perform the update operation, because the following vertices will never affect the previous vertices.

So we can do this:

 

Question 2:

Spoj rating

This question is also two dimensions. However, if there is the same value, there is no one who is bigger than the other, which is different from the stars.

Therefore, after sorting out the order, we also need to judge whether the values of the previous vertex are identical each time the vertex is added. If the values of the previous vertex are the same but not accessed, we can directly inherit the values of the previous vertex, and then update

At the same time, the points of the stars are given in order by the question itself, which is out of order here. We need to arrange the order in advance and then add the points continuously.

The method I used is to create a struct. In addition to the two-dimensional data, I also need to record the ID number. Otherwise, I cannot record the number of places that are larger than others.

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 #define N 300005 6 int sum[N<<2],D,maxn; 7 struct Edge{ 8     int x,y,id; 9     bool operator<(const Edge &m)const10     {11         return x==m.x?y<m.y:x<m.x;12     }13 }e[N];14 void update(int i)15 {16     for(;i^1;i>>=1)17         sum[i>>1]=sum[i]+sum[i^1];18 }19 int query(int s,int t)20 {21     int i=D+s-1,j=D+t+1,ans=0;22     for(;i^j^1;i>>=1,j>>=1){23         if(~i&1) ans+=sum[i^1];24         if(j&1) ans+=sum[j^1];25     }26     return ans;27 }28 int main()29 {30     int n,ans[N];31     maxn=0;32     memset(sum,0,sizeof(sum));33     scanf("%d",&n);34     for(int i=0;i<n;i++){35         scanf("%d%d",&e[i].x,&e[i].y);36         maxn=max(maxn,e[i].y);37         e[i].id=i;38     }39     D=1;40     for(;D<maxn+2;D<<=1);41     sort(e,e+n);42     int k;43     for(int i=0;i<n;i++){44         if(i>1&&e[i].x==e[i-1].x&&e[i].y==e[i-1].y){45         }46         else{47             k=query(1,e[i].y);48         }49         ans[e[i].id]=k;50         sum[D+e[i].y]++;51         update(D+e[i].y);52     }53     for(int i=0;i<n;i++)54         printf("%d\n",ans[i]);55     return 0;56 }

 

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.