#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define lc v << 1
#define rc (v << 1) + 1
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
typedef complex<double> cd;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }
const int MX_BIT = 600 * 1000 + 5;
const int MX = 60 * 1000 + 5;
int l[MX], r[MX], s[MX];
set<int> sp2id[MX];
bool used[MX];
int n, k;
int dfs(int sp, int i, int parent) {
used[sp] = true;
int sp2 = sp == l[i] ? r[i] + n : l[i];
int i2 = *sp2id[sp2].begin() == i ? *sp2id[sp2].rbegin() : *sp2id[sp2].begin();
if(sp2 == parent)
return ((sp < n) ? s[i] : -s[i]);
return ((sp < n) ? s[i] : -s[i]) + dfs(sp2, i2, parent);
}
void solve() {
cin >> n >> k;
forn(i, 2 * n) {
cin >> l[i] >> r[i] >> s[i];
l[i]--; r[i]--;
sp2id[l[i]].insert(i);
sp2id[r[i] + n].insert(i);
}
set<ii> q;
forn(sp, 2 * n) {
q.insert({sz(sp2id[sp]), sp});
}
int diff = 0;
vi diffs;
while(!q.empty() && q.begin()->F < 2) {
int ss = q.begin()->F, sp = q.begin()->S;
if(ss == 0) {
cout << "NO\n";
return;
}
int i = *sp2id[sp].begin();
int sp2 = sp == l[i] ? r[i] + n : l[i];
q.erase({sz(sp2id[sp]), sp});
q.erase({sz(sp2id[sp2]), sp2});
sp2id[sp].erase(i);
sp2id[sp2].erase(i);
q.insert({sz(sp2id[sp2]), sp2});
if(sp < n) {
diff += s[i];
}
else {
diff -= s[i];
}
}
forn(sp, 2 * n) {
if(!used[sp] && sz(sp2id[sp]) == 2) {
int i = *sp2id[sp].begin();
int j = *sp2id[sp].rbegin();
int d1 = dfs(sp, i, sp);
int d2 = dfs(sp, j, sp);
if(d2 < d1)
swap(d1, d2);
diff += d1;
diffs.pb(d2 - d1);
}
}
bitset<MX_BIT> b;
b[0] = 1;
for(int d: diffs) {
b |= b << d;
}
for(int s = b._Find_first(); s < sz(b); s = b._Find_next(s)) {
if(abs(diff + s) <= k) {
cout << "YES\n";
return;
}
}
cout << "NO\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// freopen("triangles.in", "r", stdin);
// freopen("triangles.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
2 ms |
3284 KB |
Output is correct |
4 |
Correct |
2 ms |
3284 KB |
Output is correct |
5 |
Correct |
2 ms |
3284 KB |
Output is correct |
6 |
Correct |
2 ms |
3284 KB |
Output is correct |
7 |
Correct |
2 ms |
3284 KB |
Output is correct |
8 |
Correct |
2 ms |
3284 KB |
Output is correct |
9 |
Correct |
2 ms |
3284 KB |
Output is correct |
10 |
Correct |
2 ms |
3284 KB |
Output is correct |
11 |
Correct |
2 ms |
3284 KB |
Output is correct |
12 |
Correct |
2 ms |
3284 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
2 ms |
3284 KB |
Output is correct |
15 |
Correct |
2 ms |
3284 KB |
Output is correct |
16 |
Correct |
2 ms |
3284 KB |
Output is correct |
17 |
Correct |
2 ms |
3284 KB |
Output is correct |
18 |
Correct |
2 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3284 KB |
Output is correct |
20 |
Correct |
2 ms |
3284 KB |
Output is correct |
21 |
Correct |
2 ms |
3284 KB |
Output is correct |
22 |
Correct |
2 ms |
3284 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
2 ms |
3284 KB |
Output is correct |
25 |
Correct |
2 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
2 ms |
3284 KB |
Output is correct |
4 |
Correct |
2 ms |
3284 KB |
Output is correct |
5 |
Correct |
2 ms |
3284 KB |
Output is correct |
6 |
Correct |
2 ms |
3284 KB |
Output is correct |
7 |
Correct |
2 ms |
3284 KB |
Output is correct |
8 |
Correct |
2 ms |
3284 KB |
Output is correct |
9 |
Correct |
2 ms |
3284 KB |
Output is correct |
10 |
Correct |
2 ms |
3284 KB |
Output is correct |
11 |
Correct |
2 ms |
3284 KB |
Output is correct |
12 |
Correct |
2 ms |
3284 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
2 ms |
3284 KB |
Output is correct |
15 |
Correct |
2 ms |
3284 KB |
Output is correct |
16 |
Correct |
2 ms |
3284 KB |
Output is correct |
17 |
Correct |
2 ms |
3284 KB |
Output is correct |
18 |
Correct |
2 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3284 KB |
Output is correct |
20 |
Correct |
2 ms |
3284 KB |
Output is correct |
21 |
Correct |
2 ms |
3284 KB |
Output is correct |
22 |
Correct |
2 ms |
3284 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
2 ms |
3284 KB |
Output is correct |
25 |
Correct |
2 ms |
3284 KB |
Output is correct |
26 |
Correct |
38 ms |
3956 KB |
Output is correct |
27 |
Correct |
9 ms |
3992 KB |
Output is correct |
28 |
Correct |
41 ms |
3924 KB |
Output is correct |
29 |
Correct |
9 ms |
3924 KB |
Output is correct |
30 |
Correct |
40 ms |
3924 KB |
Output is correct |
31 |
Correct |
9 ms |
3920 KB |
Output is correct |
32 |
Correct |
45 ms |
3988 KB |
Output is correct |
33 |
Correct |
9 ms |
3992 KB |
Output is correct |
34 |
Correct |
6 ms |
3924 KB |
Output is correct |
35 |
Correct |
9 ms |
3924 KB |
Output is correct |
36 |
Correct |
44 ms |
3924 KB |
Output is correct |
37 |
Correct |
9 ms |
3924 KB |
Output is correct |
38 |
Correct |
38 ms |
3924 KB |
Output is correct |
39 |
Correct |
9 ms |
3924 KB |
Output is correct |
40 |
Correct |
38 ms |
3924 KB |
Output is correct |
41 |
Correct |
9 ms |
3996 KB |
Output is correct |
42 |
Correct |
41 ms |
3924 KB |
Output is correct |
43 |
Correct |
11 ms |
3988 KB |
Output is correct |
44 |
Correct |
38 ms |
3924 KB |
Output is correct |
45 |
Correct |
9 ms |
3924 KB |
Output is correct |
46 |
Correct |
42 ms |
3924 KB |
Output is correct |
47 |
Correct |
4 ms |
3864 KB |
Output is correct |
48 |
Correct |
23 ms |
3988 KB |
Output is correct |
49 |
Correct |
24 ms |
3924 KB |
Output is correct |
50 |
Correct |
40 ms |
4012 KB |
Output is correct |
51 |
Correct |
40 ms |
3984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
6532 KB |
Output is correct |
2 |
Correct |
15 ms |
6356 KB |
Output is correct |
3 |
Correct |
120 ms |
6436 KB |
Output is correct |
4 |
Correct |
14 ms |
6356 KB |
Output is correct |
5 |
Correct |
122 ms |
6468 KB |
Output is correct |
6 |
Correct |
14 ms |
6388 KB |
Output is correct |
7 |
Correct |
120 ms |
6532 KB |
Output is correct |
8 |
Correct |
13 ms |
6380 KB |
Output is correct |
9 |
Correct |
136 ms |
6548 KB |
Output is correct |
10 |
Correct |
12 ms |
6344 KB |
Output is correct |
11 |
Correct |
119 ms |
6604 KB |
Output is correct |
12 |
Correct |
13 ms |
6356 KB |
Output is correct |
13 |
Correct |
124 ms |
6528 KB |
Output is correct |
14 |
Correct |
118 ms |
6484 KB |
Output is correct |
15 |
Correct |
13 ms |
6324 KB |
Output is correct |
16 |
Correct |
126 ms |
6532 KB |
Output is correct |
17 |
Correct |
13 ms |
6356 KB |
Output is correct |
18 |
Correct |
118 ms |
6484 KB |
Output is correct |
19 |
Correct |
14 ms |
6356 KB |
Output is correct |
20 |
Correct |
123 ms |
6476 KB |
Output is correct |
21 |
Correct |
13 ms |
6356 KB |
Output is correct |
22 |
Correct |
73 ms |
6484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
2 ms |
3284 KB |
Output is correct |
4 |
Correct |
2 ms |
3284 KB |
Output is correct |
5 |
Correct |
2 ms |
3284 KB |
Output is correct |
6 |
Correct |
2 ms |
3284 KB |
Output is correct |
7 |
Correct |
2 ms |
3284 KB |
Output is correct |
8 |
Correct |
2 ms |
3284 KB |
Output is correct |
9 |
Correct |
2 ms |
3284 KB |
Output is correct |
10 |
Correct |
2 ms |
3284 KB |
Output is correct |
11 |
Correct |
2 ms |
3284 KB |
Output is correct |
12 |
Correct |
2 ms |
3284 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
2 ms |
3284 KB |
Output is correct |
15 |
Correct |
2 ms |
3284 KB |
Output is correct |
16 |
Correct |
2 ms |
3284 KB |
Output is correct |
17 |
Correct |
2 ms |
3284 KB |
Output is correct |
18 |
Correct |
2 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3284 KB |
Output is correct |
20 |
Correct |
2 ms |
3284 KB |
Output is correct |
21 |
Correct |
2 ms |
3284 KB |
Output is correct |
22 |
Correct |
2 ms |
3284 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
2 ms |
3284 KB |
Output is correct |
25 |
Correct |
2 ms |
3284 KB |
Output is correct |
26 |
Correct |
38 ms |
3956 KB |
Output is correct |
27 |
Correct |
9 ms |
3992 KB |
Output is correct |
28 |
Correct |
41 ms |
3924 KB |
Output is correct |
29 |
Correct |
9 ms |
3924 KB |
Output is correct |
30 |
Correct |
40 ms |
3924 KB |
Output is correct |
31 |
Correct |
9 ms |
3920 KB |
Output is correct |
32 |
Correct |
45 ms |
3988 KB |
Output is correct |
33 |
Correct |
9 ms |
3992 KB |
Output is correct |
34 |
Correct |
6 ms |
3924 KB |
Output is correct |
35 |
Correct |
9 ms |
3924 KB |
Output is correct |
36 |
Correct |
44 ms |
3924 KB |
Output is correct |
37 |
Correct |
9 ms |
3924 KB |
Output is correct |
38 |
Correct |
38 ms |
3924 KB |
Output is correct |
39 |
Correct |
9 ms |
3924 KB |
Output is correct |
40 |
Correct |
38 ms |
3924 KB |
Output is correct |
41 |
Correct |
9 ms |
3996 KB |
Output is correct |
42 |
Correct |
41 ms |
3924 KB |
Output is correct |
43 |
Correct |
11 ms |
3988 KB |
Output is correct |
44 |
Correct |
38 ms |
3924 KB |
Output is correct |
45 |
Correct |
9 ms |
3924 KB |
Output is correct |
46 |
Correct |
42 ms |
3924 KB |
Output is correct |
47 |
Correct |
4 ms |
3864 KB |
Output is correct |
48 |
Correct |
23 ms |
3988 KB |
Output is correct |
49 |
Correct |
24 ms |
3924 KB |
Output is correct |
50 |
Correct |
40 ms |
4012 KB |
Output is correct |
51 |
Correct |
40 ms |
3984 KB |
Output is correct |
52 |
Correct |
124 ms |
6532 KB |
Output is correct |
53 |
Correct |
15 ms |
6356 KB |
Output is correct |
54 |
Correct |
120 ms |
6436 KB |
Output is correct |
55 |
Correct |
14 ms |
6356 KB |
Output is correct |
56 |
Correct |
122 ms |
6468 KB |
Output is correct |
57 |
Correct |
14 ms |
6388 KB |
Output is correct |
58 |
Correct |
120 ms |
6532 KB |
Output is correct |
59 |
Correct |
13 ms |
6380 KB |
Output is correct |
60 |
Correct |
136 ms |
6548 KB |
Output is correct |
61 |
Correct |
12 ms |
6344 KB |
Output is correct |
62 |
Correct |
119 ms |
6604 KB |
Output is correct |
63 |
Correct |
13 ms |
6356 KB |
Output is correct |
64 |
Correct |
124 ms |
6528 KB |
Output is correct |
65 |
Correct |
118 ms |
6484 KB |
Output is correct |
66 |
Correct |
13 ms |
6324 KB |
Output is correct |
67 |
Correct |
126 ms |
6532 KB |
Output is correct |
68 |
Correct |
13 ms |
6356 KB |
Output is correct |
69 |
Correct |
118 ms |
6484 KB |
Output is correct |
70 |
Correct |
14 ms |
6356 KB |
Output is correct |
71 |
Correct |
123 ms |
6476 KB |
Output is correct |
72 |
Correct |
13 ms |
6356 KB |
Output is correct |
73 |
Correct |
73 ms |
6484 KB |
Output is correct |
74 |
Correct |
578 ms |
13004 KB |
Output is correct |
75 |
Correct |
80 ms |
13372 KB |
Output is correct |
76 |
Correct |
562 ms |
13728 KB |
Output is correct |
77 |
Correct |
76 ms |
13260 KB |
Output is correct |
78 |
Correct |
568 ms |
13620 KB |
Output is correct |
79 |
Correct |
581 ms |
13660 KB |
Output is correct |
80 |
Correct |
83 ms |
13372 KB |
Output is correct |
81 |
Correct |
78 ms |
13464 KB |
Output is correct |
82 |
Correct |
556 ms |
13636 KB |
Output is correct |
83 |
Correct |
559 ms |
13644 KB |
Output is correct |
84 |
Correct |
86 ms |
13384 KB |
Output is correct |
85 |
Correct |
560 ms |
13624 KB |
Output is correct |
86 |
Correct |
83 ms |
13304 KB |
Output is correct |
87 |
Correct |
557 ms |
13616 KB |
Output is correct |
88 |
Correct |
77 ms |
13320 KB |
Output is correct |
89 |
Correct |
587 ms |
13656 KB |
Output is correct |
90 |
Correct |
77 ms |
13260 KB |
Output is correct |
91 |
Correct |
573 ms |
13620 KB |
Output is correct |
92 |
Correct |
81 ms |
13380 KB |
Output is correct |
93 |
Correct |
574 ms |
13508 KB |
Output is correct |
94 |
Correct |
54 ms |
13260 KB |
Output is correct |
95 |
Correct |
340 ms |
13484 KB |
Output is correct |
96 |
Correct |
337 ms |
13556 KB |
Output is correct |
97 |
Correct |
565 ms |
13728 KB |
Output is correct |
98 |
Correct |
580 ms |
13620 KB |
Output is correct |