BZOJ 1935 Tree 园丁的烦恼 (树状数组)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ 1935 Tree 园丁的烦恼 (树状数组)相关的知识,希望对你有一定的参考价值。

题意:中文题。

析:按x排序,然后用树状数组维护 y 即可。

代码如下:

#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<LL, 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 = 500000 + 10;
const int maxm = 1e7 + 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;
}
struct Query{
  int id, x, y, ty, w;
  Query() { }
  Query(int i, int xx, int yy, int t, int ww) :
  id(i), x(xx), y(yy), ty(t), w(ww) { }
  bool operator < (const Query &q) const{
    return x < q.x || x == q.x && ty < q.ty;
  }
};
Query q[maxn*5], tmp[maxn*5];

int ans[maxn];
int sum[maxm];

inline int lowbit(int x){ return -x&x; }

void add(int x, int c){
  while(x < maxm){
    sum[x] += c;
    x += lowbit(x);
  }
}

int query(int x){
  int ans = 0;
  while(x){
    ans += sum[x];
    x -= lowbit(x);
  }
  return ans;
}

void dfs(int l, int r){
  if(l == r)  return ;
  int m = l + r >> 1;
  dfs(l, m);  dfs(m+1, r);
  int i = l, j = m+1, k = l;
  while(i <= m && j <= r){
    if(q[i] < q[j]){
      tmp[k++] = q[i];
      if(!q[i].ty)  add(q[i].y, 1);
      ++i;
    }
    else{
      tmp[k++] = q[j];
      if(q[j].ty)  ans[q[j].id] += query(q[j].y) * q[j].w;
      ++j;
    }
  }
  while(i <= m){
    tmp[k++] = q[i];
    if(!q[i].ty)  add(q[i].y, 1);
    ++i;
  }
  while(j <= r){
    tmp[k++] = q[j];
    if(q[j].ty)  ans[q[j].id] += query(q[j].y) * q[j].w;
    ++j;
  }
  for(int i = l; i <= r; ++i){
    if(!q[i].ty && i <= m) add(q[i].y, -1);
    q[i] = tmp[i];
  }
}

int main(){
  scanf("%d %d", &n, &m);
  int cnt = 0;
  for(int i = 0; i < n; ++i){
    int x, y;  scanf("%d %d", &x, &y);
    q[cnt++] = Query(-1, x+1, y+1, 0, 0);
  }
  for(int i = 0; i < m; ++i){
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    q[cnt++] = Query(i, x1, y1, 1, 1);
    q[cnt++] = Query(i, x2+1, y2+1, 1, 1);

    q[cnt++] = Query(i, x1, y2+1, 1, -1);
    q[cnt++] = Query(i, x2+1, y1, 1, -1);
  }
  dfs(0, cnt-1);
  for(int i = 0; i < m; ++i)  printf("%d\n", ans[i]);
  return 0;
}

  

以上是关于BZOJ 1935 Tree 园丁的烦恼 (树状数组)的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 1935: [Shoi2007]Tree 园丁的烦恼 [树状数组 离线 离散化]

BZOJ1935/4822[Shoi2007]Tree 园丁的烦恼/[Cqoi2017]老C的任务 树状数组

bzoj千题计划143:bzoj1935: [Shoi2007]Tree 园丁的烦恼

bzoj1935: [Shoi2007]Tree 园丁的烦恼

BZOJ.1935.[SHOI2007]Tree园丁的烦恼(CDQ分治 三维偏序)

bzoj1935 [SHOI2007]Tree 园丁的烦恼