博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bzoj4552: [Tjoi2016&Heoi2016]排序
阅读量:6146 次
发布时间:2019-06-21

本文共 2813 字,大约阅读时间需要 9 分钟。

题目

Sol

二分+线段树

巧妙啊我怎么就没想到
二分答案,把数分类,大于等于\(mid\)的为\(1\),小于的为\(0\)
相当于给\(01\)序列排序,最后判断询问位置上是不是\(1\)
线段树+lazy覆盖

# include 
# define RG register# define IL inline# define Fill(a, b) memset(a, b, sizeof(a))using namespace std;typedef long long ll;const int _(1e5 + 5);IL int Input(){ RG int x = 0, z = 1; RG char c = getchar(); for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1; for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * z;}int n, m, q, a[_], ql[_], qr[_], qo[_];int sum[_ << 2], cov[_ << 2];IL void Build(RG int x, RG int l, RG int r, RG int v){ sum[x] = 0, cov[x] = -1; if(l == r){ sum[x] = a[l] >= v; return; } RG int mid = (l + r) >> 1, ls = x << 1, rs = x << 1 | 1; Build(ls, l, mid, v), Build(rs, mid + 1, r, v); sum[x] = sum[ls] + sum[rs];}IL void Pushdown(RG int x, RG int l, RG int mid, RG int r){ RG int ls = x << 1, rs = x << 1 | 1; cov[ls] = cov[rs] = cov[x]; sum[ls] = (mid - l + 1) * cov[x]; sum[rs] = (r - mid) * cov[x]; cov[x] = -1;}IL void Modify(RG int x, RG int l, RG int r, RG int L, RG int R, RG int v){ if(L <= l && R >= r){ sum[x] = (r - l + 1) * v; cov[x] = v; return; } RG int mid = (l + r) >> 1, ls = x << 1, rs = x << 1 | 1; if(cov[x] != -1) Pushdown(x, l, mid, r); if(L <= mid) Modify(ls, l, mid, L, R, v); if(R > mid) Modify(rs, mid + 1, r, L, R, v); sum[x] = sum[ls] + sum[rs];}IL int Query(RG int x, RG int l, RG int r, RG int L, RG int R){ if(cov[x] != -1) return cov[x] * (R - L + 1); if(L <= l && R >= r) return sum[x]; RG int mid = (l + r) >> 1; if(R <= mid) return Query(x << 1, l, mid, L, R); if(L > mid) return Query(x << 1 | 1, mid + 1, r, L, R); return Query(x << 1, l, mid, L, mid) + Query(x << 1 | 1, mid + 1, r, mid + 1, R);}IL bool Check(RG int mid){ Build(1, 1, n, mid); for(RG int i = 1; i <= m; ++i){ RG int cnt = Query(1, 1, n, ql[i], qr[i]); if(!cnt || cnt == qr[i] - ql[i] + 1) continue; if(qo[i]){ Modify(1, 1, n, ql[i], ql[i] + cnt - 1, 1); Modify(1, 1, n, ql[i] + cnt, qr[i], 0); } else{ Modify(1, 1, n, ql[i], qr[i] - cnt, 0); Modify(1, 1, n, qr[i] - cnt + 1, qr[i], 1); } } return Query(1, 1, n, q, q);}int main(RG int argc, RG char* argv[]){ n = Input(), m = Input(); for(RG int i = 1; i <= n; ++i) a[i] = Input(); for(RG int i = 1; i <= m; ++i) qo[i] = Input(), ql[i] = Input(), qr[i] = Input(); q = Input(); RG int l = 1, r = n, ans = 0; while(l <= r){ RG int mid = (l + r) >> 1; if(Check(mid)) ans = mid, l = mid + 1; else r = mid - 1; } printf("%d\n", ans); return 0;}

转载于:https://www.cnblogs.com/cjoieryl/p/8469487.html

你可能感兴趣的文章
1、块:ion-item
查看>>
【论文阅读】Classification of breast cancer histology images using transfer learning
查看>>
移动端处理图片懒加载
查看>>
jQuery.on() 函数详解
查看>>
谈缓存和Redis
查看>>
【转】百度地图api,根据多点注标坐标范围计算地图缩放级别zoom自适应地图
查看>>
用户调研(补)
查看>>
ExtJS之开篇:我来了
查看>>
☆1018
查看>>
oracle 去掉空格
查看>>
6.13心得
查看>>
Runtime类
查看>>
eclipse decompiler
查看>>
记一个搜索网盘资源的网站
查看>>
jdk1.7和jdk1.8的String的getByte方法的差异
查看>>
java父子进程通信
查看>>
Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
查看>>
Olap学习笔记
查看>>
Codeforces Round #431 (Div. 1)
查看>>
如何进行数组去重
查看>>