题目:

洛谷 3822

分析:

直接按题意模拟,完了。

将每次加 / 减拆成不超过 \(32\) 个对单独一位的加 / 减。

考虑给一个二进制位(下称「当前位」)加 \(1\) 时,如果这一位本来就是 \(0\) ,那么直接变成 \(1\) 。否则要考虑进位:向左(以后默认从右向左为低位至高位,与书写顺序相同)找到第一个为 \(0\) 的位 \(p\) ,将其变成 \(1\) ,并把从 \(p\) 到当前位中间所有的 \(1\) 变成 \(0\) 。

减法是类似的。退位操作就是向左找到第一个 \(1\) ,将其变成 \(0\) ,并把中间所有 \(0\) 变成 \(1\) 。

以上找第一个 \(1\) 或者 \(0\) 和区间修改均可用线段树完成,只需要维护每个结点对应的区间是否全 \(0\) 或全 \(1\) 即可。

但是将一个询问拆成 \(32\) 次常数太大,\(3.2\times 10^7\) 次修改再带上线段树的 \(\log 3\times 10^7\) 根本过不去。考虑压位,线段树每个叶子表示连续多个(我的代码中使用的是 \(60\) 个)二进制位,找第一个 \(1\) / \(0\) 改为找第一个非 \(0\) (全 \(0\) ) / 非 \(2^{60}\) (全 \(1\) )的数。这样,每次修改只需要拆成最多对两个位置的加 / 减。复杂度 \(O(n\log m)\) 其中 \(m\) 是最大位数。

代码:

