-
Notifications
You must be signed in to change notification settings - Fork 4
/
national-girls-programmin-contest-A.cpp
91 lines (84 loc) · 1.88 KB
/
national-girls-programmin-contest-A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include<vector>
#include<assert.h>
#include<algorithm>
using namespace std;
#define ll long long
ll UB=(ll)(1e18);
vector<ll>v;
int main(){
//freopen("input0.txt", "r", stdin);
//freopen("output0.txt", "w", stdout);
int n, q;
cin>>n>>q;
assert(n >= 1 && n <= 100000);
assert(q >= 1 && q <= 100000);
ll max_val = 0LL;
for(int i = 0; i < n; ++i) {
ll x;
cin>>x;
assert(x <= UB);
max_val = max(max_val, x);
v.push_back(x);
}
sort(v.begin(), v.end());
ll sm = 0LL;
bool flg=false;
while(q--){
ll op,x,y,z;
cin>>op;
if(op == 1){
cin>>x;
if(sm <= max_val) {
sm += x;
}
else flg=1;
}
else{
cin>>x>>y>>z;
if(flg) {
cout<<-1<<endl;
}
else{
int idl = lower_bound( v.begin(), v.end(), x + sm) - v.begin();
int idr = upper_bound( v.begin(), v.end(), y + sm) - v.begin();
if( (idr - idl) >= z) {
cout<<v[idl + z - 1] - sm<<endl;
}
else cout<<-1<<endl;
}
}
}
return 0;
}
// Approach 2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=100005;
ll a[MX];
int main()
{
int n,q,k,t;
ll l,r,x,y=0;
scanf("%d %d",&n,&q);
for(int i=0;i<n;i++) scanf("%lld",&a[i]);
sort(a,a+n);
while(q--){
scanf("%d",&t);
if(t==1){
scanf("%lld",&x);
y+=x;
}
else{
scanf("%lld %lld %d",&l,&r,&k);
l+=y,r+=y;
int i=lower_bound(a,a+n,l)-a;
i+=k-1;
if(i>=n || a[i]>r) i=-1;
ll ans=(i==-1)?-1:a[i]-y;
printf("%lld\n",ans);
}
}
return 0;
}