Codeforces 612D 前缀和处理区间问题

传送门:http://codeforces.com/problemset/problem/612/D

(转载请注明出处谢谢)

题意:

给出数字n和k,n表示接下来将输入n个在x轴上的闭区间[li,ri],找出被包含了至少k次的点,并求由这些点组成的连续区间的数目,并使该数目最小。输出该数目并将区间从左到右(x的正方向)输出。

比如样例1,给出区间[0,5],[-3,2],[3,8],那么被覆盖了至少两次的区间就是[0,2],[3,5],有两个。

解题思路:

处理区间覆盖,一上来就想到了前缀和,普通前缀和处理的话,以数值为下标,然后开一个标记数组,每次对于区间[l,r],tmp[l]++,tmp[r+1]--,这样求前缀和数组s后,s[i]即为i被覆盖的次数,因此只需要如此操作一遍后,O(n)扫一遍,连续的s[x]>=k的,就为一个区间,记录输出即可。

随后注意到,l和r的范围在[-1e9,1e9],开不了数组,但n只为1e6,每次输入最多2e6个数字,因此采用离散化处理。将输入是数字排序后,扫一遍所有区间,每次用lower_bound来求它所在的位置,因为一定存在,所以得到的下标一定是该数字的第一个下标(如果有重复,则为第一个该数字的下标)。

在运行样例时,发现出来的结果不对。留意到是因为此时运行出来的前缀和数组s[]和想要的不一样。一般情况下,前缀和处理区间覆盖问题时“tmp[l]++,tmp[r+1]--”中的l和r指的是数值,而离散化后指的是它的下标,在处理的时候就会发生冲突。

为了解决这个冲突,只要给每个数字都多“复制”一次就可以了,因为此时tmp[r+1]中的r+1所指是一个“多余”的位置,就不会出现冲突的情况。(语言表述有点无力,下面以第一个样例为例)

区间[0,5],[-3,2],[3,8],输入后为a[]={-3,0,2,3,5,8},然后按上述所说求前缀和数组为s[]={1,2,2,2,2,1}。复制后,a[]={-3,-3,0,0,2,2,3,3,5,5,8,8},前缀和数组为s[]={1,1,2,2,2,1,2,2,2,1,1},此时s数组中值为2的连续区间上的第一个数和第二个数(相同下标的a数组)恰好是具体区间,冲突解决。

具体实现细节看代码。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e6+10;
int n,k,cnt,a[N<<2];
int tmp[N<<2],s[N<<2];
struct seg{
    int l,r;
}sgt[N],res[N];
int main(){
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&k)){
        cnt=0;
        for(int i=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(int i=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(int i=1;i<cnt;i++)
            s[i]=s[i-1]+tmp[i];
        int tot=0,flag=0;
        for(int i=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(int i=0;i<tot;i++)
            printf("%d %d\n",res[i].l,res[i].r);
    }
    return 0;
}

时间: 2024-08-06 14:03:11

Codeforces 612D 前缀和处理区间问题的相关文章

CodeForces 52C Circular RMQ(区间循环线段树,区间更新,区间求和)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://codeforces.com/problemset/problem/52/C You are given circular array a0,?a1,?...,?an?-?1. There are two types of operations with it: inc(lf,?rg,?v) - this operation increases each element on the segm

2015沈阳网络赛1003 Minimum Cut 树链剖分 数组维护前缀和进行区间增减

2015沈阳网络赛1003  Minimum Cut   树链剖分 数组维护前缀和进行区间增减 Minimum Cut Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Given a simple unweighted graph G 

codeforces Gym 100187F F - Doomsday 区间覆盖贪心

F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F Description Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c). Over time the stars twinkle. At moment 0 the i-th

Dasha and Photos CodeForces - 761F (前缀优化)

大意: 给定n*m初始字符矩阵, 有k个新矩阵, 第$i$个矩阵是由初始矩阵区间赋值得到的, 求选择一个新矩阵, 使得其余新矩阵到它距离和最小. 字符集比较小, 可以考虑每次区间覆盖对每个字符的贡献. 区间覆盖转化为差分, 然后前缀和优化. 刚开始辅助数组开多了, 卡内存卡了好久. 看别人代码似乎可以再优化掉两个辅助数组. #include <iostream> #include <sstream> #include <algorithm> #include <c

CodeForces 149D Coloring Brackets (区间D)

题意: 给一个合法的括号序列,仅含()这两种.现在要为每对括号中的其中一个括号上色,有两种可选:蓝or红.要求不能有两个同颜色的括号相邻,问有多少种染色的方法? 思路: 这题的模拟成分比较多吧?两种颜色还有无色,用2个bit就可以表示了.然后就是各种转移,注意结果可能非常大,要取模后输出.转移主要是不让同颜色的括号在一起.处理时可以用DFS,在区间[L,R]中找到距离最远的所有合法括号,递归往下处理子问题,直到剩下一对括号直接处理就行了. 1 //#include <bits/stdc++.h>

Codeforces 557A Ilya and Diplomas 区间选数

题意:给出3个区间 [L1,R1],[L2,R2],[L3,R3] 和正整数n,要求在3个区间内各选一个正整数,使得选出来的数之和为n.如果有多种选法,取从第一个区间内选出的数最大的选法.如果仍有多种选法,取从第二个区间中选出的数最大的选法,如果仍有多种选法,取从第三个区间内选出的数最大的选法.题目保证至少存在一种选法. 水题.要使第一个区间内选出的数尽量大,意思就是尽量让后两个区间选出的数尽量小,最小可以取到区间下界.于是第一个区间选的数ans1 = min(R1,n - L2 - L3).同

CodeForces - 1114D Flood Fill (区间dp)

You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The ii-th square initially has the color cici. Let's say, that two squares ii and jj belong to the same connected component if ci=cjci=cj, and ci=ckci=ck 

codeforces 598E E. Chocolate Bar(区间dp)

题目链接: E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you ma