• 学习笔记——不带修序列莫队 (luogu2079)小B的询问


    莫队是一种对于询问的离线算法

    时间复杂度:O((n sqrt n)

    大致思想就是

    首先将询问离线,然后对原序列分块,使得每一个(l和r)都在一个块里

    然后按照左节点排序,若所在的块相等,就比较右节点

    int cmp1(Node a,Node b)
    {
    	if (pos[a.l]==pos[b.l]) return a.r<b.r;
        return a.l<b.l;
    }
    

    排序之后,我们再来分析一下时间复杂度;接下来我们会看到神奇的事情!!

    刚才分析此方法的时候,我们是从L和R的偏移量分析的;我们仍然用这种方法来分析。

    考虑一下在同一个块的时候。由于L的范围是确定的,所以每次L的偏移量是O(√N)

    但是r的范围没有确定;r的偏移量是O(N)。

    那么从一个块到另一个块呢?

    明显地,r我们不需要作考虑,仍然是O(N)。

    而L明显最多也是2*√N,而且这种情况下,很快就会到下下一块。所以也是O(√N)

    由于有√N(根号N)个块,所以r的总偏移量是O(N*√N)

    而M个询问,每个询问都可以让L偏移O(√N),所以L的总偏移量O(M*√N)

    注意了,时间复杂度分析的时候一定要注意,r的偏移量和询问数目是没有直接关系的。

    而L则恰恰相反;L的偏移量我们刚才也说明了,它和块的个数没有直接关系。

    所以总的时间复杂度是:

    O((N+M)*(sqrt n)


    在排序完之后,就按照顺序,一个一个求解,跳l和r

    下面介绍两种操作 (remove)(insert),分别是将这个位置移除、将这个位置加入答案

    QwQ我也不知道为什么我一开始把这两个合成了一个函数

    inline void update(int pos,int add)
    {
    	ans-=poer(s[c[pos]]);
    	s[c[pos]]+=add;
    	ans+=poer(s[c[pos]]);
    }
    

    然后就是注意l和r 初始要设成 1和 0

    void solve()
    {
    	int l=1,r=0;
    	for (int i=1;i<=m;i++) 
    	{
    	
    		while (r<a[i].r)
    		{
    			update(r+1,1);
    			r++;
    		}	
    		while (r>a[i].r)
    		{
    			update(r,-1);
    			r--;
    		}
    		while (l<a[i].l)
    		{
    			 update(l,-1);
    			 l++;
    		}
    		while (l>a[i].l)
    		{
    			update(l-1,1);
    			l--;
    		}
    		if (a[i].l==a[i].r)
    		{
    			a[i].ans=1;
    			continue;
    		}
    		a[i].ans=ans;
    		
    	}
    	return;
    }
    

    下面引入一个经典例题:

    题目大意:
    小B有一个序列,包含N个1~K之间的整数。他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数。

    对于全部的数据,1<=N、M、K<=50000

    那么这道题就是一道经典的序列莫队问题了

    直接上代码了

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath> 
    #define ll long long
    
    using namespace std;
    
    const int maxn = 50010;
    
    struct Node{
    	int l,r,id;
    	ll ans;
    };
    
    inline int read(){
        int f=1,x=0;char ch;
        do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
        do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
        return x*f;
    }
    
    Node a[maxn];
    int pos[maxn];
    int c[maxn],n,m,block;
    long long s[maxn];
    int k;
    ll ans;
    
    inline ll poer(ll x){
    	return x*x;
    }
    
    int cmp1(Node a,Node b)
    {
    	if (pos[a.l]==pos[b.l]) return a.r<b.r;
        return a.l<b.l;
    }
    
    int cmp2(Node a,Node b)
    {
    	return a.id<b.id;
    }
    
    inline void update(int pos,int add)
    {
    	ans-=poer(s[c[pos]]);
    	s[c[pos]]+=add;
    	ans+=poer(s[c[pos]]);
    }
    
    void solve()
    {
    	int l=1,r=0;
    	for (int i=1;i<=m;i++) 
    	{
    	
    		while (r<a[i].r)
    		{
    			update(r+1,1);
    			r++;
    		}	
    		while (r>a[i].r)
    		{
    			update(r,-1);
    			r--;
    		}
    		while (l<a[i].l)
    		{
    			 update(l,-1);
    			 l++;
    		}
    		while (l>a[i].l)
    		{
    			update(l-1,1);
    			l--;
    		}
    		if (a[i].l==a[i].r)
    		{
    			a[i].ans=1;
    			continue;
    		}
    		a[i].ans=ans;
    		
    	}
    	return;
    }
    
    int main()
    {
    	scanf("%d%d%d",&n,&m,&k);
    	for (int i=1;i<=n;i++)
    	  c[i]=read();
    	block=(int)sqrt(n);
    	for (int i=1;i<=n;i++)
    	{
    		pos[i]=(i-1)/block+1;
    	}
    	for (int i=1;i<=m;i++)
    	{
    		a[i].l=read();
    		a[i].r=read();
    		a[i].id=i;
    	}
    	ans=0;
    	sort(a+1,a+1+m,cmp1);
    	solve();
    	sort(a+1,a+1+m,cmp2);
    	for (int i=1;i<=m;i++)
    	{
    		printf("%lld
    ",a[i].ans);
    	}
    	return 0;
    } 
    
  • 相关阅读:
    JS正则表达式
    JS验证电话号是否合法
    特性Attribute 的使用
    三层架构(面向对象思想)
    oracle 游标的使用
    oracle中的net manager 无法配置
    .net学习网站汇总
    每天进步一点点之后缀表达式求值
    每天进步一点点之中缀表达式转后缀表达式
    下载Android代码
  • 原文地址:https://www.cnblogs.com/yimmortal/p/10160788.html
Copyright © 2020-2023  润新知