-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBennyAndSegments.c
11577 lines (11417 loc) · 149 KB
/
BennyAndSegments.c
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* All Tracks --> Algorithms --> Sorting --> Bubble Sort --> Problem --> Benny and Segments
Tag(s): Easy-Medium, Sorting, Two pointer
View Russian Translation
One day Benny was walking and realized that her life was boring. Everything was grey, even roads in the best park were grey.
Therefore she decided to make roads a little bit brighter. She know that every road in the park is a segment laying on the X axis with coordinates Xl, Xr (Xl <= Xr).
Roads may intersect or overlap.
She chooses any subset of roads and paints them in red. After that she wants to get one continuous red segment.
As she really likes number L the length of this segment has to be equal to L.
Your task is to determine if it is possible to choose some subset of roads and paint them to get one red segment with the length equal to L?
If it's possible print in a single line "Yes"(without quotes), otherwise print "No" (without quotes).
Input format
The first line contains one integer T - the number of test cases.
Each test case starts with two integers N and L, denoting the number of roads and Benny's favorite number L.
The next N lines contain two integers Xl, Xr, denoting the left and right borders of the road.
Output format
For every test case output "Yes" if it is possible to paint some roads and "No" otherwise.
Constraints:
1 <= sum of all N <= (2 * 10^3)
1 <= L <= 10^6
1 <= Xl <= Xr <= 10^6
1 <= N <= 20, 1 <= Xl <= Xr <= 200, holds for test cases worth 10% of the problem's score.
1 <= N <= 100, 1 <= Xl <= Xr <= 200, holds for test cases worth 20% of the problem's score.
Sample Explanation
In the first test case you can choose roads (1; 2) (2; 3) and (3; 4) the result segment is (1; 4) and its length equals 3 (4 - 1 = 3).
In the second case you can not choose any subset that will create segment with the length equal to 4.
SAMPLE INPUT
2
5 3
1 2
2 3
3 4
1 5
2 6
2 3
1 2
2 6
SAMPLE OUTPUT
Yes
No
//My C Solution:
#include <stdio.h>
#define LEN 2001
#define MAX(a,b) (a>b)?(a):(b)
int main(){
int Xi[LEN],Xj[LEN],i,j,N,L,tempi=0,tempj=0,T,maxR=0,flag=0,curR=0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&L);
for(i=0;i<N;i++){
scanf("%d",&Xi[i]);
scanf("%d",&Xj[i]);
}
for(i=0;i<N;i++){
flag=0;
for(j=i+1;j<N;j++){
if((Xi[i]>Xi[j]) && (Xj[i]>Xj[j])){
tempi= Xi[i];
Xi[i]=Xi[j];
Xi[j]=tempi;
tempj= Xj[i];
Xj[i]=Xj[j];
Xj[j]=tempj;
flag=1;
}
}
if(!flag)
break;
}
maxR=curR=0;
flag=0;
for(i=0;i<N;i++){
maxR = (Xi[i] + L);
curR = Xj[i];
for(j=i+1;j<N;j++){
if((Xi[j]<=curR) && (Xi[j]>Xi[i]) && (Xj[j]<=maxR)){
curR = MAX(curR, Xj[j]);
}
}
if (curR==maxR){
printf("Yes\n");
flag=1;
break;
}
}
if(!flag)
printf("No\n");
}
return 0;
}
//My Python Solution:
for _ in range(int(input())):
n,l = list(map(int, input().split()))
d = []
for i in range(n):
d.append(tuple(map(int, input().split())))
d.sort(key=lambda x: x[0])
#print(d)
for i in range(n):
maxR = d[i][0] + l
curR = d[i][1]
for j in range(i+1,n):
if(d[j][0]<=curR and d[j][0]>d[i][0] and d[j][1]<=maxR):
curR = max(curR, d[j][1])
if(curR==maxR):
print("Yes")
break
else:
print("No")
*/
/*Editorial:
Let's try every possible left point of the answer. Obviously it must one of the Xl. The right end will be Xl + L.
Now we need to check if there exist some subset of roads that there union equals to some segment [A , B].
Let's find the union of all roads that lie inside the segment [A, B].
If it's equal to [A, B] then we find the answer, otherwise it's impossible to get such segment.
Why? All other roads obviously can't be used in union, so we can only delete several roads.
And the exist a point inside segment [A, B] that is not covered by any road, so we won't cover this point after deleting some roads.
How to find union of roads? Let's sort all roads by their left coordinate. Now look at all roads one by one and build their union.
If current left end of road is bigger than right end of union we can say that points between them aren't covered by any road,
so union of roads can't be equal to [A, B]. Otherwise we need to update current right end of union.
Also it is necessary to check if points A and B is covered by any road.
We can sort all roads only once, so complexity of this solution will be O(n^2).
You can check solutions by setter and tester for implementation details.
Author Solution by Bandarchuk Yury
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <utility>
#include <vector>
#include <set>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
const int N = 2020;
int n, L;
int main() {
int t;
cin >> t;
while(t--) {
scanf("%d%d", &n, &L);
pair<int, int> a[N];
int flag = 0;
for (int i = 0; i < n; i++) {
scanf("%d%d", &a[i].F, &a[i].S);
}
sort(a, a + n);
for (int i = 0; i < n; i++) {
int maxRight = a[i].F + L;
int curRight = a[i].S;
for (int j = 0; j < n; j++) {
if (a[j].F <= curRight && a[j].F > a[i].F && a[j].S <= maxRight) {
curRight = max(curRight, a[j].S);
}
}
if (curRight == maxRight) {
flag = 1;
break;
}
}
if(flag == 1)
puts("Yes");
else
puts("No");
}
return 0;
}
Another Solution:
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define mod 1000000007
#define ll long long
#define FOR(X,Y) for(ll X = 0; X < Y; ++X)
#define inp(X) scanf("%lli",&X)
#define prn(X) printf("%lli",X);
typedef struct block
{
ll x1,x2;
}block;
int cmp(const void*a, const void*b)
{
block x = *(block*)a;
block y = *(block*)b;
if(x.x1 == y.x1)
return x.x2 - y.x2;
return x.x1 - y.x1;
}
typedef struct chunk{
int i,j, size;
}chunk;
int main(void)
{
ll t;
inp(t);
FOR(tt,t)
{
ll n,l;
inp(n), inp(l);
block arr[n];
FOR(i,n)
inp(arr[i].x1),inp(arr[i].x2);
qsort(arr,n,sizeof(block), cmp);
int s = -1;
ll end = -1;
chunk brr[2345];
int c_size=0,flag=0;
for(int i=0;i<n;++i){
if(s==-1){
s = i;
end = arr[i].x2;
}
else if(arr[i].x1 > end){
brr[c_size].i = s;
brr[c_size].j = i-1;
brr[c_size].size = (i-1-s+1);
++c_size;
s = i;
end = arr[i].x2;
}
}
brr[c_size].i = s;
brr[c_size].j = n-1;
brr[c_size].size = (n-1-s+1);
++c_size;
for(int i = 0; i < c_size; ++i){
for(int o=brr[i].i;o<= brr[i].j; ++o){
// printf("%lli %lli\n",arr[o].x1,arr[o].x2);
if(arr[o].x2 > arr[o].x1+l)
continue;
if(arr[o].x2 == arr[o].x1+l)
{
flag=1;
break;
}
for(int p = o+1; p<=brr[i].j+1; ++p){
// printf("%lli %lli\n",arr[p].x1,arr[p].x2);
if(arr[p].x2 == arr[o].x1+l)
{
flag=1;
break;
}
}
// puts("");
if(flag==1)
break;
}
if(flag)
break;
}
puts(flag ? "Yes" : "No");
}
return 0;
}
Tester Solution by Roman Bilyi
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:16777216")
#include <string>
#include <vector>
#include <map>
#include <list>
#include <iterator>
#include <cassert>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
#include <time.h>
#include <complex>
using namespace std;
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, b, a) for(int i=(b)-1;i>=(a);--i)
#define FILL(A,value) memset(A,value,sizeof(A))
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define Pi 3.14159265358979
#define x0 ikjnrmthklmnt
#define y0 lkrjhkltr
#define y1 ewrgrg
typedef long long Int;
typedef unsigned long long UInt;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<Int, Int> PLL;
typedef pair<double, double> PDD;
typedef complex<double> base;
const int INF = 1000000000;
const int BASE = 1000000007;
const int MAX = 100007;
const int MAX2 = 7777;
const int MAXE = 100000;
const int ADD = 1000000;
const int MOD = 1000000007;
const int CNT = 800;
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("distance.in", "r", stdin);
//freopen("distance.out", "w", stdout);
//freopen("out.txt" , "w" , stdout);
int t;
cin >> t;
int sum = 0;
FOR(tt,0,t)
{
int N , L;
cin >> N >> L;
sum += N;
assert(L <= 1000000);
assert(L >= 1);
vector<PII> A;
FOR(i,0,N)
{
int l , r;
cin >> l >> r;
assert(l >= 1 && r >= l && r <= 1000000);
A.push_back(MP(l, r));
}
sort(ALL(A));
bool isPossible = false;
FOR(i,0,N)
{
int r = A[i].first;
bool ok = 1;
FOR(j,0,N)
{
if (A[j].first >= A[i].first && A[j].second <= A[i].first + L)
{
if (A[j].first > r)
{
ok = 0;
}
else
{
r = max(r , A[j].second);
}
}
}
if (r != A[i].first + L)
{
ok = 0;
}
isPossible |= ok;
}
if (isPossible) cout << "Yes" << endl;
else cout << "No" << endl;
}
assert(sum >= 1 && sum <= 2000);
return 0;
}
*/
/*Best Submissions:
C:
#include <stdio.h>
void bubble(int a[],int n);
int main()
{
int t;
scanf("%d",&t);
while(t>0)
{
int n,l;
scanf("%d%d",&n,&l);
int a[n][2];
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
int flag=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j][1]-a[i][0]==l&&a[j][0]>=a[i][0]&&a[i][1]<=a[j][1])
{
flag++;
break;
}
}
}
if(flag==0)
printf("No\n");
else
printf("Yes\n");
t--;
}
return 0;
}
C++:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int main()
{
int testCase;
cin>>testCase;
while(testCase>0)
{
int L; //longeur pref
int N; // nb de chemins
cin>>N>>L;
vector<long> debut;
vector<long> fin;
// section pour mettre les coordonnées dans 2 vecteurs
for(int i = 0; i<N;i++)
{
int tempo1;
int tempo2;
cin>>tempo1>>tempo2;
if(tempo1==tempo2)
{
continue;
}
else
{
debut.push_back(tempo1);
fin.push_back(tempo2);
}
}
//section de classement
int i, j;
for (i = 0; i < debut.size()-1; i++)
{
// Last i elements are already in place
for (j = 0; j < fin.size()-i-1; j++)
{
if (fin[j] > fin[j+1])
{
swap(debut[j], debut[j+1]);
swap(fin[j], fin[j+1]);
}
}
}
bool flag=false;
for(int z=0; z<fin.size();z++)
{
for(int k=z; k<fin.size();k++)
{
if(fin[k]-debut[z]==L && debut[k]>=debut[z])
{
if(k==z)
{
flag=true;
break;
}
int count=0;
for(int l=k;l>z;l--)
{
if(debut[l]<=fin[l-1])
{
count++;
}
else
{
break;
}
}
if(count==k-z)
{
flag=true;
break;
}
}
if(fin[k]-debut[z]>L || flag==true)
{
break;
}
}
if(flag==true)
{
break;
}
}
if(flag==true)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
testCase--;
}
return 0;
}
C++14:
#pragma GCC optimize ("O3")
//#define ANDREIKKAA_TOPCODER
//#define ANDREIKKAA_ALLOCATOR
#define ANDREIKKAA_CLASS Solution
#define ANDREIKKAA_METHOD solve
#define ANDREIKKAA_PARAMETERS void
#define ANDREIKKAA_CALL
#define ANDREIKKAA_RETURN_TYPE void
#include "bits/stdc++.h"
using namespace std;
#define x first
#define y second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef double ld;
const ld PI = acos(-1);
const int _ML = 500;
const char _inpf[] =
#if defined(ANDREIKKAA)
"input.txt"
#else
""
#endif
;
const char _outf[] =
#if defined(ANDREIKKAA)
""
#else
""
#endif
;
#if defined(ANDREIKKAA_ALLOCATOR)
char _mem[_ML * 1024LL * 1024LL];
size_t _ptr = 0;
inline void* operator new (size_t _x) { _ptr += _x; return _mem + _ptr - _x; }
inline void operator delete (void*) { }
#endif
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
bool f(vector<pair<int, int>> a, int l, int r) {
vector<pair<int, bool>> ev;
for(const auto &i : a) {
if(i.x < l || r < i.y) {
continue;
}
ev.push_back({i.x, false});
ev.push_back({i.y, true});
}
sort(all(ev));
//cout << ev << endl;
int bal = 0;
bool hui = false;
if(ev.empty() || ev.front().x != l || ev.back().x != r) {
//cout << "her" << endl;
return false;
}
for(const auto &i : ev) {
//cout << hui << " " << bal << " " << i << endl;
if(hui && bal == 0) {
return false;
}
hui = true;
if(i.y) {
--bal;
} else {
++bal;
}
}
return true;
}
void solve() {
int n, l;
cin >> n >> l;
vector<pair<int, int>> a(n);
cin >> a;
sort(all(a));
for(int i = 0; i < n; ++i) {
if(f(a, a[i].x, a[i].x + l)) {
cout << "Yes" << endl;
return;
}
}
cout << "No" << endl;
}
inline ANDREIKKAA_RETURN_TYPE mainFunction(ANDREIKKAA_PARAMETERS)
{
int t;
cin >> t;
for(int i = 0; i < t; ++i) {
solve();
}
}
#if defined(ANDREIKKAA) || !defined(ANDREIKKAA_TOPCODER)
int main()
{
#if defined(ANDREIKKAA)
time_t _start = clock();
#endif
if (_inpf[0] != '\0')
assert(freopen(_inpf, "r", stdin) != nullptr);
if (_outf[0] != '\0')
assert(freopen(_outf, "w", stdout) != nullptr);
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << setprecision(20);
//cout << fixed;
mainFunction(ANDREIKKAA_CALL);
#if defined(ANDREIKKAA)
cout << "Time: " << (clock() - _start) / (ld)CLOCKS_PER_SEC << endl;
#endif
}
#else
class ANDREIKKAA_CLASS { public: ANDREIKKAA_RETURN_TYPE ANDREIKKAA_METHOD(ANDREIKKAA_PARAMETERS) { return mainFunction(ANDREIKKAA_CALL); } };
#endif
C#:
using System;
using System.Numerics;
using System.Collections;
namespace myName{
public class Segment: IComparable {
public Segment(int a, int b){
A = a;
B = b;
}
public int Length(){
return this.B - this.A;
}
public int A{
get; set;
}
public int B{
get; set;
}
int IComparable.CompareTo(object obj) {
Segment segment = (Segment) obj;
int compare = this.A.CompareTo(segment.A);
if(compare == 0){
compare = this.B.CompareTo(segment.B);
}
return compare;
}
}
class MyClass {
static void Main(string[] args) {
int t,n,l;
string[] s;
t = Int32.Parse(Console.ReadLine());
ArrayList arr;
Segment seg = new Segment(0, 0);
for(int i = 0; i < t; i++){
s = Console.ReadLine().Split(' ');
n = Int32.Parse(s[0]);
l = Int32.Parse(s[1]);
arr = new ArrayList(n);
bool exact = false;
for(int j = 0; j < n; j++){
s = Console.ReadLine().Split(' ');
seg = new Segment(Int32.Parse(s[0]), Int32.Parse(s[1]));
if(seg.B - seg.A == l) {
exact = true;
}
if(seg.B - seg.A < l) {
arr.Add(seg);
}
}
if(exact){
Console.WriteLine("Yes");
continue;
}
arr.Sort();
int index = 0;
Segment range = new Segment(0, 0);
while(index < arr.Count -1){
range = (Segment) arr[index];
for(int j = index + 1; j < arr.Count; j++){
seg =(Segment) arr[j];
if (range.B < seg.A){
range.A = seg.A;
range.B = seg.B;
index = j;
break;
}
if(seg.B - range.A == l){
index = arr.Count;
range.B = seg.B;
break;
}
if(seg.B - range.A < l){
range.B = seg.B;
continue;
}
}
index++;
}
if(range.Length() == l){
Console.WriteLine("Yes");
} else {
Console.WriteLine("No");
}
}
}
}
}
JAVA:
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.HashMap;
import java.util.InputMismatchException;
public class Hackerrank{
public static InputStream inputStream = System.in;
public static OutputStream outputStream = System.out;
public static FastReader in = new FastReader(inputStream);
public static PrintWriter out = new PrintWriter(outputStream);
public static void main(String[] args)throws java.lang.Exception
{
new Hackerrank().run();
out.close();
}
void run() throws java.lang.Exception
{
for(int T = ni();T>0;T--)
{
int n = ni();
int l = ni();
Pair p[] = new Pair[n];
for(int i = 0; i < n; i++)
p[i] = new Pair(ni() , ni());
Arrays.sort(p);
//System.out.println(Arrays.deepToString(p));
boolean yes = false;
for(int i = 0; i < n; i++){
if(p[i].y - p[i].x <= l){
int X = p[i].x;
int Y = p[i].y;
for(int j = i + 1; j < n; j++){
if(p[j].x <= Y && p[j].y <= p[i].x + l){
Y = Math.max(Y, p[j].y);
}
}
if(Y == X + l){
yes = true;
break;
}
}
}
out.println(yes ? "Yes" : "No");
}
}
static class Pair implements Comparable<Pair>{
int x,y;
Pair(int x,int y){ this.x = x; this.y = y; }
public int compareTo(Pair o){
if(x != o.x)
return Integer.compare(x, o.x);
return Integer.compare(y, o.y);
}
public String toString(){
return x + " " + y;
}
}
private static int ni(){
return in.nextInt();
}
private static long nl(){
return in.nextLong();
}
private static String ns(){
return in.nextString();
}
private static char nc(){
return in.nextCharacter();
}
private static double nd(){
return in.nextDouble();
}
private static char[] ns(int n)
{
char[] a = new char[n];
for(int i=0;i<n;i++) a[i] = nc();
return a;
}
private static char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i=0;i<n;i++) map[i] = ns(m);
return map;
}
private static int[] na(int n)
{
int[] a = new int[n];
for(int i=0;i<n;i++) a[i] = ni();
return a;
}
private static long[] nal(int n)
{
long[] a = new long[n];
for(int i=0;i<n;i++) a[i] = nl();
return a;
}
}
class FastReader{
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastReader(InputStream stream){
this.stream = stream;
}
public int read(){
if (numChars == -1){
throw new InputMismatchException ();
}
if (curChar >= numChars){
curChar = 0;
try{
numChars = stream.read (buf);
} catch (IOException e){
throw new InputMismatchException ();
}
if (numChars <= 0){
return -1;
}
}
return buf[curChar++];
}
public int peek(){
if (numChars == -1){
return -1;
}
if (curChar >= numChars){
curChar = 0;
try{
numChars = stream.read (buf);
} catch (IOException e){
return -1;
}
if (numChars <= 0){
return -1;
}
}
return buf[curChar];
}
public int nextInt(){
int c = read ();
while (isSpaceChar (c))
c = read ();