Hard-题目28:57. Insert Interval

本文介绍了一个关于区间集合的问题,即如何插入一个新的区间并进行合并化简。通过对比Merge Intervals问题,提供了一种解决方案,包括定义区间比较器,并实现区间插入和合并的功能。

题目原文:
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
题目大意:
给出一个区间集合,插入一个区间并化简。
题目分析:
跟前面的Hard-题目22:56. Merge Intervals是一回事,但是数据强了一些,要把数组开到40000.
源码:(language:java)

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    private class IntervalComparator implements Comparator<Interval> {
        @Override
        public int compare(Interval o1, Interval o2) {
            // TODO Auto-generated method stub
            if(o1.start<o2.start)
                return -1;
            else if(o1.start>o2.start)
                return 1;
            else {
                if(o1.end<o2.end)
                    return -1;
                else if(o1.end>o2.end)
                    return 1;
                else {
                    return 0;
                }
            }
        }

    }
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
        intervals.add(newInterval);
        List<Interval> temp = merge(intervals);
        Collections.sort(temp, new IntervalComparator());
        return temp;
    }
    public List<Interval> merge(List<Interval> intervals) {
        boolean[] x = new boolean[40000];
        boolean[] dots = new boolean[40000];
        int max = Integer.MIN_VALUE,min = Integer.MAX_VALUE;
        List<Interval> list = new ArrayList<Interval>();
        for(Interval interval : intervals) {
            for(int i=interval.start;i<interval.end;i++) {
                x[i]=true;
                if(i<min)
                    min=i;
                if(i>max)
                    max=i;
            }
        }

        int i = min;
        while(i<=max) {
            if(!x[i]) {
                i++;
                continue;
            }
            int start=i,end=start;
            while(x[end])
                end++;
            list.add(new Interval(start,end));
            i=end;

        }
        for(Interval interval : intervals) {
            int start = interval.start,end = interval.end;
            if(start==end && !dots[interval.start]) {
                dots[start] = true;
                if(start==0 && !x[0])
                    list.add(new Interval(start, end));
                else if(!x[start] && !x[start-1])
                    list.add(new Interval(start, end));
            }

        }
        return list;
    }
}

成绩:
9ms,24.58%,5ms,26.17%

