Problem:
問題文
委員長の魔女 PATRICIA は，蜘蛛のような糸を吐き結界の中で暮らしている．使い魔の MATHIEU は，委員長の魔女が吐いた糸を引っぱり自在に空を飛んでいる．糸は空間上の線分とみなすことにする．
暁美ほむら
は委員長の魔女に対して攻撃を仕掛けようとし，爆弾と間違えて花火玉を投げ込んでしまった．その結果，それぞれの糸について，魔女から距離
p_1, ..., p_M
の位置にある部分が切れるだけとなってしまった．花火玉を投げる前，それぞれの糸について，糸の
2
つの端点と魔女はこの順番で同一直線上に並んでおり，端点のどちらか一方を使い魔が引っ張っていた．爆発で糸は切れ，使い魔が持っていた部分とそこから一つ置きの部分を残して糸の一部は消えてしまった．
ほむら
は次の戦略を考えるために，残った糸についての情報が知りたいと思っている．残った糸の長さの合計値を求めよ．
入力形式
入力は以下の形式で与えられる．
N\ M\\
s_1\ t_1\\
...\\
s_N\ t_N\\
p_1\\
...\\
p_M\\
N
は空間上の糸の個数，
M
は花火玉によって発生した糸の切断の箇所の個数である．
s_i, t_i
は糸の情報であり，
s_i
は使い間が引っ張っていた側の端点，
t_i
はそうでない側の端点の魔女からの距離である．
p_i
は花火玉によって切断された位置の情報である．
出力形式
花火玉を投げた後，残った糸の長さの合計値を1行に出力せよ．
制約
1 ≤ N ≤ 10
5
1 ≤ M ≤ 10
5
1 ≤ s_i ≤ 10^9
1 ≤ t_i ≤ 10^9
1 ≤ p_i ≤ 10^9
入力値は全て整数である．
s_1, ..., s_N, t_1, ..., t_N, p_1, ..., p_M
は全て相異なる．
入出力例
入力例 1
2 3
1 8
20 5
3
7
15
出力例1
10
爆発の後，
1
つ目のひもは端点の魔女からの距離がそれぞれ
(1,3), (7,8)
の
2
つのひもに，
2
つ目のひもは
(20, 15), (7, 5)
の
2
つのひもに分かれる．結果，残ったひもの長さの合計は
2+1+5+2 = 10
となる．
入力例 2
1 1
100 1
70
出力例 2
30
入力例 3
6 8
1 10
11 23
99 2
56 58
66 78
88 49
5
15
25
35
45
55
65
75
出力例 3
99
Problem Setter: Flat35

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 1100000000;
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> s(N), t(N);
  for (int i = 0; i < N; i++){
    cin >> s[i] >> t[i];
  }
  vector<int> p(M);
  for (int i = 0; i < M; i++){
    cin >> p[i];
  }
  p.push_back(-1);
  p.push_back(INF);
  M += 2;
  sort(p.begin(), p.end());
  vector<int> d(M - 1);
  for (int i = 0; i < M - 1; i++){
    d[i] = p[i + 1] - p[i];
  }
  vector<vector<int>> S(2, vector<int>(M));
  for (int i = 0; i < 2; i++){
    S[i][0] = 0;
    for (int j = 0; j < M - 1; j++){
      S[i][j + 1] = S[i][j];
      if (j % 2 == i){
        S[i][j + 1] += d[j];
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++){
    if (s[i] < t[i]){
      int L = lower_bound(p.begin(), p.end(), s[i]) - p.begin();
      int R = lower_bound(p.begin(), p.end(), t[i]) - p.begin() - 1;
      ans += S[(L + 1) % 2][R] - S[(L + 1) % 2][L];
      ans += p[L] - s[i];
      if ((R - L) % 2 != 0){
        ans += t[i] - p[R];
      }
    }
    if (s[i] > t[i]){
      int L = lower_bound(p.begin(), p.end(), t[i]) - p.begin();
      int R = lower_bound(p.begin(), p.end(), s[i]) - p.begin() - 1;
      ans += S[R % 2][R] - S[R % 2][L];
      ans += s[i] - p[R];
      if ((R - L) % 2 != 0){
        ans += p[L] - t[i];
      }
    }
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 5740
final_rank: 5

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 100001

ll start[NUM],tail[NUM];
ll cut_pos[NUM];
ll ruiseki_odd[NUM],ruiseki_even[NUM];

int main(){

	int N,M;
	scanf("%d %d",&N,&M);

	for(int i = 0; i < N; i++){
		scanf("%lld %lld",&start[i],&tail[i]);
	}
	for(int i = 0; i < M; i++){
		scanf("%lld",&cut_pos[i]);
	}
	sort(cut_pos,cut_pos+M);

	//偶奇に分けた累積和テーブルを作成
	for(int i = 0; i < M; i++){
		ruiseki_odd[i] = 0;
		ruiseki_even[i] = 0;
	}
	//ruiseki_odd[i:奇数] = cut_pos[i:奇数]-cut_pos[i-1]の累積和
	for(int i = 1; i < M; i += 2){
		ruiseki_odd[i] = cut_pos[i]-cut_pos[i-1];
		if(i > 1){
			ruiseki_odd[i] += ruiseki_odd[i-2];
		}
	}
	//ruiseki_even[i:偶数] = cut_pos[i:偶数]-cut_pos[i-1]の累積和
	for(int i = 2; i < M; i += 2){
		ruiseki_even[i] = cut_pos[i]-cut_pos[i-1];
		ruiseki_even[i] += ruiseki_even[i-2];
	}

	ll ans = 0;
	int left,right,m;
	int max_index,min_index;

	for(int i = 0; i < N; i++){

		if(start[i] > tail[i]){

			if(cut_pos[0] >= start[i] || cut_pos[M-1] <= tail[i]){ //糸が分断されない場合
				ans += start[i]-tail[i];
				continue;
			}
			//start[i]以下の、最大の切断点を探す
			left = 0,right = M-1,m = (left+right)/2;
			while(left <= right){
				if(cut_pos[m] <= start[i]){
					max_index = m;
					left = m+1;
				}else{
					right = m-1;
				}
				m = (left+right)/2;
			}
			//tail[i]以上の、最小の切断点を探す
			left = 0,right = M-1, m = (left+right)/2;
			while(left <= right){
				if(cut_pos[m] >= tail[i]){
					min_index = m;
					right = m-1;
				}else{
					left = m+1;
				}
				m = (left+right)/2;
			}
			if(min_index > max_index){ //切断区間内にstartとtailが挟まっている場合
				ans += start[i]-tail[i];
				continue;
			}

			ans += start[i]-cut_pos[max_index]; //一番使い魔側の区間を足す

			if(max_index%2 == 0){ //最初の切れ目が偶数の場合:ruiseki_odd

				if(min_index%2 == 0){ //tailが無効区間

					ans += ruiseki_odd[max(0,max_index-1)]-ruiseki_odd[max(0,min_index-1)];

				}else{ //tailが有効区間

					ans += ruiseki_odd[max(0,max_index-1)]-ruiseki_odd[min_index]+cut_pos[min_index]-tail[i];
				}

			}else{ //max_index%2 == 1

				if(min_index%2 == 1){ //tailが無効区間

					ans += ruiseki_even[max(0,max_index-1)]-ruiseki_even[max(0,min_index-1)];

				}else{ //tailが有効区間

					ans += ruiseki_even[max(0,max_index-1)]-ruiseki_even[min_index];
					if(min_index > 0){
						ans += cut_pos[min_index]-tail[i];
					}else if(min_index != max_index && min_index == 0 && tail[i] < cut_pos[0]){
						ans += cut_pos[0]-tail[i];
					}
				}
			}

		}else{ //start[i] < tail[i]

			if(cut_pos[0] >= tail[i] || cut_pos[M-1] <= start[i]){
				ans += tail[i]-start[i];
				continue;
			}
			//start[i]以上の、最小の切断点を探す
			left = 0,right = M-1,m = (left+right)/2;
			while(left <= right){
				if(cut_pos[m] >= start[i]){
					min_index = m;
					right = m-1;
				}else{
					left = m+1;
				}
				m = (left+right)/2;
			}
			//tail[i]以下の、最大の切断点を探す
			left = 0,right = M-1,m = (left+right)/2;
			while(left <= right){
				if(cut_pos[m] <= tail[i]){
					max_index = m;
					left = m+1;
				}else{
					right = m-1;
				}
				m = (left+right)/2;
			}

			if(min_index > max_index){ //切断区間内にstartとtailが挟まっている場合
				ans += tail[i]-start[i];
				continue;
			}

			ans += cut_pos[min_index]-start[i];

			if(min_index%2 == 1){ //最初の切れ目が奇数の場合;ruiseki_odd

				if(max_index%2 == 1){ //tailが無効区間

					ans += ruiseki_odd[max_index]-ruiseki_odd[min_index];
				}else{
					ans += ruiseki_odd[max(0,max_index-1)]-ruiseki_odd[min_index]+tail[i]-cut_pos[max_index];
				}

			}else{ //min_index%2 == 0:最初の切れ間が偶数:ruiseki_even

				if(max_index%2 == 0){ //tailが無効区間

					ans += ruiseki_even[max_index]-ruiseki_even[min_index];
				}else{
					ans += ruiseki_even[max(0,max_index-1)]-ruiseki_even[min_index]+tail[i]-cut_pos[max_index];				}
			}
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 7180
final_rank: 3

code_snippet: #include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
using ll = long long int;
const ll INF = 1LL << 30;

ll N, M;

ll solve(vector<ll> cuts, vector< pair<ll, ll> > strands) {
    ll ret = 0;

    vector<ll> even, odd;
    even.push_back(0); odd.push_back(0);
    for(size_t i=1; i<cuts.size(); i++) {
        ll mo = i % 2, diff = cuts[i] - cuts[i-1];
        (mo ? odd : even).push_back( (mo ? odd : even).back() + diff );
        (mo ? even : odd).push_back( (mo ? even : odd).back()        ); // zero
    }

    for(auto e : strands) {
        ll l = e.first, r = e.second, sum = 0;
        sum += r - l;
        ll il = lower_bound(cuts.begin(), cuts.end(), l) - cuts.begin();
        ll ir = lower_bound(cuts.begin(), cuts.end(), r) - cuts.begin() - 1;

        if(ir - il >= 0) {
            if(il % 2 == 0) {
                sum -= odd [ir] - odd [il];
            }
            if(il % 2 == 1) {
                sum -= even[ir] - even[il];
            }

            if( (ir - il) % 2 == 0 ) {
                ll bound = cuts[ir];
                if(l < bound && bound < r) sum -= r - bound;
            }
        }

        // printf("[%lld, %lld], il = %lld, ir = %lld, len = %lld\n", l, r, il, ir, sum);
        ret += sum;
    }
    return ret;
}

int main() {
    scanf("%lld%lld", &N, &M);
    vector< pair<ll, ll> > strands, rev_strands;
    for(int i=0; i<N; i++) {
        ll l, r; scanf("%lld%lld", &l, &r);
        if(l < r) strands.push_back(make_pair(l, r));
        else rev_strands.push_back(make_pair(INF - l, INF - r));
    }

    vector<ll> cuts(M);
    for(int i=0; i<M; i++) {
        scanf("%lld", &cuts[i]);
    }

    ll ans = 0;
    sort(cuts.begin(), cuts.end());
    ans += solve(cuts, strands);
    reverse(cuts.begin(), cuts.end());
    for(auto &c : cuts) {
        c = INF - c;
    }
    ans += solve(cuts, rev_strands);
    printf("%lld\n", ans);
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 9184
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N,M;
int S[111111],T[111111];
int P[222222];

int sum[222222];
signed main(){
    cin>>N>>M;
    rep(i,N){
        cin>>S[i]>>T[i];
        if(S[i]>T[i])S[i]*=-1,T[i]*=-1;
    }
    rep(i,M)cin>>P[i],P[i+M]=-P[i];
    M*=2;
    sort(P,P+M);

    for(int i=1;i<M;i++){
        sum[i]=P[i]-P[i-1]+(i>1?sum[i-2]:0);
    }

    int ans=0;
    rep(i,N){
        if(T[i]<P[0]){
            ans+=T[i]-S[i];
            continue;
        }
        if(P[M-1]<S[i]){
            ans+=T[i]-S[i];
            continue;
        }
        int t=upper_bound(P,P+M,T[i])-P-1;
        int s=upper_bound(P,P+M,S[i])-P;
        if(s%2!=t%2){
            ans+=sum[t-1]-sum[s]+T[i]-P[t]+P[s]-S[i];
        }
        else{
            ans+=sum[t]-sum[s]+P[s]-S[i];
        }
    }

    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 6048
final_rank: 13

code_snippet: #include<bits/stdc++.h>

using namespace std;
struct INIT{INIT(){ios::sync_with_stdio(false);cin.tie(0);}}init;
typedef long long LL;
typedef tuple<LL,LL> T;
typedef vector<LL> VL;
typedef vector<T> VT;
#define rep(i,n) for(auto i=(n)*0;i<n;i++)
#define range(it,v) for(auto &it:v)


LL getlen(set<LL>& event,VL& point,VT& seg,LL prev){
    priority_queue<LL,vector<LL>,greater<LL>> on,off;
    int n=point.size();
    int m=seg.size();
    int i=0,j=0;
    LL res=0;
    range(it,event){
	
	res+=((LL)on.size())*(it-prev);
	prev=it;
	//	cout<<it<<" "<<res<<endl;
	if(i<n&&point[i]==it){
	    swap(on,off);
	    i++;
	}
	else {
	    if(on.empty()==false&&on.top()==it){
		on.pop();
	    }
	    else if(off.empty()==false&&off.top()==it){
		off.pop();
	    }
	    else{
		LL s,t;
		tie(s,t)=seg[j++];
		on.push(t);
	    }
	}
    }
    return res;
}
int main(){
    int N,M;
    cin>>N>>M;
    VT st;
    set<LL> e;
    rep(i,N){
	int s,t;
	cin>>s>>t;
	if(s<t){
	    st.push_back(T(s,t));
	    e.insert(t);
	    e.insert(s);
	}
	else {
	    st.push_back(T(-s,-t));
	    e.insert(-t);
	    e.insert(-s);
	}
    }
    sort(st.begin(),st.end());
    VL point;
    rep(i,M){
	LL it;
	cin>>it;
	point.push_back(it);
	point.push_back(-it);
	e.insert(it);
	e.insert(-it);
    }
    sort(point.begin(),point.end());
    cout<<getlen(e,point,st,-11*4*51*4*1919)<<endl;
    
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 25740
final_rank: 16

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXN = 100100;
int S[MAXN], T[MAXN];
int P[MAXN];
int N, M;
ll sumEven[MAXN], sumOdd[MAXN];

ll calc(int i) {
    int is = lower_bound(P, P+M, S[i]) - P;
    int it = lower_bound(P, P+M, T[i]) - P; it--;
    if (is == M || it == -1) return T[i] - S[i];
    ll ret = 0;
    if ((it-is)%2 == 0) {
        if (is%2) {
            ret = (sumOdd[it+2] - sumOdd[is]) - (sumEven[it+1] - sumEven[is+1]) - S[i];
        } else {
            ret = (sumEven[it+2] - sumEven[is]) - (sumOdd[it+1] - sumOdd[is+1]) - S[i];
        }
    } else {
        if (is%2) {
            ret = (sumOdd[it+1] - sumOdd[is]) - (sumEven[it+2] - sumEven[is+1]) + T[i] - S[i];
        } else {
            ret = (sumEven[it+1] - sumEven[is]) - (sumOdd[it+2] - sumOdd[is+1]) + T[i] - S[i];
        }
    }
    return ret;
}

ll solve() {
    memset(sumEven, 0, sizeof(sumEven));
    memset(sumOdd, 0, sizeof(sumOdd));
    for (int i = 0; i < M; i += 2) {
        sumEven[i+1] = sumEven[i];
        sumEven[i+2] = sumEven[i] + P[i];
    }
    for (int i = 1; i < M; i += 2) {
        sumOdd[i+2] = sumOdd[i] + P[i];
        sumOdd[i+1] = sumOdd[i+2];
    }
    ll ans = 0;
    for (int i = 0; i < N; i++) if (S[i] < T[i]) {
        ll tmp = calc(i);
        ans += tmp;
    }
    return ans;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> M;
    for (int i = 0; i < N; i++)
        cin >> S[i] >> T[i];
    for (int i = 0; i < M; i++) 
        cin >> P[i];
    sort(P, P+M);
    ll ans = solve();
    reverse(P, P+M);
    for (int i = 0; i < M; i++)
        P[i] *= -1;
    for (int i = 0; i < N; i++) {
        S[i] *= -1;
        T[i] *= -1;
    }
    ans += solve();
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5912
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
typedef long long ll;
using namespace std;

const int open_tag = 1;
const int close_tag = 2;
const int toggle_tag = 3;
union event_t {
    struct { int tag; int t; } common;
    struct { int tag; int l, i; } open; // [l, r)
    struct { int tag; int r, i; } close; // [l, r)
    struct { int tag; int p; } toggle;
};
bool operator < (event_t a, event_t b) { return a.common.t > b.common.t; } // weak struct ordering
int main() {
    // init
    int n, m; cin >> n >> m;
    priority_queue<event_t> que;
    repeat (i,n) {
        int s, t; cin >> s >> t;
        event_t el = { open_tag };
        event_t er = { close_tag };
        if (s < t) {
            el.open .l = s;
            er.close.r = t;
        } else {
            el.open .l = - s;
            er.close.r = - t;
        }
        el.open .i = i;
        er.close.i = i;
        que.push(el);
        que.push(er);
    }
    repeat (i,m) {
        int p; cin >> p;
        event_t e = { toggle_tag };
        e.toggle.p = p;
        que.push(e);
        e.toggle.p = - p;
        que.push(e);
    }
    // run
    ll ans = 0;
    int prv = 0;
    int on = 0;
    int off = 0;
    vector<int> ix(n, -1);
    vector<int> acc { 0 };
    while (not que.empty()) {
        event_t e = que.top(); que.pop();
        ans += on *(ll) (e.common.t - prv);
        prv = e.common.t;
        if (e.common.tag == open_tag) {
            on += 1;
            ix[e.open.i] = acc.size() - 1;
            acc.push_back(acc.back());
        } else if (e.common.tag == close_tag) {
            ((acc.back() - acc[ix[e.close.i]]) % 2 == 0 ? on : off) -= 1;
        } else if (e.common.tag == toggle_tag) {
            swap(on, off);
            acc.back() += 1;
        }
    }
    // output
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 9124
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using vld = vector<ld>; 
using vi = vector<int>; using vvi = vector<vi>;
vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }
using P = pair<ll, ll>;

template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) {  o << "(" << v.first << ", " << v.second << ")"; return o; }
template<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};
template<class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? "" : ", ") << get<Is>(t)), 0)...}; }
template<class Ch, class Tr, class... Args> 
auto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; }
ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; cout << endl; } return o; }
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]";  return o; }
template <typename T>  ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]";  return o; }
void printbits(ll mask, ll n) { rep(i, n) { cout << !!(mask & (1ll << i)); } cout << endl; }
#define ldout fixed << setprecision(40) 

