CF793G Oleg and chess [线段树优化建边,扫描线,最大流]

不相交就直接搞啊。。没啥技巧,i->j如果选了就是(i,j)选了。

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>

#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)

using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;

#define pii pair<int, int>
#define fir first
#define sec second

template <class T>

void cmax(T& x, const T& y) {
  if (x < y) x = y;
}

template <class T>

void cmin(T& x, const T& y) {
  if (x > y) x = y;
}

#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back

template <class T>

void sort(vector<T>& v) {
  sort(all(v));
}

template <class T>

void reverse(vector<T>& v) {
  reverse(all(v));
}

template <class T>

void unique(vector<T>& v) {
  sort(all(v)), v.erase(unique(all(v)), v.end());
}

void reverse(string& s) { reverse(s.begin(), s.end()); }

const int io_size = 1 << 23 | 233;
const int io_limit = 1 << 22;
struct io_in {
  char ch;
#ifndef __WIN64
  char getchar() {
    static char buf[io_size], *p1 = buf, *p2 = buf;

    return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, io_size, stdin), p1 == p2) ? EOF : *p1++;
  }
#endif
  io_in& operator>>(char& c) {
    for (c = getchar(); isspace(c); c = getchar())
      ;

    return *this;
  }
  io_in& operator>>(string& s) {
    for (s.clear(); isspace(ch = getchar());)
      ;

    if (!~ch) return *this;

    for (s = ch; !isspace(ch = getchar()) && ~ch; s += ch)
      ;

    return *this;
  }

  io_in& operator>>(char* str) {
    char* cur = str;
    while (*cur) *cur++ = 0;

    for (cur = str; isspace(ch = getchar());)
      ;
    if (!~ch) return *this;

    for (*cur = ch; !isspace(ch = getchar()) && ~ch; *++cur = ch)
      ;

    return *++cur = 0, *this;
  }

  template <class T>

  void read(T& x) {
    bool f = 0;
    while ((ch = getchar()) < 48 && ~ch) f ^= (ch == 45);

    x = ~ch ? (ch ^ 48) : 0;
    while ((ch = getchar()) > 47) x = x * 10 + (ch ^ 48);
    x = f ? -x : x;
  }

  io_in& operator>>(int& x) { return read(x), *this; }

  io_in& operator>>(ll& x) { return read(x), *this; }

  io_in& operator>>(uint& x) { return read(x), *this; }

  io_in& operator>>(ull& x) { return read(x), *this; }

  io_in& operator>>(db& x) {
    read(x);
    bool f = x < 0;
    x = f ? -x : x;
    if (ch ^ '.') return *this;

    double d = 0.1;
    while ((ch = getchar()) > 47) x += d * (ch ^ 48), d *= .1;
    return x = f ? -x : x, *this;
  }
} in;

struct io_out {
  char buf[io_size], *s = buf;
  int pw[233], st[233];

  io_out() {
    set(7);
    rep(i, pw[0] = 1, 9) pw[i] = pw[i - 1] * 10;
  }

  ~io_out() { flush(); }

  void io_chk() {
    if (s - buf > io_limit) flush();
  }

  void flush() { fwrite(buf, 1, s - buf, stdout), fflush(stdout), s = buf; }

  io_out& operator<<(char c) { return *s++ = c, *this; }

  io_out& operator<<(string str) {
    for (char c : str) *s++ = c;
    return io_chk(), *this;
  }

  io_out& operator<<(char* str) {
    char* cur = str;
    while (*cur) *s++ = *cur++;
    return io_chk(), *this;
  }

  template <class T>

  void write(T x) {
    if (x < 0) *s++ = '-', x = -x;

    do {
      st[++st[0]] = x % 10, x /= 10;
    } while (x);

    while (st[0]) *s++ = st[st[0]--] ^ 48;
  }

  io_out& operator<<(int x) { return write(x), io_chk(), *this; }

  io_out& operator<<(ll x) { return write(x), io_chk(), *this; }

