#include <bits/stdc++.h>
using namespace std;
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
vpii in;
vi endp;
vpii sump;
LLI dist[100001];
LLI bit1[200001],bit2[200001],bit3[200001],bit4[200001];
LLI query(int i,LLI *tree) {
LLI sum = 0;
for (i++; i > 0; i -= i & (-i)) sum += tree[i];
return sum;
}
int update(int i,LLI num,LLI *tree) {
for (i++; i <= endp.size(); i += i & (-i)) tree[i] += num;
return 0;
}
int main() {
int i;
int K,N;
char P,Q;
int S,T;
LLI x = 0;
scanf("%d %d",&K,&N);
for (i = 0; i < N; i++) {
scanf(" %c %d %c %d",&P,&S,&Q,&T);
if (S > T) swap(S,T);
x += T-S;
if (P != Q) in.push_back(make_pair(S,T)),x++;
}
if (K == 1) {
if (in.empty()) printf("%lld\n",x);
else {
for (i = 0; i < in.size(); i++) {
endp.push_back(in[i].first);
endp.push_back(in[i].second);
}
nth_element(endp.begin(),endp.begin()+endp.size()/2,endp.end());
int b = endp[endp.size()/2];
LLI d = 0;
for (i = 0; i < in.size(); i++) {
if (in[i].second < b) d += b-in[i].second;
else if (in[i].first > b) d += in[i].first-b;
}
printf("%lld\n",2*d+x);
}
}
else {
for (i = 0; i < in.size(); i++) {
endp.push_back(in[i].first);
endp.push_back(in[i].second);
sump.push_back(make_pair(in[i].first+in[i].second,i));
}
sort(endp.begin(),endp.end());
endp.resize(unique(endp.begin(),endp.end())-endp.begin());
sort(sump.begin(),sump.end());
for (i = 0; i < sump.size(); i++) {
int p = sump[i].second;
int l = lower_bound(endp.begin(),endp.end(),in[p].first)-endp.begin();
int r = lower_bound(endp.begin(),endp.end(),in[p].second)-endp.begin();
update(l,1,bit1),update(r,1,bit2);
update(l,endp[l],bit3),update(r,endp[r],bit4);
l = 0,r = endp.size()-1;
while (l < r) {
int m = (l+r) / 2;
if ((query(m,bit1)+query(m,bit2)) < (i+1)) l = m+1;
else r = m;
}
dist[i+1] = query(l,bit2)*endp[l] - query(l,bit4);
dist[i+1] += query(endp.size()-1,bit3)-query(l,bit3) \
- (query(endp.size()-1,bit1)-query(l,bit1))*endp[l];
}
fill(bit1,bit1+endp.size()+1,0);
fill(bit2,bit2+endp.size()+1,0);
fill(bit3,bit3+endp.size()+1,0);
fill(bit4,bit4+endp.size()+1,0);
for (i = sump.size()-1; i >= 0; i--) {
int p = sump[i].second;
int l = lower_bound(endp.begin(),endp.end(),in[p].first)-endp.begin();
int r = lower_bound(endp.begin(),endp.end(),in[p].second)-endp.begin();
update(l,1,bit1),update(r,1,bit2);
update(l,endp[l],bit3),update(r,endp[r],bit4);
l = 0,r = endp.size()-1;
while (l < r) {
int m = (l+r) / 2;
if ((query(m,bit1)+query(m,bit2)) < (sump.size()-i)) l = m+1;
else r = m;
}
dist[i] += query(l,bit2)*endp[l] - query(l,bit4);
dist[i] += query(endp.size()-1,bit3)-query(l,bit3) \
- (query(endp.size()-1,bit1)-query(l,bit1))*endp[l];
}
LLI d = dist[0];
for (i = 1; i <= sump.size(); i++) d = min(d,dist[i]);
printf("%lld\n",2*d+x);
}
return 0;
}
Compilation message
bridge.cpp: In function 'int update(int, LLI, LLI*)':
bridge.cpp:55:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i++; i <= endp.size(); i += i & (-i)) tree[i] += num;
~~^~~~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:75:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < in.size(); i++) {
~~^~~~~~~~~~~
bridge.cpp:82:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < in.size(); i++) {
~~^~~~~~~~~~~
bridge.cpp:90:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < in.size(); i++) {
~~^~~~~~~~~~~
bridge.cpp:98:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < sump.size(); i++) {
~~^~~~~~~~~~~~~
bridge.cpp:127:51: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if ((query(m,bit1)+query(m,bit2)) < (sump.size()-i)) l = m+1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:136:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 1; i <= sump.size(); i++) d = min(d,dist[i]);
~~^~~~~~~~~~~~~~
bridge.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&K,&N);
~~~~~^~~~~~~~~~~~~~~
bridge.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c %d %c %d",&P,&S,&Q,&T);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
416 KB |
Output is correct |
4 |
Correct |
3 ms |
472 KB |
Output is correct |
5 |
Correct |
4 ms |
676 KB |
Output is correct |
6 |
Correct |
3 ms |
696 KB |
Output is correct |
7 |
Correct |
3 ms |
732 KB |
Output is correct |
8 |
Correct |
3 ms |
760 KB |
Output is correct |
9 |
Correct |
3 ms |
972 KB |
Output is correct |
10 |
Correct |
3 ms |
972 KB |
Output is correct |
11 |
Correct |
3 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
972 KB |
Output is correct |
2 |
Correct |
2 ms |
972 KB |
Output is correct |
3 |
Correct |
3 ms |
972 KB |
Output is correct |
4 |
Correct |
4 ms |
972 KB |
Output is correct |
5 |
Correct |
3 ms |
972 KB |
Output is correct |
6 |
Correct |
3 ms |
992 KB |
Output is correct |
7 |
Correct |
3 ms |
1008 KB |
Output is correct |
8 |
Correct |
3 ms |
1032 KB |
Output is correct |
9 |
Correct |
2 ms |
1052 KB |
Output is correct |
10 |
Correct |
3 ms |
1076 KB |
Output is correct |
11 |
Correct |
3 ms |
1188 KB |
Output is correct |
12 |
Correct |
39 ms |
4140 KB |
Output is correct |
13 |
Correct |
60 ms |
6512 KB |
Output is correct |
14 |
Correct |
38 ms |
7588 KB |
Output is correct |
15 |
Correct |
46 ms |
7968 KB |
Output is correct |
16 |
Correct |
45 ms |
10760 KB |
Output is correct |
17 |
Correct |
54 ms |
13060 KB |
Output is correct |
18 |
Correct |
49 ms |
15088 KB |
Output is correct |
19 |
Correct |
55 ms |
17296 KB |
Output is correct |
20 |
Correct |
52 ms |
19204 KB |
Output is correct |
21 |
Correct |
49 ms |
21452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
21452 KB |
Output is correct |
2 |
Correct |
3 ms |
21452 KB |
Output is correct |
3 |
Correct |
2 ms |
21452 KB |
Output is correct |
4 |
Correct |
2 ms |
21452 KB |
Output is correct |
5 |
Correct |
2 ms |
21452 KB |
Output is correct |
6 |
Correct |
2 ms |
21452 KB |
Output is correct |
7 |
Correct |
3 ms |
21452 KB |
Output is correct |
8 |
Correct |
3 ms |
21452 KB |
Output is correct |
9 |
Correct |
2 ms |
21452 KB |
Output is correct |
10 |
Correct |
2 ms |
21452 KB |
Output is correct |
11 |
Correct |
3 ms |
21452 KB |
Output is correct |
12 |
Correct |
3 ms |
21452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
21452 KB |
Output is correct |
2 |
Correct |
3 ms |
21452 KB |
Output is correct |
3 |
Correct |
3 ms |
21452 KB |
Output is correct |
4 |
Correct |
2 ms |
21452 KB |
Output is correct |
5 |
Correct |
3 ms |
21452 KB |
Output is correct |
6 |
Correct |
2 ms |
21452 KB |
Output is correct |
7 |
Correct |
2 ms |
21452 KB |
Output is correct |
8 |
Correct |
3 ms |
21452 KB |
Output is correct |
9 |
Correct |
2 ms |
21452 KB |
Output is correct |
10 |
Correct |
2 ms |
21452 KB |
Output is correct |
11 |
Correct |
2 ms |
21452 KB |
Output is correct |
12 |
Correct |
3 ms |
21452 KB |
Output is correct |
13 |
Correct |
3 ms |
21452 KB |
Output is correct |
14 |
Correct |
3 ms |
21452 KB |
Output is correct |
15 |
Correct |
4 ms |
21452 KB |
Output is correct |
16 |
Correct |
3 ms |
21452 KB |
Output is correct |
17 |
Correct |
4 ms |
21452 KB |
Output is correct |
18 |
Correct |
3 ms |
21452 KB |
Output is correct |
19 |
Correct |
2 ms |
21452 KB |
Output is correct |
20 |
Correct |
3 ms |
21452 KB |
Output is correct |
21 |
Correct |
3 ms |
21452 KB |
Output is correct |
22 |
Correct |
3 ms |
21452 KB |
Output is correct |
23 |
Correct |
2 ms |
21452 KB |
Output is correct |
24 |
Correct |
4 ms |
21452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
21452 KB |
Output is correct |
2 |
Correct |
2 ms |
21452 KB |
Output is correct |
3 |
Correct |
3 ms |
21452 KB |
Output is correct |
4 |
Correct |
3 ms |
21452 KB |
Output is correct |
5 |
Correct |
2 ms |
21452 KB |
Output is correct |
6 |
Correct |
2 ms |
21452 KB |
Output is correct |
7 |
Correct |
3 ms |
21452 KB |
Output is correct |
8 |
Correct |
2 ms |
21452 KB |
Output is correct |
9 |
Correct |
2 ms |
21452 KB |
Output is correct |
10 |
Correct |
2 ms |
21452 KB |
Output is correct |
11 |
Correct |
2 ms |
21452 KB |
Output is correct |
12 |
Correct |
2 ms |
21452 KB |
Output is correct |
13 |
Correct |
3 ms |
21452 KB |
Output is correct |
14 |
Correct |
3 ms |
21452 KB |
Output is correct |
15 |
Correct |
4 ms |
21452 KB |
Output is correct |
16 |
Correct |
2 ms |
21452 KB |
Output is correct |
17 |
Correct |
2 ms |
21452 KB |
Output is correct |
18 |
Correct |
3 ms |
21452 KB |
Output is correct |
19 |
Correct |
3 ms |
21452 KB |
Output is correct |
20 |
Correct |
3 ms |
21452 KB |
Output is correct |
21 |
Correct |
5 ms |
21452 KB |
Output is correct |
22 |
Correct |
4 ms |
21452 KB |
Output is correct |
23 |
Correct |
2 ms |
21452 KB |
Output is correct |
24 |
Correct |
4 ms |
21452 KB |
Output is correct |
25 |
Correct |
50 ms |
23672 KB |
Output is correct |
26 |
Correct |
94 ms |
24724 KB |
Output is correct |
27 |
Correct |
281 ms |
32104 KB |
Output is correct |
28 |
Correct |
350 ms |
35116 KB |
Output is correct |
29 |
Correct |
305 ms |
37316 KB |
Output is correct |
30 |
Correct |
132 ms |
37316 KB |
Output is correct |
31 |
Correct |
46 ms |
37316 KB |
Output is correct |
32 |
Correct |
201 ms |
42552 KB |
Output is correct |
33 |
Correct |
204 ms |
44480 KB |
Output is correct |
34 |
Correct |
245 ms |
46688 KB |
Output is correct |
35 |
Correct |
68 ms |
46688 KB |
Output is correct |
36 |
Correct |
214 ms |
50916 KB |
Output is correct |
37 |
Correct |
54 ms |
50916 KB |
Output is correct |