D - Ginger的操作序列 Ⅰ

这题限制用线性做法,比赛写了几发lazytag的线段树会内存超限,正解是 mrd并查集[狗头]。不过比赛时我的并查集没有压缩路径,感谢学长没有出数据卡我路径压缩。

ACcode 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <queue>
#include <bitset>
#include <vector>
#include <map>
#include <unordered_map>
//#define int long long
#define endl '\n'
#define lowbit(x) (x) & (-x)
#define mh(x) memset(x, -1, sizeof h)
#define debug(x) cerr << #x << "=" << x << endl;
#define brk exit(0);
using namespace std;
void TLE() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); }
const int N = 2e6 + 10;
const int M = 2 * N;
const int mod = 998244353;
const double esp = 1e-6;
const double pi = acos(-1);
typedef long long ll;
int p[N];
int find(int x)
{
    if(x!=p[x])
        p[x] = find(p[x]);
    return p[x];
}
signed main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n+1; i++)
        p[i] = i;
    while (m--)
    {
        int res = 0;
        int l, r;
        scanf("%d%d", &l, &r);
        for (int i = l; i <= r;)
        {
            if (p[i] == i)
            {
                res++;
                p[i] =find(r + 1);
                i++;
            }
            else
                i = p[i];
        }
        printf("%d\n", res);
    }

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值