zoukankan      html  css  js  c++  java
  • 扫描线


    1 HDU 1828 Picture

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <stack>
     9 #include <map>
    10 #include <set>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <ctime>
    14 
    15 using namespace std;
    16 
    17 #define REP(i, n) for (int i = 0; i < (n); ++i)
    18 #define eps 1e-9
    19 #define lc id << 1
    20 #define rc id << 1 | 1
    21 #define lson low, mid, lc
    22 #define rson mid + 1, high, rc
    23 
    24 typedef long long ll;
    25 typedef pair<int, int> pii;
    26 struct Node {
    27     int x, y1, y2, flag;
    28     bool operator < (const Node& rhs) const {
    29         return x < rhs.x || (x == rhs.x && flag > rhs.flag);
    30     }
    31 };
    32 
    33 const int maxn = 1e4 + 10;
    34 int n, a, b, c, d;
    35 Node node[maxn];
    36 vector<int> v;
    37 
    38 int get_id(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; }
    39 int cnt[maxn * 4], line[maxn * 4], lbc[maxn * 4], rbc[maxn * 4], len[maxn * 4];
    40 
    41 void push_up(int id, int low, int high) {
    42     if (cnt[id]) {
    43         len[id] = v[high] - v[low - 1]; line[id] = lbc[id] = rbc[id] = 1; return;
    44     }
    45     len[id] = len[lc] + len[rc]; lbc[id] = lbc[lc]; rbc[id] = rbc[rc];
    46     line[id] = line[lc] + line[rc] - rbc[lc] * lbc[rc];
    47 }
    48 void update(int l, int r, int k, int low, int high, int id) {
    49     if (l == low && r == high) {
    50         cnt[id] += k;
    51         if (low == high && !cnt[id]) {
    52             len[id] = line[id] = lbc[id] = rbc[id] = 0; return;
    53         }
    54         push_up(id, low, high); return;
    55     }
    56     int mid = (low + high) / 2;
    57     if (r <= mid) { update(l, r, k, lson); }
    58     else if (l >= mid + 1) { update(l, r, k, rson); }
    59     else { update(l, mid, k, lson); update(mid + 1, r, k, rson); }
    60     push_up(id, low, high);
    61 }
    62 
    63 int main() {
    64 #ifdef __AiR_H
    65     freopen("in.txt", "r", stdin);
    66 //    freopen("out.txt", "w", stdout);
    67 #endif // __AiR_H
    68     while (scanf("%d", &n) != EOF) {
    69         v.clear();
    70         for (int i = 0; i < n; ++i) {
    71             scanf("%d %d %d %d", &a, &b, &c, &d); v.push_back(b); v.push_back(d);
    72             node[i * 2] = Node{a, b, d, 1}; node[i * 2 + 1] = Node{c, b, d, -1};
    73         }
    74         sort(v.begin(), v.end());
    75         v.erase(unique(v.begin(), v.end()), v.end());
    76         n *= 2; sort(node, node + n); int Size = v.size();
    77         int id1, id2, len_t = 0, line_t = 0, ans = 0;
    78         for (int i = 0; i < n; ++i) {
    79             id1 = get_id(node[i].y1); id2 = get_id(node[i].y2);
    80             update(id1, id2 - 1, node[i].flag, 1, Size, 1);
    81             if (i > 0) { ans += 2 * line_t * (node[i].x - node[i - 1].x); }
    82             ans += abs(len[1] - len_t); len_t = len[1]; line_t = line[1];
    83         }
    84         printf("%d
    ", ans);
    85     }
    86 #ifdef __AiR_H
    87     printf("Time used = %.2fs
    ", (double)clock() / CLOCKS_PER_SEC);
    88 #endif // __AiR_H
    89     return 0;
    90 }
    View Code

    2 HDU 5091 Beam Cannon

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <stack>
     9 #include <map>
    10 #include <set>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <ctime>
    14 
    15 using namespace std;
    16 
    17 #define REP(i, n) for (int i = 0; i < (n); ++i)
    18 #define eps 1e-9
    19 #define lc id << 1
    20 #define rc id << 1 | 1
    21 #define lson low, mid, lc
    22 #define rson mid + 1, high, rc
    23 
    24 struct Node {
    25     int x, y1, y2, flag;
    26     bool operator < (const Node& a) const {
    27         if (x == a.x) { return flag > a.flag; } return x < a.x;
    28     }
    29 };
    30 
    31 typedef long long ll;
    32 typedef pair<int, int> pii;
    33 
    34 const int INF = 0x7fffffff;
    35 const int maxn = 1e5 + 10;
    36 int n, h, w, Size, x, y;
    37 Node node[maxn];
    38 vector<int> v;
    39 int Max[maxn * 4], lazy[maxn * 4];
    40 
    41 int get_id(int a) { return lower_bound(v.begin(), v.end(), a) - v.begin() + 1; }
    42 void build(int low, int high, int id) {
    43     Max[id] = lazy[id] = 0; if (low == high) { return; }
    44     int mid = (low + high) / 2; build(lson); build(rson);
    45 }
    46 inline void push_up(int id) { Max[id] = max(Max[lc], Max[rc]); }
    47 inline void push_down(int id) {
    48     if (!lazy[id]) { return; }
    49     lazy[lc] += lazy[id]; Max[lc] += lazy[id];
    50     lazy[rc] += lazy[id]; Max[rc] += lazy[id];
    51     lazy[id] = 0;
    52 }
    53 void update(int l, int r, int key, int low, int high, int id) {
    54     if (l == low && r == high) {
    55         lazy[id] += key; Max[id] += key; return;
    56     }
    57     int mid = (low + high) / 2; push_down(id);
    58     if (r <= mid) { update(l, r, key, lson); }
    59     else if (l >= mid + 1) { update(l, r, key, rson); }
    60     else { update(l, mid, key, lson); update(mid + 1, r, key, rson); }
    61     push_up(id);
    62 }
    63 
    64 int main() {
    65 #ifdef __AiR_H
    66     freopen("in.txt", "r", stdin);
    67 //    freopen("out.txt", "w", stdout);
    68 #endif // __AiR_H
    69     while (scanf("%d", &n) && n >= 0) {
    70 //        printf("n = %d
    ", n);
    71         scanf("%d %d", &w, &h); v.clear();
    72         for (int i = 0; i < n; ++i) {
    73             scanf("%d %d", &x, &y);
    74             node[i * 2] = Node{x, y - h, y, 1};
    75             node[i * 2 + 1] = Node{x + w, y - h, y, -1};
    76             v.push_back(y - h); v.push_back(y);
    77         }
    78         sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
    79         n *= 2; sort(node, node + n); Size = v.size(); build(1, Size, 1);
    80         int id1, id2, ans = 0;
    81         for (int i = 0; i < n; ++i) {
    82             id1 = get_id(node[i].y1); id2 = get_id(node[i].y2);
    83             update(id1, id2, node[i].flag, 1, Size, 1);
    84             ans = max(ans, Max[1]);
    85         }
    86         printf("%d
    ", ans);
    87     }
    88 #ifdef __AiR_H
    89     printf("Time used = %.2fs
    ", (double)clock() / CLOCKS_PER_SEC);
    90 #endif // __AiR_H
    91     return 0;
    92 }
    View Code

    3 HDU 5722 Jewelry

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <stack>
     9 #include <map>
    10 #include <set>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <ctime>
    14 
    15 using namespace std;
    16 
    17 #define REP(i, n) for (int i = 0; i < (n); ++i)
    18 #define eps 1e-9
    19 #define lc id << 1
    20 #define rc id << 1 | 1
    21 #define lson low, mid, lc
    22 #define rson mid + 1, high, rc
    23 #define x first
    24 #define y second
    25 
    26 typedef long long ll;
    27 typedef pair<int, int> pii;
    28 const int maxn = 2e5 + 10;
    29 struct Node {
    30     int x, y1, y2, flag;
    31     bool operator < (const Node& rhs) const {
    32         return x < rhs.x || (x == rhs.x && flag > rhs.flag);
    33     }
    34 };
    35 int T, n, x;
    36 vector<pii> v;
    37 int cnt[maxn * 4], len[maxn * 4];
    38 Node node[maxn];
    39 
    40 void build(int low, int high, int id) {
    41     cnt[id] = len[id] = 0; if (low == high) { return; }
    42     int mid = (low + high) / 2; build(lson); build(rson);
    43 }
    44 void push_up(int id, int low, int high) {
    45     if (cnt[id]) { len[id] = high - low + 1; return; }
    46     len[id] = len[lc] + len[rc];
    47 }
    48 void update(int l, int r, int k, int low, int high, int id) {
    49     if (l == low && r == high) {
    50         cnt[id] += k;
    51         if (low == high && cnt[id] == 0) { len[id] = 0; return; }
    52         push_up(id, low, high); return;
    53     }
    54     int mid = (low + high) / 2;
    55     if (r <= mid) { update(l, r, k, lson); }
    56     else if (l >= mid + 1) { update(l, r, k, rson); }
    57     else { update(l, mid, k, lson); update(mid + 1, r, k, rson); }
    58     push_up(id, low, high);
    59 }
    60 
    61 int main() {
    62 #ifdef __AiR_H
    63     freopen("in.txt", "r", stdin);
    64 //    freopen("out.txt", "w", stdout);
    65 #endif // __AiR_H
    66     scanf("%d", &T);
    67     while (T--) {
    68         scanf("%d %d", &n, &x); v.clear(); int N = 0, a, l1, r1, l2, r2;
    69         for (int i = 0; i < n; ++i) {
    70             scanf("%d", &a); v.push_back(make_pair(a, i));
    71         }
    72         sort(v.begin(), v.end());
    73         for (int i = 0; i < n; ++i) {
    74             if (i - x + 1 < 0 || v[i].x != v[i - x + 1].x) { continue; }
    75             l1 = 0; r1 = v[i - x + 1].y; l2 = v[i].y; r2 = n - 1;
    76             if (i - x >= 0 && v[i - x].x == v[i].x) { l1 = v[i - x].y + 1; }
    77             if (i + 1 < n && v[i + 1].x == v[i].x) { r2 = v[i + 1].y - 1; }
    78             ++l1; ++r1; ++l2; ++r2;
    79             node[N++] = Node{l1, l2, r2, 1}; node[N++] = Node{r1, l2, r2, -1};
    80         }
    81         sort(node, node + N); build(1, n, 1); ll ans = 0;
    82         for (int i = 0, j; i < N; i = j) {
    83             if (i > 0) { ans += 1ll * (node[i].x - node[i - 1].x - 1) * len[1]; }
    84             j = i;
    85             while (j < N && node[j].x == node[i].x && node[j].flag == 1) {
    86                 update(node[j].y1, node[j].y2, 1, 1, n, 1); ++j;
    87             }
    88             ans += len[1];
    89             while (j < N && node[j].x == node[i].x && node[j].flag == -1) {
    90                 update(node[j].y1, node[j].y2, -1, 1, n, 1); ++j;
    91             }
    92         }
    93         printf("%lld
    ", ans);
    94     }
    95     return 0;
    96 }
    View Code

     1 POJ 1151 Atlantis

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <stack>
     9 #include <map>
    10 #include <set>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <ctime>
    14 
    15 using namespace std;
    16 
    17 #define REP(i, n) for (int i = 0; i < (n); ++i)
    18 #define eps 1e-9
    19 #define lc id << 1
    20 #define rc id << 1 | 1
    21 #define lson low, mid, lc
    22 #define rson mid + 1, high, rc
    23 
    24 typedef long long ll;
    25 typedef pair<int, int> pii;
    26 
    27 struct Node {
    28     double x, y1, y2; int flag;
    29     bool operator < (const Node& rhs) const {
    30         if (x == rhs.x) { return flag > rhs.flag; } return x < rhs.x;
    31     }
    32 };
    33 const int INF = 0x7fffffff;
    34 const int maxn = 200;
    35 vector<double> v;
    36 Node node[maxn];
    37 double len[maxn * 4];
    38 int cnt[maxn * 4];
    39 int n, Size, Case = 0;
    40 double a, b, c, d;
    41 
    42 int get_id(double x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; }
    43 void build(int low, int high, int id) {
    44     cnt[id] = 0; len[id] = 0.0; if (low == high) { return; }
    45     int mid = (low + high) / 2; build(lson); build(rson);
    46 }
    47 inline void push_up(int id, int low, int high) {
    48     if (cnt[id]) { len[id] = v[high] - v[low - 1]; return; }
    49     len[id] = len[lc] + len[rc];
    50 }
    51 void update(int l, int r, int key, int low, int high, int id) {
    52     if (l == low && r == high) {
    53         cnt[id] += key;
    54         if (low == high && !cnt[id]) { len[id] = 0.0; return; }
    55         push_up(id, low, high); return;
    56     }
    57     int mid = (low + high) / 2;
    58     if (r <= mid) { update(l, r, key, lson); }
    59     else if (l >= mid + 1) { update(l, r, key, rson); }
    60     else { update(l, mid, key, lson); update(mid + 1, r, key, rson); }
    61     push_up(id, low, high);
    62 }
    63 
    64 int main() {
    65 #ifdef __AiR_H
    66     freopen("in.txt", "r", stdin);
    67 //    freopen("out.txt", "w", stdout);
    68 #endif // __AiR_H
    69     while (scanf("%d", &n) && n) {
    70         v.clear();
    71         for (int i = 0; i < n; ++i) {
    72             scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
    73             node[i * 2] = Node{a, b, d, 1};
    74             node[i * 2 + 1] = Node{c, b, d, -1};
    75             v.push_back(b); v.push_back(d);
    76         }
    77         sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
    78         n *= 2; sort(node, node + n); Size = v.size(); build(1, Size, 1);
    79         int id1, id2; double ans = 0.0, len_t;
    80         for (int i = 0; i < n; ++i) {
    81             id1 = get_id(node[i].y1); id2 = get_id(node[i].y2);
    82             update(id1, id2 - 1, node[i].flag, 1, Size, 1);
    83             if (i > 0) { ans += len_t * (node[i].x - node[i - 1].x); }
    84             len_t = len[1];
    85         }
    86         printf("Test case #%d
    Total explored area: %.2f
    
    ", ++Case, ans);
    87     }
    88 #ifdef __AiR_H
    89     printf("Time used = %.2fs
    ", (double)clock() / CLOCKS_PER_SEC);
    90 #endif // __AiR_H
    91     return 0;
    92 }
    View Code

    2 POJ 1177 Picture

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <stack>
     9 #include <map>
    10 #include <set>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <ctime>
    14 
    15 using namespace std;
    16 
    17 #define REP(i, n) for (int i = 0; i < (n); ++i)
    18 #define eps 1e-9
    19 #define lc id << 1
    20 #define rc id << 1 | 1
    21 #define lson low, mid, lc
    22 #define rson mid + 1, high, rc
    23 
    24 typedef long long ll;
    25 typedef pair<int, int> pii;
    26 struct Node {
    27     int x, y1, y2, flag;
    28     bool operator < (const Node& rhs) const {
    29         return x < rhs.x || (x == rhs.x && flag > rhs.flag);
    30     }
    31 };
    32 
    33 const int maxn = 1e4 + 10;
    34 int n, a, b, c, d;
    35 Node node[maxn];
    36 vector<int> v;
    37 
    38 int get_id(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; }
    39 int cnt[maxn * 4], line[maxn * 4], lbc[maxn * 4], rbc[maxn * 4], len[maxn * 4];
    40 
    41 void push_up(int id, int low, int high) {
    42     if (cnt[id]) {
    43         len[id] = v[high] - v[low - 1]; line[id] = lbc[id] = rbc[id] = 1; return;
    44     }
    45     len[id] = len[lc] + len[rc]; lbc[id] = lbc[lc]; rbc[id] = rbc[rc];
    46     line[id] = line[lc] + line[rc] - rbc[lc] * lbc[rc];
    47 }
    48 void update(int l, int r, int k, int low, int high, int id) {
    49     if (l == low && r == high) {
    50         cnt[id] += k;
    51         if (low == high && !cnt[id]) {
    52             len[id] = line[id] = lbc[id] = rbc[id] = 0; return;
    53         }
    54         push_up(id, low, high); return;
    55     }
    56     int mid = (low + high) / 2;
    57     if (r <= mid) { update(l, r, k, lson); }
    58     else if (l >= mid + 1) { update(l, r, k, rson); }
    59     else { update(l, mid, k, lson); update(mid + 1, r, k, rson); }
    60     push_up(id, low, high);
    61 }
    62 
    63 int main() {
    64 #ifdef __AiR_H
    65     freopen("in.txt", "r", stdin);
    66 //    freopen("out.txt", "w", stdout);
    67 #endif // __AiR_H
    68     while (scanf("%d", &n) != EOF) {
    69         v.clear();
    70         for (int i = 0; i < n; ++i) {
    71             scanf("%d %d %d %d", &a, &b, &c, &d); v.push_back(b); v.push_back(d);
    72             node[i * 2] = Node{a, b, d, 1}; node[i * 2 + 1] = Node{c, b, d, -1};
    73         }
    74         sort(v.begin(), v.end());
    75         v.erase(unique(v.begin(), v.end()), v.end());
    76         n *= 2; sort(node, node + n); int Size = v.size();
    77         int id1, id2, len_t = 0, line_t = 0, ans = 0;
    78         for (int i = 0; i < n; ++i) {
    79             id1 = get_id(node[i].y1); id2 = get_id(node[i].y2);
    80             update(id1, id2 - 1, node[i].flag, 1, Size, 1);
    81             if (i > 0) { ans += 2 * line_t * (node[i].x - node[i - 1].x); }
    82             ans += abs(len[1] - len_t); len_t = len[1]; line_t = line[1];
    83         }
    84         printf("%d
    ", ans);
    85     }
    86 #ifdef __AiR_H
    87     printf("Time used = %.2fs
    ", (double)clock() / CLOCKS_PER_SEC);
    88 #endif // __AiR_H
    89     return 0;
    90 }
    View Code

    1 Codeforces 377D Developing Game

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <stack>
     9 #include <map>
    10 #include <set>
    11 #include <cmath>
    12 #include <cctype>
    13 #include <ctime>
    14 
    15 using namespace std;
    16 
    17 #define REP(i, n) for (int i = 0; i < (n); ++i)
    18 #define eps 1e-9
    19 #define lc id << 1
    20 #define rc id << 1 | 1
    21 #define lson low, mid, lc
    22 #define rson mid + 1, high, rc
    23 
    24 typedef long long ll;
    25 typedef pair<int, int> pii;
    26 struct Node {
    27     int x, y1, y2, flag;
    28     bool operator < (const Node& rhs) const {
    29         return x < rhs.x || (x == rhs.x && flag > rhs.flag);
    30     }
    31 };
    32 const int maxn = 2e5 + 10;
    33 int n;
    34 int l[maxn], v[maxn], r[maxn], max_cnt[maxn * 4], max_r[maxn * 4], lazy[maxn * 4];
    35 Node node[maxn];
    36 vector<int> y;
    37 
    38 int get_id(int x) { return lower_bound(y.begin(), y.end(), x) - y.begin() + 1; }
    39 void build(int low, int high, int id) {
    40     max_r[id] = y[high - 1]; if (low == high) { return; }
    41     int mid = (low + high) / 2; build(lson); build(rson);
    42 }
    43 void push_up(int id) {
    44     max_r[id] = (max_cnt[lc] >= max_cnt[rc]) ? max_r[lc] : max_r[rc];
    45     max_cnt[id] = max(max_cnt[lc], max_cnt[rc]);
    46 }
    47 void push_down(int id) {
    48     if (!lazy[id]) { return; }
    49     max_cnt[lc] += lazy[id]; lazy[lc] += lazy[id];
    50     max_cnt[rc] += lazy[id]; lazy[rc] += lazy[id]; lazy[id] = 0;
    51 }
    52 void update(int l, int r, int k, int low, int high, int id) {
    53     if (l == low && r == high) { max_cnt[id] += k; lazy[id] += k; return; }
    54     int mid = (low + high) / 2; push_down(id);
    55     if (r <= mid) { update(l, r, k, lson); }
    56     else if (l >= mid + 1) { update(l, r, k, rson); }
    57     else { update(l, mid, k, lson); update(mid + 1, r, k, rson); }
    58     push_up(id);
    59 }
    60 
    61 int main() {
    62 #ifdef __AiR_H
    63     freopen("in.txt", "r", stdin);
    64 //    freopen("out.txt", "w", stdout);
    65 #endif // __AiR_H
    66     scanf("%d", &n);
    67     for (int i = 0; i < n; ++i) {
    68         scanf("%d %d %d", &l[i], &v[i], &r[i]);
    69         y.push_back(v[i]); y.push_back(r[i]);
    70         node[i * 2] = Node{l[i], v[i], r[i], 1}; node[i * 2 + 1] = Node{v[i], v[i], r[i], -1};
    71     }
    72     sort(y.begin(), y.end());
    73     y.erase(unique(y.begin(), y.end()), y.end());
    74     n *= 2; sort(node, node + n);
    75     int Size = y.size(), ans = 0, id1, id2, ans_l, ans_r, cnt = 0;
    76     build(1, Size, 1);
    77     for (int i = 0; i < n; ++i) {
    78         id1 = get_id(node[i].y1); id2 = get_id(node[i].y2);
    79         update(id1, id2, node[i].flag, 1, Size, 1);
    80         if (max_cnt[1] > ans) { ans = max_cnt[1]; ans_l = node[i].x; ans_r = max_r[1]; }
    81     }
    82     printf("%d
    ", ans); n /= 2;
    83     for (int i = 0; i < n; ++i) {
    84         if (l[i] <= ans_l && ans_r <= r[i] && ans_l <= v[i] && v[i] <= ans_r) {
    85             printf(++cnt == 1 ? "%d" : " %d", i + 1);
    86         }
    87     }
    88     printf("
    ");
    89 #ifdef __AiR_H
    90     printf("Time used = %.2fs
    ", (double)clock() / CLOCKS_PER_SEC);
    91 #endif // __AiR_H
    92     return 0;
    93 }
    View Code

  • 相关阅读:
    ElasticSearch「1」本地安裝Elasticsearch 6.0.1 + Elasticsearch-head插件
    HDFS Erasure Coding介绍
    Cassandra VS HBase
    Hadoop入门 【1】 下载源码,构建
    HBase ProcedureV2 分析
    github创建maven项目过程
    ruby, gem install 出现网络错误
    Ketama Consisent Hash
    [转]产品经理 书目录
    [算法]动态规划之最长公共子序列
  • 原文地址:https://www.cnblogs.com/zhaoyz/p/7273698.html
Copyright © 2011-2022 走看看