[CERC2017] Buffalo Barricades__cerc

Source: Internet
Author: User
Tags joins
train of Thought

set+ and search the collection + Metaphysics mess ...
A flexible use of the STL problem ...
This I also read the Claris Great God's blog to write out (%%%)
Okay, no more nonsense, let's get to the point.
This question is actually to ask a point for the upper right corner can surround how many points (nonsense)
According to the official idea of the solution, first of all to use a metaphysical scan line maintenance.
If you want to scan the line, then certainly first sort ah.
Press Y to sort from large to small, scan from top down.
All right, now we're going to throw away the perimeter and just pay attention to the vertices of the fence.
First, the vertices of a fence emit rays to the left and down.
The ray emitted downward is the position of itself on the x-axis, as long as preserving this point in set is equivalent to preserving the ray.
So now we only consider the rays emitted from the left.
If there are two points below:

Here if a joins the time earlier than D, then the final form is this:

If A is later than D, then this is true:

As you can see, in the second case above, point A will be deleted from set when D joins. (see "Comment 1" in code)
Well, we're done with this one.
But what about the number 3rd and point 4th in the sample?
As you can see, the sample number 3rd points contains 4th points.
In statistics, the answer to point 3rd should be added to the number 4th point.
However, if it is 1 and 3, although 3 also contains 1, there is no need to add the 3rd point to the number 1th point answer. ("Note 5")
Then we can build one and look up the set.
When you add a point each time, record a point on the right side of it that is larger than its time; (see note 2 in code)
The final reverse enumeration each point, with and check set maintenance, the point of the answer is this point is the sum of the number of the collection. ("Note 3")
Then add this point to the point on the right side of the insertion and check it in.

One last question: What is the use of italic text above?
The "reverse enumeration" is due to the fact that we have added all the points to the entire diagram at the top of the statistics.
And now need to separate statistics, in reverse order to delete each edge, is equivalent to a topological sort. ("Note 4") code

STL have what is bad, the code quantity is short, if the handwriting splay still want various operations, STL more concise and clear ah #include <cstdio> #include <set> #include <utility>

Include <algorithm> const int maxn=300000;

struct point {int x,y,t;};
typedef STD::SET&LT;STD::p air<int,int> > Set_pair;

typedef STD::SET&LT;STD::p air<int,int> >::iterator set_pair_iterator;
Set_pair s;
int cnt[maxn+10],n,m,par[maxn+10],ans[maxn+10];

Point p[(maxn<<1) +10];

  struct Union_find_set {int fa[maxn+10];
  int find (int x) {return fa[x]?fa[x]=find (Fa[x]): x;
    inline int merge (int x,int y) {x=find (x);
    Y=find (y);
        if (x!=y) {fa[x]=y;
      CNT[Y]+=CNT[X];
  return 0;

}
};

Union_find_set F;
    BOOL CMP (const point &a,const point &b) {if (A.Y==B.Y) {return a.x<b.x;
Return a.y>b.y;
  int main () {scanf ("%d", &n);
      for (register int i=1; i<=n; ++i) {scanf ("%d%d", &p[i].x,&p[i].y);
    p[i].x= (p[i].x<<1)-1;  P[i].y= (p[i].y<<1)-1;
    p[i].t=0;
  } scanf ("%d", &m);
      for (register int i=1; i<=m; ++i) {scanf ("%d%d", &p[n+i].x,&p[n+i].y);
      p[n+i].x<<=1;
      p[n+i].y<<=1;
    p[n+i].t=i;
  } std::sort (P+1,P+N+M+1,CMP);
      for (register int i=1; i<=n+m; ++i) {std::p air<int,int> Q=std::make_pair (p[i].x,p[i].t);
          if (p[i].t) {s.insert (q);
          Set_pair_iterator Now=s.find (q); if ((++now)!=s.end ()) {par[p[i].t]=now->second;//Note 2: The point on the right side of it} while (1
              )//NOTE 1: This while loop is doing this step {now=s.find (q);
                if (Now==s.begin ()) {break;
                } if ((--now)->second<p[i].t) {break;
            } s.erase (now); } else {Set_pair_iterator it=s.lower_bound (q);
          if (It!=s.end ()) {++cnt[it->second]; 
      for (register int i=m i; i)//Note 3: This for loop is doing such a step//Note 4: Note The Reverse enumeration {Ans[i]=cnt[f.find (i)];
          if (Par[i]) {f.merge (i,par[i]);
    Note 5: There is no special handling of the above situation//because if par is longer than I, then adding the same and checking the set does not affect the result} for (register int 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.