• BZOJ 3110 K大数查询 (树套树)


    3110: [Zjoi2013]K大数查询

    Time Limit: 20 Sec  Memory Limit: 512 MB
    Submit: 9489  Solved: 2805
    [Submit][Status][Discuss]

    Description

    有N个位置,M个操作。操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c
    如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少。

    Input

    第一行N,M
    接下来M行,每行形如1 a b c或2 a b c

    Output

    输出每个询问的结果

    Sample Input

    2 5
    1 1 2 1
    1 1 2 2
    2 1 1 2
    2 1 1 1
    2 1 2 3

    Sample Output

    1
    2
    1

    HINT



    【样例说明】

    第一个操作 后位置 1 的数只有 1 , 位置 2 的数也只有 1 。 第二个操作 后位置 1

    的数有 1 、 2 ,位置 2 的数也有 1 、 2 。 第三次询问 位置 1 到位置 1 第 2 大的数 是

    1 。 第四次询问 位置 1 到位置 1 第 1 大的数是 2 。 第五次询问 位置 1 到位置 2 第 3

    大的数是 1 。‍


    N,M<=50000,N,M<=50000

    a<=b<=N

    1操作中abs(c)<=N

    2操作中c<=Maxlongint


    Source

    析:第一维用权值线段树,也就是来维护每个值,由于是有负数,所以,要多一开倍,询问的是第 k 大,可以把每个值c,都变成 n - c + 1,这样查询的时候就是第 k 小了,然后第二维用区间线段树,维护一个区间,用快速给一些区间加1操作,这样,就可以直接来查询某个区间的第 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>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #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 fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #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 = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-3;
    const int maxn = 1e5 + 10;
    const int maxm = (100000 << 8) + 10;
    const int mod = 1000000007;
    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 root[maxn<<2], tot, lc[maxm], rc[maxm];
    unsigned int sum[maxm], addv[maxm];
    
    void push_up(int rt){
      sum[rt] = sum[lc[rt]] + sum[rc[rt]];
    }
    
    void push_down(int rt, int len){
      if(!lc[rt])  lc[rt] = ++tot;  // note
      if(!rc[rt])  rc[rt] = ++tot;  // note
      sum[lc[rt]] += (LL)(len - (len>>1)) * addv[rt];
      sum[rc[rt]] += (LL)(len >> 1) * addv[rt];
      addv[lc[rt]] += addv[rt];
      addv[rc[rt]] += addv[rt];
      addv[rt] = 0;
    }
    
    void update(int L, int R, int l, int r, int &rt){
      if(!rt)  rt = ++tot;
      if(L <= l && r <= R){
        sum[rt] += r - l + 1;
        ++addv[rt];
        return ;
      }
      if(addv[rt])  pd(rt, r - l + 1);
      int m = l + r >> 1;
      if(L <= m)  update(L, R, l, m, lc[rt]);
      if(R > m)   update(L, R, m+1, r, rc[rt]);
      pu(rt);
    }
    
    void update(int L, int R, int k){
      int l = 1, r = n<<1, rt = 1;
      while(l < r){
        int m = l + r >> 1;
        update(L, R, 1, n, root[rt]);
        if(k <= m)  rt <<= 1, r = m;
        else rt = rt<<1|1, l = m + 1;
      }
      update(L, R, 1, n, root[rt]);
    }
    
    unsigned int query(int L, int R, int l, int r, int rt){
      if(L <= l && r <= R)  return sum[rt];
      if(addv[rt])  pd(rt, r - l + 1);
      int m = l + r >> 1;
      unsigned int ans = 0;
      if(L <= m)  ans = query(L, R, l, m, lc[rt]);
      if(R > m)   ans += query(L, R, m+1, r, rc[rt]);
      return ans;
    }
    
    int query(int L, int R, int k){
      int l = 1, r = n<<1, rt = 1;
      unsigned int tmp;
      while(l < r){
        int m = l + r >> 1;
        if((tmp = query(L, R, 1, n, root[rt<<1])) >= k)  rt <<= 1, r = m;
        else rt = rt<<1|1, l = m + 1, k -= tmp;
      }
      return l;
    }
    
    int main(){
      scanf("%d %d", &n, &m);
      int op, l, r, k;
      while(m--){
        scanf("%d %d %d %d", &op, &l, &r, &k);
        if(op == 1)  update(l, r, n - k + 1);
        else printf("%d
    ", n - query(l, r, k) + 1);
      }
      return 0;
    }
    

     整体二分,跑的真是快:

    #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>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #include <numeric>
    #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 fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #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 = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-3;
    const int maxn = 1e5 + 40;
    const int maxm = (100000 << 8) + 10;
    const int mod = 1000000007;
    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 a[maxn], b[maxn], s[maxn];
    
    void add(int x, int c){
      for(int i = x; i <= (n<<1); i += i&-i)  a[i] += c, b[i] += c * (x-1);
    }
    
    LL query(int x){
      int t0 = 0;  LL t1 = 0;
      for(int i = x; i; i -= -i&i)  t0 += a[i], t1 += b[i];
      return 1LL * x * t0 - t1;
    }
    
    struct Query{
      int ty, l, r, id, k;
    };
    Query q[maxn], q1[maxn], q2[maxn];
    int ans[maxn];
    
    void dfs(int fro, int rear, int l, int r){
      if(l > r || fro > rear)  return ;
      if(l == r){
        for(int i = fro; i <= rear; ++i)
          if(q[i].id != -1)  ans[q[i].id] = n - l + 1;
        return ;
      }
      int m = l + r >> 1;
      int lx = 0, rx = 0;
      for(int i = fro; i <= rear; ++i){
        if(q[i].ty == 1){
          if(q[i].k <= m){
            add(q[i].l, 1);
            add(q[i].r+1, -1);
            q1[lx++] = q[i];
          }
          else  q2[rx++] = q[i];
        }
        else{
          LL t = query(q[i].r) - query(q[i].l-1);
          if(t >= q[i].k)  q1[lx++] = q[i];
          else q[i].k -= t, q2[rx++] = q[i];
        }
      }
      for(int i = fro; i <= rear; ++i)
        if(q[i].ty == 1 && q[i].k <= m){
          add(q[i].l, -1);  add(q[i].r+1, 1);
        }
      for(int i = fro, j = 0; j < lx; ++j, ++i)  q[i] = q1[j];
      for(int i = lx+fro, j = 0; j < rx; ++j, ++i)  q[i] = q2[j];
      dfs(fro, fro + lx - 1, l, m);
      dfs(fro + lx, rear, m+1, r);
    }
    
    int main(){
      scanf("%d %d", &n, &m);
      int idx = 0;
      for(int i = 0; i < m; ++i){
        scanf("%d %d %d %d", &q[i].ty, &q[i].l, &q[i].r, &q[i].k);
        if(q[i].ty == 1){
          q[i].k = n - q[i].k + 1;
          q[i].id = -1;
        }
        else q[i].id = idx++;
      }
      dfs(0, m-1, 0, (n<<1)+2);
      for(int i = 0; i < idx; ++i)  printf("%d
    ", ans[i]);
      return 0;
    }
    

      

  • 相关阅读:
    DB2基本语句操作
    makefile简析与例子
    io多路复用epoll归纳
    Selenium 3----设置元素等待
    Selenium 3----获取断言信息
    Selenium 3----鼠标、键盘操作常用方法
    Selenium 3----WebDriver常用方法
    算法时间复杂度求解
    Selenium 3----控制浏览器操作
    操作系统基础知识学习
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7763464.html
Copyright © 2020-2023  润新知