Submission #163744

# Submission time Handle Problem Language Result Execution time Memory
163744 2019-11-15T05:07:45 Z jwvg0425 초음속철도 (OJUZ11_rail) C++17
100 / 100
717 ms 56948 KB
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second
#define MOD ((i64)1e9 + 7)

using namespace std;

using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

class Mapping
{
public:
	void init(const vector<int>& raw, int base = 0)
	{
		start = base;
		arr = raw;
		sort(arr.begin(), arr.end());
		arr.erase(unique(arr.begin(), arr.end()), arr.end());
	}

	int get_idx(int k)
	{
		return start + (lower_bound(all(arr), k) - arr.begin());
	}

	int get_value(int idx)
	{
		return arr[idx - start];
	}

	int size()
	{
		return arr.size();
	}

private:
	int start;
	vector<int> arr;
};

i64 two[300005];

void updateLazy(vector<i64>& tree, vector<i64>& lazy, int start, int end, int node)
{
	if (lazy[node] == 0)
		return;

	tree[node] = (tree[node] * two[lazy[node]]) % MOD;

	if (start != end)
	{
		lazy[node * 2] += lazy[node];
		lazy[node * 2 + 1] += lazy[node];
	}

	lazy[node] = 0;
}

// * 2
void multRange(vector<i64>& tree, vector<i64>& lazy, int start, int end, int node, int left, int right)
{
	updateLazy(tree, lazy, start, end, node);

	if (left > end || right < start)
		return;

	if (left <= start && end <= right)
	{
		tree[node] = (tree[node] * 2) % MOD;

		if (start != end)
		{
			lazy[node * 2] += 1;
			lazy[node * 2 + 1] += 1;
		}

		return;
	}

	int mid = (start + end) / 2;
	multRange(tree, lazy, start, mid, node * 2, left, right);
	multRange(tree, lazy, mid + 1, end, node * 2 + 1, left, right);
	tree[node] = (tree[node * 2] + tree[node * 2 + 1]) % MOD;
}

void add(vector<i64>& tree, vector<i64>& lazy, int start, int end, int node, int pos, i64 diff)
{
	updateLazy(tree, lazy, start, end, node);

	if (pos > end || pos < start)
		return;

	if (start == end)
	{
		tree[node] = (tree[node] + diff) % MOD;
		return;
	}

	int mid = (start + end) / 2;
	add(tree, lazy, start, mid, node * 2, pos, diff);
	add(tree, lazy, mid + 1, end, node * 2 + 1, pos, diff);
	tree[node] = (tree[node * 2] + tree[node * 2 + 1]) % MOD;
}

i64 query(vector<i64>& tree, vector<i64>& lazy, int start, int end, int node, int left, int right)
{
	updateLazy(tree, lazy, start, end, node);

	if (left > end || right < start)
		return 0;

	if (left <= start && end <= right)
		return tree[node];

	int mid = (start + end) / 2;
	return query(tree, lazy, start, mid, node * 2, left, right) +
		query(tree, lazy, mid + 1, end, node * 2 + 1, left, right);
}


int main()
{
	int n, m;
	scanf("%d %d", &n, &m);

	vector<ii> train(m);
	vector<int> pos = { 1, n };

	two[0] = 1;

	for (int i = 1; i <= m + 5; i++)
		two[i] = (two[i - 1] * 2) % MOD;

	for (int i = 0; i < m; i++)
	{
		scanf("%d %d", &train[i].xx, &train[i].yy);
		pos.push_back(train[i].xx);
		pos.push_back(train[i].yy);
	}

	Mapping mapping;
	mapping.init(pos);

	for (int i = 0; i < m; i++)
	{
		train[i].xx = mapping.get_idx(train[i].xx);
		train[i].yy = mapping.get_idx(train[i].yy);
	}

	sort(all(train));

	int k = mapping.size();
	vector<i64> tree(k * 8);
	vector<i64> lazy(k * 8);

	add(tree, lazy, 0, k, 1, 0, 1);

	for (int i = 0; i < m; i++)
	{
		// 모든 구간에 대해 경우의 수 유지하며 다음 단계로
		// train[i].yy 지점에서 train[i].xx ~ train[i].yy 까지 전부 더함
		i64 q = query(tree, lazy, 0, k, 1, train[i].xx, train[i].yy);
		add(tree, lazy, 0, k, 1, train[i].yy, q);

		// train[i].yy 지점 이후는 값이 2배씩 상승함(train[i] 고르거나 안 고르거나)
		multRange(tree, lazy, 0, k, 1, train[i].yy + 1, k);
	}

	printf("%lld\n", query(tree, lazy, 0, k, 1, k - 1, k - 1));

	return 0;
}

Compilation message

