BZOJ 1455 罗马游戏

可并堆的裸题,pbds练手用。

#include <ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
typedef __gnu_pbds::priority_queue <int, greater<int>, pairing_heap_tag> Heap;
erase(iterator) 根据迭代器删除元素
modify(iterator, val) 根据迭代器修改值
join(other), 使用之后另一个会被清空
其他同STL
#define others
#ifdef poj
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#endif // poj
#ifdef others
#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/priority_queue.hpp>
#endif // others
//#define file
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define eps 1e-8
const double pi = acos(-1.0);

typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
void umax(int &a, int b) {
    a = max(a, b);
}
void umin(int &a, int b) {
    a = min(a, b);
}
int dcmp(double x) {
    return fabs(x) <= eps?0:(x > 0?1:-1);
}
void file() {
    freopen("data_in.txt", "r", stdin);
    freopen("data_out.txt", "w", stdout);
}
const LL mod = 1e9+7;

ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

namespace solver {
/*
#include <ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;

*/
    const int maxn = 1100000;
    struct A {
        int val;
        int id;
        bool operator < (const A & b) const {
            return val > b.val || (val == b.val && id > b.id);
        }
    };
    typedef __gnu_pbds::priority_queue <A> Heap;
    Heap heap[maxn];
    int fa[maxn];
    int find(int x) {
        return x == fa[x]?x:fa[x] = find(fa[x]);
    }
    int vis[maxn];
    void solve() {
        int n, m;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            int v;
            cin >> v;
            heap[i].push({v, i});
            fa[i] = i;
        }
        cin >> m;
        for (int i = 1; i <= m; i++) {
            string str;
            cin >> str;
            if (str[0] == 'M') {
                int x, y;
                cin >> x >> y;
                if(vis[x] || vis[y]) continue;
                x = find(x);
                y = find(y);
                if(x != y) {
                    fa[x] = y;
                    heap[y].join(heap[x]);
                }
            } else {
                int x;
                cin >> x;
                if(vis[x]) {
                    cout << 0 << '\n';
                    continue;
                }
                x = find(x);
                A buf = heap[x].top();
                cout << buf.val << '\n';
                heap[x].pop();
                vis[buf.id] = 1;
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
//    file();
    solver::solve();
    return 0;
}

/*
1,2,4,6,9,13,17,21,26,32,38,45,53,61,69,77,86,96,106,117,129,141,153,166,180,194,209,225,241,257,273,
*/

猜你喜欢

转载自blog.csdn.net/meopass/article/details/81235753