Problem:
Description fetch failed. Please view: https://onlinejudge.u-aizu.ac.jp/problems/0536

Submitted solutions:

code_snippet: #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

enum class Balance {
	Left, Neutral, Right
};
template<typename T>
class AVLArray;
template<typename T>
class Node {
public:
	virtual unsigned int size() const = 0;
	virtual unsigned int height() const = 0;
	virtual Balance balance() const = 0;
	virtual void appendToVector(std::vector<std::pair<unsigned int, T>>& destination) const = 0;
};

template<typename T>
class Leaf : public Node<T> {
public:
	unsigned int mSize;
	T mValue;
	Leaf(const unsigned int size, const T& value) : mSize{ size }, mValue{ value } {
		assert(size > 0);
	};
	unsigned int size() const override {
		return mSize;
	}
	unsigned int height() const override {
		return 1;
	}
	Balance balance() const override {
		return Balance::Neutral;
	}
	void appendToVector(std::vector<std::pair<unsigned int, T>>& destination) const override {
		destination.emplace_back(mSize, mValue);
	}
};

template<typename T>
class Branch : public Node<T> {
public:
	std::shared_ptr<Node<T>> mLeft, mRight;
	unsigned int mSize, mHeight;
	Branch(std::shared_ptr<Node<T>> left, std::shared_ptr<Node<T>> right) : mLeft{ std::move(left) }, mRight{ std::move(right) }, mSize{ mLeft->size() + mRight->size() }, mHeight{ std::max(mLeft->height(), mRight->height()) + 1 } {
		assert(mLeft);
		assert(mRight);
		assert(std::abs(static_cast<int>(mLeft->height()) - static_cast<int>(mRight->height())) <= 1);
	};
	unsigned int size() const override {
		return mSize;
	}
	unsigned int height() const override {
		return mHeight;
	}
	Balance balance() const override {
		switch (mLeft->height() - mRight->height()) {
		case 1: return Balance::Left;
		case 0: return Balance::Neutral;
		case -1: return Balance::Right;
		default:
			assert(false);
		}
	}
	void appendToVector(std::vector<std::pair<unsigned int, T>>& destination) const override {
		mLeft->appendToVector(destination);
		mRight->appendToVector(destination);
	}
};
template<typename T>
class AVLArray {
	std::shared_ptr<Node<T>> root{ nullptr };
	static std::shared_ptr<Node<T>> balanceLeft(const std::shared_ptr<Node<T>>& left, const std::shared_ptr<Node<T>>& right) {
		if (std::abs(static_cast<int>(left->height()) - static_cast<int>(right->height())) <= 1) {
			return std::make_shared<Branch<T>>(left, right);
		}
		assert(left->height() == right->height() + 2);
		const auto leftBranch{ dynamic_cast<Branch<T>*>(left.get()) };
		assert(leftBranch);
		if (leftBranch->balance() == Balance::Right) {
			const auto leftRightBranch{ dynamic_cast<Branch<T>*>(leftBranch->mRight.get()) };
			assert(leftRightBranch);
			return std::make_shared<Branch<T>>(std::make_shared<Branch<T>>(leftBranch->mLeft, leftRightBranch->mLeft), std::make_shared<Branch<T>>(leftRightBranch->mRight, right));
		}
		else {
			return std::make_shared<Branch<T>>(leftBranch->mLeft, std::make_shared<Branch<T>>(leftBranch->mRight, right));
		}
	}
	static std::shared_ptr<Node<T>> balanceRight(const std::shared_ptr<Node<T>>& left, const std::shared_ptr<Node<T>>& right) {
		if (std::abs(static_cast<int>(left->height()) - static_cast<int>(right->height())) <= 1) {
			return std::make_shared<Branch<T>>(left, right);
		}
		assert(left->height() + 2 == right->height());
		const auto rightBranch{ dynamic_cast<Branch<T>*>(right.get()) };
		assert(rightBranch);
		if (rightBranch->balance() == Balance::Left) {
			const auto rightLeftBranch{ dynamic_cast<Branch<T>*>(rightBranch->mLeft.get()) };
			assert(rightLeftBranch);
			return std::make_shared<Branch<T>>(std::make_shared<Branch<T>>(left, rightLeftBranch->mLeft), std::make_shared<Branch<T>>(rightLeftBranch->mRight, rightBranch->mRight));
		}
		else {
			return std::make_shared<Branch<T>>(std::make_shared<Branch<T>>(left, rightBranch->mLeft), rightBranch->mRight);
		}
	}
	static std::shared_ptr<Node<T>> merge(const std::shared_ptr<Node<T>>& left, const std::shared_ptr<Node<T>>& right) {
		if (left == nullptr) {
			return right;
		}
		else if (right == nullptr) {
			return left;
		}
		else {
			const auto diff{ static_cast<int>(left->height()) - static_cast<int>(right->height()) };
			if (diff < -1) {
				const auto rightBranch{ dynamic_cast<Branch<T>*>(right.get()) };
				assert(rightBranch);
				return balanceLeft(merge(left, rightBranch->mLeft), rightBranch->mRight);
			}
			else if (1 < diff) {
				const auto leftBranch{ dynamic_cast<Branch<T>*>(left.get()) };
				assert(leftBranch);
				return balanceRight(leftBranch->mLeft, merge(leftBranch->mRight, right));
			}
			else {
				return std::make_shared<Branch<T>>(left, right);
			}
		}
	}
	static std::pair<std::shared_ptr<Node<T>>, std::shared_ptr<Node<T>>> split(const std::shared_ptr<Node<T>>& node, const unsigned int size) {
		if (size == 0) {
			return std::make_pair(nullptr, node);
		}
		else if (node == nullptr || node->size() <= size) {
			return std::make_pair(node, nullptr);
		}
		else if (const auto branch = dynamic_cast<Branch<T>*>(node.get()); branch) {
			if (size == branch->mLeft->size()) {
				return std::make_pair(branch->mLeft, branch->mRight);
			}
			else if (size < branch->mLeft->size()) {
				const auto [left, right] = split(branch->mLeft, size);
				return std::make_pair(left, merge(right, branch->mRight));
			}
			else {
				const auto [left, right] = split(branch->mRight, size - branch->mLeft->size());
				return std::make_pair(merge(branch->mLeft, left), right);
			}
		}
		else {
			const auto leaf{ dynamic_cast<Leaf<T>*>(node.get()) };
			assert(leaf);
			return std::make_pair(std::make_shared<Leaf<T>>(size, leaf->mValue), std::make_shared<Leaf<T>>(leaf->size() - size, leaf->mValue));
		}
	}
	AVLArray(const std::shared_ptr<Node<T>>& root) : root{ root } {};
public:
	AVLArray() : AVLArray(nullptr) {};
	AVLArray<T> operator+(const AVLArray<T>& other) const {
		return AVLArray<T>{merge(root, other.root)};
	}
	AVLArray<T> appended(const unsigned int size, const T& value) const {
		return merge(root, std::make_shared<Leaf<T>>(size, value));
	}
	AVLArray<T> prepended(const unsigned int size, const T& value) const {
		return merge(std::make_shared<Leaf<T>>(size, value), root);
	}
	std::pair<AVLArray<T>, AVLArray<T>> splited(const unsigned int size) const {
		const auto [left, right] = split(root, size);
		return std::make_pair(AVLArray{ left }, AVLArray{ right });
	}
	std::vector<std::pair<unsigned int, T>> convertToVector() const {
		std::vector<std::pair<unsigned int, T>> vec;
		if (root) {
			root->appendToVector(vec);
		}
		return vec;
	}
	unsigned int size() const {
		return root ? root->size() : 0;
	}
};
int solve(const int n, const int p, const int q, const int r, const std::vector<std::pair<int, int>>& queries) {
	AVLArray<bool> array;
	array = array.prepended(r, true);
	array = array.appended(n - r, false);
	for (const auto [x, y] : queries) {
		const auto [leftMid, right] = array.splited(y);
		const auto [left, mid] = leftMid.splited(x);
		array = right + mid + left;
	}
	const auto resultVec = array.splited(q).first.splited(p).second.convertToVector();
	return std::accumulate(resultVec.begin(), resultVec.end(), 0, [](const int acc, const std::pair<unsigned int, bool>& p) {
		return acc + (p.second ? p.first : 0);
		});
}
int main() {
	while (true) {
		int n; std::cin >> n;
		if (n == 0) break;
		int m; std::cin >> m;
		int p, q, r; std::cin >> p >> q >> r; --p;
		std::vector<std::pair<int, int>> queries(m);
		for (auto& [x, y] : queries) {
			std::cin >> x >> y;
		}
		const auto result{ solve(n, p, q, r, queries) };
		std::cout << result << '\n';
	}
}
accuracy: 1
time_ms: 40
memory_kb: 4048
final_rank: 19

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

