CF220B Little Elephant and Array(扫描线+树状数组)

简介: CF220B Little Elephant and Array(扫描线+树状数组)

原题链接

思路:

首先,我们可以先将所有的询问进行离线,区间问题借助扫描线的思想,枚举右端点,计算左端点产生的贡献。

考虑什么时候的区间是一个合法的区间,比如当前枚举到数x,如果x前面有x − 1个x都在区间里,并且这个x前面的第x个x不在区间里,这个区间就是合法的区间。

举一个很典型的例子,[ 2 , 2 , 2 , 2 ]

当右端点为1时,整个数组产生的贡献为[ 0 , 0 , 0 , 0 ];

当右端点为2时,整个数组产生的贡献为[ 1 , 0 , 0 , 0 ];

当右端点为3时,整个数组产生的贡献为[ − 1 , 1 , 0 , 0 ];

当右端点为4时,整个数组产生的贡献为[ 0 , − 1 , 1 , 0 ];

具体有什么规律呢,首先对于遍历到的右端点先将其出现的次数加1,假设此时出现的数为x,那么这个数前面从右往左出现的第x个位置+ 1,第x + 1个位置− 2 ,第x + 2个位置+ 1.

观察右端点从3 − > 4的过程也可以看出这点,简单说一下理由:

当右端点为4时,我们计算出来的前缀和数组应该为[ 0 , − 1 , 0 , 0 ],此时的合法区间为[ 3 , 4 ],那么s u m [ 4 ] − s u m [ 3 − 1 ] = 1,符合题意;而[ 2 , 4 ]为不合法区间,s u m [ 4 ] − s u m [ 2 − 1 ] = 0,也同样符合题意。

用理论也可以解释:

对于从左向右出现的第x个位置,将他的贡献从0变为1,是因为这时候已经满足x出现了x次。

对于第x + 1个位置,由于计算上一个x的贡献时候,他的贡献已经被改成1了,所以要将他的贡献改成− 1。

对于第x + 2个位置,由于计算上一个合法x的区间的时候,他的贡献被改成− 1了,但其实他的贡献应该为0。

具体做法为:

1.将所有询问离线下来,并且记录每个数出现的位置。

2.枚举右端点,按照上述规律去维护贡献。

3.求前缀和后记录答案。

还有一个问题就是,a [ i ]的范围是1 e 9,如果直接对每个数都存出现的位置的话,空间也不允许。但是因为只有出现次数大于本身的值的数才有可能产生贡献,而n < = 1 e 5,所以a [ i ] > = n的数永远不会产生贡献,就可以忽略不计了。

代码:

// Problem: B. Little Elephant and Array
// Contest: Codeforces - Codeforces Round #136 (Div. 1)
// URL: http://codeforces.com/problemset/problem/220/B
// Memory Limit: 256 MB
// Time Limit: 4000 ms
// Author:Cutele
// 
// Powered by CP Editor (https://cpeditor.org)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}
inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
const int maxn=1e5+7;
int a[maxn],n,m,ans[maxn],tr[maxn];
vector<PII>q[maxn];
vector<int>g[maxn];
int lowbit(int x){
  return x&-x;
}
void update(int pos,int val){
  while(pos<=n){
    tr[pos]+=val;pos+=lowbit(pos);
  }
}
int query(int pos){
  int res=0;
  while(pos){
    res+=tr[pos];pos-=lowbit(pos);
  }
  return res;
}
int main(){
  n=read,m=read;
  rep(i,1,n) a[i]=read;
  rep(i,1,m){
    int l=read,r=read;
    q[r].push_back({l,i});
  } 
  rep(i,1,n){
    int x=a[i];
    if(x<=n){
      g[x].push_back(i);
      int siz=g[x].size();
      if(siz>=x){
        update(g[x][siz-x],1);
        if(siz-x-1>=0) update(g[x][siz-x-1],-2);
        if(siz-x-2>=0) update(g[x][siz-x-2],1);
      }
    }
    for(int j=0;j<q[i].size();j++){
      PII t=q[i][j];
      int l=t.first,id=t.second;
      ans[id]=query(i)-query(l-1);
    }
  }
  rep(i,1,m) printf("%d\n",ans[i]);
  return 0;
}
目录
相关文章
|
人工智能
Codeforces 220B-Little Elephant and Array-扫描线 & 树状数组
题意: 给出一个长度为n的数组,有m个询问,每次询问给出一个区间,问这个区间内有多少个数x恰好出现x次
109 0
Codeforces 220B-Little Elephant and Array-扫描线 & 树状数组
|
2月前
|
Python
使用array()函数创建数组
使用array()函数创建数组。
29 3
|
2月前
|
存储 安全 Swift
在Swift中,数组(Array)
在Swift中,数组(Array)
38 3
|
2月前
|
JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(下)
一个数组的元素可以是另外一个数组,这样就构成了多维数组(Multi-dimensional Array)。
|
2月前
|
存储 JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(上)
数组对象是使用单独的变量名来存储一系列的值。
|
2月前
|
Ruby
|
7天前
|
存储 安全 算法
C++的内置数组和STL array、STL vector
C++的内置数组和STL array、STL vector
|
2月前
|
JavaScript 前端开发 索引
在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
【4月更文挑战第16天】在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
31 4
|
8月前
|
算法 Python
数组倍增(Array Doubling
数组倍增(Array Doubling)是一种常见的算法技术,用于解决数组相关的查找、插入、删除等问题。该技术的核心思想是将数组的大小乘以 2,新数组的长度是原数组长度的两倍,然后将原数组中的元素复制到新数组中。在某些情况下,这种技术可以提高算法的效率,尤其是对于动态数据结构的问题。
158 1
|
2月前
|
存储 索引 Python
多数pythoneer只知有列表list却不知道python也有array数组
多数pythoneer只知有列表list却不知道python也有array数组
34 0