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 ...
随机推荐
- Oracle.EntityFrameworkCore使用时报错:ORA-00933:SQL命令未正确结束
如果你在使用 Oracle.EntityFrameworkCore 在执行一些分页查询或者其他数据操作时,遇到“ORA-00933:SQL命令未正确结束”, 请先检查你的DbContext中UseOr ...
- 【转载】C#中ToArray方法将List集合转换为对应的数组
在C#的List集合操作中,可以使用List集合自带的ToArray方法来将List集合转换为对应的Array数组元素.ToArray方法的签名为T[] ToArray(),存在于命名空间System ...
- Node.js学习之(第三章:简易小demo)
前言 我们前面已经学习完了Node中一些核心模块还有如何正确配置响应头的Content-Type,今天我们来实现一个简单的demo,巩固下之前学习的内容. 需求 我们平时访问百度或者其他大的门户网站的 ...
- [React] 函数定义组件
函数定义组件的例子 function Welcome(props) { return <h1>Hello, {props.name}</h1>; } 该函数是一个有效的 Rea ...
- [AIR] NativeExtension在IOS下的开发实例 --- IOS项目的创建 (一)
来源:http://bbs.9ria.com/thread-102037-1-1.html 最近看到本版块的很多关于NativeExtension的应用.但是都是在Android下面的应用.也有很多朋 ...
- sublime的package control的中文镜像
参加: http://packagecontrol.cn/installation
- unity 3D物体使用EventSystem响应事件
在ugui中创建一个canvas 之后会自动创建一个EventSystem,用来处理UI上的时间响应.(可以通过UI>EventSystem创建EventSystem) EventSystem ...
- Python学习日记(九) 装饰器函数
1.import time a.time.time() 获取到当前的时间,返回值为浮点型 import time print(time.time()) #1565422783.6497557 b.ti ...
- MyBatis之Oracle、Mysql批量插入
Mybatis中Dao层 public interface UsersMapper { public void insertEntitys(List<UserEntity> users); ...
- 交易开拓者旗舰版(TB旗舰版)软件升级中如何迁移用户数据
方法一: 1.导出工作区,桌面和系统设置.操作:文件->导入和导出 2.导出公式.操作:tb公式->公式导入导出. 3.拷贝工作室文件.目录在:user\tb用户名\Workroom 4. ...