签到题【牛客算法周周练6E】【暴力枚举+线段树】

题目链接


  题目保证数据随机,数据随机真的是太强了,直接可以跑最坏时候是O(N^2)的复杂度。

  直接暴力建线段树,然后更新的时候更新到底,查询的时候也是查询到底,因为数据随机,所以其实被处理的次数是很少的,因为要刚好是set里有的,或者是set里没有的,这就使得更新的操作变得很少。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define eps 1e-8
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(x, y) make_pair(x, y)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
set<pair<int, int>> L;
int N, maxL, tree[maxN << 2] = {0};
void pushdown(int rt)
{
    if(tree[rt])
    {
        tree[lsn] += tree[rt]; tree[rsn] += tree[rt];
        tree[rt] = 0;
    }
}
void pushup(int rt)
{
    int tmp = min(tree[lsn], tree[rsn]);
    tree[rt] += tmp;
    tree[lsn] -= tmp; tree[rsn] -= tmp;
}
void update(int rt, int l, int r, int ql, int qr, int val)
{
    if(ql <= l && qr >= r) { tree[rt] += val; return; }
    pushdown(rt);
    int mid = HalF;
    if(qr <= mid) update(QL, val);
    else if(ql > mid) update(QR, val);
    else { update(QL, val); update(QR, val); }
    pushup(rt);
}
int query(int rt, int l, int r)
{
    if(tree[rt]) return r - l + 1;
    if(l == r) return 0;
    int mid = HalF;
    return query(Lson) + query(Rson);
}
int main()
{
    scanf("%d%d", &N, &maxL);
    while(N--)
    {
        int op, x, y;
        scanf("%d%d%d", &op, &x, &y);
        if(op == 1)
        {
            if(L.find(MP(x,y)) != L.end()) continue;
            L.insert(MP(x,y));
            update(1, 1, maxL, x, y, 1);
        }
        if(op == 2)
        {
            if(L.find(MP(x,y)) == L.end()) continue;
            L.erase(MP(x,y));
            update(1, 1, maxL, x, y, -1);
        }
        if(op == 3)
        {
            printf("%d\n", query(1, 1, maxL));
        }
    }
    return 0;
}

由于更新次数很少,所以这样的写法或许会更好

  减少了查询所用的次数:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define eps 1e-8
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(x, y) make_pair(x, y)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
set<pair<int, int>> L;
int N, maxL, tree[maxN << 2] = {0}, sum[maxN << 4] = {0};
void pushup(int rt, int l, int r)
{
    if(tree[rt]) sum[rt] = r - l + 1;
    else sum[rt] = sum[lsn] + sum[rsn];
}
void update(int rt, int l, int r, int ql, int qr, int val)
{
    if(ql <= l && qr >= r)
    {
        tree[rt] += val;
        pushup(myself);
        return;
    }
    int mid = HalF;
    if(qr <= mid) update(QL, val);
    else if(ql > mid) update(QR, val);
    else { update(QL, val); update(QR, val); }
    pushup(myself);
}
int main()
{
    scanf("%d%d", &N, &maxL);
    while(N--)
    {
        int op, x, y;
        scanf("%d%d%d", &op, &x, &y);
        if(op == 1)
        {
            if(L.find(MP(x,y)) != L.end()) continue;
            L.insert(MP(x,y));
            update(1, 1, maxL, x, y, 1);
        }
        if(op == 2)
        {
            if(L.find(MP(x,y)) == L.end()) continue;
            L.erase(MP(x,y));
            update(1, 1, maxL, x, y, -1);
        }
        if(op == 3)
        {
            printf("%d\n", sum[1]);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值