Codevs1541[USACO]围墙涂色

离散加差分有点涨姿势啊 对我这种菜鸡而言还是第一次看到啊qwq

题面

大意 :n次,每次覆盖一个区间,求覆盖过m次的节点个数

sol:大概是差分的思想加上离散,就可以解决普通差分无法解决的问题了,比如本题中的1e9的范围

我的理解就是每次把坐标存在数组中,并且记录(普通差分中的1或-1)再排便序

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
struct node
{
    int x, w;
}a[200005];
int ans = 0, n, K, tot = 0;
inline bool cmp(node a,node b){return a.x < b.x;}
signed main()
{
    scanf("%lld%lld", &n, &K);
    char ch;
    int now = 0;
    for(int i = 1, x; i <= n; i++)
    {
        cin >> x >> ch;
        if (ch == 'R')
        a[++tot].x = now,
        a[tot].w = 1,
        now += x,
        a[++tot].x = now,
        a[tot].w = -1;
        else
        a[++tot].x = now,
        a[tot].w = -1,
        now -= x,
        a[++tot].x = now,
        a[tot].w = 1;
    }
    sort(a + 1, a + tot + 1, cmp);
    int sum = 0;
    for(int i = 1; i <= tot; i++)
    {
        sum += a[i].w;
        if (sum >= K) ans += a[i + 1].x - a[i].x;
    }
    printf("%lld\n", ans) ;
}
View Code

 

posted @ 2018-08-14 21:16  yccdu  阅读(121)  评论(0编辑  收藏  举报