rail.cpp: In function 'int main()':
rail.cpp:142:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
rail.cpp:154:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &train[i].xx, &train[i].yy);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 252 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 252 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 2 ms 256 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 2 ms 356 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 15 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 256 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 18 ms 636 KB Output is correct
5 Correct 330 ms 13800 KB Output is correct
6 Correct 471 ms 52584 KB Output is correct
7 Correct 454 ms 52072 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 434 ms 48904 KB Output is correct
10 Correct 456 ms 51400 KB Output is correct
11 Correct 178 ms 8724 KB Output is correct
12 Correct 479 ms 52524 KB Output is correct
13 Correct 462 ms 51880 KB Output is correct
14 Correct 99 ms 6760 KB Output is correct
15 Correct 426 ms 31808 KB Output is correct
16 Correct 103 ms 6888 KB Output is correct
17 Correct 471 ms 52676 KB Output is correct
18 Correct 469 ms 52776 KB Output is correct
19 Correct 467 ms 46184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 252 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 2 ms 256 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 2 ms 356 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 15 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 256 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 13 ms 1784 KB Output is correct
37 Correct 12 ms 1528 KB Output is correct
38 Correct 9 ms 888 KB Output is correct
39 Correct 4 ms 504 KB Output is correct
40 Correct 10 ms 1144 KB Output is correct
41 Correct 12 ms 1784 KB Output is correct
42 Correct 13 ms 1832 KB Output is correct
43 Correct 2 ms 376 KB Output is correct
44 Correct 23 ms 1400 KB Output is correct
45 Correct 4 ms 504 KB Output is correct
46 Correct 10 ms 1144 KB Output is correct
47 Correct 17 ms 1272 KB Output is correct
48 Correct 11 ms 1144 KB Output is correct
49 Correct 6 ms 508 KB Output is correct
50 Correct 6 ms 504 KB Output is correct
51 Correct 6 ms 504 KB Output is correct
52 Correct 4 ms 636 KB Output is correct
53 Correct 13 ms 1784 KB Output is correct
54 Correct 7 ms 760 KB Output is correct
55 Correct 13 ms 1784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 252 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 2 ms 256 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 2 ms 356 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 15 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 256 KB Output is correct
28 Correct 2 ms 256 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 256 KB Output is correct
38 Correct 2 ms 256 KB Output is correct
39 Correct 18 ms 636 KB Output is correct
40 Correct 330 ms 13800 KB Output is correct
41 Correct 471 ms 52584 KB Output is correct
42 Correct 454 ms 52072 KB Output is correct
43 Correct 2 ms 376 KB Output is correct
44 Correct 434 ms 48904 KB Output is correct
45 Correct 456 ms 51400 KB Output is correct
46 Correct 178 ms 8724 KB Output is correct
47 Correct 479 ms 52524 KB Output is correct
48 Correct 462 ms 51880 KB Output is correct
49 Correct 99 ms 6760 KB Output is correct
50 Correct 426 ms 31808 KB Output is correct
51 Correct 103 ms 6888 KB Output is correct
52 Correct 471 ms 52676 KB Output is correct
53 Correct 469 ms 52776 KB Output is correct
54 Correct 467 ms 46184 KB Output is correct
55 Correct 13 ms 1784 KB Output is correct
56 Correct 12 ms 1528 KB Output is correct
57 Correct 9 ms 888 KB Output is correct
58 Correct 4 ms 504 KB Output is correct
59 Correct 10 ms 1144 KB Output is correct
60 Correct 12 ms 1784 KB Output is correct
61 Correct 13 ms 1832 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
63 Correct 23 ms 1400 KB Output is correct
64 Correct 4 ms 504 KB Output is correct
65 Correct 10 ms 1144 KB Output is correct
66 Correct 17 ms 1272 KB Output is correct
67 Correct 11 ms 1144 KB Output is correct
68 Correct 6 ms 508 KB Output is correct
69 Correct 6 ms 504 KB Output is correct
70 Correct 6 ms 504 KB Output is correct
71 Correct 4 ms 636 KB Output is correct
72 Correct 13 ms 1784 KB Output is correct
73 Correct 7 ms 760 KB Output is correct
74 Correct 13 ms 1784 KB Output is correct
75 Correct 694 ms 56948 KB Output is correct
76 Correct 422 ms 37368 KB Output is correct
77 Correct 300 ms 6760 KB Output is correct
78 Correct 96 ms 6756 KB Output is correct
79 Correct 479 ms 16428 KB Output is correct
80 Correct 698 ms 56908 KB Output is correct
81 Correct 11 ms 1148 KB Output is correct
82 Correct 614 ms 51052 KB Output is correct
83 Correct 82 ms 6760 KB Output is correct
84 Correct 554 ms 44364 KB Output is correct
85 Correct 558 ms 31848 KB Output is correct
86 Correct 555 ms 31836 KB Output is correct
87 Correct 175 ms 6888 KB Output is correct
88 Correct 165 ms 6760 KB Output is correct
89 Correct 688 ms 56856 KB Output is correct
90 Correct 678 ms 56932 KB Output is correct
91 Correct 672 ms 56940 KB Output is correct
92 Correct 621 ms 55556 KB Output is correct
93 Correct 549 ms 31828 KB Output is correct
94 Correct 677 ms 56796 KB Output is correct
95 Correct 717 ms 56908 KB Output is correct