File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
kuyhochung/1. 매주 문제풀이/51주차 Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < algorithm>
3+ #include < map>
4+ using namespace std ;
5+ #define MAX 1000001
6+
7+ int N, K, Q;
8+ long long a[MAX] = { 0 , };
9+ long long sum[MAX] = { 0 , };
10+ map<int , int > x_elements;
11+
12+ void something (int jump, int cnt) {
13+ int i = 0 ;
14+ while (i < N) {
15+ a[i] = a[i] + cnt;
16+ i = i + jump;
17+ }
18+ }
19+
20+ int main () {
21+ ios::sync_with_stdio (false ); cin.tie (NULL ); cout.tie (NULL );
22+
23+ // 입력을 받는다.
24+ cin >> N >> K;
25+ // 입력받는 x원소의 갯수를 map 자료구조를 이용하여 세어 준다.
26+ for (int i = 0 ; i < K; i++) {
27+ int temp;
28+ cin >> temp;
29+ x_elements[temp]++;
30+ }
31+ // 체크한 x원소의 등장 횟수를 이용하여, something 함수를 실행할 때 x원소가 나온 횟수만큼 더해 준다.
32+ for (auto next : x_elements) {
33+ // jump: x원소의 값, cnt: x원소의 등장 횟수
34+ int jump = next.first ;
35+ int cnt = next.second ;
36+ something (jump, cnt);
37+ }
38+
39+ // 누적합 계산
40+ sum[0 ] = a[0 ];
41+ for (int i = 1 ; i < N; i++) {
42+ sum[i] = sum[i - 1 ] + a[i];
43+ }
44+
45+ cin >> Q;
46+ for (int i = 0 ; i < Q; i++) {
47+ int L, R;
48+ cin >> L >> R;
49+ cout << sum[R] - sum[L - 1 ] << ' \n ' ;
50+ }
51+
52+ return 0 ;
53+ }
You can’t perform that action at this time.
0 commit comments