Codeforces 612D prefixes and processing interval issues

Source: Internet
Author: User

Portal:Http://codeforces.com/problemset/problem/612/D

(Reprint Please specify source thank you)

Test instructions

Given the number of characters N and K,n means that the next one will enter a closed interval of n on the X- axis [Li,ri], to find the points that have at least K times, and to ask for the numbers of successive intervals consisting of these points, and make the number smallest. Output this number and output the interval from left to right (the square of the X).

For example 1, given the interval [0,5],[-3,2],[3,8], then the interval that is covered at least two times is [0,2],[3,5], two.

Problem Solving Ideas:

Processing interval coverage, one comes up to think of prefixes and, ordinary prefixes and processing words, the value of the subscript, and then open a tag array, each time for the interval [l,r],tmp[l]++,tmp[r+1]--, so that the prefix and array s, S[i] is the I was overwritten, so only need to do so once,O (n) sweep again, continuous s[x]>=k, is an interval, record output can.

It is then noted that the range of L and R is in [ -1e9,1e9], the array is not open, but n is only 1e6, with a maximum of 2e6 digits per input, so discretization is used. After the input is a numeric sort, sweep through all intervals, each time using Lower_bound to find its location, because there must be, so the resulting subscript must be the first subscript of the number (if there is a repetition, then the number of the first subscript).

When you run the sample, you find that the result is incorrect. Notice that the prefixes and arrays that run out at this time are not the same as the s[. In general, the prefix and the processing interval coverage problem when the "tmp[l]++,tmp[r+1]--" in the " L" and R refers to the numerical value, and the discretization refers to its subscript, when processing the conflict will occur.

In order to resolve this conflict, just give each number "copy" one more time, because at this time tmp[r+1] r+1 refers to a "redundant" position, there will be no conflict situation. (The language is a little weak, the first example below)

Interval [0,5],[-3,2],[3,8], entered as a[]={-3,0,2,3,5,8}, and then the prefix and array as described above are s[]={1,2,2,2,2,1}. After copying,a[]={-3,-3,0,0,2,2,3,3,5,5,8,8}, prefix and array s[]={1,1,2,2,2,1,2,2,2,1,1}, the value in the S The first number and the second number (a array of the same subscript) on a continuous interval of 2 are exactly the interval, and the conflict is resolved.

See the code for specific implementation details.

Code:

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intn=1e6+Ten;intn,k,cnt,a[n<<2];inttmp[n<<2],s[n<<2];structseg{intL,r;} Sgt[n],res[n];intMain () {//freopen ("In.txt", "R", stdin);     while(~SCANF ("%d%d",&n,&k)) {CNT=0;  for(intI=0; i<n;i++) {scanf ("%d%d",&sgt[i].l,&SGT[I].R); A[cnt++]=sgt[i].l,a[cnt++]=SGT[I].L; A[cnt++]=sgt[i].r,a[cnt++]=SGT[I].R; } sort (A,a+CNT); memset (TMP,0,sizeof(TMP));  for(intI=0; i<n;i++) {Tmp[lower_bound (A,a+CNT,SGT[I].L)-a]++; Tmp[lower_bound (A,a+CNT,SGT[I].R)-a+1]--; } s[0]=tmp[0];  for(intI=1; i<cnt;i++) S[i]=s[i-1]+Tmp[i]; inttot=0, flag=0;  for(intI=0; i<cnt;i++){            if(s[i]>=k) {                if(!flag) {RES[TOT].L=A[i]; Flag=1; }            }            Else{                if(flag) {flag=0; Res[tot++].r=a[i-1]; }}} printf ("%d\n", tot);  for(intI=0; i<tot;i++) printf ("%d%d\n", RES[I].L,RES[I].R); }    return 0;}
View Code

Codeforces 612D prefixes and processing interval issues

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.