集合帖:区间问题

一、AcWing 803:区间合并
(1)题目来源:https://www.acwing.com/problem/content/805/
(2)算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/145067059

#include <bits/stdc++.h>
using namespace std;
 
const int maxn=1e5+5;
struct Section {
    int x;
    int y;
} a[maxn];
 
bool cmp(Section u,Section v) {
    if(u.x==v.x) return u.y<v.y;
    return u.x<v.x;
}
 
int main() {
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i].x>>a[i].y;
    }
 
    sort(a+1,a+1+n,cmp);
 
    int base=a[1].y;
    int cnt=1;
    for(int i=1; i<=n; i++) {
        if(a[i].x<=base) base=max(base,a[i].y);
        else cnt++,base=a[i].y;
    }
    cout<<cnt<<endl;
 
    return 0;
}
 
/*
in:
10
-15 -8
-20 23
-2 11
2 22
18 23
11 27
-8 21
-18 14
-17 -12
-23 8
out:
1
*/

二、洛谷 P1496:火烧赤壁
(1)题目来源:https://www.luogu.com.cn/problem/P1496
(2)算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/145073398

#include <bits/stdc++.h>
using namespace std;
 
const int maxn=2e5;
struct Point {
    int x;
    int y;
} a[maxn];
 
bool cmp(Point u,Point v) {
    if(u.x==v.x) return u.y<v.y;
    return u.x<v.x;
}
 
int main() {
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i].x>>a[i].y;
    }
 
    sort(a+1,a+1+n,cmp);
 
    int cnt=0;
    int from=a[1].x;
    int base=a[1].y;
    for(int i=1; i<=n; i++) {
        if(a[i].x<=base) base=max(base,a[i].y);
        else {
            cnt+=(base-from);
            from=a[i].x;
            base=a[i].y;
        }
    }
    cnt+=(base-from);
    cout<<cnt<<endl;
 
    return 0;
}
 
/*
in:
3
-1 1
5 11
2 9
out:
11
*/

三、AcWing 905:区间选点
(1)题目来源:https://www.acwing.com/problem/content/907/
(2)算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/142840548

#include <bits/stdc++.h>
using namespace std;
 
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;
 
struct Scope {
    int le,ri;
} a[maxn];
 
bool up(Scope u,Scope v) {
    return u.ri<v.ri;
}
 
int main() {
    int n;
    cin>>n;
    for(int i=0; i<n; i++) {
        cin>>a[i].le>>a[i].ri;
    }
    sort(a,a+n,up);
 
    int ans=0;
    int t_ri=-inf;
    for(int i=0; i<n; i++)
        if(t_ri<a[i].le) {
            ans++;
            t_ri=a[i].ri;
        }
    cout<<ans<<endl;
 
    return 0;
}
 
/*
in:
3
-1 1
2 4
3 5
out:
2
*/

四、AcWing 906:区间分组
(1)题目来源:https://www.acwing.com/problem/content/908/
(2)算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/145081557

#include <bits/stdc++.h>
using namespace std;

const int maxn=1e5+5;
pair<int,int> a[maxn];
int n;

int main() {
    cin>>n;
    for(int i=0; i<n; i++) {
        cin>>a[i].first>>a[i].second;
    }

    sort(a,a+n);

    priority_queue<int,vector<int>,greater<int>> q;
    for(int i=0; i<n; i++) {
        if(q.size() && a[i].first>q.top()) q.pop();
        q.push(a[i].second);
    }
    cout<<q.size()<<endl;

    return 0;
}

/*
in:
10
-15 -8
-20 23
-2 11
2 22
18 23
11 27
-8 21
-18 14
-17 -12
-23 8

out:
6
*/

五、AcWing 907:区间覆盖
(1)题目来源:
https://www.acwing.com/problem/content/909/
(2)算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/145083747

#include <bits/stdc++.h>
using namespace std;

const int maxn=1e5+5;
struct Section {
    int x;
    int y;
} a[maxn];

bool cmp(Section u,Section v) {
    if(u.x==v.x) return u.y<v.y;
    return u.x<v.x;
}

int main() {
    int st,en;
    cin>>st>>en;

    int n;
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i].x>>a[i].y;
    }
    sort(a+1,a+1+n,cmp);

    int ans=0;
    bool flag=false;
    for(int i=1; i<=n; i++) {
        int j=i,t=INT_MIN;
        while(j<=n && a[j].x<=st) {
            t=max(t,a[j].y);
            j++;
        }

        if(t<st) break;
        ans++;

        if(t>=en) {
            flag=true;
            break;
        }
        st=t,i=j-1;
    }

    if(!flag) ans=-1;
    cout<<ans<<endl;

    return 0;
}

/*
in:
1 5
3
-1 3
2 4
3 5

out:
2
*/

六、AcWing 802:区间和
(1)题目来源:https://www.acwing.com/problem/content/804/
(2)算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/143309807

#include <bits/stdc++.h>
using namespace std;
 
typedef pair<int,int> PII;
const int maxn=3e5+5;
int a[maxn],s[maxn];
int n,m;
vector<int> alls;
vector<PII> add,query;
 
int find(int x) { //discretization
    int le=0;
    int ri=alls.size()-1;
    while(le<ri) {
        int mid=(le+ri)>>1;
        if(alls[mid]>=x) ri=mid;
        else le=mid+1;
    }
    return ri+1;
}
 
int main() {
    cin>>n>>m;
    for(int i=0; i<n; i++) {
        int x,c;
        cin>>x>>c;
        alls.push_back(x);
        add.push_back({x,c});
    }
 
    for(int i=0; i<m; i++) {
        int le,ri;
        cin>>le>>ri;
        alls.push_back(le), alls.push_back(ri);
        query.push_back({le,ri});
    }
 
    //de-weight
    sort(alls.begin(),alls.end());
    alls.erase(unique(alls.begin(),alls.end()), alls.end());
 
    for(auto v:add) {
        int x=find(v.first);
        a[x]+=v.second;
    }
 
    for(int i=1; i<=alls.size(); i++) {
        s[i]=s[i-1]+a[i]; //prefix sum
    }
 
    for(auto v:query) {
        int le=find(v.first);
        int ri=find(v.second);
        cout<<s[ri]-s[le-1]<<endl;
    }
 
    return 0;
}
 
/*
in:
3 3
1 2
3 6
7 5
1 3
4 6
7 8
out:
8
0
5
*/





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值