2021 Spring Individual Tournament-7 Supplement

A.Zero Array

Meaning: given nnn number andqqq queries, each query contains an operand, if the operand is1 11 , then enter two more numbersp, vp, vp,v , leta [p] = va[p]=va[p]=v if the operand is2 22 , the output makes this array become0 0The minimum number of 0 arrays.
The definition of zero array: You can subtract a certain numberxx froma non-zero number in the arrayx , regarded as one operation, until the array becomes all0 00
analysis:only need to calculate how many non-zeroin the array0 , but not the same number. The data range is1e 9 1e91 e 9 , so usemap mapm a p , pay attention tomap mapm a p cannot directly usesize () size()size()

Code:

#include<stdio.h>
#include<map>
using namespace std;
const int N=1e5+5;
int t,n,q,a[N],op,u,v;
int main(){
    
    
	scanf("%d",&t);
	while(t--){
    
    
		int res=0;
		map<int,int> s;
		scanf("%d%d",&n,&q);
		for(int i=1;i<=n;i++){
    
    
			scanf("%d",&a[i]);
			if(a[i]&&!s[a[i]]) res++;
			s[a[i]]++;
		}
		while(q--){
    
    
			scanf("%d",&op);
			if(op==1){
    
    
				scanf("%d%d",&u,&v);
				if(a[u]&&s[a[u]]==1) res--;
				s[a[u]]--;
				if(v&&!s[v]) res++;
				s[v]++;
				a[u]=v;
			}
			else if(op==2){
    
    
				printf("%d\n",res);
			}
		}
	}
}

C.Intersections

Meaning: given nnn numberaaa andnnn numberbbb , they do not repeat,aaa array is on top,bbb array is down, now fromaaAll the numbers in the a array are paired withbbFor all the numbers in the b array, if the matching is successful (equal), connect a line to find the number of intersections of the line segments.
Analysis:Find the number of pairs in reverse order

Code:

#include<stdio.h>
#include<string.h>
const int N=1e5+5;
int t,n,a[N],b[N],tmp[N],x;
long long int res;
void merge_sort(int l,int r){
    
    
    if(l>=r) return ;
    int mid=l+r>>1;
    merge_sort(l,mid);
    merge_sort(mid+1,r);
    int i=l,j=mid+1,k=l;
    while(i<=mid&&j<=r){
    
    
        if(a[i]<=a[j]) tmp[k++]=a[i++];
        else{
    
    
            tmp[k++]=a[j++];
            res+=(mid-i+1);
        }
    }
    while(i<=mid) tmp[k++]=a[i++];
    while(j<=r) tmp[k++]=a[j++];
    for(i=l;i<=r;i++) a[i]=tmp[i];
}
int main(){
    
    
    scanf("%d",&t);
    while(t--){
    
    
        res=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
    
    
            scanf("%d",&x);
            b[x]=i;
        }
        for(int i=1;i<=n;i++){
    
    
            scanf("%d",&x);
            a[i]=b[x];
        }
        merge_sort(1,n);
        printf("%lld\n",res);
    }
}

Guess you like

Origin blog.csdn.net/messywind/article/details/114950570