forked from chr4ss1/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKGSS.cpp
More file actions
148 lines (120 loc) · 3.07 KB
/
KGSS.cpp
File metadata and controls
148 lines (120 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <string.h>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
#define SUPERINT unsigned long long int
#define MAX_ELEMENTS 100001 // 10^5
#define MAX_NODES 800008
int N, Q;
char OP;
SUPERINT PARAM1, PARAM2;
SUPERINT NUMBERS[MAX_ELEMENTS];
struct node
{
int start;
int end;
SUPERINT maximum_element;
SUPERINT next_maximum_element;
} NODES[MAX_NODES];
struct intermediate_result
{
node temp_node;
bool valid;
};
node combine(const node &left, const node &right)
{
node result;
if(left.maximum_element == right.maximum_element){
result.maximum_element = left.maximum_element;
result.next_maximum_element = left.maximum_element;
return result;
}
if(left.maximum_element > right.maximum_element){
result.maximum_element = left.maximum_element;
result.next_maximum_element = max(left.next_maximum_element, right.maximum_element);
} else{
result.maximum_element = right.maximum_element;
result.next_maximum_element = max(right.next_maximum_element, left.maximum_element);
}
return result;
}
void build(int index, int low, int high)
{
NODES[index].start = low;
NODES[index].end = high;
if(low == high){
NODES[index].maximum_element = NUMBERS[low];
NODES[index].next_maximum_element = 0;
return;
}
int mid = (low + high) / 2;
build(2*index, low, mid);
build(2*index+1, mid + 1, high);
NODES[index] = combine(NODES[2*index], NODES[2*index + 1]);
}
void update(int index, int low, int high, int element)
{
if(low > element || high < element) return;
if(low == high){
NODES[index].maximum_element = NUMBERS[low];
NODES[index].next_maximum_element = 0;
return;
}
int mid = (low + high) / 2;
update(2*index, low, mid, element);
update(2*index+1, mid + 1, high, element);
NODES[index] = combine(NODES[2*index], NODES[2*index + 1]);
}
intermediate_result query(int index, int range1, int range2, int low, int high)
{
intermediate_result result;
if(range1 > high || range2 < low){
result.valid = false;
return result;
}
if(low >= range1 && high <= range2){
result.valid = true;
result.temp_node = NODES[index];
return result;
}
int mid = (low + high) / 2;
intermediate_result f1 = query(2*index, range1, range2, low, mid);
intermediate_result f2 = query(2*index+1, range1, range2, mid + 1, high);
if(!f1.valid && !f2.valid){
result.valid = false;
return result;
}
result.valid = true;
if(!f1.valid || !f2.valid){
result.temp_node = f1.valid ? f1.temp_node : f2.temp_node;
return result;
}
result.temp_node = combine(f1.temp_node, f2.temp_node);
return result;
}
int main()
{
scanf("%d", &N);
for(int i = 1; i <= N; i++)
scanf("%llu", &NUMBERS[i]);
build(1, 1, N);
scanf("%d\n", &Q);
while(Q--){
scanf("%c %llu %llu\n", &OP, &PARAM1, &PARAM2);
switch(OP){
case 'U':
NUMBERS[PARAM1] = PARAM2;
update(1, 1, N, PARAM1);
break;
case 'Q':
intermediate_result result = query(1, (int)PARAM1, (int)PARAM2, 1, N);
printf("%llu\n", result.temp_node.maximum_element + result.temp_node.next_maximum_element);
break;
}
}
return 0;
}