注意线段树上二分找第一个非全 \(0\) / 非全 \(1\) 的数的做法。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std; namespace zyt
{
template<typename T>
inline bool read(T &x)
{
char c;
bool f = false;
x = 0;
do
c = getchar();
while (c != EOF && c != '-' && !isdigit(c));
if (c == EOF)
return false;
if (c == '-')
f = true, c = getchar();
do
x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f)
x = -x;
return true;
}
template<typename T>
inline void write(T x)
{
static char buf[20];
char *pos = buf;
if (x < 0)
putchar('-'), x = -x;
do
*pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf)
putchar(*--pos);
}
typedef unsigned long long ull;
const int N = 1e6 + 10, DIGIT = 60;
const ull BASE = 1ULL << DIGIT;
namespace Segment_Tree
{
struct node
{
ull val;
bool all0, all1, tag0, tag1;
}tree[N << 2];
void cov0(const int rot)
{
tree[rot].val = 0;
tree[rot].all0 = tree[rot].tag0 = true;
tree[rot].all1 = tree[rot].tag1 = false;
}
void cov1(const int rot)
{
tree[rot].val = BASE - 1ULL;
tree[rot].all1 = tree[rot].tag1 = true;
tree[rot].all0 = tree[rot].tag0 = false;
}
void update(const int rot)
{
tree[rot].all0 = (tree[rot << 1].all0 && tree[rot << 1 | 1].all0);
tree[rot].all1 = (tree[rot << 1].all1 && tree[rot << 1 | 1].all1);
}
void pushdown(const int rot)
{
if (tree[rot].tag0)
{
cov0(rot << 1), cov0(rot << 1 | 1);
tree[rot].tag0 = false;
}
else if (tree[rot].tag1)
{
cov1(rot << 1), cov1(rot << 1 | 1);
tree[rot].tag1 = false;
}
}
void cover0(const int rot, const int lt, const int rt, const int ls, const int rs)
{
if (ls <= lt && rt <= rs)
{
cov0(rot);
return;
}
int mid = (lt + rt) >> 1;
pushdown(rot);
if (ls <= mid)
cover0(rot << 1, lt, mid, ls, rs);
if (rs > mid)
cover0(rot << 1 | 1, mid + 1, rt, ls, rs);
update(rot);
}
void cover1(const int rot, const int lt, const int rt, const int ls, const int rs)
{
if (ls <= lt && rt <= rs)
{
cov1(rot);
return;
}
int mid = (lt + rt) >> 1;
pushdown(rot);
if (ls <= mid)
cover1(rot << 1, lt, mid, ls, rs);
if (rs > mid)
cover1(rot << 1 | 1, mid + 1, rt, ls, rs);
update(rot);
}
void change(const int rot, const int lt, const int rt, const int pos, const ull x)
{
if (pos > rt)
return;
if (lt == rt)
{
tree[rot].val = x;
tree[rot].all0 = (x == 0);
tree[rot].all1 = (x == (BASE - 1ULL));
return;
}
int mid = (lt + rt) >> 1;
pushdown(rot);
if (pos <= mid)
change(rot << 1, lt, mid, pos, x);
else
change(rot << 1 | 1, mid + 1, rt, pos, x);
update(rot);
}
ull query(const int rot, const int lt, const int rt, const int pos)
{
if (lt == rt)
return tree[rot].val;
int mid = (lt + rt) >> 1;
pushdown(rot);
if (pos <= mid)
return query(rot << 1, lt, mid, pos);
else
return query(rot << 1 | 1, mid + 1, rt, pos);
}
int find0(const int rot, const int lt, const int rt, const int pos)
{
if (lt == rt)
return lt;
int mid = (lt + rt) >> 1;
pushdown(rot);
if (pos <= mid && !tree[rot << 1].all1)
{
int ans = find0(rot << 1, lt, mid, pos);
if (ans <= N)
return ans;
}
if (!tree[rot << 1 | 1].all1)
return find0(rot << 1 | 1, mid + 1, rt, pos);
else
return N + 1;
}
int find1(const int rot, const int lt, const int rt, const int pos)
{
if (lt == rt)
return lt;
int mid = (lt + rt) >> 1;
pushdown(rot);
if (pos <= mid && !tree[rot << 1].all0)
{
int ans = find1(rot << 1, lt, mid, pos);
if (ans <= N)
return ans;
}
if (!tree[rot << 1 | 1].all0)
return find1(rot << 1 | 1, mid + 1, rt, pos);
else
return N + 1;
}
void init()
{
cov0(1);
}
}
ull extract(const ull a, const int l, const int r)
{
return (a & ((1ULL << r) - 1ULL)) >> l;
}
bool check(const ull a, const int p)
{
return a & (1ULL << p);
}
void add(const ull a, const int p)
{
using namespace Segment_Tree;
ull now = query(1, 0, N, p);
if (now + a >= BASE)
{
int pos = find0(1, 0, N, p + 1);
ull tmp = query(1, 0, N, pos);
change(1, 0, N, pos, tmp + 1ULL);
if (pos > p + 1)
cover0(1, 0, N, p + 1, pos - 1);
}
change(1, 0, N, p, (now + a) % BASE);
}
void sub(const ull a, const int p)
{
using namespace Segment_Tree;
ull now = query(1, 0, N, p);
if (now < a)
{
int pos = find1(1, 0, N, p + 1);
ull tmp = query(1, 0, N, pos);
change(1, 0, N, pos, tmp - 1ULL);
if (pos > p + 1)
cover1(1, 0, N, p + 1, pos - 1);
}
change(1, 0, N, p, (now - a + BASE) % BASE);
}
int work()
{
using namespace Segment_Tree;
int n, t1, t2, t3;
read(n), read(t1), read(t2), read(t3);
init();
while (n--)
{
int opt;
read(opt);
if (opt == 1)
{
int a, b;
read(a), read(b);
if (a > 0) //ADD
{
add(extract(a, 0, DIGIT - b % DIGIT) << (b % DIGIT), b / DIGIT);
add(extract(a, DIGIT - b % DIGIT, DIGIT), b / DIGIT + 1);
}
else if (a < 0)
{
a = -a;
sub(extract(a, 0, DIGIT - b % DIGIT) << (b % DIGIT), b / DIGIT);
sub(extract(a, DIGIT - b % DIGIT, DIGIT), b / DIGIT + 1);
}
}
else
{
int k;
read(k);
write(check(query(1, 0, N, k / DIGIT), k % DIGIT) ? 1 : 0), putchar('\n');
}
}
return 0;
}
}
int main()
{
#ifdef BlueSpirit
freopen("3822.in", "r", stdin);
freopen("3822.out", "w", stdout);
#endif
return zyt::work();
}

