zoukankan      html  css  js  c++  java
  • Bzoj3289: Mato的文件管理

    题面

    传送门

    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
    ", ans[i]);
    	return 0;
    }
    
    
  • 相关阅读:
    Vuejs之Component slot 插槽详解
    ASP.NET MVC生命周期与管道模型
    Unity IoC Base On MVC
    轻量级IoC框架Ninject.NET搭建
    原创【前端控件】之日历控件
    出现了内部错误-网站中X509Certificate2加载证书时出错
    windows service 1053错误 启动失败
    关于浏览器Number.toFixed的错误修复
    vue中用mock制造模拟接口(本文主要解决坑),一定要看完哦
    ubuntu开发项目不能执行热更新
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8330931.html
Copyright © 2011-2022 走看看