#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
const ll INF = 1e18;
int M, K, N;
pii A[MAXN+10];
ll ans;
int S;
vector<int> comp;
int getcomp(int x) { return lower_bound(comp.begin(), comp.end(), x)-comp.begin(); }
pll operator + (const pll &p, const pll &q) { return pll(p.first+q.first, p.second+q.second); }
struct SEG
{
int tree[MAXN*4+10], sz;
pll tree2[MAXN+10];
SEG()
{
int i, j;
for(i=0; i<MAXN*4+10; i++) tree[i]=0;
for(i=0; i<MAXN+10; i++) tree2[i]=pll(0, 0);
sz=0;
}
void update(int node, int tl, int tr, int pos)
{
if(tl==tr) { tree[node]++; return; }
int mid=tl+tr>>1;
if(pos<=mid) update(node*2, tl, mid, pos);
else update(node*2+1, mid+1, tr, pos);
tree[node]=tree[node*2]+tree[node*2+1];
}
void update2(int i, pll k) { for(; i<=S; i+=(i&-i)) tree2[i]=tree2[i]+k; }
void update(int l, int r)
{
update(1, 1, S, getcomp(l));
update(1, 1, S, getcomp(r));
update2(1, pll(-2, 2*l));
update2(getcomp(l), pll(2, -2*l));
update2(getcomp(r), pll(2, -2*r));
//printf("!%d %d\n", getcomp(l), getcomp(r));
sz++;
}
int query(int node, int tl, int tr, int k)
{
if(tl==tr) return tl;
int mid=tl+tr>>1;
if(k<=tree[node*2]) return query(node*2, tl, mid, k);
else return query(node*2+1, mid+1, tr, k-tree[node*2]);
}
pll query2(int i) { pll ret(0, 0); for(; i>0; i-=(i&-i)) ret=ret+tree2[i]; return ret; }
ll query()
{
int t=query(1, 1, S, sz);
pll tt=query2(t);
//printf("%d / %lld %lld\n", t, tt.first, tt.second);
return tt.first*comp[t]+tt.second;
}
}seg;
ll P[MAXN+10], Q[MAXN+10];
int main()
{
int i, j;
scanf("%d%d", &K, &M);
for(i=1; i<=M; i++)
{
char a, c;
int b, d;
scanf(" %c%d %c%d", &a, &b, &c, &d);
if(b>d) swap(b, d);
ans+=d-b;
if(a!=c) A[++N]=pii(b, d), ans++;
}
if(N==0) return !printf("%lld\n", ans);
sort(A+1, A+N+1, [&](const pii &p, const pii &q) { return p.first+p.second<q.first+q.second; });
for(i=1; i<=N; i++)
{
comp.push_back(A[i].first);
comp.push_back(A[i].second);
}
comp.push_back(-1);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
S=comp.size()-1;
seg=SEG();
for(i=1; i<=N; i++)
{
seg.update(A[i].first, A[i].second);
P[i]=seg.query();
}
seg=SEG();
for(i=N; i>=1; i--)
{
seg.update(A[i].first, A[i].second);
Q[i]=seg.query();
}
if(K==1) printf("%lld\n", ans+P[N]);
else
{
ll p=INF;
for(i=1; i<=N+1; i++) p=min(p, P[i-1]+Q[i]);
printf("%lld\n", ans+p);
}
}
Compilation message
bridge.cpp: In constructor 'SEG::SEG()':
bridge.cpp:29:10: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
bridge.cpp: In member function 'void SEG::update(int, int, int, int)':
bridge.cpp:37:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
bridge.cpp: In member function 'int SEG::query(int, int, int, int)':
bridge.cpp:56:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
bridge.cpp: In function 'int main()':
bridge.cpp:74:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
bridge.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &K, &M);
~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c%d %c%d", &a, &b, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6524 KB |
Output is correct |
2 |
Correct |
9 ms |
6520 KB |
Output is correct |
3 |
Correct |
17 ms |
12920 KB |
Output is correct |
4 |
Correct |
19 ms |
12920 KB |
Output is correct |
5 |
Correct |
18 ms |
12920 KB |
Output is correct |
6 |
Correct |
17 ms |
12920 KB |
Output is correct |
7 |
Correct |
17 ms |
12920 KB |
Output is correct |
8 |
Correct |
19 ms |
12920 KB |
Output is correct |
9 |
Correct |
18 ms |
12920 KB |
Output is correct |
10 |
Correct |
16 ms |
12920 KB |
Output is correct |
11 |
Correct |
22 ms |
12920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6648 KB |
Output is correct |
2 |
Correct |
9 ms |
6648 KB |
Output is correct |
3 |
Correct |
16 ms |
12920 KB |
Output is correct |
4 |
Correct |
18 ms |
12920 KB |
Output is correct |
5 |
Correct |
18 ms |
12920 KB |
Output is correct |
6 |
Correct |
17 ms |
12920 KB |
Output is correct |
7 |
Correct |
18 ms |
12920 KB |
Output is correct |
8 |
Correct |
17 ms |
12920 KB |
Output is correct |
9 |
Correct |
21 ms |
13048 KB |
Output is correct |
10 |
Correct |
17 ms |
12920 KB |
Output is correct |
11 |
Correct |
18 ms |
12920 KB |
Output is correct |
12 |
Correct |
61 ms |
16104 KB |
Output is correct |
13 |
Correct |
314 ms |
16108 KB |
Output is correct |
14 |
Correct |
189 ms |
15848 KB |
Output is correct |
15 |
Correct |
174 ms |
14832 KB |
Output is correct |
16 |
Correct |
66 ms |
16108 KB |
Output is correct |
17 |
Correct |
201 ms |
16360 KB |
Output is correct |
18 |
Correct |
178 ms |
16108 KB |
Output is correct |
19 |
Correct |
215 ms |
16108 KB |
Output is correct |
20 |
Correct |
70 ms |
16104 KB |
Output is correct |
21 |
Correct |
206 ms |
16108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6520 KB |
Output is correct |
2 |
Correct |
10 ms |
6648 KB |
Output is correct |
3 |
Correct |
16 ms |
12920 KB |
Output is correct |
4 |
Correct |
17 ms |
12920 KB |
Output is correct |
5 |
Correct |
17 ms |
12920 KB |
Output is correct |
6 |
Correct |
16 ms |
12792 KB |
Output is correct |
7 |
Correct |
17 ms |
12920 KB |
Output is correct |
8 |
Correct |
16 ms |
12792 KB |
Output is correct |
9 |
Correct |
16 ms |
12792 KB |
Output is correct |
10 |
Correct |
17 ms |
12792 KB |
Output is correct |
11 |
Correct |
16 ms |
12920 KB |
Output is correct |
12 |
Correct |
17 ms |
12792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6648 KB |
Output is correct |
2 |
Correct |
9 ms |
6520 KB |
Output is correct |
3 |
Correct |
16 ms |
12920 KB |
Output is correct |
4 |
Correct |
17 ms |
12792 KB |
Output is correct |
5 |
Correct |
17 ms |
12792 KB |
Output is correct |
6 |
Correct |
17 ms |
12792 KB |
Output is correct |
7 |
Correct |
17 ms |
12792 KB |
Output is correct |
8 |
Correct |
17 ms |
12792 KB |
Output is correct |
9 |
Correct |
17 ms |
12792 KB |
Output is correct |
10 |
Correct |
17 ms |
12792 KB |
Output is correct |
11 |
Correct |
17 ms |
12792 KB |
Output is correct |
12 |
Correct |
17 ms |
12796 KB |
Output is correct |
13 |
Correct |
17 ms |
12924 KB |
Output is correct |
14 |
Correct |
18 ms |
12920 KB |
Output is correct |
15 |
Correct |
18 ms |
12920 KB |
Output is correct |
16 |
Correct |
17 ms |
12792 KB |
Output is correct |
17 |
Correct |
17 ms |
12920 KB |
Output is correct |
18 |
Correct |
17 ms |
12920 KB |
Output is correct |
19 |
Correct |
17 ms |
12920 KB |
Output is correct |
20 |
Correct |
18 ms |
13048 KB |
Output is correct |
21 |
Correct |
18 ms |
12920 KB |
Output is correct |
22 |
Correct |
19 ms |
12920 KB |
Output is correct |
23 |
Correct |
17 ms |
12920 KB |
Output is correct |
24 |
Correct |
18 ms |
12920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6520 KB |
Output is correct |
2 |
Correct |
8 ms |
6648 KB |
Output is correct |
3 |
Correct |
15 ms |
12792 KB |
Output is correct |
4 |
Correct |
17 ms |
12920 KB |
Output is correct |
5 |
Correct |
18 ms |
12792 KB |
Output is correct |
6 |
Correct |
17 ms |
12920 KB |
Output is correct |
7 |
Correct |
16 ms |
12792 KB |
Output is correct |
8 |
Correct |
16 ms |
12792 KB |
Output is correct |
9 |
Correct |
17 ms |
12792 KB |
Output is correct |
10 |
Correct |
17 ms |
12792 KB |
Output is correct |
11 |
Correct |
17 ms |
12920 KB |
Output is correct |
12 |
Correct |
16 ms |
12920 KB |
Output is correct |
13 |
Correct |
17 ms |
12920 KB |
Output is correct |
14 |
Correct |
18 ms |
12920 KB |
Output is correct |
15 |
Correct |
18 ms |
12920 KB |
Output is correct |
16 |
Correct |
17 ms |
12924 KB |
Output is correct |
17 |
Correct |
17 ms |
12920 KB |
Output is correct |
18 |
Correct |
18 ms |
12924 KB |
Output is correct |
19 |
Correct |
18 ms |
12920 KB |
Output is correct |
20 |
Correct |
18 ms |
12920 KB |
Output is correct |
21 |
Correct |
19 ms |
12920 KB |
Output is correct |
22 |
Correct |
18 ms |
12924 KB |
Output is correct |
23 |
Correct |
17 ms |
12920 KB |
Output is correct |
24 |
Correct |
19 ms |
12924 KB |
Output is correct |
25 |
Correct |
65 ms |
17128 KB |
Output is correct |
26 |
Correct |
99 ms |
17132 KB |
Output is correct |
27 |
Correct |
296 ms |
18028 KB |
Output is correct |
28 |
Correct |
325 ms |
18464 KB |
Output is correct |
29 |
Correct |
319 ms |
18412 KB |
Output is correct |
30 |
Correct |
161 ms |
15856 KB |
Output is correct |
31 |
Correct |
67 ms |
17768 KB |
Output is correct |
32 |
Correct |
204 ms |
18500 KB |
Output is correct |
33 |
Correct |
181 ms |
18152 KB |
Output is correct |
34 |
Correct |
208 ms |
18408 KB |
Output is correct |
35 |
Correct |
106 ms |
18156 KB |
Output is correct |
36 |
Correct |
224 ms |
18280 KB |
Output is correct |
37 |
Correct |
63 ms |
16876 KB |
Output is correct |