-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRMQWithShifts_UVA12299OJ.cpp
127 lines (97 loc) · 2.25 KB
/
RMQWithShifts_UVA12299OJ.cpp
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
#include<bits/stdc++.h>
using namespace std;
class fenwickTree
{
private:
vector<int> c;
int lowbit(int x)
{
return x & (-x);
}
public:
vector<int> a;
fenwickTree(vector<int> & in)
{
int n = in.size();
a = in;
c.assign(n + 1, INT_MAX);
for (int i = 0; i <= n - 1; ++i)
{
update(i, a[i]);
}
}
void update(int k, int temp)
{
++k;
int n = c.size();
a[k - 1] = temp;
for (int i = k; i <= n; i += lowbit(i))
{
c[i] = min(c[i], temp);
}
}
int query(int l, int r)
{
++l; ++r;
int ans = a[r - 1];
while (l != r)
{
for (--r; r > l + lowbit(r); r -= lowbit(r))
{
ans = min(ans, c[r]);
}
ans = min(ans, a[r - 1]);
}
return ans;
}
};
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n, q; cin >> n >> q;
vector<int> a(n);
for (int i = 0; i <= n - 1; ++i)
{
cin >> a[i];
}
fenwickTree tree(a);
for (int c = 1; c <= q; ++c)
{
string in; cin >> in;
int size = in.size();
string temp = "";
vector<int> num;
for (int i = 0; i <= size - 1; ++i)
{
char now = in[i];
if ('0' <= now && now <= '9')
{
temp += now;
}
else
{
if (temp.size() > 0)
{
num.push_back(stoi(temp));
temp = "";
}
}
}
if (in[0] == 'q')
{
cout << tree.query(num[0] - 1, num[1] - 1) << '\n';
}
else
{
int sizeN = num.size();
for (int i = 0; i <= sizeN - 2; ++i)
{
int tempA = tree.a[num[i] - 1], tempB = tree.a[num[i + 1] - 1];
tree.update(num[i] - 1, tempB); tree.update(num[i + 1] - 1, tempA);
}
}
}
cout.flush();
return 0;
}