/*
Normie's Template v2.0
*/
// Standard library in one include.
#include <bits/stdc++.h>
using namespace std;
// ordered_set library.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set(el) tree<el,null_type,less<el>,rb_tree_tag,tree_order_statistics_node_update>
// AtCoder library. (Comment out these two lines if you're not submitting in AtCoder.) (Or if you want to use it in other judges, run expander.py first.)
//#include <atcoder/all>
//using namespace atcoder;
//Pragmas (Comment out these three lines if you're submitting in szkopul.)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//File I/O.
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
//Fast I/O.
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define endl "\n"
//Order checking.
#define ord(a,b,c) ((a>=b)and(b>=c))
//min/max redefines, so i dont have to resolve annoying compile errors.
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
//Constants.
#define MOD (ll(998244353))
#define MAX 300001
#define mag 320
//Pairs and 3-pairs.
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pii(element_type) pair<element_type,element_type>
#define piii(element_type) pair<element_type,pii(element_type)>
//Quick power of 2.
#define pow2(x) (ll(1)<<x)
//Short for-loops.
#define ff(i,__,___) for(int i=__;i<=___;i++)
#define rr(i,__,___) for(int i=__;i>=___;i--)
//Typedefs.
#define bi BigInt
typedef long long ll;
typedef long double ld;
typedef short sh;
//---------END-------//
vector<ll> loc;
vector<ll> add;
vector<ll> slop;
ll n,m,i,j,k,t,t1,u,v,a,b;
ll shft,fin=1e18;
vector<pii(ll)> lis;
char c1,c2;
void reset()
{
for (ll i=0;i<add.size();i++) add[i]=0;
for (ll i=0;i<add.size();i++) slop[i]=0;
}
void addbf(ll i, ll d)
{
ll x=loc[i];
add[0]+=min(d,x-loc[0]);
ll u=lower_bound(loc.begin(),loc.end(),x-d)-loc.begin();
slop[u]-=1;
slop[i]+=1;
if (u) add[u]-=loc[u]-(x-d);
}
void addaf(ll i, ll d)
{
ll x=loc[i];
slop[i]+=1;
ll u=upper_bound(loc.begin(),loc.end(),x+d)-loc.begin()-1;
slop[u]-=1;
if (u<loc.size()-1) add[u+1]+=(x+d)-loc[u];
}
ll get()
{
ll cur=0,cur_slop=0,minn=1e18;
for (ll i=0;i<loc.size();i++)
{
if (i)
{
cur+=cur_slop*(loc[i]-loc[i-1]);
}
cur+=add[i];
minn=min(minn,cur);
cur_slop+=slop[i];
}
return minn;
}
int main()
{
fio;
cin>>t>>n;
for (i=1;i<=n;i++)
{
cin>>c1>>a>>c2>>b;
if (c1==c2)
{
shft+=abs(a-b);
}
else
{
shft+=abs(a-b);
shft++;
if (a>b) swap(a,b);
lis.push_back({a,b});
loc.push_back(a);
loc.push_back(b);
}
}
sort(loc.begin(),loc.end());
auto last=unique(loc.begin(),loc.end());
loc.erase(last, loc.end());
for (ll g : loc) {add.push_back(0); slop.push_back(0);}
if (t==1)
{
for (auto g : lis)
{
u=lower_bound(loc.begin(),loc.end(),g.fi)-loc.begin();
addbf(u,1e9);
u=lower_bound(loc.begin(),loc.end(),g.se)-loc.begin();
addaf(u,1e9);
}
fin=get();
if (fin==1e18) fin=0;
cout<<shft+fin*2;
}
else
{
for (i=0;i<loc.size();i++)
{
reset();
for (auto g : lis)
{
if (loc[i]>g.se) a=loc[i]-g.se;
else if (loc[i]<g.fi) a=-loc[i]+g.fi;
else a=0;
u=lower_bound(loc.begin(),loc.end(),g.fi)-loc.begin();
addbf(u,a);
u=lower_bound(loc.begin(),loc.end(),g.se)-loc.begin();
addaf(u,a);
}
b=get();
fin=min(fin,b);
}
if (fin==1e18) fin=0;
cout<<shft+fin*2;
}
}
Compilation message
bridge.cpp:20: warning: ignoring #pragma comment [-Wunknown-pragmas]
20 | #pragma comment(linker, "/stack:200000000")
|
bridge.cpp: In function 'void reset()':
bridge.cpp:77:15: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (ll i=0;i<add.size();i++) add[i]=0;
| ~^~~~~~~~~~~
bridge.cpp:78:15: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (ll i=0;i<add.size();i++) slop[i]=0;
| ~^~~~~~~~~~~
bridge.cpp: In function 'void addaf(ll, ll)':
bridge.cpp:95:7: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | if (u<loc.size()-1) add[u+1]+=(x+d)-loc[u];
| ~^~~~~~~~~~~~~
bridge.cpp: In function 'll get()':
bridge.cpp:100:15: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for (ll i=0;i<loc.size();i++)
| ~^~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:136:11: warning: unused variable 'g' [-Wunused-variable]
136 | for (ll g : loc) {add.push_back(0); slop.push_back(0);}
| ^
bridge.cpp:152:13: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | for (i=0;i<loc.size();i++)
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
380 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
29 ms |
4328 KB |
Output is correct |
13 |
Correct |
93 ms |
10832 KB |
Output is correct |
14 |
Correct |
57 ms |
4824 KB |
Output is correct |
15 |
Correct |
51 ms |
5976 KB |
Output is correct |
16 |
Correct |
35 ms |
5328 KB |
Output is correct |
17 |
Correct |
56 ms |
10832 KB |
Output is correct |
18 |
Correct |
61 ms |
10448 KB |
Output is correct |
19 |
Correct |
86 ms |
10704 KB |
Output is correct |
20 |
Correct |
37 ms |
5584 KB |
Output is correct |
21 |
Correct |
78 ms |
10704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
364 KB |
Output is correct |
10 |
Correct |
3 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
3 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
3 ms |
364 KB |
Output is correct |
9 |
Correct |
3 ms |
364 KB |
Output is correct |
10 |
Correct |
3 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
3 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
15 ms |
364 KB |
Output is correct |
15 |
Correct |
495 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
8 ms |
364 KB |
Output is correct |
18 |
Correct |
55 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
338 ms |
364 KB |
Output is correct |
21 |
Correct |
348 ms |
612 KB |
Output is correct |
22 |
Correct |
440 ms |
484 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
373 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
3 ms |
364 KB |
Output is correct |
9 |
Correct |
3 ms |
364 KB |
Output is correct |
10 |
Correct |
3 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
3 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
15 ms |
364 KB |
Output is correct |
15 |
Correct |
489 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
8 ms |
364 KB |
Output is correct |
18 |
Correct |
57 ms |
444 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
346 ms |
364 KB |
Output is correct |
21 |
Correct |
349 ms |
492 KB |
Output is correct |
22 |
Correct |
440 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
372 ms |
484 KB |
Output is correct |
25 |
Correct |
33 ms |
4304 KB |
Output is correct |
26 |
Correct |
1477 ms |
4728 KB |
Output is correct |
27 |
Execution timed out |
2061 ms |
10064 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |