码迷,mamicode.com
首页 > 其他好文 > 详细

Bzoj3289: Mato的文件管理

时间:2018-01-22 20:33:02      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:inline   turn   pos   http   mes   size   离线   统计   read   

题面

传送门

Sol

求区间逆序对个数,离线莫队搞,开树状数组统计,记得开\(longlong\)
不然WA无数遍不知道为什么

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(50005);

IL ll Read(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, q, bl[_], val[_], o[_], len;
ll bit[_], ans[_], cnt;
struct Qry{
    int l, r, id;
    IL bool operator <(RG Qry B) const{  return bl[l] != bl[B.l] ? bl[l] < bl[B.l] : r < B.r;  }
} qry[_];

IL void Add(RG int x, RG int d){  for(; x <= len; x += x & -x) bit[x] += d;  }

IL ll Query(RG int x){  RG ll ret = 0; for(; x; x -= x & -x) ret += bit[x]; return ret;  }

int main(RG int argc, RG char* argv[]){
    n = Read(); RG int blo = sqrt(n);
    for(RG int i = 1; i <= n; ++i) o[i] = val[i] = Read(), bl[i] = (i - 1) / blo + 1;
    sort(o + 1, o + n + 1); len = unique(o + 1, o + n + 1) - o - 1;
    for(RG int i = 1; i <= n; ++i) val[i] = lower_bound(o + 1, o + len + 1, val[i]) - o;
    q = Read();
    for(RG int i = 1; i <= q; ++i) qry[i].l = Read(), qry[i].r = Read(), qry[i].id = i;
    sort(qry + 1, qry + q + 1);
    for(RG int L = qry[1].l + 1, R = qry[1].l, i = 1, num = 0; i <= q; ++i){
        while(L < qry[i].l) cnt -= Query(val[L] - 1), --num, Add(val[L], -1), ++L;
        while(L > qry[i].l) --L, Add(val[L], 1), ++num, cnt += Query(val[L] - 1);
        while(R < qry[i].r) ++R, Add(val[R], 1), ++num, cnt += num - Query(val[R]);
        while(R > qry[i].r) cnt -= num - Query(val[R]), --num, Add(val[R], -1), --R;
        ans[qry[i].id] = cnt;
    }
    for(RG int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
    return 0;
}

Bzoj3289: Mato的文件管理

标签:inline   turn   pos   http   mes   size   离线   统计   read   

原文地址:https://www.cnblogs.com/cjoieryl/p/8330931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!