static const double EPS = 1e-14;
static const long long INF = 1e18;
static const long long mo = 1e9+7;

// ?´???????????¨????
// ?§????O(n)
// ?????¨???O(1)
const function<bool(ll)> f1_default = [](ll x) { return 1; };
class Sum1d {
public:
    vll data;
    vll sumdata;
    Sum1d(vll& d, function<bool(ll)> f = f1_default) {
        int n = d.size();
        data = d;
        sumdata = vll(n+1, 0);
        rep(i, n) if(f(i)) sumdata[i+1] = data[i];
        rep(i, n) sumdata[i+1] += sumdata[i];
    }
    // [i, j)????°???????????????? (????????????)
    ll sum(int i, int j) {
        if (i >= j)
            return 0;
        return sumdata[j]-sumdata[i];
    }
    // [i, i+ilen)????°???????????????? (????????????)
    ll suml(int i, int len) {
        return this->sum(i, i+len);
    }
    void print(void) {
        rep(i, sumdata.size()) cout << sumdata[i] << " "; cout << endl;
    }
};
int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    ll n, m; cin >> n >> m;
    vll s(n), t(n), p(m); 
    rep(i, s.size()) 
        cin >> s[i] >> t[i]; 
    rep(i, p.size()) 
        cin >> p[i];

    sort(all(p));

    vll pd_even(m), pd_odd(m);
    pd_even[0] = p[0];
    repi(i, 1, m) {
        (i % 2 == 0 ? pd_even[i] : pd_odd[i]) = p[i]-p[i-1];
        (i % 2 == 1 ? pd_even[i] : pd_odd[i]) = 0;
    }

    Sum1d s_even(pd_even), s_odd(pd_odd);

    ll ret = 0;
    rep(i, s.size()) {
        ll is = lower_bound(all(p), s[i]) - p.begin();
        ll it = lower_bound(all(p), t[i]) - p.begin();
        if (is == it) {
            ret += abs(t[i] - s[i]);
            continue;
        }

        if (is < it) {
            ret += abs(p[is] - s[i]);
            if (it % 2 == is % 2) {
                ret += abs(p[it-1] - t[i]);
            }
            ret += (is % 2 == 0 ? s_even : s_odd).sum(is+1, it);
        } else {
            ret += abs(p[is-1] - s[i]);
            if (it % 2 == is % 2) {
                ret += abs(p[it] - t[i]);
            }
            ret += (is % 2 == 0 ? s_even : s_odd).sum(it+1, is);
        }
    }
    cout << ret << endl;

    
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 9960
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using vld = vector<ld>; 
using vi = vector<int>; using vvi = vector<vi>;
vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }
using P = pair<ll, ll>;

