BZOJ 4552: [Tjoi2016&Heoi2016]排序 线段树 二分

此代码是个假代码,只能糊弄luogu,以后再改,路过大佬也可以帮一下辣

/*
	//fang zhi luan ma er xie E and C hun xue
	yi kai shi que shi mei kan chu lai dan diao xing
	mo bu shi zai jia wo
	ran hou guo duan liang fa ti jie hou hai shi kan bu chu dan xiao xing
	na jiou zai lai yi fa ti jie
	interesting
	yuan lai check han shu shi zhe yang de ya
	23333
	I konw
	bing bu shi zhi jie pan duan zhe ge mid shi bu shi ans
	er shi check zhe ge mid he ans de da xiao guan xi
	other:
	yuan lai seg tree hai ke yi qu jian fu zhi me
	interesting
	xian duan tree tql
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int maxn = 1e5 + 7;
const int maxm = 4e5 + 7;

int n, m, k, a[maxn], b[maxn], S[maxn], T[maxn];;
// lazy is first 1 space
int sum[maxm], lazy[maxn];
bool is_up_down[maxn];

int read() {
	int x = 0, f = 1; char s = getchar();
	for (; s < '0' || s > '9'; s = getchar()) if (s == '-') f = -1;
	for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
	return x * f;
}

void pushup(int rt) {
	sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void pushdown(int rt, int lsize, int rsize) {
	if (lazy[rt] != -1) {
		lazy[rt << 1] = lazy[rt];
		lazy[rt << 1 | 1] = lazy[rt];
		sum[rt << 1] = lazy[rt] * lsize;
		sum[rt << 1 | 1] = lazy[rt] * rsize;
		lazy[rt] = -1;
	}
}

void build(int l, int r, int rt) {
	if (l == r) {
		sum[rt] = b[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(lson);
	build(rson);
	pushup(rt);
}

void update(int L, int R, int k, int l, int r, int rt) {
	if (L > R) return;
	if (L <= l && r <= R) {
		sum[rt] = k * (r - l + 1);
		lazy[rt] = k;
		return;
	}
	int mid = (l + r) >> 1;
	pushdown(rt, mid - l + 1, r - mid);
	if (L <= mid) update(L, R, k, lson);
	if (R > mid) update(L, R, k, rson);
	pushup(rt);
}

int query(int L, int R , int l, int r, int rt) {
	if (L > R) return 0;
	if (L <= l && r <= R) {
		return sum[rt];
	}
	int mid = (l + r) >> 1;
	pushdown(rt, mid - l + 1, r - mid );
	int ans = 0;
	if (L <= mid) ans += query(L, R, lson);
	if (R > mid) ans += query(L, R, rson);
	pushup(rt);
	return ans;
}

bool check(int x) {
	memset(sum, 0, sizeof(sum));
	memset(lazy, -1, sizeof(lazy));
	for (int i = 1; i <= n; ++i)
		b[i] = (a[i] >= x ? 1 : 0);//, cout << b[i] << " "; printf("\n");
	build(root);
	for (int i = 1; i <= m; ++i) {
		int size = query(S[i], T[i], root);
		if (is_up_down[i]) {
			if(size)
				update(S[i], S[i] + size - 1, 1, root);
			if(size!=(T[i]-S[i]+1))
				update(S[i] + size, T[i], 0, root);
		} else {
			if(size!=(T[i]-S[i]+1))
				update(S[i], T[i] - size, 0, root);
			if(size)
				update(T[i] - size + 1, T[i], 1, root);
		}
		// printf("debug\n");
		// if(is_up_down[i])
		// 	printf("%d -> %d\n", S[i], T[i]);
		// else
		// 	printf("%d <- %d\n", S[i], T[i]);
		// printf("size=%d\n", size);
		// if (is_up_down[i]) {
		// 	printf("%d %d %d\n", S[i], S[i] + size - 1, 1);
		// 	printf("%d %d %d\n", S[i] + size, T[i], 0);
		// } else {
		// 	printf("%d %d %d\n", S[i], T[i] - size, 0);
		// 	printf("%d %d %d\n", T[i] - size + 1, T[i], 1);
		// }
		// printf("become\n");
		// for (int i = 1; i <= n; ++i)
		// 	   printf("%d ", query(i, i, root));
		// printf("\n");
	}
	// printf("debug1 all=%d\n", query(1, n, root));
	return query(k, k, root);
}

int main() {
	//freopen("a.in", "r", stdin);
	n = read(), m = read();
	for (int i = 1; i <= n; ++i) {
		a[i] = read();
	}
	for (int i = 1; i <= m; ++i) {
		is_up_down[i] = read(), S[i] = read(), T[i] = read();
		if(S[i] > T[i]) swap(S[i],T[i]);
	}
	k = read();
	int l = 0, r = 1e7, ans = 0;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (check(mid)) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	printf("%d\n", ans);
	return 0;
}

update 10.6

换了个struct 版本的线段树(应该不是这个锅)
反正改对了就好
二分答案
check用线段树区间修改01

/**************************************************************
    Problem: 4552
    User: 3010651817
    Language: C++
    Result: Accepted
    Time:12964 ms
    Memory:173168 kb
****************************************************************/
 
