zoukankan      html  css  js  c++  java
  • bzoj1176: [Balkan2007]Mokia【cdq分治】

      把询问搞成4个,cdq分治。

     1 #include <bits/stdc++.h>
     2 #define rep(i, a, b) for (int i = a;i <= b; i++)
     3 #define drep(i, a, b) for (int i = a; i >= b; i--)
     4 #define REP(i, a, b) for (int i = a; i < b; i++)
     5 #define mp make_pair
     6 #define pb push_back
     7 #define clr(x) memset(x, 0, sizeof(x))
     8 #define xx first
     9 #define yy second
    10 using namespace std;
    11 typedef long long i64;
    12 typedef pair<int, int> pii;
    13 const int inf = ~0U >> 1;
    14 const i64 INF = ~0ULL >> 1;
    15 //**********************************
    16  
    17 const int maxn = 200005;
    18  
    19 int c[2000005], w;
    20 struct Complex {
    21     int flag;
    22     int x, y, c;
    23     int id, ans;
    24     int pos, l;
    25     inline bool operator < (const Complex &a) const {
    26         return x < a.x || 
    27             x == a.x && y < a.y ||
    28             x == a.x && y == a.y && c < a.c;
    29     }
    30 } src[maxn], t[maxn];
    31  
    32 inline void add(int x, int v) {
    33     while (x <= w) {
    34         c[x] += v;
    35         x += x & -x;
    36     }
    37 }
    38 inline int get(int x) {
    39     int ret(0);
    40     while (x > 0) {
    41         ret += c[x];
    42         x -= x & -x;
    43     }
    44     return ret;
    45 }
    46 int ans[10005];
    47  
    48 void cdq(int l, int r) {
    49     if (l == r) return;
    50     int mid = l + r >> 1, l1 = l, l2 = mid + 1;
    51     rep(i, l, r) {
    52         if (src[i].id <= mid && !src[i].l) add(src[i].y, src[i].c);
    53         if (src[i].id > mid && src[i].l) ans[src[i].pos] += src[i].l * get(src[i].y);
    54     }
    55     rep(i, l, r) if (src[i].id <= mid && !src[i].l) add(src[i].y, -src[i].c);
    56     rep(i, l, r) if (src[i].id <= mid) t[l1++] = src[i]; else t[l2++] = src[i];
    57     memcpy(src + l, t + l, (r - l + 1) * sizeof(Complex));
    58     cdq(l, mid); cdq(mid + 1, r);
    59 }
    60  
    61 int main() {
    62     int cnt(0), n(0), s;
    63     scanf("%d%d", &s, &w);
    64     int flag;
    65     while (scanf("%d", &flag), flag ^ 3) {
    66         if (flag == 1) {
    67             ++n;
    68             src[n].id = n; src[n].l = 0; src[n].pos = 0;
    69             scanf("%d%d%d", &src[n].x, &src[n].y, &src[n].c);
    70         }
    71         else {
    72             int x, y, a, b; scanf("%d%d%d%d", &x, &y, &a, &b);
    73  
    74             ans[++cnt] = s * (a - x) * (b - y);
    75  
    76             ++n;
    77             src[n].id = n; src[n].l = 1; src[n].pos = cnt;
    78             src[n].x = a, src[n].y = b, src[n].c = inf;
    79  
    80             ++n;
    81             src[n].id = n; src[n].l = -1; src[n].pos = cnt;
    82             src[n].x = a, src[n].y = y - 1, src[n].c = inf;
    83  
    84             ++n;
    85             src[n].id = n; src[n].l = -1; src[n].pos = cnt;
    86             src[n].x = x - 1, src[n].y = b, src[n].c = inf;
    87  
    88             ++n;
    89             src[n].id = n; src[n].l = 1; src[n].pos = cnt;
    90             src[n].x = x - 1, src[n].y = y - 1, src[n].c = inf;
    91         }
    92     }
    93     sort(src + 1, src + n + 1);
    94     cdq(1, n);
    95     rep(i, 1, cnt) printf("%d
    ", ans[i]);
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    C++多态的实现原理
    C++编程之面向对象的三个基本特征
    C/C++中static关键字详解
    C/C++中static关键字作用总结
    Linux防CC攻击脚本
    linux下防火墙iptables原理及使用
    linux下使用 TC 对服务器进行流量控制
    awr报告与statspack报告
    awr报告
    statspack报告
  • 原文地址:https://www.cnblogs.com/y7070/p/5069550.html
Copyright © 2011-2022 走看看