[Luogu] 送花

Posted xayata

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Luogu] 送花相关的知识,希望对你有一定的参考价值。

https://www.luogu.org/problemnew/show/2073

自己yy,明显错

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 10;
const int oo = 999999999;

#define gc getchar()

struct Node{
    int w, b, bef;
}flower[N];
int Ans1, Ans2, opt, W, B, Max, Min;
map<int, bool> Map;
int js = 1, tot = 1;

inline int read(){
    int x = 0, f = 1; char c = gc;
    while(c < 0 || c > 9) {if(c == -) f = -1; c = gc;}
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = gc;
    return x * f;
}

int main()
{
    Max = 1;
    Min = 1;
    opt = read();
    while(opt != 1) opt = read();
    flower[Max].w = read(); flower[Max].b = read(); Map[flower[Max].w] = 1;
    flower[Max].bef = 1;
    while(true){
        opt = read();
        if(opt == -1) break;
        if(opt == 1){
            flower[++ js].w = read(); flower[js].b = read();
            W = flower[js].w; B = flower[js].b;
            if(Map[W]) {js --; continue ;} else tot ++, Map[W] = 1;
            if(W > flower[Max].w || ! flower[Max].w) {flower[js].bef = Max; Max = js;}
            if(W < flower[Min].w || ! flower[Min].w) {flower[js].bef = Min; Min = js;}
        }
        else if(opt == 2){
            if(tot <= 0) continue;
            int ma = Max;
            Map[flower[Max].w] = 0;
            flower[Max].w = 0; flower[Max].b = 0;
            Max = flower[ma].bef; flower[ma].bef = 0;
            tot --;
        }
        else {
            if(tot <= 0) continue;
            int mi = Min;
            Map[flower[Min].w] = 0;
            flower[Min].w = 0; flower[Min].b = 0;
            Min = flower[mi].bef; flower[mi].bef = 0; 
            tot --;
        }
    }
    for(int i = 1; i<= N - 9; i ++) 
    Ans1 += flower[i].w, Ans2 += flower[i].b;
    cout << Ans1 << " " << Ans2;
    
    return 0;
}
/*
1 2 2
1 7 20
3
1 16 3
1 2 16
2
-1

*/

以权值为下标建立权值线段树

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ls jd<<1
#define rs jd<<1|1

using namespace std;
const int N = 1e6 + 10;
const int inf = 0x7f7f7f7f;

int opt;
bool vis[N];//用来判重;
struct Tree {int l, r, sumw, sumc, Min,Max;} T[N<<2];

void build(int jd, int ll, int rr) {
    T[jd].l = ll, T[jd].r = rr, T[jd].Min = inf;
    if(ll == rr) return;
    int m = ll + rr >> 1;
    build(ls, ll, m), build(rs, m + 1, rr);
}

void update(int jd, int pos, int val1, int val2, int val3, int val4) {
    if(T[jd].l == pos && T[jd].r == pos) {
        T[jd].sumc = val1;
        T[jd].sumw = val2;
        T[jd].Min = val3;
        T[jd].Max = val4;
        return;
    }
    if(T[ls].r >= pos) update(ls, pos, val1, val2, val3, val4);
    else update(rs, pos, val1, val2, val3, val4);
    T[jd].sumc = T[ls].sumc + T[rs].sumc;
    T[jd].sumw = T[ls].sumw + T[rs].sumw;
    T[jd].Min = min(T[ls].Min, T[rs].Min);
    T[jd].Max = max(T[ls].Max, T[rs].Max);
}

int main() 
{
    int n = N - 10;
    build(1, 1, n);
    while(scanf("%d", &opt) == 1 && opt != -1) {
        int w, c;
        if(opt == 1) {
            scanf("%d %d", &w, &c);
            if(vis[c]) continue;
            update(1, c, c, w, c, c);
            vis[c] = true;
        }
        if(opt == 3) {
            if(T[1].Min == inf) continue;//此时无花,下同;
            vis[T[1].Min] = false, update(1, T[1].Min, 0, 0, inf, 0);
        }
        if(opt == 2) {
            if(T[1].Max == 0) continue;
            vis[T[1].Max] = false, update(1, T[1].Max, 0, 0, inf, 0);
        }
    }
    printf("%d %d",T[1].sumw, T[1].sumc);
}

 

以上是关于[Luogu] 送花的主要内容,如果未能解决你的问题,请参考以下文章

Luogu2073 送花 (平衡树)

[Luogu 2073] 送花

[Luogu2073]送花

题解Luogu P2073 送花

直播送花特效

P2073 送花