zoukankan      html  css  js  c++  java
  • POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化

    代码如下:

    # include<iostream>
    # include<cstdio>
    # include<queue>
    # include<vector>
    # include<list>
    # include<map>
    # include<set>
    # include<cstdlib>
    # include<string>
    # include<cstring>
    # include<algorithm>
    using namespace std;
    # define LL long long
    
    const int N=10000;
    const int INF=1<<30;
    const double oo=1e20;
    const double eps=1e-20;
    
    int x[N+5],y[N+5];
    vector<int>v;
    map<int,int>mp;
    int lazy[N*8+5];
    int tr[N*8+5];
    set<int>s;
    
    void pushDown(int o,int l,int r)
    {
    	if(lazy[o]!=-1){
    		lazy[o<<1]=lazy[o<<1|1]=lazy[o];
    		tr[o<<1]=tr[o<<1|1]=lazy[o];
    		lazy[o]=-1;
    	}
    }
    
    void build(int o,int l,int r)
    {
    	tr[o]=-1;
    	lazy[o]=-1;
    	if(l==r) return ;
    	int mid=l+(r-l)/2;
    	build(o<<1,l,mid);
    	build(o<<1|1,mid+1,r);
    }
    
    void update(int o,int l,int r,int L,int R,int val)
    {
    	if(L<=l&&r<=R){
    		tr[o]=lazy[o]=val;
    	}else{
    		pushDown(o,l,r);
    		int mid=l+(r-l)/2;
    		if(L<=mid) update(o<<1,l,mid,L,R,val);
    		if(R>mid) update(o<<1|1,mid+1,r,L,R,val);
    	}
    }
    
    void query(int o,int l,int r)
    {
    	if(l==r){
    		if(tr[o]!=-1) s.insert(tr[o]);
    	}else{
    		pushDown(o,l,r);
    		int mid=l+(r-l)/2;
    		query(o<<1,l,mid);
    		query(o<<1|1,mid+1,r);
    	}
    }
    
    int solve(int m,int n)
    {
    	build(1,1,m);
    	for(int i=0;i<n;++i){
    		update(1,1,m,mp[x[i]],mp[y[i]],i);
    	}
    	s.clear();
    	query(1,1,m);
    	return s.size();
    }
    
    int main()
    {
    	int T,n;
    	scanf("%d",&T);
    	while(T--)
    	{
    		mp.clear();
    		v.clear();
    		scanf("%d",&n);
    		for(int i=0;i<n;++i){
    			scanf("%d%d",x+i,y+i);
    			v.push_back(x[i]);
    			v.push_back(y[i]);
    		}
    		sort(v.begin(),v.end());
    		int len=unique(v.begin(),v.end())-v.begin();
    		for(int i=0;i<len;++i)
    			mp[v[i]]=i+1;
    		printf("%d
    ",solve(len,n));
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    java 14 -7 Date
    java 14 -6 BigInteger和BigDecimal
    java 14 -5 System类
    java14-4 Pattern和Matcher类的使用
    java 14-3 正则表达式的分割
    转:StringBuilder与StringBuffer的区别(转)
    kafka之config/server.properties配置参数说明
    Kafka内核理解:消息的收集/消费机制
    kafka删除topic及其相关数据
    kafka使用问题解决
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/5536191.html
Copyright © 2011-2022 走看看