现在不会TLE了,但还是WA了: #include <bits/stdc++.h> using namespace std; #define int long long const int INF = 0x3f3f3f3f; const int MAX_N = 2e5 + 50; int T, n, m, a[MAX_N]; #define ls(cur) cur << 1 #define rs(cur) cur << 1 | 1 namespace SegmentTree1 { // the case of a[i] <= x long long sum[MAX_N << 2], vtag[MAX_N << 2]; int cnt[MAX_N << 2], stag[MAX_N << 2]; void pushup(int cur) { sum[cur] = sum[ls(cur)] + sum[rs(cur)]; cnt[cur] = cnt[ls(cur)] + cnt[rs(cur)]; } void mark(int cur, int sign, long long val) { sum[cur] *= sign; sum[cur] += cnt[cur] * val; stag[cur] *= sign; vtag[cur] += val; } void pushdown(int cur) { if (stag[cur] != 1) { mark(ls(cur), stag[cur], 0); mark(rs(cur), stag[cur], 0); stag[cur] = 1; } if (vtag[cur]) { mark(ls(cur), 1, vtag[cur]); mark(rs(cur), 1, vtag[cur]); vtag[cur] = 0; } } void build(int cur, int l, int r) { stag[cur] = 1; vtag[cur] = 0; if (l == r) { sum[cur] = cnt[cur] = 0; return ; } int mid = l + r >> 1; build(ls(cur), l, mid); build(rs(cur), mid + 1, r); pushup(cur); } void insert(int cur, int l, int r, int idx, int val) { if (l == r) { sum[cur] = val; cnt[cur] = 1; return ; } pushdown(cur); int mid = l + r >> 1; if (idx <= mid) insert(ls(cur), l, mid, idx, val); else insert(rs(cur), mid + 1, r, idx, val); pushup(cur); } void modify(int cur, int l, int r, int L, int R, int val) { if (L <= l && r <= R) { mark(cur, -1, val); return ; } pushdown(cur); int mid = l + r >> 1; if (L <= mid) modify(ls(cur), l, mid, L, R, val); if (mid + 1 <= R) modify(rs(cur), mid + 1, r, L, R, val); pushup(cur); } long long query(int cur, int l, int r, int L, int R) { if (L <= l && r <= R) return sum[cur]; pushdown(cur); int mid = l + r >> 1; long long res = 0; if (L <= mid) res += query(ls(cur), l, mid, L, R); if (mid + 1 <= R) res += query(rs(cur), mid + 1, r, L, R); return res; } }; namespace SegmentTree2 { // the case of a[i] > x long long sum[MAX_N << 2], tag[MAX_N << 2]; int cnt[MAX_N << 2], mn[MAX_N << 2]; void pushup(int cur) { sum[cur] = sum[ls(cur)] + sum[rs(cur)]; cnt[cur] = cnt[ls(cur)] + cnt[rs(cur)]; mn[cur] = min(mn[ls(cur)], mn[rs(cur)]); } void mark(int cur, long long val) { sum[cur] -= cnt[cur] * val; mn[cur] -= bool(cnt[cur]) * val; tag[cur] += val; } void pushdown(int cur) { if (tag[cur]) { mark(ls(cur), tag[cur]); mark(rs(cur), tag[cur]); tag[cur] = 0; } } void build(int cur, int l, int r, int val[]) { tag[cur] = 0; if (l == r) { sum[cur] = mn[cur] = val[l]; cnt[cur] = 1; return ; } int mid = l + r >> 1; build(ls(cur), l, mid, val); build(rs(cur), mid + 1, r, val); pushup(cur); } void modify(int cur, int l, int r, int L, int R, int val) { if (l == r && mn[cur] <= val) { SegmentTree1::insert(1, 1, n, l, mn[cur]); sum[cur] = cnt[cur] = 0, mn[cur] = INF; return ; } if (L <= l && r <= R && mn[cur] > val) { mark(cur, val); return ; } pushdown(cur); int mid = l + r >> 1; if (L <= mid) modify(ls(cur), l, mid, L, R, val); if (mid + 1 <= R) modify(rs(cur), mid + 1, r, L, R, val); pushup(cur); } long long query(int cur, int l, int r, int L, int R) { if (L <= l && r <= R) return sum[cur]; pushdown(cur); int mid = l + r >> 1; long long res = 0; if (L <= mid) res += query(ls(cur), l, mid, L, R); if (mid + 1 <= R) res += query(rs(cur), mid + 1, r, L, R); return res; } }; signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> T; while (T--) { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; SegmentTree1::build(1, 1, n); SegmentTree2::build(1, 1, n, a); for (int opt, l, r, x; m--; ) { cin >> opt >> l >> r; if (opt == 1) { cin >> x; SegmentTree2::modify(1, 1, n, l, r, x); SegmentTree1::modify(1, 1, n, l, r, x); } else { long long ans1 = SegmentTree1::query(1, 1, n, l, r); long long ans2 = SegmentTree2::query(1, 1, n, l, r); cout << ans1 + ans2 << '\n'; } } } return 0; }
07-29
分析一下:Line 30048: 03-24 15:41:07.295 5535 6980 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=6 latency=0 timeout=500 status=0 Line 30178: 03-24 15:41:08.149 5535 6980 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=36 latency=0 timeout=500 status=0 Line 30188: 03-24 15:41:08.229 5535 5535 D BluetoothGatt: discoverServices() - device: XX:XX:XX:XX:0B:28 Line 30190: 03-24 15:41:08.235 5535 6980 D BluetoothGatt: onSearchComplete() = Device=EF:B8:39:25:0B:28 Status=0 Line 30203: 03-24 15:41:08.236 5535 5535 D BluetoothGatt: configureMTU() - device: XX:XX:XX:XX:0B:28 mtu: 512 Line 30212: 03-24 15:41:08.238 5535 6980 D BluetoothGatt: onConfigureMTU() - Device=EF:B8:39:25:0B:28 mtu=247 status=0 Line 30220: 03-24 15:41:08.239 5535 5535 D BluetoothGatt: setCharacteristicNotification() - uuid: 00002760-08c2-11e1-9073-0e8ac72e0012 enable: true Line 31018: 03-24 15:41:11.656 5535 6980 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=12 latency=0 timeout=400 status=0 Line 31215: 03-24 15:41:12.591 5535 5535 D BluetoothGatt: setCharacteristicNotification() - uuid: 00002760-08c2-11e1-9073-0e8ac72e0015 enable: true Line 41166: 03-24 15:41:36.464 5535 5535 D BluetoothGatt: cancelOpen() - device: XX:XX:XX:XX:0B:28 Line 41174: 03-24 15:41:36.469 5535 5535 D BluetoothGatt: close() Line 41175: 03-24 15:41:36.469 5535 5535 D BluetoothGatt: unregisterApp() - mClientIf=6 Line 41211: 03-24 15:41:36.482 5535 14917 D BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 connected=false device=EF:B8:39:25:0B:28 Line 60027: 03-24 15:42:25.021 5535 5535 D BluetoothGatt: connect() - device: XX:XX:XX:XX:0B:28, auto: false, eattSupport: false Line 60028: 03-24 15:42:25.021 5535 5535 D BluetoothGatt: registerApp() Line 60029: 03-24 15:42:25.022 5535 5535 D BluetoothGatt: registerApp() - UUID=65f6d13c-55fd-43aa-a124-f2c161c6d710 Line 60036: 03-24 15:42:25.035 5535 26044 D BluetoothGatt: onClientRegistered() - status=0 clientIf=6 Line 60604: 03-24 15:42:25.746 5535 26044 D BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 connected=true device=EF:B8:39:25:0B:28 Line 61034: 03-24 15:42:26.209 5535 26044 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=6 latency=0 timeout=500 status=0 Line 61759: 03-24 15:42:27.358 5535 5535 D BluetoothGatt: discoverServices() - device: XX:XX:XX:XX:0B:28 Line 61851: 03-24 15:42:27.491 5535 26044 D BluetoothGatt: onSearchComplete() = Device=EF:B8:39:25:0B:28 Status=0 Line 61863: 03-24 15:42:27.496 5535 5535 D BluetoothGatt: configureMTU() - device: XX:XX:XX:XX:0B:28 mtu: 512 Line 61881: 03-24 15:42:27.527 5535 26044 D BluetoothGatt: onConfigureMTU() - Device=EF:B8:39:25:0B:28 mtu=247 status=0 Line 61891: 03-24 15:42:27.530 5535 5535 D BluetoothGatt: setCharacteristicNotification() - uuid: 00002760-08c2-11e1-9073-0e8ac72e0012 enable: true Line 61905: 03-24 15:42:27.568 5535 26044 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=36 latency=0 timeout=500 status=0 Line 62298: 03-24 15:42:28.606 5535 5535 D BluetoothGatt: setCharacteristicNotification() - uuid: 00002760-08c2-11e1-9073-0e8ac72e0015 enable: true Line 64132: 03-24 15:42:31.136 5535 5559 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=12 latency=0 timeout=400 status=0
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值