// 可持久化数组 struct Node { int v; Node *ls, *rs; }pool[N*32], *tail=pool, *root[N]; Node *build(int lf, int rg) { Node *nd = ++tail; if(lf == rg) { nd->v = aa[lf]; } else { int mid = (lf + rg) >> 1; nd->ls = build(lf, mid); nd->rs = build(mid+1, rg); } return nd; } int query(Node *nd, int lf, int rg, int pos) { if(lf == rg) { return nd->v; } int mid = (lf + rg) >> 1; if(pos <= mid) return query(nd->ls, lf, mid, pos); if(pos > mid) return query(nd->rs, mid+1, rg, pos); } Node * modify(Node *nd, int lf, int rg, int pos, int val) { Node *nnd = ++tail; if(lf == rg) { nnd->v = val; return nnd; } int mid = (lf + rg) >> 1; if(pos <= mid) { nnd->ls = modify(nd->ls, lf, mid, pos, val); nnd->rs = nd->rs; } else { nnd->ls = nd->ls; nnd->rs = modify(nd->rs, mid+1, rg, pos, val); } return nnd; } Node * modify(Node *nd, int pos, int val) { return modify(nd, 1, n, pos, val); } Node * print(Node *nd, int pos) { printf("%d\n", query(nd, 1, n, pos)); return nd; } Node * back(int t) { return root[t]; } int main() { root[0] = build(1, n); for(int i = 1; i <= q; i++) { root[i] = modify(root[i-1], pos, val); // modify root[i] = print(root[i-1], pos); // print root[i] = back(t); // back } } // 动态开节点 struct Node { int sum; Node *ls, *rs; void update() { sum = ls->sum + rs->sum; } }pool[N*32], *tail=pool, *zero, *root; Node *newnode(int val) { Node *nd = ++tail; nd->ls = nd->rs = zero; nd->sum = val; return nd; } void build() { zero = ++tail; zero->ls = zero->rs = zero; zero->sum = 0; root = newnode(0); } int query(Node *nd, int lf, int rg, int L, int R) { if(nd == zero) return 0; if(L<= lf && rg <= R) return nd->sum; int mid = (lf + rg) >> 1; int rt = 0; if(L <= mid) rt += query(nd->ls, lf, mid, L, R); if(R > mid) rt += query(nd->rs, mid+1, rg, L, R); return rt; } void modify(Node *nd, int lf, int rg, int pos, int val) { if(lf == rg) { nd->sum = val; return; } int mid = (lf + rg) >> 1; if(pos <= mid) { if(nd->ls == zero) { nd->ls = newnode(0); } modify(nd->ls, lf, mid, pos, val); } else { if(nd->rs == zero) { nd->rs = newnode(0); } modify(nd->rs, mid+1, rg, pos, val); } nd->update(); } // 区间第k大 struct Node { int cnt; Node *ls, *rs; void update() { cnt = ls->cnt + rs->cnt; } }pool[N*32], *tail=pool, *zero, *root[N]; int n; int aa[N]; Node *newnode() { Node *nd = ++tail; nd->ls = nd->rs = zero; nd->cnt = 0; return nd; } void build() { zero = ++tail; zero->ls = zero->rs = zero; zero->cnt = 0; root[0] = newnode(); } int query(Node *nd, int lf, int rg, int L, int R) { if(nd == zero) return 0; if(L <= lf && rg <= R) { return nd->cnt; } int mid = (lf + rg) >> 1; int rt = 0; if(L <= mid) rt += query(nd->ls, lf, mid, L, R); if(R > mid) rt += query(nd->rs, mid+1, rg, L, R); return rt; } Node * modify(Node *nd, int lf, int rg, int pos, int delta) { Node *nnd = ++tail; if(lf == rg) { nnd->cnt = nd->cnt + delta; return nnd; } int mid = (lf + rg) >> 1; if(pos <= mid) { nnd->ls = modify(nd->ls, lf, mid, pos, delta); nnd->rs = nd->rs; } else { nnd->ls = nd->ls; nnd->rs = modify(nd->rs, mid+1, rg, pos, delta); } nnd->update(); return nnd; } int query(Node *lnd, Node *rnd, int lf, int rg, int k) { if(lf == rg) return lf; int mid = (lf + rg) >> 1; int lz = rnd->ls->cnt - lnd->ls->cnt; if(k <= lz) return query(lnd->ls, rnd->ls, lf, mid, k); else return query(lnd->rs, rnd->rs, mid+1, rg, k - lz); } int query(int lf, int rg, int k) { return query(root[lf-1], root[rg], 1, n, k); } int main() { build(); for(int i = 1; i <= n; i++) { root[i] = modify(root[i-1], 1, n, aa[i], +1); } printf("%d\n", query(1,5,3)); } // 离散化
#include<bits/stdc++.h> using namespace std; const int M = 10005; int n, q; double yy[M*2]; struct Event{ double x, y1, y2; int d; }; vector <Event> e; struct Node { double sum; int cnt; Node *ls , *rs; void up(int l, int r){ if(cnt) sum = yy[r] - yy[l-1]; else if(l != r) sum = ls->sum + rs->sum; else sum = 0; } }pool[M << 4], *root, *tail = pool; Node *build(int l = 1, int r = q){ Node * nd = ++tail; if(l == r)nd->sum = 0, nd->cnt = 0; else { int mid = (l + r) >> 1; nd->ls = build(l, mid); nd->rs = build(mid + 1, r); nd->up(l ,r); } } #define Ls l, mid, nd -> ls #define Rs mid+1, r, nd -> rs void modify(int L, int R, int d, int l = 1, int r = q, Node * nd = root){ if(L <= l && R >= r)nd->cnt += d, nd->up(l , r); else { int mid = (l + r) >> 1; if(L <= mid)modify(L, R, d, Ls); if(R > mid)modify(L, R, d, Rs); nd -> up(l, r); } } bool cmp(Event a, Event b){ return a.x < b.x; } int main(){ int k = 0; while(scanf("%d", &n) == 1){ if(!n)break; k++; int cnt = 0; double ans = 0; e.clear(); memset(yy, 0, sizeof(yy)); for(int i = 1; i <= n; i++){ double x1, x2, y1, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); e.push_back((Event){x1, y1, y2, 1}); e.push_back((Event){x2, y1, y2, -1}); yy[++cnt] = y1; yy[++cnt] = y2; } sort(yy+1, yy+1+cnt); sort(e.begin(), e.end(), cmp); q = unique(yy+1, yy+1+cnt) - yy - 1; root = build(); for(int i = 0; i < 2*n; i++){ double dx = i == 0 ? 0 : e[i].x - e[i - 1].x; ans += root->sum * dx; int p1 = find(yy+1, yy+1+q, e[i].y1) - yy; int p2 = find(yy+1, yy+1+q, e[i].y2) - yy; modify(p1+1, p2, e[i].d); //cout<<ans<<endl; } printf("Test case #%d\nTotal explored area: %.2lf\n\n", k, ans); } }