hdu4747(求mex区间之和)

求区间所有mex之和,用线段树会快一秒,但是写的很长,所以我写了递推版的。

#include
#include
#include
#include
#include
#include
#include
#include
#define mem(a,x) memset(a,x,sizeof(a))
#define s1(x) scanf("%d",&x)
#define s2(x,y) scanf("%d%d",&x,&y)
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k)
#define ff(a,n) for(int i = 0 ; i < n; i++) scanf("%d",a+i)
#define tp(x) printf("x = %d\n",x)
#define ansp(x) printf("%d\n",x)
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair pii;
const ll inf = 0x3f3f3f3f;

const int mx = 2e5+10;
int num[mx],f[mx],last[mx],n;
int main(){
	//int T=10;	scanf("%d",&T);
		freopen("F:\\in.txt","r",stdin);
	while(s1(n) &&  n){
		for(int i = 1; i <= n; i++){
			s1(num[i]);
		}
		mem(f,0);
		mem(last,0);
		ll sum = 0,ans=0;
		for(int i = 1; i <= n ;i++){
			if(num[i] < n){
	
				int la = last[num[i]];
				last[num[i]] = i;	
				for(int j = num[i]; j < n; j++){	//这里错写la 
					if(j)
						f[j] = min(f[j-1],last[j]);
					else
						f[j] = i;
						
					if(la < f[j]){
						sum +=  f[j] - la;
					}
					else
						break;
				}
			} 
			//cout<

 

你可能感兴趣的:(日常训练)