  io_out& operator<<(uint x) { return write(x), io_chk(), *this; }

  io_out& operator<<(ull x) { return write(x), io_chk(), *this; }

  int len, lft, rig;

  void set(int _length) { len = _length; }

  io_out& operator<<(db x) {
    bool f = x < 0;
    x = f ? -x : x, lft = x, rig = 1. * (x - lft) * pw[len];
    return write(f ? -lft : lft), *s++ = '.', write(rig), io_chk(), *this;
  }
} out;

int n, m, s, t;
const int maxn = 3e4 + 43;
const int inf = 1e9;

namespace Dinic {
int head[maxn << 6], cur[maxn << 6];
struct List {
  int v, nxt, w;
} e[maxn << 7];
int cnt = 1;
void add(int u, int v, int w) {
  e[++cnt] = { v, head[u], w }, head[u] = cnt;
  e[++cnt] = { u, head[v], 0 }, head[v] = cnt;
}

int d[maxn << 6];
bool bfs(int s) {
  memset(d, 0, sizeof(d));
  queue<int> q;
  d[s] = 1, q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = e[i].nxt) {
      int& v = e[i].v;
      if (!d[v] && e[i].w) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
  return d[t];
}

int dfs(int u, int flow) {
  if (u == t) return flow;
  int fl = 0;
  for (int i = cur[u]; i; i = e[i].nxt) {
    int& v = e[i].v;
    cur[u] = i;
    if (d[v] == d[u] + 1 && e[i].w) {
      int dis = dfs(v, min(flow, e[i].w));
      if (dis) {
        e[i].w -= dis;
        e[i ^ 1].w += dis;
        flow -= dis;
        fl += dis;
        if (!flow) return fl;
      }
    }
  }
  return fl;
}

int dinic() {
  int ans = 0;
  while (bfs(s)) {
    memcpy(cur, head, sizeof(head));
    ans += dfs(s, inf);
  }
  return ans;
}
}  // namespace Dinic

using Dinic ::add;

namespace SegMentTree {
int id[maxn << 2];
int tag[maxn << 2];
int cnt;

void pushup(int p) {
  id[p] = ++cnt;
  if (!tag[p << 1]) add(id[p], id[p << 1], inf);
  if (!tag[p << 1 | 1]) add(id[p], id[p << 1 | 1], inf);
}

void build(int p, int l, int r) {
  if (l == r) {
    id[p] = (n + l);
    return;
  }
  int mid = l + r >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  pushup(p);
}

void upd(int p, int a, int b, int l, int r, int v) {
  if (a <= l && r <= b) {
    tag[p] += v;
    return;
  }
  int mid = l + r >> 1;
  if (a <= mid) upd(p << 1, a, b, l, mid, v);
  if (b > mid) upd(p << 1 | 1, a, b, mid + 1, r, v);
  pushup(p);
}
}  // namespace SegMentTree

using SegMentTree ::build;
using SegMentTree ::upd;

struct chg {
  int l, r, val;
};
vector<chg> v[maxn];

signed main() {
  // code begin.
  in >> n >> m;
  s = 2 * n + 1, t = 2 * n + 2, SegMentTree ::cnt = 2 * n + 2;
  build(1, 1, n);
  while (m--) {
    int x1, y1, x2, y2;
    in >> x1 >> y1 >> x2 >> y2;
    v[x1].push_back({ y1, y2, 1 });
    v[x2 + 1].push_back({ y1, y2, -1 });
  }
  rep(i, 1, n) add(s, i, 1), add(i + n, t, 1);
  rep(i, 1, n) {
    for (auto x : v[i]) {
      int l = x.l, r = x.r, v = x.val;
      upd(1, l, r, 1, n, v);
    }
    if (!SegMentTree ::tag[1]) add(i, SegMentTree ::id[1], inf);
  }
  int ans = Dinic ::dinic();
  out << ans << '\n';
  return 0;
  // code end.
}

posted @ 2020-04-11 02:23  _Isaunoya  阅读(219)  评论(0编辑  收藏  举报