牛客 - 货物种类(差分)

题目链接:点击查看

题目大意:有 n 个仓库,接下来有 m 次进货,每次进货会向[ l , r ]区间内的仓库进入种类为 w 的货物,最后问哪个仓库的货物种类最多

题目分析:看到是区间问题,下意识想到了线段树,但实际上线段树处理这种不同颜色的问题一般都是状态压缩,但显然这个题目货物的种类范围到达了1e9,所以肯定不是颜色统计的模板题了,比赛的时候差分的想法在脑中浮现过一次,但很快就被自己否决了,原因还是因为自己不会呀,赛后补提的时候看了一下大佬们的代码,恍然大悟,差分+一个map维护一下区间内的出现次数就好了,非常简单的一道思维题

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;
     
typedef long long LL;
    
typedef unsigned long long ull;
     
const int inf=0x3f3f3f3f;

const int N=1e5+100;

vector<int>add[N],del[N];

map<int,int>vis;

int main()
{
#ifndef ONLINE_JUDGE
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
   	int n,m;
   	scanf("%d%d",&n,&m);
   	while(m--)
   	{
   		int l,r,w;
   		scanf("%d%d%d",&l,&r,&w);
   		add[l].push_back(w);
   		del[r+1].push_back(w);
	}
	int mmax=-inf,mark,cnt=0;
   	for(int i=1;i<=n;i++)
   	{
   		for(auto j:add[i])
   		{
   			if(vis[j]==0)
   				cnt++;
   			vis[j]++;
		}
		for(auto j:del[i])
		{
			vis[j]--;
			if(vis[j]==0)
				cnt--;
		}
		if(cnt>mmax)
		{
			mmax=cnt;
			mark=i;
		}
	}
	printf("%d\n",mark);
   	
   	
   	
    
    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
}
发布了655 篇原创文章 · 获赞 21 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104453627