vector<pair<ll,ll>> shu(vector<pair<ll,ll>> S,ll x,ll y){
    vector<pair<ll,ll>> A,B,C;
    int N=S.size();
    ll sm=0;
    for(int i=0;i<N;i++){
        ll d=S[i].second-S[i].first+1;
        if(sm<x)A.push_back(S[i]);
        else if(sm<y)B.push_back(S[i]);
        else C.push_back(S[i]);
        sm+=d;
    }
    for(auto v:B)C.push_back(v);
    for(auto v:A)C.push_back(v);
    return C;
}


vector<pair<ll,ll>> sep(vector<pair<ll,ll>> S,ll x){
    int N=S.size();
    vector<pair<ll,ll>> A;
    ll sm=0;
    for(int i=0;i<N;i++){
        ll d=S[i].second-S[i].first+1;
        if(sm+d<=x){
            A.push_back(S[i]);
        }
        else if(sm<x){
            A.push_back({S[i].first,S[i].first+x-sm-1});
            A.push_back({S[i].first+x-sm,S[i].second});
        }
        else A.push_back(S[i]);
        sm+=d;
    }
    return A;
}


int main() {
    ll N,M;
    while(cin>>N,N!=0){
    cin>>M;
    vector<pair<ll,ll>> C={{1,N}};
    ll P,Q,R;
    cin>>P>>Q>>R;
    for(int i=0;i<M;i++){
        ll x,y;
        cin>>x>>y;
        C=sep(C,x);
        C=sep(C,y);
        C=shu(C,x,y);
        // for(auto [l,r]:C)cout<<l<<" "<<r<<endl;
    }
    if(P!=1)C=sep(C,P-1);
    C=sep(C,Q);

    int CN=C.size();
    ll sm=0;
    ll an=0;
    for(int i=0;i<CN;i++){
        ll d=C[i].second-C[i].first+1;
        if(sm<P-1){
        }
        else if(sm<Q){
            an+=min(R,C[i].second);
            an-=min(R,C[i].first-1);
        }
        else {

        }
        sm+=d;
    }
    cout<<an<<endl;
}}
accuracy: 1
time_ms: 760
memory_kb: 3684
final_rank: 20

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> tmp_card_info_vec0;
		vector<CardInfo> tmp_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int i = -1;
		int j = -1;
		int k = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			tmp_card_info_vec0.clear();
			tmp_card_info_vec1.clear();
			is_found = false;

			for (j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					tmp_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (j = 0; j < tmp_card_info_vec0.size(); j++) {
				int& z = tmp_card_info_vec0[j].begin_num;

				c_num_cards += tmp_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					tmp_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec1.push_back(tmp_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (k = j_b + 1; k < tmp_card_info_vec1.size(); k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3196
final_rank: 1

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> tmp_card_info_vec0;
		vector<CardInfo> tmp_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			tmp_card_info_vec0.clear();
			tmp_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					tmp_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < tmp_card_info_vec0.size(); j++) {
				int& z = tmp_card_info_vec0[j].begin_num;

				c_num_cards += tmp_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					tmp_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec1.push_back(tmp_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < tmp_card_info_vec1.size(); k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3488
final_rank: 11

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> tmp_card_info_vec0;
		vector<CardInfo> tmp_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			tmp_card_info_vec0.clear();
			tmp_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					tmp_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < tmp_card_info_vec0.size(); j++) {
				int& z = tmp_card_info_vec0[j].begin_num;

				c_num_cards += tmp_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					tmp_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec1.push_back(tmp_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < tmp_card_info_vec1.size(); k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int zq = zb + q - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int ze = zb + card_info.num_cards - 1;
				int idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int ze = zb + card_info.num_cards - 1;
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int zq = zb + q - p_num_cards - 1;
				int idx_r = r - zb;

				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3344
final_rank: 5

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> tmp_card_info_vec0;
		vector<CardInfo> tmp_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			tmp_card_info_vec0.clear();
			tmp_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					tmp_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < tmp_card_info_vec0.size(); j++) {
				int& z = tmp_card_info_vec0[j].begin_num;

				c_num_cards += tmp_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					tmp_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec1.push_back(tmp_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < tmp_card_info_vec1.size(); k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3508
final_rank: 14

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> tmp_card_info_vec0;
		vector<CardInfo> tmp_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			int x = -1;
			int y = -1;
			int j_a = -1;
			int j_b = -1;

			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			tmp_card_info_vec0.clear();
			tmp_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					tmp_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < tmp_card_info_vec0.size(); j++) {
				int& z = tmp_card_info_vec0[j].begin_num;

				c_num_cards += tmp_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					tmp_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec1.push_back(tmp_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < tmp_card_info_vec1.size(); k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 3480
final_rank: 12

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> tmp_card_info_vec0;
		vector<CardInfo> tmp_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		int ze = -1;
		int zp = -1;
		int zq = -1;
		int idx_r = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			tmp_card_info_vec0.clear();
			tmp_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					tmp_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < tmp_card_info_vec0.size(); j++) {
				int& z = tmp_card_info_vec0[j].begin_num;

				c_num_cards += tmp_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					tmp_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec1.push_back(tmp_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < tmp_card_info_vec1.size(); k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;

			ze = zb + card_info.num_cards - 1;
			zp = zb + p - p_num_cards - 1;
			zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 3460
final_rank: 13

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> tmp_card_info_vec0;
		vector<CardInfo> tmp_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		int ze = -1;
		int zp = -1;
		int zq = -1;
		int idx_r = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			tmp_card_info_vec0.clear();
			tmp_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					tmp_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < tmp_card_info_vec0.size(); j++) {
				int& z = tmp_card_info_vec0[j].begin_num;

				c_num_cards += tmp_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					tmp_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec1.push_back(tmp_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < tmp_card_info_vec1.size(); k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;


			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				zq = zb + q - p_num_cards - 1;
				idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				zp = zb + p - p_num_cards - 1;
				ze = zb + card_info.num_cards - 1;
				idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				ze = zb + card_info.num_cards - 1;
				idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				zq = zb + q - p_num_cards - 1;
				idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3344
final_rank: 5

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> tmp_card_info_vec0;
		vector<CardInfo> tmp_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		int ze = -1;
		int zp = -1;
		int zq = -1;
		int idx_r = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			tmp_card_info_vec0.clear();
			tmp_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					tmp_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < tmp_card_info_vec0.size(); j++) {
				int& z = tmp_card_info_vec0[j].begin_num;

				c_num_cards += tmp_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					tmp_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec1.push_back(tmp_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < tmp_card_info_vec1.size(); k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;

			ze = zb + card_info.num_cards - 1;
			zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				zp = zb + p - p_num_cards - 1;
				idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				zp = zb + p - p_num_cards - 1;
				idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3428
final_rank: 8

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> tmp_card_info_vec0;
		vector<CardInfo> tmp_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			tmp_card_info_vec0.clear();
			tmp_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					tmp_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < tmp_card_info_vec0.size(); j++) {
				int& z = tmp_card_info_vec0[j].begin_num;

				c_num_cards += tmp_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					tmp_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						tmp_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					tmp_card_info_vec1.push_back(tmp_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < tmp_card_info_vec1.size(); k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(tmp_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int zq = zb + q - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int ze = zb + card_info.num_cards - 1;
				int idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int ze = zb + card_info.num_cards - 1;
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int zq = zb + q - p_num_cards - 1;
				int idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3264
final_rank: 3

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> n_card_info_vec0;
		vector<CardInfo> n_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			n_card_info_vec0.clear();
			n_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					n_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					n_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < n_card_info_vec0.size(); j++) {
				int& z = n_card_info_vec0[j].begin_num;

				c_num_cards += n_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					n_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					n_card_info_vec1.push_back(n_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < n_card_info_vec1.size(); k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3460
final_rank: 9

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		vector<CardInfo> card_info_vec;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			int x = -1;
			int y = -1;
			vector<CardInfo> n_card_info_vec0;
			vector<CardInfo> n_card_info_vec1;
			int j_a = -1;
			int j_b = -1;

			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			n_card_info_vec0.clear();
			n_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					n_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					n_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < n_card_info_vec0.size(); j++) {
				int& z = n_card_info_vec0[j].begin_num;

				c_num_cards += n_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					n_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					n_card_info_vec1.push_back(n_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < n_card_info_vec1.size(); k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 3460
final_rank: 17

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> n_card_info_vec0;
		vector<CardInfo> n_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			int x = -1;
			int y = -1;

			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			n_card_info_vec0.clear();
			n_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					n_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					n_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < n_card_info_vec0.size(); j++) {
				int& z = n_card_info_vec0[j].begin_num;

				c_num_cards += n_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					n_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					n_card_info_vec1.push_back(n_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < n_card_info_vec1.size(); k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3580
final_rank: 16

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> n_card_info_vec0;
		vector<CardInfo> n_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			n_card_info_vec0.clear();
			n_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					n_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					n_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < n_card_info_vec0.size(); j++) {
				int& z = n_card_info_vec0[j].begin_num;

				c_num_cards += n_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					n_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					n_card_info_vec1.push_back(n_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			for (int k = j_b + 1; k < n_card_info_vec1.size(); k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zp = -1;
			int zq = zb + q - p_num_cards - 1;
			int idx_r = -1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				zp = zb + p - p_num_cards - 1;
				idx_r = r - zp;
				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				zp = zb + p - p_num_cards - 1;
				idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				idx_r = r - zb;
				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3480
final_rank: 15

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> n_card_info_vec0;
		vector<CardInfo> n_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			n_card_info_vec0.clear();
			n_card_info_vec1.clear();
			n_card_info_vec0.reserve(sizeof(CardInfo) * m);
			n_card_info_vec1.reserve(sizeof(CardInfo) * m);
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int& z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					n_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					n_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < n_card_info_vec0.size(); j++) {
				int& z = n_card_info_vec0[j].begin_num;

				c_num_cards += n_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					n_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					n_card_info_vec1.push_back(n_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}

			card_info_vec.clear();
			card_info_vec.reserve(sizeof(CardInfo) * m);
			for (int k = j_b + 1; k < n_card_info_vec1.size(); k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
		}

		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zp = -1;
			int zq = zb + q - p_num_cards - 1;
			int idx_r = -1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				zp = zb + p - p_num_cards - 1;
				idx_r = r - zp;
				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				zp = zb + p - p_num_cards - 1;
				idx_r = r - zp;
				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				idx_r = r - zb;
				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				idx_r = r - zb;
				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3344
final_rank: 5

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>

using namespace std;

class CardInfo {
public:
	int begin_num;
	int num_cards;

public:
	CardInfo(int b, int n) : begin_num(b), num_cards(n) {
	}

	~CardInfo() {
	}
};


int main(int argc, char** argv) {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int m = -1;
		int p = -1;
		int q = -1;
		int r = -1;
		vector<CardInfo> card_info_vec;
		vector<CardInfo> n_card_info_vec0;
		vector<CardInfo> n_card_info_vec1;
		int p_num_cards = -1;
		int c_num_cards = -1;
		int x = -1;
		int y = -1;
		int j_a = -1;
		int j_b = -1;
		bool is_found = false;
		int result = 0;

		cin >> n;
		if (n == 0) {
			break;
		}

		cin >> m >> p >> q >> r;
		card_info_vec.push_back(CardInfo(1, n));

		for (int i = 0; i < m; i++) {
			cin >> x >> y;
			p_num_cards = 0;
			c_num_cards = 0;
			n_card_info_vec0.clear();
			n_card_info_vec1.clear();
			is_found = false;

			for (int j = 0; j < card_info_vec.size(); j++) {
				int &z = card_info_vec[j].begin_num;

				c_num_cards += card_info_vec[j].num_cards;
				if (x <= c_num_cards && !is_found) {
					int offset = x - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - x);

					n_card_info_vec0.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec0.push_back(card_info1);
					}
					j_a = j;
					is_found = true;
				}
				else {
					n_card_info_vec0.push_back(card_info_vec[j]);
				}
				p_num_cards = c_num_cards;
			}

			p_num_cards = 0;
			c_num_cards = 0;
			is_found = false;
			for (int j = 0; j < n_card_info_vec0.size(); j++) {
				int& z = n_card_info_vec0[j].begin_num;

				c_num_cards += n_card_info_vec0[j].num_cards;
				if (y <= c_num_cards && !is_found) {
					int offset = y - p_num_cards;
					CardInfo card_info0(z, offset);
					CardInfo card_info1(z + offset, c_num_cards - y);

					n_card_info_vec1.push_back(card_info0);
					if (card_info1.num_cards > 0) {
						n_card_info_vec1.push_back(card_info1);
					}
					j_b = j;
					is_found = true;
				}
				else {
					n_card_info_vec1.push_back(n_card_info_vec0[j]);
				}
				p_num_cards = c_num_cards;
			}
			
			card_info_vec.clear();

			for (int k = j_b + 1; k < n_card_info_vec1.size(); k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = j_a + 1; k < j_b + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
			for (int k = 0; k < j_a + 1; k++) {
				card_info_vec.push_back(n_card_info_vec1[k]);
			}
		}
		p_num_cards = 0;
		c_num_cards = 0;
		is_found = false;
		result = 0;
		for (CardInfo& card_info : card_info_vec) {
			int& zb = card_info.begin_num;
			int ze = zb + card_info.num_cards - 1;
			int zq = zb + q - p_num_cards - 1;

			c_num_cards += card_info.num_cards;
			if (q <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), zq - zp) + 1;
				break;
			}
			else if (p <= c_num_cards && !is_found) {
				int zp = zb + p - p_num_cards - 1;
				int idx_r = r - zp;

				result += min(max(idx_r, -1), ze - zp) + 1;
				is_found = true;
			}
			else if (q > c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), ze - zb) + 1;
			}
			else if (q <= c_num_cards && is_found) {
				int idx_r = r - zb;

				result += min(max(idx_r, -1), zq - zb) + 1;
				break;
			}
			p_num_cards = c_num_cards;
		}
		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3480
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcount(v)
#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))
using namespace std;
using ll = long long;
template<class T>using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    while(1){
    INT(n);
    if(!n)return 0;
    INT(m);
    deque<pair<int,int>> dq;
    dq.push_back({1,n});
    INT(p,q,r);
    auto shuffle=[&](int x,int y){
        deque<pair<int,int>> A,B;
        y-=x;
        while(x>=1){
            if(x>=dq.front().second-dq.front().first+1){
                x-=dq.front().second-dq.front().first+1;
                A.push_back(dq.front());
                dq.pop_front();
            }else{
                int xr=dq.front().first+x-1,xrr=dq.front().second;
                A.push_back({dq.front().first,xr});
                dq.pop_front();
                dq.push_front({xr+1,xrr});
                x=0;
            }
        }
        while(y>=1){
            if(y>=dq.front().second-dq.front().first+1){
                y-=dq.front().second-dq.front().first+1;
                B.push_back(dq.front());
                dq.pop_front();
            }else{
                int yr=dq.front().first+y-1,yrr=dq.front().second;
                B.push_back({dq.front().first,yr});
                dq.pop_front();
                dq.push_front({yr+1,yrr});
                y=0;
            }
        }
        while(!B.empty()){
            dq.push_back(B.front());
            B.pop_front();
        }
        while(!A.empty()){
            dq.push_back(A.front());
            A.pop_front();
        }
    };
    auto count=[&](int x,int y,int r){
        int ans=0;
        x--;
        y-=x;
        while(x>=1){
            if(x>=dq.front().second-dq.front().first+1){
                x-=dq.front().second-dq.front().first+1;
                dq.pop_front();
            }else{
                int xr=dq.front().first+x-1,xrr=dq.front().second;
                dq.pop_front();
                dq.push_front({xr+1,xrr});
                x=0;
            }
        }
        while(y>=1){
            if(y>=dq.front().second-dq.front().first+1){
                y-=dq.front().second-dq.front().first+1;
                ans+=max(0,min(r,dq.front().second)-dq.front().first+1);
                dq.pop_front();
            }else{
                int yr=dq.front().first+y-1,yrr=dq.front().second;
                ans+=max(0,min(r,yr)-dq.front().first+1);
                dq.pop_front();
                y=0;
            }
        }
        return ans;
    };
    while(m--){
        INT(x,y);
        shuffle(x,y);
    }
    out(count(p,q,r));
    }
}
accuracy: 1
time_ms: 120
memory_kb: 3340
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
//typedef int ll;
typedef pair<ll, ll> P;
const long long INF = 1e9;
template <typename T>
void input_arr(vector<T>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i];
	}
}
template <typename T, typename Q>
void input_arr(vector<pair<T, Q>>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i].first >> A[i].second;
	}
}
template <typename T>
void input_arr(vector<vector<T>>& A, ll H, ll W) {
	for (ll i = 0; i < H; i++) {
		for (ll j = 0; j < W; j++) {
			cin >> A[i][j];
		}
	}
}
bool cmp(P a, P b) {
    if (a.first != b.first) return a.first < b.first;
    else return a.second > b.second;
}
int main() {
    ll N, M; cin >> N >> M;
    while (N != 0) {
    ll x, y, z; cin >> x >> y >> z;
    vector<P>A;
    A.push_back({1, N});
    for (ll i = 0; i < M; i++) {
        vector<vector<P>>B(3);
        ll a, b; cin >> a >> b;
        ll pos = 0, left = A[0].first, right = A[0].second;
        ll sum = 0;
        while (pos < A.size()) {
            if (sum + (right - left + 1) < a) {
                sum += (right - left + 1);
                B[0].push_back({left, right});
                pos++;
                if (pos < A.size()) {left = A[pos].first; right = A[pos].second;}
            }
            else if (sum < a && sum + (right - left + 1) >= a) {
                B[0].push_back({left, left + (a - sum - 1)});
                left += (a - sum);
                sum = a;
                if (left > right) {
                    pos++;
                    left = A[pos].first; right = A[pos].second;
                }
            }
            else if (sum >= a) {
                if (sum + (right - left + 1) < b) {
                    sum += (right - left + 1);
                    B[1].push_back({left, right});
                    pos++;
                    if (pos < A.size()) {left = A[pos].first; right = A[pos].second;}
                }
                else if (sum < b && sum + (right - left + 1) >= b) {
                    B[1].push_back({left, left + (b - sum - 1)});
                    left += (b - sum);
                    sum = b;
                    if (left > right) {
                        pos++;
                        left = A[pos].first; right = A[pos].second;
                    }
                }
                else if (sum >= b) {
                    B[2].push_back({left, right});
                    sum += (right - left + 1);
                    pos++;
                    if (pos < A.size()) {left = A[pos].first; right = A[pos].second;}
                }
            }
        }
        swap(B[0], B[2]);
        A.clear();
        for (ll j = 0; j < 3; j++) {
            for (P a : B[j]) {
                A.push_back(a);
            }
        }
    }
    ll sum = 0, ans = 0;
    for (ll i = 0; i < A.size(); i++) {
        if (sum + (A[i].second - A[i].first + 1) < x) {
            sum += (A[i].second - A[i].first + 1);
        }
        else if (sum < x && sum + (A[i].second - A[i].first + 1) >= x) {
            if (sum + (A[i].second - A[i].first + 1) < y) {
                ll left = A[i].first + (x - sum - 1);
                ans += min(z, A[i].second) - min(z, max(0LL, left - 1));
                sum += (A[i].second - A[i].first + 1);
            }
            else {
                ll left = A[i].first + (x - sum - 1), right = A[i].first + (y - sum - 1);
                ans += min(z, right) - min(z, max(0LL, left - 1));
                break;
            }
        }
        else if (sum >= x) {
            if (sum >= y) break;
            if (sum + (A[i].second - A[i].first + 1) < y) {
                sum += (A[i].second - A[i].first + 1);
                ans += min(z, A[i].second) - min(z, max(0LL, A[i].first - 1));
            }
            else {
                ll right = A[i].first + (y - sum - 1);
                ans += min(z, right) - min(z, max(0LL, A[i].first - 1));
                break;
            }
        }
    }
    cout << ans << endl; cin >> N >> M;
    }
}
accuracy: 1
time_ms: 290
memory_kb: 3648
final_rank: 18

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

// This struct represents interval [s,t]
struct Bundle{
    int s,t;
    Bundle() : s(0), t(0) {}
    Bundle(int s_, int t_) : s(s_), t(t_) {}
    int count(void){
        return t-s+1;
    }
};

std::vector<Bundle> Shuffle(int x, int y, std::vector<Bundle> card){
    std::vector<Bundle> first, second;
    std::vector<Bundle> res;
    int total = 0;
    int idx = 0;
    while(total + card[idx].count() < x){
        first.push_back(card[idx]);
        total += card[idx].count();
        ++idx;
    }
    if(total + card[idx].count() > x){
        first.emplace_back(card[idx].s, card[idx].s+x-total-1);
        total += first.back().count();
        card[idx].s = first.back().t+1;
    }else{
        first.push_back(card[idx]);
        total += card[idx++].count();
    }
    while(total + card[idx].count() < y){
        second.push_back(card[idx]);
        total += card[idx].count();
        ++idx;
    }
    if(total + card[idx].count() > y){
        second.emplace_back(card[idx].s, card[idx].s+y-total-1);
        total += second.back().count();
        card[idx].s = second.back().t+1;
    }else{
        second.push_back(card[idx]);
        total += card[idx++].count();
    }
    while(idx < card.size()) res.push_back(card[idx++]);
    for(auto s : second) res.push_back(s);
    for(auto f : first) res.push_back(f);
    return res;
}

int count(int P, int Q, int R, std::vector<Bundle> card){
    int ans = 0;
    int idx = 0;
    int count = 0; // total cards card[0..idx] bundles
    while(count + card[idx].count() < P)
        count += card[idx++].count();

    if(count + card[idx].count() >= P){
        if(card[idx].s > R){
            count += card[idx++].count();
        }else{
            int p = card[idx].s + (P - count-1);
            int q = std::min(card[idx].t, R);
            ans += std::min(q-p+1, Q-count);
            count += card[idx++].count();
        }
    }
    while(idx < card.size() && Q > count){
        if(card[idx].s > R){
            count += card[idx++].count();
            continue;
        }
        int p = card[idx].s;
        int q = std::min(card[idx].t, R);
        ans += std::min(q-p+1, Q-count);
        count += card[idx++].count();
    }

    return ans;
}

int main(){
    int N,M;
    while(std::cin >> N, N){
        std::vector<Bundle> card;
        card.emplace_back(1,N);
        std::cin >> M;
        int P,Q,R; std::cin >> P >> Q >> R;
        for(int m=0; m<M; ++m){
            int x,y; std::cin >> x >> y;
            card = Shuffle(x, y, card);
        }
        std::cout << count(P, Q, R, card) << std::endl;
    }
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 3260
final_rank: 4