Problem 2 线段树
$des$
一个无限长的 01 序列,初始全为 0,每次选择一个区间 [l,r] 进行操作,有三种操作:
1. l r 将 [l,r] 中所有元素变成 1。
2. l r 将 [l,r] 中所有元素变成 0。
3. l r 将 [l,r] 中所有元素异或上 1。
每次操作后询问最左边的 0 在哪个位置.
$sol$
线段树
将所有可能成为答案的点加入线段树的根节点维护
$code$
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cmath>
#include <cstring> using namespace std; #define LL long long
#define Rep(i, a, b) for(int i = a; i <= b; i ++)
#define gc getchar() inline LL read() {
LL x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} const int N = 1e5 + , M = N * * ; struct Node {
LL opt, l, r;
} Ask[N];
LL A[M], js;
int n; struct Node_ {
int Cnt[][M << ], Size[M << ], F[M << ]; #define lson jd << 1
#define rson jd << 1 | 1 void Build_tree(int l, int r, int jd) {
F[jd] = -;
Size[jd] = r - l + ;
Cnt[][jd] = Size[jd];
if(l == r) return ;
int mid = (l + r) >> ;
Build_tree(l, mid, lson), Build_tree(mid + , r, rson);
} void Push_down(int jd) {
int f = F[jd];
if(f == ) {
F[lson] = F[rson] = ;
Cnt[][lson] = Size[lson], Cnt[][rson] = Size[rson];
Cnt[][lson] = , Cnt[][rson] = ;
F[jd] = -;
} else if(f == ) {
F[lson] = F[rson] = ;
Cnt[][lson] = Size[lson], Cnt[][rson] = Size[rson];
Cnt[][lson] = , Cnt[][rson] = ;
F[jd] = -;
} else {
if(F[lson] == ) {
swap(Cnt[][lson], Cnt[][lson]);
F[lson] = -;
} else {
if(F[lson] == -) {
F[lson] = ;
swap(Cnt[][lson], Cnt[][lson]);
} else if(F[lson] == ) {
F[lson] = ;
Cnt[][lson] = Size[lson], Cnt[][lson] = ;
} else {
F[lson] = ;
Cnt[][lson] = Size[lson], Cnt[][lson] = ;
}
}
if(F[rson] == ) {
swap(Cnt[][rson], Cnt[][rson]);
F[rson] = -;
} else {
if(F[rson] == -) {
F[rson] = ;
swap(Cnt[][rson], Cnt[][rson]);
} else if(F[rson] == ) {
F[rson] = ;
Cnt[][rson] = Size[rson], Cnt[][rson] = ;
} else {
F[rson] = ;
Cnt[][rson] = Size[rson], Cnt[][rson] = ;
}
}
F[jd] = -;
}
} void Sec_G(int l, int r, int jd, int x, int y, int num){
if(x <= l && r <= y) {
Cnt[num][jd] = Size[jd];
Cnt[num ^ ][jd] = ;
F[jd] = num;
// cout << Cnt[1][jd] << " " << Cnt[0][jd] << "\n";
return ;
}
if(F[jd] != -) Push_down(jd);
int mid = (l + r) >> ;
// cout << Cnt[0][lson] << "\n";
if(x <= mid) Sec_G(l, mid, lson, x, y, num);
if(y > mid) Sec_G(mid + , r, rson, x, y, num);
Cnt[][jd] = Cnt[][lson] + Cnt[][rson];
Cnt[][jd] = Cnt[][lson] + Cnt[][rson];
} int Ask(int l, int r, int jd) {
if(l == r) {
return l;
}
if(F[jd] != -) Push_down(jd);
int mid = (l + r) >> ;
// cout << Cnt[0][lson] << "\n";
if(Cnt[][lson]) return Ask(l, mid, lson);
else return Ask(mid + , r, rson);
} void Seg_fz(int l, int r, int jd, int x, int y) {
if(x <= l && r <= y) {
if(F[jd] == -) {
swap(Cnt[][jd], Cnt[][jd]);
F[jd] = ;
return ;
} else if(F[jd] == ) {
F[jd] = ;
Cnt[][jd] = Size[jd];
Cnt[][jd] = ;
} else if(F[jd] == ) {
F[jd] = ;
Cnt[][jd] = ;
Cnt[][jd] = Size[jd];
} else {
F[jd] = -;
swap(Cnt[][jd], Cnt[][jd]);
}
return ;
}
if(F[jd] != -) Push_down(jd);
int mid = (l + r) >> ;
// cout << Cnt[0][lson] << "\n";
if(x <= mid) Seg_fz(l, mid, lson, x, y);
if(y > mid) Seg_fz(mid + , r, rson, x, y);
Cnt[][jd] = Cnt[][lson] + Cnt[][rson];
Cnt[][jd] = Cnt[][lson] + Cnt[][rson];
}
} Segtree; inline LL Getans() {
int w = Segtree.Ask(, js, );
// if(A[w - 1] + 1 != A[w]) return A[w - 1] + 1;
// else return A[w];
return A[w];
} int main() {
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
n = read();
Rep(i, , n) {
LL opt = read(), l = read(), r = read();
A[++ js] = l, A[++ js] = r; A[++ js] = max(1ll, l - ), A[++ js] = r + ;
Ask[i] = (Node) {
opt, l, r
};
}
A[++ js] = ;
sort(A + , A + js + );
js = unique(A + , A + js + ) - A - ;
Segtree.Build_tree(, js, );
// Rep(i, 1, 10) cout << Segtree.F[i] << " ";
// return 0;
Rep(i, , n) {
LL opt = Ask[i].opt, l = Ask[i].l, r = Ask[i].r;
int wl = lower_bound(A + , A + js + , l) - A;
int wr = lower_bound(A + , A + js + , r) - A;
if(opt == ) {
Segtree.Sec_G(, js, , wl, wr, );
cout << Getans() << "\n";
} else if(opt == ) {
Segtree.Sec_G(, js, , wl, wr, );
cout << Getans() << "\n";
} else {
Segtree.Seg_fz(, js, , wl, wr);
cout << Getans() << "\n";
}
} return ;
}
Problem 2 线段树的更多相关文章
- BZOJ_2298_[HAOI2011]problem a_线段树
BZOJ_2298_[HAOI2011]problem a_线段树 Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话( ...
- HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...
- codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)
题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...
- ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- zoj 3686 A Simple Tree Problem (线段树)
Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...
- [Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】
题目链接:CF - R296 - d2 - D 题目大意 一个特殊的图,一些数轴上的点,每个点有一个坐标 X,有一个权值 W,两点 (i, j) 之间有边当且仅当 |Xi - Xj| >= Wi ...
- HDU5107---K-short Problem (线段树区间 合并、第k大)
题意:二维平面上 N 个高度为 Hi 建筑物,M次询问,每次询问输出 位于坐标(x ,y)左下角(也就是xi <= x && yi <= y)的建筑物中的第k高的建筑物的高 ...
- CodeForces903G Yet Another Maxflow Problem 扫描线 + 线段树 + 最小割
给定两条链\(A, B\),其中\(A\)链某些点向\(B\)链有连边,支持修改\(A\)链中的某条边权以及查询\(A_1\)到\(B_n\)的最大流 显而易见,\(A\)和\(B\)链中一定满足左部 ...
- ZOJ 3686 A Simple Tree Problem(线段树)
Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...
随机推荐
- RAC环境备份,RMAN异机在单实例环境恢复redo问题
目的:客户环境11.2.0.4 ,多节点RAC环境,现在使用带库的备份,在单机环境测试备份有效性 1.normal参数文件 删除集群相关参数cluster 2.mount 使用备份的控制文件进行还原 ...
- Centos7 在线安装开发环境 jdk1.8+mysql+tomcat
写在最前 刚刚开始接触Linux,并折腾着在服务器上部署自己的项目,当然作为一个后端开发人员,必不可少的东西肯定是 JDK Mysql Tomcat容器 每天记录一天,每天进步一点点~~ 1.更新系统 ...
- ①将SVN迁移到GitLab-单分支迁移
将SVN上的代码迁移到GitLab上,实际原理是将所迁移的服务器上,拷贝SVN上的相关代码,在服务器上生成Git相关仓库,然后推送到GitLab仓库,并保存SVN相关的提交记录,分支,标签等信息. 一 ...
- BUAA_OO第三单元总结性博客作业——JML
一.JML 在第三单元的面向对象课程中我们第一次接触了JML语言以及基于JML规范的规格化设计.在之前一系列关于面向对象思想的学习认识中,我们知道了Java是一种面向对象的语言,面向对象思想的一个重要 ...
- 1+X学习日志——扇形2D效果
section{ width: 500px; height: 300px; border-bottom: 10px solid black; position: relative; margin: 1 ...
- [LeetCode] 72. 编辑距离 ☆☆☆☆☆(动态规划)
https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-mian-shi-ti-xiang-jie-by-labu ...
- Android笔记(五十八)Android总结:四大组件——Activity篇
什么是Activity Activity是一种包含用户界面的组件,主要用于和用户进行交互,一个APP通常由多个Activity组成. 每个Activity都对应一个布局文件,通过setContentV ...
- 详解微信公众平台UnionID和OpenID的区别
OpenID: 普通用户的标识,对当前开发者帐号唯一.一个openid对应一个公众号. UnionID :用户统一标识.针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的. 微信的 ...
- java8 lamb表达式对List排序
场景一:List<Long> 或其他泛型,非对象 List<Long> ids = new ArrayList(); ids.add(100000001L); ids.add( ...
- c# MemoryStream 类