template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) {  o << "(" << v.first << ", " << v.second << ")"; return o; }
template<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};
template<class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? "" : ", ") << get<Is>(t)), 0)...}; }
template<class Ch, class Tr, class... Args> 
auto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; }
ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; cout << endl; } return o; }
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]";  return o; }
template <typename T>  ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]";  return o; }
void printbits(ll mask, ll n) { rep(i, n) { cout << !!(mask & (1ll << i)); } cout << endl; }
#define ldout fixed << setprecision(40) 

static const double EPS = 1e-14;
static const long long INF = 1e18;
static const long long mo = 1e9+7;

// ?´???????????¨????
// ?§????O(n)
// ?????¨???O(1)
const function<bool(ll)> f1_default = [](ll x) { return 1; };
class Sum1d {
public:
    vll data;
    vll sumdata;
    Sum1d(vll& d, function<bool(ll)> f = f1_default) {
        int n = d.size();
        data = d;
        sumdata = vll(n+1, 0);
        rep(i, n) if(f(i)) sumdata[i+1] = data[i];
        rep(i, n) sumdata[i+1] += sumdata[i];
    }
    // [i, j)????°???????????????? (????????????)
    ll sum(int i, int j) {
        if (i >= j)
            return 0;
        return sumdata[j]-sumdata[i];
    }
    // [i, i+ilen)????°???????????????? (????????????)
    ll suml(int i, int len) {
        return this->sum(i, i+len);
    }
    void print(void) {
        rep(i, sumdata.size()) cout << sumdata[i] << " "; cout << endl;
    }
};
int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    ll n, m; cin >> n >> m;
    vll s(n), t(n), p(m); rep(i, s.size()) cin >> s[i] >> t[i]; rep(i, p.size()) cin >> p[i];
    sort(all(p));

    vll pd_even(m), pd_odd(m);
    pd_even[0] = p[0];
    repi(i, 1, m) {
        if (i % 2 == 0) {
            pd_even[i] = p[i]-p[i-1];
            pd_odd[i] = 0;
        } else {
            pd_even[i] = 0;
            pd_odd[i] = p[i]-p[i-1];
        }
    }
    /*
    cout << pd_even << endl;
    cout << pd_odd << endl;
    */

    Sum1d s_even(pd_even), s_odd(pd_odd);
    /*
    s_even.print();
    s_odd.print();
    cout << s_even.sum(1, 4) << endl;
    */

    ll ret = 0;
    rep(i, s.size()) {
        ll is = lower_bound(all(p), s[i]) - p.begin();
        ll it = lower_bound(all(p), t[i]) - p.begin();
        /*
        cout << is << " " << it << endl;
        */
        if (is == it) {
            ret += abs(t[i] - s[i]);
            continue;
        }

        if (is < it) {
//            cout << "#Forward" << endl;
            ret += abs(p[is] - s[i]);
//            cout << abs(p[is] - s[i]) << endl;
            if (it % 2 == is % 2) {
                ret += abs(p[it-1] - t[i]);
//                cout << p[it-1]  << " " <<  t[i] << "#detail" << endl;
//                cout << abs(p[it-1] - t[i]) << endl;
            }
            ret += (is % 2 == 0 ? s_even : s_odd).sum(is+1, it);
//            cout <<(is % 2 == 0 ? s_even : s_odd).sum(is+1, it) << endl;
        } else {
//            cout << "#Inverse" << endl;
            ret += abs(p[is-1] - s[i]);
//            cout <<  abs(p[is-1] - s[i]) << endl;
            if (it % 2 == is % 2) {
                ret += abs(p[it] - t[i]);
//                cout << abs(p[it] - t[i]) << endl;
            }
            ret += (is % 2 == 0 ? s_even : s_odd).sum(it+1, is);
//            cout << (is % 2 == 0 ? s_even : s_odd).sum(it+1, is) << endl;
        }
    }
    cout << ret << endl;

    
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 9960
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

