QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#356683#8301. Hold the LineFOY#ML 4409ms216332kbC++141.8kb2024-03-18 09:16:262024-03-18 09:16:27

Judging History

你现在查看的是最新测评结果

  • [2024-03-18 09:16:27]
  • 评测
  • 测评结果:ML
  • 用时:4409ms
  • 内存:216332kb
  • [2024-03-18 09:16:26]
  • 提交

answer

#include <vector>
#include <set>
#include <iostream>

using namespace std;
using ll = long long;
const ll inf = -1e12;
struct X {
	set<ll> vals;

	X operator+(X rhs) {
		set<ll> out = {vals.begin(), vals.end()};
		for (ll j : rhs.vals) out.insert(j);
		return { out };
	}
};

ll closest(ll x, set<ll> &a) {
	auto u = a.upper_bound(x);
	auto l = a.lower_bound(x);
	ll ans = -inf;
	if (u != a.begin()) 
		ans = *prev(u);
	if (l != a.end()) 
		if (abs(ans-x) > abs(*l-x)) ans = *l;
	return ans;
}

ll closest(ll x, ll y, ll z) {
	if (abs(x-y) < abs(x-z)) return y;
	else return z;
}

struct Segtree {
    ll n;
    vector<X> tree;
    Segtree(ll n): n(n), tree(4*n) {
    }

    void insert(ll idx, ll val, ll cl = 0, ll cr = -1, ll k = 1) {
        if (cr == -1) cr = n;
				tree[k].vals.insert(val);
				if (cl == cr-1) return;
        ll mid = (cl+cr)/2;
        if (idx < mid) insert(idx, val, cl, mid, k*2);
        else insert(idx, val, mid, cr, k*2+1);
    }

    ll act(ll l, ll r, ll targ, ll cl = 0, ll cr = -1, ll k = 1) {
        if (cr == -1) cr = n;
        if (cr <= l || r <= cl) return -inf;
        if (l <= cl && r >= cr) {
					return closest(targ, tree[k].vals);
        }
        ll mid = (cl+cr)/2;
				ll a = act(l, r, targ, cl, mid, k*2);
				ll b = act(l, r, targ, mid, cr, k*2+1);
        return closest(targ, a, b);
    }
};

void solve() {
	int n, m; cin >> n >> m;
	Segtree s(n);
	for (int i = 0; i < m; i++) {
		ll type; cin >> type;
		if (type == 0) {
			ll x, h; cin >> x>> h;
			s.insert(x-1, h);
		}
		else {
			ll l, r, h; cin >> l >> r >> h;
			l--;
			ll ans = s.act(l, r, h);
			if (ans == -inf) cout << -1 << endl;
			else cout << abs(ans-h) << endl;
		}
	}

}

int main() {
	int t; cin >> t;
	while (t--) solve();

}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3620kb

input:

1
3 5
1 1 3 2
0 1 1
0 3 3
1 1 2 2
1 2 3 2

output:

-1
1
1

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 904ms
memory: 3652kb

input:

3000
100 200
0 59 64091111
1 10 94 45205032
0 41 67249140
1 15 93 79570187
0 51 83215051
1 3 22 20062363
0 21 5188814
1 43 94 79642299
0 73 39313603
1 43 67 17001771
0 65 10784990
1 51 69 73368509
0 42 57377517
1 36 49 17483147
0 40 67280095
1 3 41 25139505
0 56 22833553
1 26 65 15640242
0 22 189761...

output:

18886079
12321047
-1
3572752
47089340
9277398
39894370
19950691
4855252
2221206
1596905
-1
3120922
34260194
3353597
-1
2499997
-1
15114024
1193064
49448136
734969
3981124
4159424
5836824
61155540
5163746
-1
283130
3982548
-1
-1
-1
9647216
2356693
8711627
379947
70230536
2637615
7856589
153976
148089...

result:

ok 700000 lines

Test #3:

score: 0
Accepted
time: 1327ms
memory: 4320kb

input:

300
1000 2000
0 421 19938
1 153 254 35195
0 567 74665
1 88 371 61709
0 689 57559
1 39 744 67718
0 770 25816
1 576 955 75098
0 215 17619
1 133 133 29547
0 207 25038
1 929 965 45024
0 820 40726
1 245 505 82535
0 52 99669
1 631 819 77027
0 436 69966
1 102 243 65413
0 878 73531
1 331 759 23736
0 698 594...

output:

-1
-1
6947
17539
-1
-1
62597
19468
40375
3798
45299
-1
-1
11815
-1
-1
-1
6706
-1
-1
-1
9628
1883
-1
1822
-1
972
978
818
-1
3362
1758
53092
-1
712
-1
16697
-1
-1
1592
11462
-1
24068
12896
335
964
2836
29744
501
-1
-1
2298
1859
-1
6311
10290
2543
1589
838
920
7210
719
631
2781
-1
59401
2809
77412
1149...

result:

ok 700000 lines

Test #4:

score: 0
Accepted
time: 1991ms
memory: 12232kb

input:

30
10000 20000
0 6444 22597278
1 940 8167 40648977
0 630 71321002
1 4054 4055 30762754
0 303 59383865
1 3410 3454 1633609
0 3376 42435219
1 6856 7397 92892411
0 1534 14886520
1 474 1932 21770410
0 9387 10819286
1 1640 1726 34316178
0 7331 75627104
1 8763 8764 83586695
0 3923 78696653
1 5016 5016 923...

output:

18051699
-1
-1
-1
6883890
-1
-1
-1
44912717
2247991
-1
5148557
-1
4713193
6643123
2730913
-1
6589982
-1
-1
-1
-1
-1
3691582
-1
1774051
-1
41333276
-1
1422761
-1
-1
-1
-1
895071
-1
692358
-1
2207326
21917947
3850486
-1
53145894
-1
2896385
45348895
3875216
-1
2503573
514164
-1
-1
-1
10502418
-1
458238...

result:

ok 700000 lines

Test #5:

score: 0
Accepted
time: 4409ms
memory: 216332kb

input:

3
100000 200000
0 77354 7
1 24769 44491 1
0 75190 6
1 3816 98172 1
0 45000 4
1 54504 97112 6
0 27855 3
1 53289 54534 9
0 87688 1
1 13220 13577 1
0 31245 7
1 4547 4547 3
0 43311 1
1 429 429 6
0 30798 2
1 28708 84952 4
0 99958 4
1 22719 22734 6
0 46564 3
1 1612 1664 7
0 95692 9
1 77806 93572 9
0 91654...

output:

-1
5
0
-1
-1
-1
-1
0
-1
-1
8
1
-1
-1
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
2
-1
-1
-1
-1
-1
3
2
-1
-1
-1
-1
-1
-1
-1
1
-1
1
0
-1
2
0
2
2
-1
-1
-1
-1
-1
-1
-1
-1
-1
6
-1
2
0
-1
5
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
0
1
-1
-1
0
0
-1
4
8
-1
-1
0
-1
0
-1
-1
-1
-1
-1
-1
-1
1
0
0
-1
-1
-1
0
-1
-1
-1...

result:

ok 500000 lines

Test #6:

score: -100
Memory Limit Exceeded

input:

1
500000 1000000
0 500000 10611289
1 449739 449917 13213816
0 1 94492701
1 21362 21393 55157967
0 499999 22844866
1 188062 188899 88627032
0 2 16392895
1 436969 437010 47518757
0 499998 74741014
1 339202 339203 89522398
0 3 97448236
1 351554 351622 37177728
0 499997 93234547
1 104463 104504 40778310...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result: