Submission #1792959


Source Code Expand

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }


const int MAX = 210000;
long long X;
int K, Q;
long long R[MAX];
long long T[MAX], A[MAX];

long long leftX[MAX], leftY[MAX], rightX[MAX], rightY[MAX];

// r: 偶数~ down, 奇数~ up

int main() {
	while (cin >> X >> K) {
		R[0] = 0;
		for (int i = 0; i < K; ++i) cin >> R[i+1];
		++K;
		cin >> Q;
		for (int i = 0; i < Q; ++i) cin >> T[i] >> A[i];

		leftX[0] = 0, leftY[0] = 0, rightX[0] = X, rightY[0] = X;
		for (int r = 0; r < K - 1; ++r) {
			long long diff = R[r + 1] - R[r];
			leftX[r + 1] = leftX[r], leftY[r + 1] = leftY[r], rightX[r + 1] = rightX[r], rightY[r + 1] = rightY[r];
			if (r % 2 == 0) {
				if (leftY[r] >= diff) leftY[r + 1] -= diff, rightY[r + 1] -= diff;
				else if (rightY[r] >= diff) leftX[r + 1] += diff - leftY[r], leftY[r + 1] = 0, rightY[r + 1] -= diff;
				else leftX[r + 1] = rightX[r], leftY[r + 1] = 0, rightY[r + 1] = 0;
			}
			else {
				if (X - rightY[r] >= diff) leftY[r + 1] += diff, rightY[r + 1] += diff;
				else if (X - leftY[r] >= diff) leftY[r + 1] += diff, rightX[r + 1] -= diff - (X - rightY[r]), rightY[r + 1] = X;
				else leftY[r + 1] = X, rightX[r + 1] = leftX[r], rightY[r + 1] = X;
			}
			//cout << r + 1 << " (" << R[r + 1] << "): " << make_pair(leftX[r + 1], leftY[r + 1]) << ", " << make_pair(rightX[r + 1], rightY[r + 1]) << endl;
		}

		for (int i = 0; i < Q; ++i) {
			int it = upper_bound(R, R + K, T[i]) - R;
			--it;
			long long diff = T[i] - R[it];
			long long cur = 0;
			if (A[i] <= leftX[it]) cur = leftY[it];
			else if (A[i] >= rightX[it]) cur = rightY[it];
			else cur = leftY[it] + (A[i] - leftX[it]);

			if (it % 2 == 0) {
				cur -= diff;
				chmax(cur, 0LL);
			}
			else {
				cur += diff;
				chmin(cur, X);
			}
			//cout << it << ", " << R[it] << ", " << make_pair(leftX[it], leftY[it]) << ", " << make_pair(rightX[it], rightY[it]) << ", " << diff << ", " <<  cur << endl;
			cout << cur << endl;
		}
	}
}













Submission Info

Submission Time
Task F - Sandglass
User drken
Language C++14 (GCC 5.4.1)
Score 700
Code Size 3370 Byte
Status AC
Exec Time 272 ms
Memory 11008 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 42
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 1_033.txt, 1_034.txt, 1_035.txt, 1_036.txt, 1_037.txt, 1_038.txt, 1_039.txt, 1_040.txt, 1_041.txt
Case Name Status Exec Time Memory
0_000.txt AC 3 ms 8448 KB
0_001.txt AC 3 ms 8448 KB
0_002.txt AC 3 ms 8448 KB
1_003.txt AC 235 ms 10240 KB
1_004.txt AC 236 ms 10240 KB
1_005.txt AC 249 ms 10240 KB
1_006.txt AC 229 ms 10240 KB
1_007.txt AC 238 ms 10240 KB
1_008.txt AC 250 ms 10240 KB
1_009.txt AC 230 ms 10240 KB
1_010.txt AC 248 ms 10240 KB
1_011.txt AC 250 ms 10368 KB
1_012.txt AC 236 ms 10368 KB
1_013.txt AC 242 ms 10368 KB
1_014.txt AC 255 ms 10368 KB
1_015.txt AC 239 ms 10496 KB
1_016.txt AC 247 ms 10496 KB
1_017.txt AC 257 ms 10496 KB
1_018.txt AC 241 ms 10624 KB
1_019.txt AC 248 ms 10624 KB
1_020.txt AC 262 ms 10624 KB
1_021.txt AC 246 ms 10496 KB
1_022.txt AC 262 ms 10752 KB
1_023.txt AC 271 ms 10624 KB
1_024.txt AC 251 ms 10496 KB
1_025.txt AC 257 ms 10752 KB
1_026.txt AC 265 ms 10752 KB
1_027.txt AC 256 ms 10496 KB
1_028.txt AC 260 ms 10624 KB
1_029.txt AC 271 ms 10880 KB
1_030.txt AC 262 ms 10624 KB
1_031.txt AC 266 ms 10624 KB
1_032.txt AC 272 ms 11008 KB
1_033.txt AC 223 ms 9216 KB
1_034.txt AC 222 ms 9216 KB
1_035.txt AC 259 ms 10752 KB
1_036.txt AC 229 ms 9344 KB
1_037.txt AC 227 ms 9344 KB
1_038.txt AC 268 ms 10880 KB
1_039.txt AC 237 ms 9088 KB
1_040.txt AC 230 ms 9472 KB
1_041.txt AC 272 ms 11008 KB