-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASHIGIFT.cpp
More file actions
60 lines (54 loc) · 1.16 KB
/
Copy pathASHIGIFT.cpp
File metadata and controls
60 lines (54 loc) · 1.16 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
struct triplet{
lli p, q, r;
};
bool compare(const triplet &a, const triplet &b){
return a.p <= b.p;
}
lli f(lli num, vector<triplet> v){
for(int i=0;i<v.size();i++)
if(v[i].q == -1) num -= v[i].r;
else if(v[i].q <= num) num += v[i].r;
return num;
}
lli lowerBound(lli k, lli low, lli high, vector<triplet> v){
while(low<high){
lli mid = low + (high-low)/2;
if(k<=f(mid, v)) high = mid;
else low = mid+1;
}
return low;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t, X, B, C;
lli x, y, p, q, r;
cin>>t;
while(t--){
lli ans = 0;
vector<triplet> c;
cin>>X>>B;
for(int i=0;i<B;i++){
cin>>x>>y;
c.push_back({x, -1, y});
}
cin>>C;
for(int i=0;i<C;i++){
cin>>p>>q>>r;
c.push_back({p, q, r});
}
if(C==0){
for(int i=0;i<B;i++) ans += c[i].r;
cout<<ans+1<<"\n";
}
else{
// main logic
sort(c.begin(), c.end(), compare);
cout<<lowerBound(1, 1, LONG_MAX, c)<<"\n";
}
}
return 0;
}