HDU 5602 记忆化搜索

HDU 5603
题目链接:
题意:
有n(<=3*1e5)条线段。
M个分组,每个分组里有一些点。所有分组的点数和不超过3*1e5
点和线段的点范围均在1e6以内。
为对于一个分组,有多少条线段至少覆盖分组里的一个点。
思路:
基本照着div1里大神们的代码写的,万分感谢~

Solution1:
反过来看,只要求每个分组里不覆盖点的线段就可以。
如果把数轴的左端点0和右端点1e6也算进去的话,发现这些线段存在于相邻的两个节点的区间内。
所以一个粗糙的做法是按照边的右端点排序,每次遍历到一个新的点时把右端点小于新点的边加入优先队列,优先队列中按照左端点排序,弹出左端点小于等于上一个点的边。剩下的优先队列的元素个数就加到答案里。
然而由于分组数过大,所以赤裸裸的TLE。
分组虽然大,但是端点数却很少,所以如果能按照所有可能遍历到的端点排序,然后记录分组和同一个分组中前一个点的标号存到数组里即可。
那么必须要换一种维护的方式。
题解采用一种聪明的方式,即维护一个后缀和(具体用树状数组维护),c[i]表示目前遍历到的边中左端点大于等于i的边有多少条。目前遍历到的边右端点小于新点,所以容易得到ans += c[now] - c[pre](象征性表示,具体见代码)

Solution2:
是一种正着推的方法。
边按照左端点排序,每次把左端点小于等于当前点的边加入,把[L,R]这一段区间和加1。然后把边压入栈里面待弹出。
弹出右端点小于当前的边[L,R]这段区间减一。
ans += c[now], if(now != 分组最后一个点) ans -= c[now + 1]
源码:
Solution1:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 3 * 1e5 + 5;
const int MAXM = 1e6 + 5;
int n, m;
struct E
{
    int L, R;
//    bool operator < (const E &rbs){return R > rbs.R;}
}e[MAXN];
bool operator < (E a, E b){return a.R > b.R;}
struct D
{
    int u, mark;
    int num;
}d[MAXN];
int tot;
int ans[MAXN];
bool cmp1(D a, D b){return a.u < b.u;}
bool cmp2(E a, E b){return a.L < b.L;}
int c[MAXN];
int lowbit(int x){return x & -x;}
void add(int x, int y)
{
    while(x < MAXM) c[x] += y, x += lowbit(x);
}
int query(int x)
{
    int ans1 = 0;
    while(x > 0){
        ans1 += c[x];
        x -= lowbit(x);
    }
    return ans1;
}
vector<int>lin[MAXN];
priority_queue<E>que;
int main()
{
    while(scanf("%d%d", &n, &m) != EOF){
        memset(ans, 0, sizeof(ans));
        memset(c, 0, sizeof(c));
        while(!que.empty())  que.pop();
        for(int i = 1 ; i <= m ; i++)   lin[i].clear();
        for(int i = 1 ; i <= n ; i++)   scanf("%d%d", &e[i].L, &e[i].R);
        sort(e + 1 , e + n + 1, cmp2);
        tot = 0;
        for(int i = 1 ; i <= m ; i++){
            int temp;   scanf("%d", &temp);
            for(int j = 1 ; j <= temp ; j++){
                scanf("%d", &d[++tot].u);
                lin[i].push_back(d[tot].u);
                d[tot].mark = i;
                d[tot].num = j % temp;
            }
        }
        sort(d + 1, d + 1 + tot, cmp1);
        int now = 1;
        for(int i = 1 ; i <= tot ; i++){
            while(now <= n && e[now].L <= d[i].u){
                add(e[now].L, 1);
                add(e[now].R + 1, -1);
                que.push(e[now++]);
            }
            while(!que.empty() && que.top().R < d[i].u){
                add(que.top().L, -1);
                add(que.top().R + 1, 1);
                que.pop();
            }
            ans[d[i].mark] += query(d[i].u);
            if(d[i].num != 0)   ans[d[i].mark] -= query(lin[d[i].mark][d[i].num]);
        }
        for(int i = 1 ; i <= m ; i++)   printf("%d\n", ans[i]);
    }
    return 0;
}

Solution2:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 3 * 1e5 + 5;
const int MAXM = 1e6 + 5;
struct E
{
    int L, R;
}e[MAXM];
struct D
{
    int u, mark;
    int pre;
}d[MAXM];
int tot;
int c[MAXM + 5];
int lowbit(int x){return x & -x;}
void add(int x, int y){while(x < MAXM){ c[x] += y; x += lowbit(x);}}
int query(int x)
{
    int ans = 0;
    while(x > 0)    ans += c[x], x -= lowbit(x);
    return ans;
}
bool cmp1(E a, E b){return a.R < b.R;}
bool cmp2(D a, D b){return a.u < b.u;}
int ans[MAXN];
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF){
        tot = 0;
        memset(c, 0, sizeof(c));
        memset(ans, 0, sizeof(ans));
        for(int i = 0 ; i < n ; i++){scanf("%d%d", &e[i].L, &e[i].R);}
        for(int i = 0 ; i < m ; i++){
            int temp;   scanf("%d", &temp);
            int pre = 0;
            for(int j = 1 ; j <= temp ; j++){
                int u;  scanf("%d", &u);
                d[tot].u = u, d[tot].mark = i, d[tot].pre = pre;
                tot++;
                pre = u;
            }
            d[tot].u = MAXM - 1, d[tot].mark = i, d[tot].pre = pre;
            tot++;
        }
        sort(e, e + n, cmp1);
        sort(d, d + tot, cmp2);
        memset(ans, 0, sizeof(ans));
        int now = 0;
        for(int i = 0 ; i < tot ; i++){
//            printf("i = %d, d[i].u = %d\n", i, d[i].u);
            while(now < n && e[now].R < d[i].u) add(e[now].L, 1), now++;
            ans[d[i].mark] += query(d[i].u) - query(d[i].pre);
        }
        for(int i = 0 ; i < m ; i++)    printf("%d\n", n - ans[i]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值