Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树

题解看这里 liouzhou_101的博客园

更简洁的代码看这里:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define X first
#define Y second
inline void read(int &x) {
    int flag = 1; char ch;
    while(!isdigit(ch=getchar()))if(ch=='-')flag=-flag;
    for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    x*=flag;
}
struct node {
    node *ls, *rs;
    bool cov; //是否被全覆盖
    int val; //被覆盖的值
    LL sum, lsum;
}*root;
inline node* Newnode() {
    node *re = new node;
    re->ls = re->rs = 0;
    re->sum = re->lsum = re->val = re->cov = 0;
    return re;
}
inline void upd(node *&p) { //update
	if(!p->ls) p->ls = Newnode();
	if(!p->rs) p->rs = Newnode();
	p->sum = p->ls->sum + p->rs->sum;
	p->lsum = min(p->ls->lsum, p->ls->sum + p->rs->lsum);
}
inline void cover(node *&p, int l, int r, int val) { //覆盖
	if(!p) p = Newnode();
	p->cov = 1, p->val = val;
	p->sum = 1ll * (r-l+1) * val;
	p->lsum = val >= 0 ? 0 : p->sum;
}
inline void pd(node *&p, int l, int r, int mid) { //pushdown
	if(p->cov) {
		cover(p->ls, l, mid, p->val);
		cover(p->rs, mid+1, r, p->val);
		p->val = p->cov = 0;
	}
}
void Modify(node *&p, int l, int r, int x, int y, int val) {
	//printf("(%d, %d)\n", l, r);
	if(!p) p = Newnode();
	if(x == l && y == r) {
		cover(p, l, r, val);
		return;
	}
	int mid = (l + r) >> 1;
	pd(p, l, r, mid);
	if(y <= mid) Modify(p->ls, l, mid, x, y, val);
	else if(x > mid) Modify(p->rs, mid+1, r, x, y, val);
	else Modify(p->ls, l, mid, x, mid, val), Modify(p->rs, mid+1, r, mid+1, y, val);
	upd(p);
}
#define pdl pair<double, long long>
pdl query(node *&p, int l, int r, int x, int y, LL V) {
	if(!p) p = Newnode();
	if(l == x && r == y) {
		if(V + p->lsum > 0) return pdl(-1, p->sum);
		if(l == r || p->cov) return pdl(l-1.0*V/(p->sum/(r-l+1)), p->sum); //p->sum < 0
	}
	int mid = (l + r) >> 1;
	pd(p, l, r, mid);
	if(y <= mid) return query(p->ls, l, mid, x, y, V);
	else if(x > mid) return query(p->rs, mid+1, r, x, y, V);
	else {
		auto l_ans = query(p->ls, l, mid, x, mid, V);
		if(l_ans.X > 0) return l_ans;
		else {
			auto r_ans = query(p->rs, mid+1, r, mid+1, y, V + l_ans.Y);
			return pdl(r_ans.X, l_ans.Y + r_ans.Y);
		}
	}
}
map<int, int>H;
int main () {
	int L = 1, R = 1000000000, Q;
	read(Q); H[L-1] = H[R+1] = 0;
	root = Newnode();
	int op, l, r, v;
	while(Q--) {
        read(op);
        if(op == 1) {
            read(l), read(v); H[l] = v;
            auto it = H.find(l), nxt = it;
			++nxt;
            Modify(root, L, R, l, min(nxt->X-1, R), v);
        }
        else if(op == 2) {
            read(l);
            auto it = H.find(l), pre = it, nxt = it;
            --pre, ++nxt;
            Modify(root, L, R, l, min(nxt->X-1, R), pre->Y);
            H.erase(l);
        }
        else {
            read(l), read(r), read(v);
			if(v == 0) printf("%d\n", l);
			else {
				auto it = H.lower_bound(l);
				l = it->X;
				if(l >= r) puts("-1");
				else {
					auto ans = query(root, L, R, l, r-1, v);
					if(ans.X < 0) puts("-1");
					else printf("%.8f\n", ans.X);
				}
			}
        }
	}
}
posted @ 2019-12-14 14:50  _Ark  阅读(95)  评论(0编辑  收藏  举报