当前位置: 首页 > news >正文

BZOJ4939 [YNOI2016]掉进兔子洞

题目蓝链

Solution

首先,很显然这题是要用莫队来处理的。我们先把输入的数字另外排一下序,然后记录一下\(p_i\)表示每一个数字对应在排好序的数列里面是排第几个。询问的时候要把一个询问拆成\(3\)个询问,然后再并起来。然后在莫队的时候记录\(cnt_i\)表示当前数字\(i\)出现的次数,再开一个\(bitset\),假设当前需要加入\(bitset\)的数字是\(x\),我们就令\(bitset\)中的第\(p_x + cnt_x\)位为\(1\),然后\(++cnt_x\)。这样就会使得把三个\(bitset\)并起来之后,恰好剩下的就是在三个区间都出现了的数字,也就是需要删除的数字,我们就只需要看并起来之后还剩多少个\(1\)就行了

还有一个问题,就是我们必须要对每一个询问(拆之前)都要开一个\(bitset\)来存答案,但这会开不下。所以我们就要平衡一下空间复杂度和时间复杂度,把询问分为几块来处理

所以其实我的程序还可以更快一些,那就是在给所有的块排好序之后再分块。但这样写起来有点麻烦,所以我就懒得卡了

Code

#include <bits/stdc++.h>

using namespace std;

#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef pair<int, int> pii;

inline int read() {
    int sum = 0, fg = 1; char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') fg = -1;
    for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
    return fg * sum;
}

const int maxn = 1e5 + 10;
const int maxm = 33333 + 10;

bitset<maxn> s[maxm];

int len;
struct node {
    int l, r, id;
    bool operator < (const node &t) const {
        if (l / len == t.l / len) return r < t.r;
        return l < t.l;
    }
}op[maxn];

int n, m, top, a[maxn], t[maxn], sum[maxn], cnt[maxn];

void solve(int q) {
    top = 0;
    for (int i = 1; i <= q; i++) {
        int x1 = read(), y1 = read(), x2 = read(), y2 = read(), x3 = read(), y3 = read();
        sum[i] = y1 - x1 + y2 - x2 + y3 - x3 + 3;
        op[++top] = (node){x1, y1, i}, op[++top] = (node){x2, y2, i}, op[++top] = (node){x3, y3, i};
        s[i].set();
    }
    sort(op + 1, op + top + 1);

    int l = 1, r = 0;
    static bitset<maxn> now; now.reset();
    memset(cnt, 0, sizeof cnt);
    for (int i = 1; i <= top; i++) {
        int x = op[i].l, y = op[i].r, id = op[i].id;
        while (l > x) {
            --l;
            now.flip(a[l] + cnt[a[l]]);
            ++cnt[a[l]];
        }
        while (r < y) {
            ++r;
            now.flip(a[r] + cnt[a[r]]);
            ++cnt[a[r]];
        }
        while (l < x) {
            --cnt[a[l]];
            now.flip(a[l] + cnt[a[l]]);
            ++l;
        }
        while (r > y) {
            --cnt[a[r]];
            now.flip(a[r] + cnt[a[r]]);
            --r;
        }
        s[id] &= now;
    }

    for (int i = 1; i <= q; i++) printf("%d\n", sum[i] - s[i].count() * 3);
}

int main() {
#ifdef xunzhen
    freopen("bitset.in", "r", stdin);
    freopen("bitset.out", "w", stdout);
#endif

    n = read(), m = read(); len = sqrt(n);
    for (int i = 1; i <= n; i++) t[i] = a[i] = read();
    sort(t + 1, t + n + 1);
    for (int i = 1; i <= n; i++) a[i] = lower_bound(t + 1, t + n + 1, a[i]) - t;

    while (m) {
        int x = min(m, 33334);
        solve(x), m -= x;
    }

    return 0;
}

Summary

这算是我第一次接触\(bitset\)吧,它可以快速的完成大量的位运算操作,之后要多做点\(bitset\)的题找找感觉

转载于:https://www.cnblogs.com/xunzhen/p/9686161.html

相关文章:

  • HDU 2010 水仙花数
  • 题解 P1494 【[国家集训队]小Z的袜子】
  • JQuery Mobile - 解决切换页面时,闪屏,白屏等问题
  • codeforce round#511
  • HDU 5763 Another Meaning (KMP/哈希+DP)
  • 阻止冒泡,阻止默认事件
  • eclipse安装详解以及遇到的问题
  • org.hibernate.hql.internal.ast.QuerySyntaxException: Ledger is not mapped [......]报错解决
  • cc2540-led/timer
  • POJ 1741 点分治
  • 深入解析Java反射(1) - 基础
  • linux zip tar
  • KindEditor 简单使用笔记
  • iOS客户端与网页交互文档
  • react简书
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • $translatePartialLoader加载失败及解决方式
  • [译]Python中的类属性与实例属性的区别
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • input的行数自动增减
  • JavaScript HTML DOM
  • JavaScript-Array类型
  • Just for fun——迅速写完快速排序
  • Redis的resp协议
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 记一次用 NodeJs 实现模拟登录的思路
  • 聊聊redis的数据结构的应用
  • 推荐一个React的管理后台框架
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 微信小程序--------语音识别(前端自己也能玩)
  • 新手搭建网站的主要流程
  • 由插件封装引出的一丢丢思考
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • ​2021半年盘点,不想你错过的重磅新书
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • #mysql 8.0 踩坑日记
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (2)STM32单片机上位机
  • (3)nginx 配置(nginx.conf)
  • (bean配置类的注解开发)学习Spring的第十三天
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (二)linux使用docker容器运行mysql
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (转)大型网站架构演变和知识体系
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • ../depcomp: line 571: exec: g++: not found
  • .net core开源商城系统源码,支持可视化布局小程序
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • .NETCORE 开发登录接口MFA谷歌多因子身份验证
  • .NET精简框架的“无法找到资源程序集”异常释疑
  • .NET应用架构设计:原则、模式与实践 目录预览
  • .sdf和.msp文件读取