int main()
{
    int N, M, s, t, p;
    cin >> N >> M;
    vector<pii> v[2];
    for (int i = 0; i < N; i++) {
        cin >> s >> t;
        int j = i+1;
        if (s < t) {
            v[0].push_back(pii(s, j));
            v[0].push_back(pii(t, j));
        } else {
            v[1].push_back(pii(s, -j));
            v[1].push_back(pii(t, -j));
        }
    }
    for (int i = 0; i < M; i++) {
        cin >> p;
        v[0].push_back(pii(p, 0));
        v[1].push_back(pii(p, 0));
    }

    sort(v[0].begin(), v[0].end());
    sort(v[1].rbegin(), v[1].rend());

    ll res = 0;
    for (int i = 0; i < 2; i++) {
        vector<int> use(N, -1);
        int cut = 0, even = 0, odd = 0;
        for (int j = 0; j < (int)v[i].size(); j++) {
            int k = (i == 0 ? +1 : -1);
            int x = v[i][j].second * k;
            if (j > 0) {
                ll cost = k * (ll)(v[i][j].first - v[i][j-1].first) * even;
                res += cost;
            }
                
            if (x != 0) {
                x--;
                if (use[x] == -1) {
                    use[x] = cut;
                    even++;
                } else {
                    if ((cut - use[x]) % 2 == 0) {
                        even--;
                    } else {
                        odd--;
                    }
                }
            } else {
                cut++;
                swap(even, odd);
            }
        }
    }
    cout << res << endl;
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 6440
final_rank: 12

code_snippet: #include<bits/stdc++.h>
#define N 100005
using namespace std;
long long n,m,s[N],t[N],p[N],d[N],ans;

int bynary_search(int x){
  int l=0,r=m;
  while(l<r){
    int mid=(l+r)/2;
    if(x<=p[mid])r=mid;
    else l=mid+1;
  }
  return l;
}

int main(){
  cin>>n>>m;
  for(int i=0;i<n;i++)cin>>s[i]>>t[i];
  for(int i=0;i<m;i++)cin>>p[i];
  sort(p,p+m);
  d[0]=p[0],d[1]=p[1]-p[0];
  ans=0;
  for(int i=2;i<m;i++)d[i]=d[i-2]+p[i]-p[i-1];
  for(int i=0;i<n;i++){
    int r1=bynary_search(s[i]);
    int r2=bynary_search(t[i]);
    if(r1==r2)ans+=max(s[i],t[i])-min(s[i],t[i]);
    else if(abs(r1-r2)==1){
      if(s[i]<t[i])ans+=p[r1]-s[i];
      else ans+=s[i]-p[r1-1];
    }
    else if(s[i]<t[i]){
      if(r2%2==r1%2)ans+=p[r1]-s[i]+d[r2-2]-d[r1]+t[i]-p[r2-1];
      else ans+=p[r1]-s[i]+d[r2-1]-d[r1];
    }else{
      if(r2%2==r1%2)ans+=s[i]-p[r1-1]+d[r1-2]-d[r2]+p[r2]-t[i];
      else ans+=s[i]-p[r1-1]+d[r1-2]-d[r2-1];
    }
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 4300
final_rank: 10

code_snippet: #include<bits/stdc++.h>
#define N 10005
using namespace std;
long long n,m,s[N],t[N],p[N],d[N],ans;

int bynary_search(int x){
  int l=0,r=m;
  while(l<r){
    int mid=(l+r)/2;
    if(x<=p[mid])r=mid;
    else l=mid+1;
  }
  return l;
}

int main(){
  cin>>n>>m;
  for(int i=0;i<n;i++)cin>>s[i]>>t[i];
  for(int i=0;i<m;i++)cin>>p[i];
  sort(p,p+m);
  d[0]=p[0],d[1]=p[1]-p[0];
  ans=0;
  for(int i=2;i<m;i++)d[i]=d[i-2]+p[i]-p[i-1];
  for(int i=0;i<n;i++){
    int r1=bynary_search(s[i]);
    int r2=bynary_search(t[i]);
    if(r1==r2)ans+=max(s[i],t[i])-min(s[i],t[i]);
    else if(abs(r1-r2)==1){
      if(s[i]<t[i])ans+=p[r1]-s[i];
      else ans+=s[i]-p[r1-1];
    }
    else if(s[i]<t[i]){
      if(r2%2==r1%2)ans+=p[r1]-s[i]+d[r2-2]-d[r1]+t[i]-p[r2-1];
      else ans+=p[r1]-s[i]+d[r2-1]-d[r1];
    }else{
      if(r2%2==r1%2)ans+=s[i]-p[r1-1]+d[r1-2]-d[r2]+p[r2]-t[i];
      else ans+=s[i]-p[r1-1]+d[r1-2]-d[r2-1];
    }
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 0.19047619047619047
time_ms: 10
memory_kb: 1488
final_rank: 18

code_snippet: #include<bits/stdc++.h>
#define N 10005
using namespace std;
int n,m,s[N],t[N],p[N],d[N],ans;

int bynary_search(int x){
  int l=0,r=m;
  while(l<r){
    int mid=(l+r)/2;
    if(x<=p[mid])r=mid;
    else l=mid+1;
  }
  return l;
}

int main(){
  cin>>n>>m;
  for(int i=0;i<n;i++)cin>>s[i]>>t[i];
  for(int i=0;i<m;i++)cin>>p[i];
  sort(p,p+m);
  d[0]=p[0],d[1]=p[1]-p[0];
  ans=0;
  for(int i=2;i<m;i++)d[i]=d[i-2]+p[i]-p[i-1];
  for(int i=0;i<n;i++){
    int r1=bynary_search(s[i]);
    int r2=bynary_search(t[i]);
    if(r1==r2)ans+=max(s[i],t[i])-min(s[i],t[i]);
    else if(abs(r1-r2)==1){
      if(s[i]<t[i])ans+=p[r1]-s[i];
      else ans+=s[i]-p[r1-1];
    }
    else if(s[i]<t[i]){
      if(r2%2==r1%2)ans+=p[r1]-s[i]+d[r2-2]-d[r1]+t[i]-p[r2-1];
      else ans+=p[r1]-s[i]+d[r2-1]-d[r1];
    }else{
      if(r2%2==r1%2)ans+=s[i]-p[r1-1]+d[r1-2]-d[r2]+p[r2]-t[i];
      else ans+=s[i]-p[r1-1]+d[r1-2]-d[r2-1];
    }
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 0.09523809523809523
time_ms: 10
memory_kb: 1332
final_rank: 19

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
	EACH(x,vec) is >> x;
	return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
	for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
		if (i) os << " ";
		os << get<0>(t);
	}
	return os;
}
*/
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
	REP(i,vec.size()) {
		if (i) os << " ";
		os << vec[i];
	}
	return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
	REP(i,vec.size()) {
		if (i) os << endl;
		os << vec[i];
	}
	return os;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	ll N, M; cin >> N >> M;
	vector<P> v;
	REP(i, N) {
		ll s, t; cin >> s >> t;
		v.pb({s, t});
	}
	vector<ll> p(M); cin >> p; p.pb(0); p.pb(linf);
	sort(p.begin(), p.end());
	vector<ll> sOdd(p.size(), 0), sEven(p.size(), 0);
	REP(i, (int)p.size()-1) {
		sOdd[i+1] += sOdd[i];
		sEven[i+1] +=+ sEven[i];
		if (i % 2 == 0) {
			sEven[i+1] += p[i+1]-p[i];
		}
		else {
			sOdd[i+1] += p[i+1]-p[i];
		}
	}
	ll ans = 0;
	REP(i, N) {
		ll s, t; tie(s, t) = v[i];
		bool f = false;
		if (s > t) {
			f = true;
			int temp = s; s = t; t = temp;
		}
		int sid = (upper_bound(ALL(p), s)-p.begin())-1;
		int tid = (lower_bound(ALL(p), t)-p.begin())-1;
		// [sid, tid)
		// cout << s << " " << t << " " << sid << " " << tid << endl;
		// ll ba = ans;
		if (tid > sid+1) {
			if ((!f && (sid % 2 == 0)) || (f && (tid%2 == 0))) {
				ans += sEven[tid] - sEven[sid+1];
			}
			else {
				ans += sOdd[tid] - sOdd[sid+1];
			}
		}
		if (sid == tid) { // [s, t)
			ans += t-s;
		}
		else {
			if ((tid-sid)%2 == 0 || !f) {
				ans += p[sid+1]-s;
			}
			if ((tid-sid)%2 == 0 || f) {
				ans += t-p[tid];
			}
		}
		// cout << ans-ba << endl;
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 6848
final_rank: 4

code_snippet: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
	int n, m;
	cin >> n >> m;
	vector<pair<ll, char>> vpii;
	vector<ll> hanabi;
	map<ll, bool> mib;
	REP(i, n) {
		int a, b;
		cin >> a >> b;
		if (a < b) {
			vpii.push_back({ a, 'b' });
			vpii.push_back({ b, 'e' });
		}else{
			vpii.push_back({ a, 'e' });
			vpii.push_back({ b, 'b' });
		}
	}

	REP(i, m) {
		int a;
		cin >> a;
		vpii.push_back({ a,'f' });
		hanabi.push_back(a);
	}
	SORT(hanabi);
	REP(i, n) {
		if (vpii[i * 2].first < vpii[i * 2 + 1].first) {
			mib[vpii[i * 2 + 1].first] =
				(bool)((lower_bound(ALL(hanabi), vpii[i * 2 + 1].first) -
						lower_bound(ALL(hanabi), vpii[i * 2].first)+1) % 2);
			mib[vpii[i * 2].first] = true;
		}
		else {
			mib[vpii[i * 2+1].first] =
				(bool)((lower_bound(ALL(hanabi), vpii[i * 2].first) -
						lower_bound(ALL(hanabi), vpii[i * 2+1].first)+1) % 2);
			mib[vpii[i * 2].first] = true;
		}
	}
	SORT(vpii);
	ll ans = 0;
	ll ex = 0;
	ll vo = 0;
	if (vpii[0].second == 'b')
		if (mib[vpii[0].first]) {
			ex++;
		}
		else {
			vo++;
		}
	FOR(i, 1, vpii.size()) {
		ans += ex*(vpii[i].first - vpii[i - 1].first);
		if (vpii[i].second == 'b')
			if (mib[vpii[i].first]) {
				ex++;
			}
			else {
				vo++;
			}
		if (vpii[i].second == 'e') {
			if (mib[vpii[i].first]) {
				ex--;
			}
			else {
				vo--;
				
				
			}
		}
		if (vpii[i].second == 'f') {
			swap(ex,vo);
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 280
memory_kb: 20792
final_rank: 15

code_snippet: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
	int n, m;
	cin >> n >> m;
	vector<pair<int, char>> vpii;
	vector<int> hanabi;
	map<int, bool> mib;
	REP(i, n) {
		int a, b;
		cin >> a >> b;
		if (a < b) {
			vpii.push_back({ a, 'b' });
			vpii.push_back({ b, 'e' });
		}else{
			vpii.push_back({ a, 'e' });
			vpii.push_back({ b, 'b' });
		}
	}
	REP(i, m) {
		int a;
		cin >> a;
		vpii.push_back({ a,'f' });
		hanabi.push_back(a);
	}
	SORT(hanabi);
	REP(i, n) {
		if (vpii[i * 2].first < vpii[i * 2 + 1].first) {
			mib[vpii[i * 2 + 1].first] =
				(bool)((lower_bound(ALL(hanabi), vpii[i * 2 + 1].first) -
						lower_bound(ALL(hanabi), vpii[i * 2].first)+1) % 2);
			mib[vpii[i * 2].first] = true;
		}
		else {
			mib[vpii[i * 2+1].first] =
				(bool)((lower_bound(ALL(hanabi), vpii[i * 2].first) -
						lower_bound(ALL(hanabi), vpii[i * 2+1].first)+1) % 2);
			mib[vpii[i * 2].first] = true;
		}
	}
	SORT(vpii);
	ll ans = 0;
	int ex = 0;
	int vo = 0;
	if (vpii[0].second == 'b')
		if (mib[vpii[0].first]) {
			ex++;
		}
		else {
			vo++;
		}
	FOR(i, 1, vpii.size()) {
		ans += ex*(vpii[i].first - vpii[i - 1].first);
		if (vpii[i].second == 'b')
			if (mib[vpii[i].first]) {
				ex++;
			}
			else {
				vo++;
			}
		if (vpii[i].second == 'e') {
			if (mib[vpii[i].first]) {
				ex--;
			}
			else {
				vo--;
				
				
			}
		}
		if (vpii[i].second == 'f') {
			swap(ex,vo);
		}
	}
	cout << ans << endl;
}
accuracy: 0.5476190476190477
time_ms: 230
memory_kb: 15032
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
 
 
int N, M;
int s[100000], t[100000];
int sum[2][100003];
 
int main()
{
   
  scanf("%d %d", &N, &M);
  for(int i = 0; i < N; i++) {
    scanf("%d %d", &s[i], &t[i]);
  }
  vector< int > P(M);
  for(int i = 0; i < M; i++) {
    scanf("%d", &P[i]);
  }
  sort(P.begin(), P.end());
 
  long long ret = 0;
  for(int i = 0; i < N; i++) {
    auto p = lower_bound(P.begin(), P.end(), s[i]);
    auto q = lower_bound(P.begin(), P.end(), t[i]);
    if(p == q) {
      ret += abs(t[i] - s[i]);
    } else {
      s[i] > t[i] ? --p : --q;
 
      if((p - q) % 2 != 0) {
        ret += abs(*p - s[i]);
        ret += abs(*q - t[i]);
        if(abs(p - q) == 1) continue;
        s[i] < t[i] ? (++p, --q) : (--p, ++q);
        auto mm = min(p - P.begin(), q - P.begin());
        sum[mm & 1][mm]++;
        sum[mm & 1][max(p - P.begin(), q - P.begin())]--;
      } else {
        ret += abs(*p - s[i]);
        if(abs(p - q) == 0) continue;
        s[i] < t[i] ? ++p : --p;
        auto mm = min(p - P.begin(), q - P.begin());
        sum[mm & 1][mm]++;
        sum[mm & 1][max(p - P.begin(), q - P.begin())]--;
      }
    }
  }
  for(int i = 1; i < M; i++) {
    sum[0][i] += sum[0][i - 1];
    sum[1][i] += sum[1][i - 1];
  }
  for(int i = 0; i < M - 1; i++) {
    ret += 1LL * sum[i & 1][i] * (P[i + 1] - P[i]);
  }
  printf("%lld\n", ret);
}
accuracy: 1
time_ms: 40
memory_kb: 4820
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main()
{
	int n, m;
	cin >> n >> m;
	vector<pii> v;
	REP(i, n)
	{
		int a, b;
		cin >> a >> b;
		v.emplace_back(a, b);
	}
	vi p(m);
	REP(i, m) cin >> p[i];
	sort(ALL(p));
	vi os, es;
	es.push_back(0), es.push_back(p[0]);
	os.push_back(0);
	if(m > 1) os.push_back(p[1] - p[0]);
	else os.push_back(0);
	FOR(i, 2, m)
	{
		if (i % 2) os.push_back(os.back() + p[i] - p[i - 1]);
		else es.push_back(es.back() + p[i] - p[i - 1]);
	}
	ll ans = 0;
	REP(i, n) 
	{
		int a = v[i].first, b = v[i].second;
		ll tmp = 0;
		if (a < b)
		{
			int l = upper_bound(ALL(p), a) - p.begin();
			int r = lower_bound(ALL(p), b) - p.begin() - 1;			
			if (l > r)
			{
				tmp += b - a;
			}
			else if (l % 2)
			{
				if (r % 2)
				{
					tmp += os[r / 2 + 1] - os[l / 2 + 1];
					tmp += p[l] - a;
				}
				else
				{
					tmp += os[(r-1) / 2 + 1] - os[l / 2 + 1];
					tmp += p[l] - a;
					tmp += b - p[r];
				}
			}
			else
			{
				if (r % 2)
				{
					tmp = es[r / 2 + 1] - es[l / 2 + 1];
					tmp += p[l] - a;
					tmp += b - p[r];
				}
				else
				{
					tmp += es[r / 2 + 1] - es[l / 2 + 1];
					tmp += p[l] - a;
				}
			}
		}
		else
		{
			int l = upper_bound(ALL(p), b) - p.begin();
			int r = lower_bound(ALL(p), a) - p.begin() - 1;
			if (l > r)
			{
				tmp += a - b;
			}
			else if (r % 2)
			{
				if (l % 2)
				{
					tmp += es[r / 2 + 1] - es[l / 2 + 1];
					tmp += a - p[r];
				}
				else
				{
					tmp += es[(r - 1) / 2 + 1] - es[l / 2 + 1];
					tmp += p[l] - b;
					tmp += a - p[r];
				}
			}
			else
			{
				if (l % 2)
				{
					tmp = os[(r-1) / 2 + 1] - os[l / 2 + 1];
					tmp += p[l] - b;
					tmp += a - p[r];
				}
				else
				{
					tmp += os[r / 2] - os[l / 2];
					tmp += a - p[r];
				}
			}
		}
		//cout << tmp << endl;
		ans += tmp;
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5748
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

struct sec{
    int s,t;
};

struct event{
    int type;//0:cut +1:begin(on) +2:begin(off) +3:end(on) +4:end(off)
    int x;
    event(int type, int x){
        this->type=type,this->x=x;
    }
};

int main(){
    int N,M;
    cin>>N>>M;
    sec S[N];
    int C[M];
    for(auto&s:S) scanf("%d %d",&s.s,&s.t);
    for(auto&c:C) scanf("%d",&c);

    vector<event> E;
    for(auto&c:C) E.emplace_back(event(0,c));
    sort(C,C+M);
    long long sum=0;

    for(auto&s:S){
        auto lo=lower_bound(C,C+M,min(s.s,s.t));
        auto up=upper_bound(C,C+M,max(s.s,s.t));
        if(lo==up){
            sum+=abs(s.s-s.t);
            continue;
        }
        if(s.s<s.t){
            sum+=*lo-s.s;
            E.emplace_back(event(2,*lo));
            if((up-lo)%2==0){
                sum+=s.t-*(up-1);
                E.emplace_back(event(4,*(up-1)));
            }else{
                E.emplace_back(event(3,*(up-1)));
            }
        }else{
            sum+=s.s-*(up-1);
            E.emplace_back(event(4,*(up-1)));
            if((up-lo)%2){
                E.emplace_back(event(1,*lo));
            }else{
                E.emplace_back(event(2,*lo));
                sum+=*lo-s.t;
            }
        }
    }
    sort(E.begin(),E.end(),[](const event& a, const event& b){return a.x==b.x?a.type<b.type:a.x<b.x;});

    int on_n=0,off_n=0;
    for(auto e=E.begin();e!=E.end();e++){
        // cout << e->x << " " << e->type << endl;
        switch(e->type){
            case 0:
                if(on_n)sum+=(long long int)on_n*(e->x - (e-1)->x);
                swap(on_n,off_n);
                break;
            case 1:
                on_n++;
                break;
            case 2:
                off_n++;
                break;
            case 3:
                off_n--;
                break;
            case 4:
                on_n--;
                break;
        }
    }
    cout << sum << endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 8292
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

struct event{
    int x;
    int type; //0:cut +:?§???? -:??????
    event(int type, int x){
        this->type=type; this->x=x;
    }
};

struct section{
    int s, t;
    bool on;
};

int main(){
    int N,M;
    cin>>N>>M;
    vector<event> E1,E2;
    section S[N+1];
    for(int i=1;i<=N;i++){
        cin>>S[i].s>>S[i].t;
        if(S[i].s<S[i].t){
            E1.push_back(event(i,S[i].s));
            E1.push_back(event(-i,S[i].t));
        }else{
            E2.push_back(event(i,S[i].s));
            E2.push_back(event(-i,S[i].t));
        }
    }
    for(int i=0;i<M;i++){
        int x;
        cin>>x;
        E1.push_back(event(0,x));
        E2.push_back(event(0,x));
    }
    sort(E1.begin(),E1.end(),[](const event& a, const event& b){return a.x==b.x?a.type>b.type:a.x<b.x;});
    sort(E2.begin(),E2.end(),[](const event& a, const event& b){return a.x==b.x?a.type>b.type:a.x>b.x;});
    int sum=0;
    set<int> I;
    for(auto& i:S)i.on=true;
    for(auto& e:E1){
        if(e.type>0){
            I.insert(e.type);
        }else if(e.type<0){
            if(S[-e.type].on)sum+=S[-e.type].t-S[-e.type].s;
            I.erase(-e.type);
        }else{
            for(auto& i:I){
                if(S[i].on){
                    S[i].on=false;
                    sum+=e.x-S[i].s;
                    S[i].s=e.x;
                }else{
                    S[i].on=true;
                    S[i].s=e.x;
                }
            }
        }
    }
    for(auto& i:I){
        if(S[i].on)sum+=S[i].t-S[i].s;
    }
    I.clear();

    for(auto& e:E2){
        if(e.type>0){
            I.insert(e.type);
        }else if(e.type<0){
            if(S[-e.type].on)sum+=-S[-e.type].t+S[-e.type].s;
            I.erase(-e.type);
        }else{
            for(auto& i:I){
                if(S[i].on){
                    S[i].on=false;
                    sum+=-e.x+S[i].s;
                    S[i].s=e.x;
                }else{
                    S[i].on=true;
                    S[i].s=e.x;
                }
            }
        }
    }
    for(auto& i:I){
        if(S[i].on)sum+=-S[i].t+S[i].s;
    }
    I.clear();
    cout << sum << endl;
}
accuracy: 0.09523809523809523
time_ms: 420
memory_kb: 3412
final_rank: 20