跳到正文

题解

P1856 题解

__LYC__qwq约 6 分钟
原文
文章信息与版本

文章信息

发布
2024年7月17日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 6 分钟
题目
P1856

版本

  1. v1当前

可以做两边扫描线, 对于一条加边, 给扫描线上的区间 + 1, 对于一条删边, 扫描线上的区间 - 1, 题目统计的东西就是非 0 元素组成的快的数量, 若你一开始在左右两边放了 0 , 就等价于 0 的联通快数量 - 1 (每两个 0 的联通快中都夹着一个非 0 联通快)

时间复杂度 O(nlog⁡n)O(n \log n)

C++
#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e6 + 5;

int n, tot;
long long ans;

struct add_{
  int c[N], sum;
  int lowbit(int x){
    return x & -x;
  }
  void updata(int x, int k){
    for(; x <= 20006; c[x] += k, x += lowbit(x)){
    }
  }
  int getsum(int x){
    for(sum = 0; x; sum += c[x], x -= lowbit(x)){
    }
    return sum;
  }
  void clear(int x){
    for(int i = 1; i <= x; ++i){
      c[i] = 0;
    }
  }
}ad_d;

struct Tr{
  int minx, mcnt, lazy, l, r;
}t[N * 4];

struct Node{
  int x, y, x2, y2;
}a[N];

struct NODE{
  int x, y, y2, w;
}b[N];

void renew(int p){
  t[p].minx = min(t[p * 2].minx, t[p * 2 + 1].minx);
  int op1 = ad_d.getsum(t[p * 2].r), op2 = ad_d.getsum(t[p * 2 + 1].l);
  t[p].mcnt = (t[p * 2].minx == t[p].minx ? t[p * 2].mcnt : 0) + (t[p * 2 + 1].minx == t[p].minx ? t[p * 2 + 1].mcnt : 0) - (op1 == t[p].minx && op1 == op2);
}

void tog(int p, int L){
  t[p].lazy += L;
  t[p].minx += L;
}

void Lazy(int p){
  tog(p * 2, t[p].lazy);
  tog(p * 2 + 1, t[p].lazy);
  t[p].lazy = 0;
}

void build(int p, int l, int r){
  t[p].l = l, t[p].r = r, t[p].minx = 0, t[p].mcnt = 1, t[p].lazy = 0;
  if(l == r){
    return;
  }
  int mid = (l + r) >> 1;
  build(p * 2, l, mid), build(p * 2 + 1, mid + 1, r);
  renew(p);
}

void updata(int p, int l, int r, int L){
  if(t[p].l >= l && t[p].r <= r){
    tog(p, L);
    return;
  }
  Lazy(p);
  int mid = (t[p].l + t[p].r) >> 1;
  if(l <= mid){
    updata(p * 2, l, r, L);
  }
  if(r > mid){
    updata(p * 2 + 1, l, r, L);
  }
  renew(p);
}

int getsum(int p, int l, int r){
  if(t[p].l >= l && t[p].r <= r){
    return t[p].mcnt;
  }
  int op1 = -1, op2 = -1;
  Lazy(p);
  int mid = (t[p].l + t[p].r) >> 1, sum = 0;
  if(l <= mid){
    if(t[p * 2].minx == t[p].minx){
      sum += getsum(p * 2, l, r);
      op1 =  ad_d.getsum(t[p * 2].r);
    }
  }
  if(r > mid){
    if(t[p * 2 + 1].minx == t[p].minx){
      sum += getsum(p * 2 + 1, l, r);
      op2 = ad_d.getsum(t[p * 2 + 1].l);
    }
  }
  sum -= (op1 == t[p].minx && op1 == op2);
  return sum;
}

void Wolk(){
  ad_d.clear(20006);
  sort(b + 1, b + tot + 1, [](const NODE &i, const NODE &j){  return i.x < j.x || i.x == j.x && i.w < j.w;});
  build(1, 1, 20006);
  for(int i = 1; i <= tot; ++i){
    ans += 2ll * (getsum(1, 1, 20006) - 1) * (b[i].x - b[i - 1].x);
    ad_d.updata(b[i].y, b[i].w), ad_d.updata(b[i].y2 + 1, -b[i].w);
    updata(1, b[i].y, b[i].y2, b[i].w);
  }
}

signed main(){
  cin >> n;
  tot = 0;
  for(int i = 1; i <= n; ++i){
    cin >> a[i].x >> a[i].y >> a[i].x2 >> a[i].y2;
    a[i].x += 10003, a[i].y += 10003, a[i].x2 += 10003, a[i].y2 += 10003;
    b[++tot] = {a[i].x, a[i].y, a[i].y2 - 1, 1}, b[++tot] = {a[i].x2, a[i].y, a[i].y2 - 1, -1};
  }
  Wolk();
  tot = 0;
  for(int i = 1; i <= n; ++i){
    b[++tot] = {a[i].y, a[i].x, a[i].x2 - 1, 1}, b[++tot] = {a[i].y2, a[i].x, a[i].x2 - 1, -1};
  }
  Wolk();
  cout << ans;
  return 0;
}
原文