标签:pac push ctime ble can priority air lse 0.00
有一根长度为 \(L\) 的白色条状物。有两种操作:
输入 \(L\) 和 \(n\) 次操作,要你输出每次操作之后
观察出题目的询问每次都是一样的,其实也只有一种修改(添和删相当于互逆操作)
我们可以建一棵线段树
\(v\) 表示有多少个黑区间
\(len\) 表示黑区间的总长度
\(tag\) 表示该区间添加的整布条数
显然答案就是线段树的根节点的数据
那么怎么维护这些信息?
我们继续记录一下信息:
\(lbd\) 表示该区间左端点是否覆盖
\(rbd\) 表示该区间右端点是否覆盖
先看如何上传
显然 \(lbd(x)=lbd(ls(x)),rbd(x)=rbd(rs(x)),len=len(ls(x))+len(rs(x))\),那么 \(v\) 怎么搞? 是 \(v(ls(x))+v(rs(x))\) 么
值得注意的是,需要特判左右拼接的情况,即 \(rbd(ls(x))\) 和 \(lbd(rs(x))\) 都为正的时候,\(v\) 要减一
然后重点是下传
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 200010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }
template <class T> void chmin(T &a, T b) { if (a > b) a = b ; }
template <class T> void chmax(T &a, T b) { if (a < b) a = b ; }
int n, m ;
char s[10] ;
struct SegTree {
int l, r, v, len, lbd, rbd, tag, sz ;
#define ls(x) x << 1
#define rs(x) x << 1 | 1
#define l(x) tr[x].l
#define r(x) tr[x].r
#define v(x) tr[x].v
#define sz(x) tr[x].sz
#define len(x) tr[x].len
#define lbd(x) tr[x].lbd
#define rbd(x) tr[x].rbd
#define tag(x) tr[x].tag
} tr[N << 2] ;
void pushup(int x) {
if (tag(x) > 0) {
lbd(x) = rbd(x) = v(x) = 1 ;
len(x) = sz(x) ;
} else {
lbd(x) = lbd(ls(x)) ;
rbd(x) = rbd(rs(x)) ;
len(x) = len(ls(x)) + len(rs(x)) ;
v(x) = v(ls(x)) + v(rs(x)) ;
if (rbd(ls(x)) && lbd(rs(x))) v(x)-- ;
}
}
void build(int x, int l, int r) {
l(x) = l, r(x) = r, sz(x) = r(x) - l(x) + 1 ;
if (l == r) return ;
int mid = (l(x) + r(x)) >> 1 ;
build(ls(x), l, mid) ;
build(rs(x), mid + 1, r) ;
// pushup(x) ;
}
void modify(int x, int l, int r, int v) {
// cout << x << endl ;
if (l <= l(x) && r(x) <= r) {
tag(x) += v ;
if (l(x) == r(x)) { // 叶子结点
if (tag(x) > 0) {
len(x) = lbd(x) = rbd(x) = v(x) = 1 ;
} else {
len(x) = lbd(x) = rbd(x) = v(x) = 0 ;
}
} else {
pushup(x) ;
}
return ;
}
int mid = (l(x) + r(x)) >> 1 ;
if (l <= mid) modify(ls(x), l, r, v) ;
if (mid < r) modify(rs(x), l, r, v) ;
pushup(x) ;
}
signed main(){
// freopen("test.in", "r", stdin) ;
// freopen("test.out", "w", stdout) ;
scanf("%d%d", &n, &m) ;
build(1, 0, n) ;
rep(i, 1, m) {
int op, x, v ; scanf("%d%d%d", &op, &x, &v) ;
if (op == 1) modify(1, x, x + v - 1, 1) ;
else modify(1, x, x + v - 1, -1) ;
printf("%d %d\n", v(1), len(1)) ;
}
return 0 ;
}
/*
写代码时请注意:
1.ll?数组大小,边界?数据范围?
2.精度?
3.特判?
4.至少做一些
思考提醒:
1.最大值最小->二分?
2.可以贪心么?不行dp可以么
3.可以优化么
4.维护区间用什么数据结构?
5.统计方案是用dp?模了么?
6.逆向思维?
*/
标签:pac push ctime ble can priority air lse 0.00
原文地址:https://www.cnblogs.com/harryhqg/p/10525157.html