zoukankan      html  css  js  c++  java
  • 【线段树】【P4198】 楼房重建

    Description

    小A在平面上(0,0)点的位置,第i栋楼房可以用一条连接(i,0)和(i,Hi)的线段表示,其中Hi为第i栋楼房的高度。如果这栋楼房上任何一个高度大于0的点与(0,0)的连线没有与之前的线段相交,那么这栋楼房就被认为是可见的。

    施工队的建造总共进行了M天。初始时,所有楼房都还没有开始建造,它们的高度均为0。在第i天,建筑队将会将横坐标为Xi的房屋的高度变为Yi(高度可以比原来大—修建,也可以比原来小—拆除,甚至可以保持不变—建筑队这天什么事也没做)。请你帮小A数数每天在建筑队完工之后,他能看到多少栋楼房?

    Input

    第一行两个正整数 (n,m)

    下面 (m) 行每行两个正整数代表 (x_i,y_i)

    Output

    每次操作后输出一行一个数字代表当前答案

    Hint

    (1~leq~x_i~leq~n~leq~100000~,~1~leq~m~leq~100000, 0~leq~y_i~leq~10^9)

    Solution

    我们考虑将问题放到线段树上。

    显然一栋楼能被看到当且仅当他和原点之间的所有楼房中没有一栋楼的楼顶到原点的斜率不小于它的楼顶到原点的斜率。

    对每个区间维护区间最大斜率和只考虑当前区间的ans。

    考虑合并两个区间的情况。

    如果左区间的最大斜率不小于右区间的最大斜率,那么显然右区间被完全挡住,直接上传左区间信息。

    否则考虑左区间对右区间的限制

    考虑右区间的左右两个子树,当左子树的最大斜率不大于左区间的最大斜率时,左子树被完全挡住,递归处理右子树的答案

    当左子树的斜率大于左区间的斜率时,左子树对右子树的限制大于左区间对右子树的限制,即我们无需考虑左区间对右子树的限制,于是我们递归计算左子树的答案,加上左子树对右子树的限制就是右区间做出的贡献。而左子树对右子树的限制就是右区间的答案剪去左子树的答案。

    时间复杂度 (O(n~log^2n))

    Code

    #include <cstdio>
    #ifdef ONLINE_JUDGE
    #define freopen(a, b, c)
    #define printtime()
    #else
    #include <ctime>
    #define printtime() printf("Times used  = %ld ms
    ", clock())
    #endif
    #define ci const int
    #define cl const long long
    
    typedef long long int ll;
    
    namespace IPT {
    	const int L = 1000000;
    	char buf[L], *front=buf, *end=buf;
    	char GetChar() {
    		if (front == end) {
    			end = buf + fread(front = buf, 1, L, stdin);
    			if (front == end) return -1;
    		}
    		return *(front++);
    	}
    }
    
    template <typename T>
    inline void qr(T &x) {
    	char ch = IPT::GetChar(), lst = ' ';
    	while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
    	while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
    	if (lst == '-') x = -x;
    }
    
    template <typename T>
    inline void ReadDb(T &x) {
    	char ch = IPT::GetChar(), lst = ' ';
    	while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
    	while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
    	if (ch == '.') {
    		ch = IPT::GetChar();
    		double base = 1;
    		while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
    	}
    	if (lst == '-') x = -x;
    }
    
    namespace OPT {
    	char buf[120];
    }
    
    template <typename T>
    inline void qw(T x, const char aft, const bool pt) {
    	if (x < 0) {x = -x, putchar('-');}
    	int top=0;
    	do {OPT::buf[++top] = static_cast<char>(x % 10 + '0');} while (x /= 10);
    	while (top) putchar(OPT::buf[top--]);
    	if (pt) putchar(aft);
    }
    
    const int maxn = 100010;
    
    struct Info {
    	int v;
    	double mh;
    	Info() {v = mh = 0;}
    };
    
    struct Tree {
    	Tree *ls, *rs;
    	int l, r;
    	Info v;
    
    	Tree() {ls = rs = NULL;}
    
    	int calc(double _v) {
    		if (this->v.mh <= _v) return 0;
    		if (!this->ls) return 1;
    		if (this->ls->v.mh <= _v) return this->rs->calc(_v);
    		else return this->ls->calc(_v) + this->v.v - this->ls->v.v;
    	}
    
    	void pushup() {
    		if (this->ls->v.mh >= this->rs->v.mh)	{
    			this->v = this->ls->v; return;
    		} else {
    			this->v.mh = this->rs->v.mh;
    			this->v.v = this->ls->v.v + this->rs->calc(this->ls->v.mh);
    		}
    	}
    };
    Tree *rot;
    
    int n, m;
    
    void build(Tree*, ci, ci);
    void update(Tree*, ci, const double);
    
    int main() {
    	freopen("1.in", "r", stdin);
    	qr(n); qr(m);
    	rot = new Tree;
    	build(rot, 0, n);
    	int a, b;
    	while (m--) {
    		a = b = 0; qr(a); qr(b);
    		update(rot, a, 1.0 * b / a);
    		qw(rot->v.v - 1, '
    ', true);
    	}
    	printtime();
    }
    
    void build(Tree *u, ci l, ci r) {
    	u->l = l; u->r = r;
    	if (l == r) {u->v.v = 1; return;}
    	int mid = (l + r) >> 1;
    	build(u->ls = new Tree, l, mid);
    	build(u->rs = new Tree, mid + 1, r);
    	u->pushup();
    }
    
    void update(Tree *u, ci p, const double v) {
    	if ((u->l > p) || (u->r < p)) return;
    	if (u->l == u->r) {
    		u->v.mh = v; return;
    	}
    	update(u->ls, p, v);
    	update(u->rs, p, v);
    	u->pushup();
    }
    
  • 相关阅读:
    LeetCode 252. Meeting Rooms
    LeetCode 161. One Edit Distance
    LeetCode 156. Binary Tree Upside Down
    LeetCode 173. Binary Search Tree Iterator
    LeetCode 285. Inorder Successor in BST
    LeetCode 305. Number of Islands II
    LeetCode 272. Closest Binary Search Tree Value II
    LeetCode 270. Closest Binary Search Tree Value
    LeetCode 329. Longest Increasing Path in a Matrix
    LintCode Subtree
  • 原文地址:https://www.cnblogs.com/yifusuyi/p/10367654.html
Copyright © 2011-2022 走看看