Hello 2022

B.Integers Shop

B. Integers Shop
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The integers shop sells n
segments. The i-th of them contains all integers from li to ri and costs ci

coins.

Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.

After shopping, Vasya will get some more integers as a gift. He will get integer x

as a gift if and only if all of the following conditions are satisfied:

    Vasya hasn't bought x

.
Vasya has bought integer l
that is less than x
.
Vasya has bought integer r
that is greater than x

    . 

Vasya can get integer x

as a gift only once so he won't have the same integers after receiving a gift.

For example, if Vasya buys segment [2,4]
for 20 coins and segment [7,8] for 22 coins, he spends 42 coins and receives integers 2,3,4,7,8 from these segments. He also gets integers 5 and 6

as a gift.

Due to the technical issues only the first s
segments (that is, segments [l1,r1],[l2,r2],…,[ls,rs]

) will be available tomorrow in the shop.

Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.

For each s
from 1 to n, find how many coins will Vasya spend if only the first s

segments will be available.
Input

The first line contains a single integer t
(1≤t≤1000

) — the number of test cases.

The first line of each test case contains the single integer n
(1≤n≤105

) — the number of segments in the shop.

Each of next n
lines contains three integers li, ri, ci (1≤li≤ri≤109,1≤ci≤109) — the ends of the i

-th segments and its cost.

It is guaranteed that the total sum of n
over all test cases doesn't exceed 2⋅105

.
Output

For each test case output n
integers: the s-th (1≤s≤n) of them should be the number of coins Vasia will spend in the shop if only the first s

segments will be available.
Example
Input
Copy

3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1

Output
Copy

20
42
42
42
4
13
11
256
271
271

Note

In the first test case if s=1
then Vasya can buy only the segment [2,4] for 20 coins and get 3

integers.

The way to get 7
integers for 42 coins in case s=2

is described in the statement.

In the second test case note, that there can be the same segments in the shop.
思路:该题挺有意思的,有dp的思想,需要维护最左lmin和最右rmax,并且需要考虑lmin和rmax在同一次取时只花费min(该次,lmin.c+rmax.c);时间复杂度只需要O(n);
我的评价是:帅题!
#include<bits/stdc++.h>
#define int long long

using namespace std;
typedef pair<int,int> PII;
typedef pair<bool,int> PBI;

struct node{
    int l,r,c;
};
const int maxn=1e5+10;
struct noded{
    int l,c;
}L[maxn];
struct nodes{
    int r,c;
}R[maxn];

void solve(){
    int n;cin>>n;
    map<PII,PBI>mp;
    L[0].l=1e9+10;
    R[0].r=-1;
    L[0].c=R[0].c=1e9+10;
    for(int i=1;i<=n;++i){
        int l,r,c;cin>>l>>r>>c;
        if(l<=L[i-1].l){
            L[i].l=l;
            if(l==L[i-1].l) L[i].c=min(L[i-1].c,c);
            else L[i].c=c;
        }else{
            L[i].l=L[i-1].l;
            L[i].c=L[i-1].c;
        }
        if(r>=R[i-1].r){
            R[i].r=r;
            if(r==R[i-1].r) R[i].c=min(R[i-1].c,c);
            else R[i].c=c;
        }else{
            R[i].r=R[i-1].r;
            R[i].c=R[i-1].c;
        }
        if(L[i].l==l&&R[i].r==r){
            if(mp[{l,r}].first) mp[{l,r}].second=min(c,mp[{l,r}].second);
            else mp[{l,r}].second=c;
            mp[{l,r}].first=true;
            cout<<min(L[i].c+R[i].c,mp[{l,r}].second)<<endl;
        }else{
            if(mp[{L[i].l,R[i].r}].first) cout<<min(mp[{L[i].l,R[i].r}].second,L[i].c+R[i].c)<<endl;
            else cout<<L[i].c+R[i].c<<endl;
        }
    }
}

signed main(){
    ios::sync_with_stdio(false);
    int T;cin>>T;
    while(T--) solve();for(;;);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值