#include<bits/stdc++.h>
#define Pair pair<int, int> 
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
const int MAXN = 4e6 + 10, INF = 1e9 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-')f =- 1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, Q;
int a[MAXN], b[MAXN], opt[MAXN], L[MAXN], R[MAXN];
#define ls k << 1
#define rs k << 1 | 1
struct Node {
    int l, r, siz, tag, cnt[2];
}T[MAXN];   
Pair operator + (const Pair &a, const Pair &b) {
    return MP(a.fi + b.fi, a.se + b.se);
}
void update(int k) {
    for(int i = 0; i <= 1; i++) T[k].cnt[i] = T[ls].cnt[i] + T[rs].cnt[i];
}
void ps(int k, int val) {
    T[k].cnt[0] = T[k].cnt[1] = 0;
    T[k].cnt[val] = T[k].siz;
    T[k].tag = val;
}
void pushdown(int k) {
    if(T[k].tag == -1) return ;
    ps(ls, T[k].tag); ps(rs, T[k].tag);
    T[k].tag = -1;
}
void Build(int k, int ll, int rr) {
    T[k].l = ll; T[k].r = rr; T[k].siz = rr - ll + 1; T[k].tag = -1; T[k].cnt[0] = T[k].cnt[1] = 0;
    if(ll == rr) {T[k].cnt[b[ll]]++; return ;}
    int mid = ll + rr >> 1;
    Build(ls, ll, mid); Build(rs, mid + 1, rr);
    update(k);
}
Pair Query(int k, int ll, int rr) {
    if(ll <= T[k].l && T[k].r <= rr) return MP(T[k].cnt[0], T[k].cnt[1]);
    int mid = T[k].l + T[k].r >> 1;
    pushdown(k);
    if(ll > mid) return Query(rs, ll, rr);
    if(rr <= mid) return Query(ls, ll, rr);
    return Query(ls, ll, rr) + Query(rs, ll, rr);
}
void Mem(int k, int ll, int rr, int val) {
    if(ll <= T[k].l && T[k].r <= rr) {
        ps(k, val); return ;
    }
    pushdown(k);
    int mid = T[k].l + T[k].r >> 1;
    if(ll <= mid) Mem(ls, ll, rr, val);
    if(rr >  mid) Mem(rs, ll, rr, val);
    update(k);
}
int Point(int k, int pos) {
    if(T[k].l == T[k].r) {
        if(T[k].cnt[1]) return 1;
        else return 0;
    }
    pushdown(k);
    int mid = T[k].l + T[k].r >> 1;
    if(pos <= mid) return Point(ls, pos);
    else return Point(rs, pos);
}
void dfs(int k) {
    if(T[k].l == T[k].r) {
        if(T[k].cnt[1]) printf("1 ");
        else printf("0 ");
        return ;
    }
    pushdown(k);
    dfs(ls); dfs(rs);
}
bool check(int val) {
    for(int i = 1; i <= N; i++) b[i] = (a[i] >= val);
    Build(1, 1, N);
    //dfs(1); puts("");
    for(int i = 1; i <= M; i++) {
        Pair now = Query(1, L[i], R[i]);
        if(opt[i] == 0) Mem(1, L[i], L[i] + now.fi - 1, 0), Mem(1, L[i] + now.fi, R[i], 1);
        else Mem(1, L[i], L[i] + now.se - 1, 1), Mem(1, L[i] + now.se, R[i], 0);
    //  dfs(1); puts("");
    }
    return Point(1, Q);
}
main() {
    N = read(); M = read();
    for(int i = 1; i <= N; i++) a[i] = read();
    for(int i = 1; i <= M; i++) opt[i] = read(), L[i] = read(), R[i] = read();
    Q = read();
    int l = 1, r = N, ans = -1;
    while(l <= r) {
        int mid = l + r >> 1;
        if(check(mid)) ans = mid, l = mid + 1;
        else r = mid - 1;
    }
    printf("%d", ans);
}

posted @ 2018-10-05 21:36  复杂的哈皮狗  阅读(117)  评论(0编辑  收藏  举报