HDU 4325 discretization + Tree array or do not use a tree-like array

Source: Internet
Author: User

Test instructions: Give some open time periods for flowers, and then ask a few flowers to open at some point in time.

Because of the ti<1e9, we need to first discretization the time, and then to abstract the point of time into an array of points, obviously, we need to do interval update and single point query, you can consider the line segment tree and tree array two practices, general, the tree array is used to maintain the interval and with a single point of modification, then, How to make interval update and single point query through tree array, Lenovo to difference fractional group, difference fractional group can be updated in O (1) time interval, but the efficiency of the single point query is O (n), obviously can not be used to deal with this problem, at this time, consider the tree array maintenance Difference Fractional group, you can O (logn) Interval Update (Update difference fraction Group L, r+1 can, make sub[l]++,sub[r+1]--), O (logn) to query single point values (differential fractional group prefix and)//tree array Maintenance difference fraction

#include <bits/stdc++. h#defineN 100005#defineMoD 998244353using namespaceStd;typedefLong Longll;intsub[n<<1],n,l[n],r[n],tim[n],nn;intLowbit (intx) {returnx&-x;};intAddintXintval) {     while(x<=nn) {Sub[x]+=Val; X+=lowbit (x); }}intQueryintx) {    intans=0;  while(x>0) {ans+=Sub[x]; X-=lowbit (x); }    returnans;}intMain () {intT; CIN>>T;  for(intCa=1; ca<=t;ca++) {memset (sub,0,sizeof(sub)); Vector<int>MP; intQ,l,r; CIN>>n>>Q;  for(intI=1; i<=n;i++) {scanf ("%d%d", l+i,r+i);            Mp.push_back (L[i]);        Mp.push_back (R[i]); }         for(intI=1; i<=q;i++) {scanf ("%d", tim+i);        Mp.push_back (Tim[i]); } NN=mp.size ();        Sort (Mp.begin (), Mp.end ());        Unique (Mp.begin (), Mp.end ());  for(intI=1; i<=n;i++) {L=lower_bound (Mp.begin (), Mp.end (), L[i])-mp.begin () +1; R=lower_bound (Mp.begin (), Mp.end (), R[i])-mp.begin () +1; Add (L,1); Add (R+1,-1); }        //for (int i=1;i<=mp.size (); i++) cerr<<query (i) <<endl;printf"Case #%d:\n", CA);  for(intI=1; i<=q;i++)        {            intPos=lower_bound (Mp.begin (), Mp.end (), Tim[i])-mp.begin () +1; printf ("%d\n", Query (POS)); }    }    return 0;}

Boingonium hindering ~ ~ Blog after writing, I regretted, the subject of the interval update and single-point query operation is separate, then why I make so much trouble also with the tree-like array, the direct difference between the fractional group can not be O (1) Single point query it? But the overall complexity is constant, still O (NLOGN)
(n is discretized, the number of map midpoint,), the constant is much lower, although the run time is only from 312ms to 296ms, but it is much simpler to write, the following is not using the tree array of AC code

//tree-like arrays maintain poor fractional groups#include <bits/stdc++.h>#defineN 100005#defineMoD 998244353using namespaceStd;typedefLong Longll;intsub[n<<1],n,l[n],r[n],tim[n],nn;intMain () {intBT; CIN>>T;  for(intCa=1; ca<=t;ca++) {memset (sub,0,sizeof(sub)); Vector<int>MP; intQ,l,r; CIN>>n>>Q;  for(intI=1; i<=n;i++) {scanf ("%d%d", l+i,r+i);            Mp.push_back (L[i]);        Mp.push_back (R[i]); }         for(intI=1; i<=q;i++) {scanf ("%d", tim+i);        Mp.push_back (Tim[i]); } NN=mp.size ();        Sort (Mp.begin (), Mp.end ());        Unique (Mp.begin (), Mp.end ());  for(intI=1; i<=n;i++) {L=lower_bound (Mp.begin (), Mp.end (), L[i])-mp.begin () +1; R=lower_bound (Mp.begin (), Mp.end (), R[i])-mp.begin () +1; SUB[L]++; Sub[r+1]--; }         for(intI=1; i<=nn;i++) sub[i]+=sub[i-1]; printf ("Case #%d:\n", CA);  for(intI=1; i<=q;i++)        {            intPos=lower_bound (Mp.begin (), Mp.end (), Tim[i])-mp.begin () +1; printf ("%d\n", Sub[pos]); }    }    return 0;}

It turns out that if you think more, the problem will be easier. It's a little bit of inspiration.



HDU 4325 discretization + Tree array or do not use a 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.