• Dynamic Rankings(动态第k大+树套树)


    题目链接:

      http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112

    题目:

    思路:

      树套树板子题。

    代码实现如下:

      1 #include <set>
      2 #include <map>
      3 #include <deque>
      4 #include <queue>
      5 #include <stack>
      6 #include <cmath>
      7 #include <ctime>
      8 #include <bitset>
      9 #include <cstdio>
     10 #include <string>
     11 #include <vector>
     12 #include <cstdlib>
     13 #include <cstring>
     14 #include <iostream>
     15 #include <algorithm>
     16 using namespace std;
     17 
     18 typedef long long LL;
     19 typedef pair<LL, LL> pLL;
     20 typedef pair<LL, int> pLi;
     21 typedef pair<int, LL> pil;;
     22 typedef pair<int, int> pii;
     23 typedef unsigned long long uLL;
     24 
     25 #define lson rt<<1
     26 #define rson rt<<1|1
     27 #define lowbit(x) x&(-x)
     28 #define name2str(name) (#name)
     29 #define bug printf("*********
    ")
     30 #define debug(x) cout<<#x"=["<<x<<"]" <<endl
     31 #define FIN freopen("D://code//in.txt","r",stdin)
     32 #define IO ios::sync_with_stdio(false),cin.tie(0)
     33 
     34 const double eps = 1e-8;
     35 const int mod = 1000000007;
     36 const int maxn = 5e4 + 7;
     37 const double pi = acos(-1);
     38 const int inf = 0x3f3f3f3f;
     39 const LL INF = 0x3f3f3f3f3f3f3f3fLL;
     40 
     41 char op[5];
     42 int n, q, x, y, tot1, tot2, cnt;
     43 int a[maxn], q1[maxn], q2[maxn], root1[maxn], root2[maxn];
     44 vector<int> v;
     45 
     46 struct que {
     47     int op, l, r, x;
     48 }ask[maxn];
     49 
     50 struct node {
     51     int l, r, sum;
     52 }tree[maxn*40];
     53 
     54 int getid(int x) {
     55     return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
     56 }
     57 
     58 void update(int l, int r, int& x, int y, int pos, int val) {
     59     tree[++cnt] = tree[y], tree[cnt].sum += val, x = cnt;
     60     if(l == r) return;
     61     int mid = (l + r) >> 1;
     62     if(pos <= mid) update(l, mid, tree[x].l, tree[y].l, pos, val);
     63     else update(mid + 1, r, tree[x].r, tree[y].r, pos, val);
     64 }
     65 
     66 int query(int l, int r, int x, int y, int k) {
     67     if(l == r) return l;
     68     int cnt = tree[tree[y].l].sum - tree[tree[x].l].sum;
     69     for(int i = 0; i < tot1; ++i) {
     70         cnt -= tree[tree[q1[i]].l].sum;
     71     }
     72     for(int i = 0; i < tot2; ++i) {
     73         cnt += tree[tree[q2[i]].l].sum;
     74     }
     75     int mid = (l + r) >> 1;
     76     if(cnt >= k) {
     77         for(int i = 0; i < tot1; ++i) {
     78             q1[i] = tree[q1[i]].l;
     79         }
     80         for(int i = 0; i < tot2; ++i) {
     81             q2[i] = tree[q2[i]].l;
     82         }
     83         return query(l, mid, tree[x].l, tree[y].l, k);
     84     } else {
     85         for(int i = 0; i < tot1; ++i) {
     86             q1[i] = tree[q1[i]].r;
     87         }
     88         for(int i = 0; i < tot2; ++i) {
     89             q2[i] = tree[q2[i]].r;
     90         }
     91         return query(mid + 1, r, tree[x].r, tree[y].r, k - cnt);
     92     }
     93 }
     94 
     95 int main(){
     96 #ifndef ONLINE_JUDGE
     97     FIN;
     98 #endif
     99     int t;
    100     scanf("%d", &t);
    101     while(t--) {
    102         scanf("%d%d", &n, &q);
    103         cnt = tot1 = tot2 = 0;
    104         v.clear();
    105         memset(root2, 0, sizeof(root2));
    106         for(int i = 1; i <= n; ++i) {
    107             scanf("%d", &a[i]);
    108             v.push_back(a[i]);
    109         }
    110         for(int i = 1; i <= q; ++i) {
    111             scanf("%s", op);
    112             if(op[0] == 'C') ask[i].op = 1;
    113             else ask[i].op = 2;
    114             if(ask[i].op == 1) {
    115                 scanf("%d%d", &ask[i].l, &ask[i].x);
    116                 v.push_back(ask[i].x);
    117             } else {
    118                 scanf("%d%d%d", &ask[i].l, &ask[i].r, &ask[i].x);
    119             }
    120         }
    121         sort(v.begin(), v.end());
    122         v.erase(unique(v.begin(), v.end()), v.end());
    123         int sz = v.size();
    124         for(int i = 1; i <= n; ++i) {
    125             a[i] = getid(a[i]);
    126             update(1, sz, root1[i], root1[i-1], a[i], 1);
    127         }
    128         for(int i = 1; i <= q; ++i) {
    129             int op = ask[i].op;
    130             if(op == 1) {
    131                 int pos = ask[i].l, x = ask[i].x, y = a[pos];
    132                 x = getid(x);
    133                 a[pos] = x;
    134                 while(pos <= n) {
    135                     update(1, sz, root2[pos], root2[pos], y, -1);
    136                     update(1, sz, root2[pos], root2[pos], x, 1);
    137                     pos += lowbit(pos);
    138                 }
    139             } else {
    140                 int l = ask[i].l, r = ask[i].r, k = ask[i].x;
    141                 int x = l - 1;
    142                 tot1 = tot2 = 0;
    143                 while(x) {
    144                     q1[tot1++] = root2[x];
    145                     x -= lowbit(x);
    146                 }
    147                 x = r;
    148                 while(x) {
    149                     q2[tot2++] = root2[x];
    150                     x -= lowbit(x);
    151                 }
    152                 printf("%d
    ", v[query(1, sz, root1[l-1], root1[r], k)-1]);
    153             }
    154         }
    155     }
    156     return 0;
    157 }
  • 相关阅读:
    Codeforces-754D Fedor and coupons
    LightOJ
    LightOJ
    LightOJ
    LightOJ
    POJ
    HDU
    HDU
    HDU-2159
    方法的重写
  • 原文地址:https://www.cnblogs.com/Dillonh/p/10727234.html
Copyright © 2020-2023  润新知