This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |