Codeforces Round #216 (Div. 2) E. Valera and Queries tree-like array for offline processing

Source: Internet
Author: User

Test instructions: n segments [Li, Ri], M queries, each query consists of CNT dots, and the output contains the total number of segments at any point in the CNT point.

Because there is no modification, so we should first go offline to think, but I did not think out.

First, the total number of line segments that do not contain this CNT point is required, so the segments that do not contain these points must be between the CNT points (here you need to add two points one is 0, one is max),

We can classify all segments by RI and then loop through the right end, for the current segment can be at Li +1, and then for each query between the two points (x, y) between the number of segments,

Only need to query the left endpoint is greater than or equal to the number of X is good, because it is the right endpoint from small to large traversal, so do not consider using the endpoint.

found that most of the offline processing is the first one of the one-dimensional order from small to large, and then processing another dimension. This is equivalent to dimensionality reduction.

1#include <bits/stdc++.h>2 3 Const intMAXN = 1e6 +Ten;4typedef std::p Air <int,int>PII;5Std::vector <int>SEG[MAXN], Q[MAXN];6Std::vector <pii>RQ[MAXN];7 namespacefenwicktree{8     intARR[MAXN];9     voidInc (intXintd) {Ten          while(X <MAXN) { OneARR[X] + =D; Ax + = x &-x; -         } -     } the     intQueryintx) { -         intres =0; -          while(X >0){ -Res + =Arr[x]; +X-= x &-x; -         } +         returnRes; A     } at     intQueryintUaintUB) { -         returnQuery (UB)-Query (ua-1); -     } - } - intANS[MAXN]; - voidinit () { inmemset (ans,0,sizeof(ans)); -      for(inti =0; i < MAXN; i++){ to seg[i].clear (); + q[i].clear (); - rq[i].clear (); the     } * } $ intMain ()Panax Notoginseng { - #ifndef Online_judge theFreopen ("In.txt","R", stdin); +     #endif //Online_judge A     intN, M; the      while(~ scanf ("%d%d", &n, &m)) { + init (); -          for(inti =0; I < n; i++){ $             intUA, UB; $scanf"%d%d", &ua, &UB); -ua++, ub++; - Seg[ub].push_back (UA); the         } -          for(inti =0; I < m; i++){Wuyi             intcnt, p; thescanf ("%d", &CNT); -Q[i].push_back (1); Wu              while(cnt--){ -scanf ("%d", &p); Aboutp++; $ Q[i].push_back (p); -             } -Q[i].push_back (maxn-1); -              for(intj =0; J < Q[i].size ()-1; J + +){ A                 intUA = q[i][j]+1; +                 intUB = q[i][j+1]-1; the Rq[ub].push_back (Std::make_pair (UA, i)); -             } $         } the          for(inti =1; i < MAXN; i++){ the              for(intj =0; J < Seg[i].size (); J + +){ the                 intTMP =Seg[i][j]; theFenwicktree::inc (Seg[i][j],1); -             } in              for(intj =0; J < Rq[i].size (); J + +){ the                 intIDX =Rq[i][j].second; the                 intTMP =Rq[i][j].first; AboutANS[IDX] + =Fenwicktree::query (Rq[i][j].first, i); the             } the         } the          for(inti =0; I < m; i++){ +printf"%d\n"Nans[i]); -         } the     }Bayi     return 0; the}

Codeforces Round #216 (Div. 2) E. Valera and Queries tree-like array for offline processing

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.