• BZOJ_3289_Mato的文件管理_莫队+树状数组


    BZOJ_3289_Mato的文件管理_莫队+树状数组

    Description

    Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号
    。为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能访问。Mato每天随机选一个区间[l,r
    ],他今天就看编号在此区间内的这些资料。Mato有一个习惯,他总是从文件大小从小到大看资料。他先把要看的
    文件按编号顺序依次拷贝出来,再用他写的排序程序给文件大小排序。排序程序可以在1单位时间内交换2个相邻的
    文件(因为加密需要,不能随机访问)。Mato想要使文件交换次数最小,你能告诉他每天需要交换多少次吗?

    Input

    第一行一个正整数n,表示Mato的资料份数。
    第二行由空格隔开的n个正整数,第i个表示编号为i的资料的大小。
    第三行一个正整数q,表示Mato会看几天资料。
    之后q行每行两个正整数l、r,表示Mato这天看[l,r]区间的文件。
    n,q <= 50000

    Output

    q行,每行一个正整数,表示Mato这天需要交换的次数。

    Sample Input

    4
    1 4 2 3
    2
    1 2
    2 4

    Sample Output

    0
    2
    //样例解释:第一天,Mato不需要交换
    第二天,Mato可以把2号交换2次移到最后。
     
    分析:
     
    UPD:naive!
     
    莫队裸题。
    指针l,r移动时用树状数组更新答案,考虑每个数的贡献即可。
     
    代码:
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    #define N 50050
    #define LL long long
    int c[N], n, q, v[N], pos[N];
    struct A {
    	int num,d,id;
    }b[N];
    bool cmp3(const A &x,const A &y) {return x.num<y.num; }
    bool cmp4(const A &x,const A &y) {return x.id < y.id ; }
    struct B {
    	int s,t,id;
    	LL ans;
    }a[N];
    bool cmp1(const B &x,const B &y) {
    	if(pos[y.s] == pos[x.s]) return x.t < y.t;
    	return pos[x.s] < pos[y.s];
    }
    bool cmp2(const B &x,const B &y) {
    	return x.id < y.id;
    }
    LL now;
    int main() {
    	scanf("%d",&n);
    	int i, j, block = sqrt(n), l, r = 0;
    	for(i = 1;i <= block; ++ i) {
    		l = r + 1;
    		r = i * block;
    		for(j = l;j <= r; ++ j) pos[j] = i;
    	}
    	if(r != n) {
    		++ block;
    		l = r + 1;
    		r = n;
    		for(i = l;i <= r; ++ i) pos[i] = block;
    	}
    	for(i = 1;i <= n; ++ i) scanf("%d", &b[i].num), b[i].id = i;
    	sort(b + 1, b + n + 1, cmp3); b[0].num = -84234820;
    	for(i = 1, j = 0;i <= n; ++ i) {if(b[i].num != b[i - 1].num)j ++; b[i].d = j; }
    	sort(b + 1, b + n + 1, cmp4);
    	for(i = 1;i <= n; ++ i) v[i] = b[i].d;
    	scanf("%d", &q);
    	for(i = 1;i <= q; ++ i) scanf("%d%d", &a[i].s, &a[i].t),a[i].id = i;
    	sort(a + 1, a + q + 1, cmp1);
    	for(l = 1, r = 0, i = 1;i <= q; ++ i) {
    		while(l < a[i].s) {
    			int t = v[l] - 1, re = 0;
    			for(; t ;t -= t & (-t)) re += c[t];
    			now -= re;
    			t = v[l];
    			for(;t <= n;t += t & (-t)) c[t]--;
    			l ++; 
    		}
    		while(l > a[i].s) {
    			int t = v[l - 1] - 1, re = 0;
    			for(; t ;t -= t & (-t)) re += c[t];
    			now += re;
    			t = v[l - 1];
    			for(;t <= n;t += t & (-t)) c[t]++;
    			l --; 
    		}
    		while(r > a[i].t) {
    			int t = v[r], re = 0;
    			for(; t ;t -= t & (-t)) re += c[t];
    			re = r - l + 1- re;
    			now -= re;
    			t = v[r];
    			for(;t <= n;t += t & (-t)) c[t]--;
    			r --;
    		}
    		while(r < a[i].t) {
    			int t = v[r + 1], re = 0;
    			for(; t ;t -= t & (-t)) re += c[t];
    			re = r - l + 1 - re;
    			now += re;
    			t = v[r + 1];
    			for(;t <= n;t += t & (-t)) c[t]++;
    			r ++;
    		}
    		a[i].ans = now;
    	}
    	sort(a + 1, a + q + 1, cmp2);
    	for(i = 1;i <= q; ++ i) printf("%lld
    ",a[i].ans);
    }
    
  • 相关阅读:
    淘宝网-六属性
    软件架构之黑板模式
    06需求工程-软件建模与分析阅读笔记之六
    05需求工程-软件建模与分析阅读笔记之五
    04需求工程-软件建模与分析阅读笔记之四
    03需求工程-软件建模与分析阅读笔记之三
    第二次冲刺团队绩效评估
    第二次冲刺站立会议07
    第二次冲刺站立会议06
    第二次冲刺站立会议05
  • 原文地址:https://www.cnblogs.com/suika/p/8678041.html
Copyright © 2020-2023  润新知