UVaLive 5031 Graph and Queries (Treap)

时间:2023-02-03 19:03:33

题意:初始时给出一个图,每个点有一个权值,三种操作:(1)删除某个边;(2)修改每个点的权值;(3)询问与节点x在一个连通分量中所有点的第K大的权值。

析:首先是要先离线,然后再倒着做,第一个操作就成了加边操作,很容易实现,第二操作,就是分成两个操作,先把x结点删掉,然后再插入一个新结点,

最后一个是就是求某个连通分量的第 k 大,直接用treap直接查找就好,注意问是第 k 大,不是第 k 小。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

int w[maxn], a[maxn], b[maxn];
bool removed[maxn];
LL cnt, tot;

struct Command{
  char type;
  int x, p;
};
Command com[maxn];
int p[maxn];
int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); }

struct Node{
  Node *ch[2];
  int r, v, s;
  Node(int v) : v(v){ ch[0] = ch[1] = nullptr;  r = rand();  s = 1; }
  bool operator < (const Node &p) const{
    return r < p.r;
  }
  int cmp(int x) const{
    if(x == v)  return -1;
    return x < v ? 0 : 1;
  }
  void maintain(){
    s = 1;
    if(ch[0] != nullptr)  s += ch[0]->s;
    if(ch[1] != nullptr)  s += ch[1]->s;
  }
};

void Rotate(Node* &o, int d){
  Node *k = o->ch[d^1];  o->ch[d^1] = k->ch[d];  k->ch[d] = o;
  o->maintain();  k->maintain();  o = k;
}

void Insert(Node* &o, int x){
  if(o == nullptr) o = new Node(x);
  else{
    int d = (x < o->v ? 0 : 1);
    Insert(o->ch[d], x);
    if(o->ch[d]->r > o->r)  Rotate(o, d^1);
  }
  o->maintain();
}

void Remove(Node* &o, int x){
  int d = o->cmp(x);
  if(d == -1){
    Node* u = o;
    if(o->ch[0] != nullptr && o->ch[1] != nullptr){
      int d2 = (o->ch[0]->r > o->ch[1]->r ? 1 : 0);
      Rotate(o, d2);  Remove(o->ch[d2], x);
    }
    else{
      if(o->ch[0] == nullptr)  o = o->ch[1];
      else  o = o->ch[0];
      delete u;
    }
  }
  else  Remove(o->ch[d], x);
  if(o != nullptr)  o->maintain();
}
Node* root[maxn];

void removeTree(Node* &o){
  if(o->ch[0] != nullptr)  removeTree(o->ch[0]);
  if(o->ch[1] != nullptr)  removeTree(o->ch[1]);
  delete o;
  o = nullptr;
}

void Merge(Node* &src, Node* &des){
  if(src->ch[0] != nullptr)  Merge(src->ch[0], des);
  if(src->ch[1] != nullptr)  Merge(src->ch[1], des);
  Insert(des, src->v);
  delete src;
  src = nullptr;
}

void add(int i){
  int x = Find(a[i]);
  int y = Find(b[i]);
  if(x != y){
    if(root[x]->s > root[y]->s){ p[y] = x; Merge(root[y], root[x]); }
    else{ p[x] = y;  Merge(root[x], root[y]); }
  }
}

int kTh(Node* &o, int k){
  if(o == nullptr || k <= 0 || k > o->s)  return 0;
  int s = (o->ch[1] == nullptr ? 0 : o->ch[1]->s);
  if(s + 1 == k)  return o->v;
  if(k <= s)  return kTh(o->ch[1], k);
  return kTh(o->ch[0], k - s - 1);
}

void change(int i, int v){
  int x = Find(i);
  Remove(root[x], w[i]);
  Insert(root[x], v);
  w[i] = v;
}

void query(int x, int k){
  ++cnt;  x = Find(x);
  tot += kTh(root[x], k);
}

int main(){
  srand(233333);
  int kase = 0;
  while(scanf("%d %d", &n, &m) == 2 && m+n){
    for(int i = 1; i <= n; ++i)  scanf("%d", w+i);
    for(int i = 1; i <= m; ++i)  scanf("%d %d", a+i, b+i);
    memset(removed, 0, sizeof removed);

    int c = 0;
    while(1){
      int x, p = 0, v = 0;
      char type;
      scanf(" %c", &type);
      if(type == 'E')  break;
      scanf("%d", &x);
      if(type == 'D')  removed[x] = 1;
      else if(type == 'Q')  scanf("%d", &p);
      else {
        scanf("%d", &v);
        p = w[x];  w[x] = v;
      }
      com[c++] = (Command){type, x, p};
    }
    for(int i = 1; i <= n; ++i){
      p[i] = i;
      root[i] = new Node(w[i]);
    }
    for(int i = 1; i <= m; ++i)  if(!removed[i])  add(i);

    cnt = tot = 0;
    for(int i = c-1; i >= 0; --i){
      if(com[i].type == 'D')  add(com[i].x);
      else if(com[i].type == 'C')  change(com[i].x, com[i].p);
      else query(com[i].x, com[i].p);
    }
    printf("Case %d: %f\n", ++kase, cnt == 0 ? 0 : (double)tot / cnt);
    for(int i = 1; i <= n; ++i)  if(root[i] != nullptr)  removeTree(root[i]);
  }
  return 0;
}