【洛谷3822】[NOI2017] 整数(线段树压位)的更多相关文章

  1. [BZOJ4942][Noi2017]整数 线段树+压位

    用线段树来模拟加减法过程,维护连续一段中是否全为0/1. 因为数字很大,我们60位压一位来处理. #include<iostream> #include<cstring> #i ...

  2. 洛谷3822 [NOI2017] 整数 【线段树】【位运算】

    题目分析: 首先这题的询问和位(bit)有关,不难想到是用线段树维护位运算. 现在我们压32位再来看这道题. 对于一个加法操作,它的添加位置可以得到,剩下的就是做不超过32的位移.这样根据压位的理论. ...

  3. UOJ #314. 【NOI2017】整数 | 线段树 压位

    题目链接 UOJ 134 题解 可爱的电音之王松松松出的题--好妙啊. 首先想一个朴素的做法! 把当前的整数的二进制当作01序列用线段树维护一下(序列的第i位就是整数中位权为\(2^k\)的那一位). ...

  4. 【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

    [BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依 ...

  5. 2018.10.30 bzoj4942: [Noi2017]整数(线段树压位)

    传送门 直接把修改的数拆成logloglog个二进制位一个一个修改是会TLETLETLE的. 因此我们把303030个二进制位压成一位储存在线段树里面. 然后维护区间中最靠左二进制位不为0/1的下标. ...

  6. 【BZOJ】1012: [JSOI2008]最大数maxnumber /【洛谷】1198(线段树)

    Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插 ...

  7. 洛谷P1558 色板游戏 [线段树]

    题目传送门 色板游戏 题目背景 阿宝上学了,今天老师拿来了一块很长的涂色板. 题目描述 色板长度为L,L是一个正整数,所以我们可以均匀地将它划分成L块1厘米长的小方格.并从左到右标记为1, 2, .. ...

  8. 洛谷题解P4314CPU监控--线段树

    题目链接 https://www.luogu.org/problemnew/show/P4314 https://www.lydsy.com/JudgeOnline/problem.php?id=30 ...

  9. 洛谷P3372/poj3468(线段树lazy_tag)(询问区间和,支持区间修改)

    洛谷P3372 //线段树 询问区间和,支持区间修改 #include <cstdio> using namespace std; struct treetype { int l,r; l ...

随机推荐

  1. ArcSDE数据库连接(直连、服务连)与GT_Geometry存

    http://ziliao1.com/Article/Show/48126AB1A8F563D35E3D0345677C906B 众说周知,ArcSDE空间数据库引擎提供了两种连接数据库的方式.一是服 ...

  2. webpack-Hot Module Replacement(热更新)

    模块热替换(Hot Module Replacement) 模块热替换(HMR - Hot Module Replacement)功能会在应用程序运行过程中替换.添加或删除模块,而无需重新加载整个页面 ...

  3. Binder系列8—如何使用Binder(转)

    一.Native层Binder 源码结构: ClientDemo.cpp: 客户端程序 ServerDemo.cpp:服务端程序 IMyService.h:自定义的MyService服务的头文件 IM ...

  4. Atomic Builtins - Using the GNU Compiler Collection (GCC) GCC 提供的原子操作

    http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Atomic-Builtins.html gcc从4.1.2提供了__sync_*系列的built-in函数,用 ...

  5. 深入浅出 - Android系统移植与平台开发(十二)- Android JNI机制

    第五章.JNI机制 4.1 JNI概述 由前面基础知识可知,Android的应用层由Java语言编写,Framework框架层则是由Java代码与C/C++语言实现,之所以由两种不同的语言组合开发框架 ...

  6. 嵌入式开发之davinci--- DVRRDK, EZSDK和DVSDK这三者有什么区别

    下载的时候选择信息要避免security类型的产品,这个是要审查的. DVRRDK是专门针对DVR的开发包是非公开的,针对安防的客户定制的,效率要高. EZSDK是开放的版本架构上使用openmax可 ...

  7. UISlider无法拖动进度条的问题解决

    UISlider无法拖动进度条的问题解决 最近业务中的视频播放使用到了UISlider,但是有一个奇怪的问题,就是在Modar出来的控制器中UISlider是可以正常使用的,但是在Push出来的控制器 ...

  8. flask 文件下载 文件服务器 请求参数 函数修饰符

    180M电子书 通过已经下载到pc,在上传到私有公网服务器,之后,再异地下载 以下代码,文件内容错乱 [root@bigdata-server- mydataweb]# tree . ├── mydl ...

  9. diy数据库(二)--网络通信类

    一.首先,我们先实现OSS层的ossSocket类.供数据库client和数据库引擎进行通信 友情提示:相应上面的类图的头文件和源码附在了本文的最以下. int _fd ;//socket的文件描写叙 ...

  10. YTU 2982: 奔跑吧,小明!

    2982: 奔跑吧,小明! 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 2 题目描述 小明陷入一个充满陷阱的密道之中,现在他要逃脱这里!到达密道的出口即可离开这处绝境! ...