Beruflich Dokumente
Kultur Dokumente
(2) Given a range [i, j] return if the product Xi , Xi+1 , . . . , Xj−1 , Xj is positive, negative or zero.
As usual we are going to start asking ourselves if it's possible to solve this problem under the 2
seconds time constraint using the brute force approach? The answer is "maybe" after the UVA
administrator install the new Quantum Computers servers :), because this is not the case a Brute
Force solution is going to timeout.
To solve this problem I am going to describe two approaches, the first one using Segment Tree and
the second one using Binary Indexed Trees (BIT):
In case that the reader is not familiar with this data structures I personally recommend the following
TopCoder tutorials:
The main problem of the Brute Force approach is that we need O(j − i + 1) time to answer each of
the second operation queries. Using Segment Trees we can reduce this time to O(logN ) where N is
the size of our sequence.
The main idea is to construct a Segment Tree of the sub-intervals products of our sequence, for
example, given the sequence (−2, 6, 0, −1) we got the following tree:
Labels
There is just a little issue with this, the values of the given sequence are in the range (1) lcm (3) math (19) number
−100 ⩽ Xi ⩽ 100 . This implies that we are in risk to get an overflow/underflow for some theory (7) perfect squares (1) planar graph
sequences, an easy one is (100, 100, 100, 100, 100), if we execute the second operation over (1) prime factors (3) probability (1)
range [0, 4] we got the value 1005 that clearly surpass the 32 bit integer capacity. programming (1) programming contest (2)
recursion (5) Segment tree (1) sieve of
Let's remember that this problem care just for the sign (e.g. negative, positive) between certain eratosthenes (2) simulation (2) sorting (3)
interval [i, j] . So to avoid the overflow problem we simply need to substitute all the negative numbers strings (5) topcoder (2) tree (1) triangular
with -1 and all the positives values by 1. After applying the previous operations we got a tree that look numbers (2) trie (1) tries (1) two pointers (2)
like this: uva (12)
Blog Archive
► 2013 (4)
▼ 2012 (29)
▼ November (2)
UVA 12532 - Interval
Product
UVA 11076 - Add Again
► October (2)
► September (2)
► August (3)
This solution will give us an overall time complexity of O(K logN ) which is enough to pass the 2 ► July (11)
seconds time limit.
► June (7)
► May (2)
The solution with BIT is a little bit less intuitive than the previous exposed, but we can easily came
with it by asking ourselves the correct questions.
When there is not zeros in the interval, and the total number of negative number between [i, j] is
odd.
When none of the other conditions holds then product of the interval [i, j] is positive.
After considering these three questions it seems important to keep track of the frequency of zeros and
negative numbers for any interval [i, j] of our sequence. The easiest way to accomplish this task is
maintaining two arrays with the cumulative frequencies of both types, for example:
So if we want to know the numbers of zeros between certain interval [i, j] we just need to apply the
old trick of f req[j] − f req[i − 1] (assuming that f req[0] is 0).
f req[4] − f req[1] = 1
f req[4] = 2
f req[1] = 1
2−1 = 1
Until this point everything seems just fine, however there is another problem we haven't consider yet,
what happen when we update one of the values of our sequence (operation #1) ? if that's the case we
also need to update our cumulative frequency array. It's not hard to see that even when we can
perform operation #2 in O(1) time, the operation #1 is going to take in the worst case scenario O(N )
time, that due to the vast amount of queries we can't afford for this problem.
Considering this problems, the necessity of using BIT seems more evident. We just need to carefully
maintain the cumulative frequency updated after each operation is performed. The overall time
complexity of this solution is O(K logN ) .
1 #include <iostream> ?
2 #include <cstdio>
3 #include <vector>
4 #include <cmath>
5 #include <cstring>
6
7 #define DEBUG(x) cout << #x << ": " << x << endl
8
9 using namespace std;
10 typedef vector<int> VI;
11
12 const int MAXN = 1000002;
13 int zero[MAXN], neg[MAXN] , X[MAXN];
14
15 int read(int *tree, int idx) {
16 int res = 0;
17 while(idx > 0) {
18 res+=tree[idx];
19 idx-=(idx & -idx);
20 }
21 return res;
22 }
23
24 void update(int *tree, int idx, int val) {
25 while(idx < MAXN) {
26 tree[idx]+=val;
27 idx +=(idx & -idx);
28 }
29 }
30
31 int main() {
32 ios_base::sync_with_stdio(false);
33 cin.tie(NULL);
34 int n, q, x, ind, val, lo, hi;
35 char cmd;
36 while(cin >> n >> q) {
37 memset(zero, 0, sizeof(zero));
38 memset(neg, 0, sizeof(neg));
39 for(int i = 1; i <= n; ++i) {
40 cin >> x;
41 if(x < 0) {
42 // increase the cumulative frequency of the negative values
43 update(neg, i, +1);
44 } else if(x == 0) {
45 // increase the cumulative frequency of the zeros
46 update(zero, i, +1);
47 }
48 X[i] = x;
49 }
50 while(q-- > 0) {
51 cin >> cmd;
52 if(cmd == 'C') {
53 cin >> ind >> val;
54 if(val < 0) {
55 // negative value replace zero or positive
56 // increase the negatives cumulative frequency
57 if(X[ind] >= 0) update(neg, ind, +1);
58
59 // negative value replace zero
60 // decrease the zeros cumulative frequency
61 if(X[ind] == 0) update(zero, ind, -1);
62 } else if(val == 0) {
63
64 // zero value replace positive or negative
65 // increase the zeros cumulative frequency
66 if(X[ind] != 0) update(zero, ind, +1);
67
68 // zero value replace positive or negative
69 // decrease the negatives cumulative frequency
70 if(X[ind] < 0) update(neg, ind, -1);
71 } else {
72 // positive value replace zero
73 // decrease the zeros cumulative frequency
74 if(X[ind] == 0) update(zero, ind, -1);
75
76 // positive value replace negative
77 // decrease the negatives cumulative frequency
78 if(X[ind] < 0) update(neg, ind, -1);
79 }
80 X[ind] = val;
81 } else {
82 cin >> lo >> hi;
83
84 // total count of zeros
85 int z = read(zero, hi) - read(zero, lo - 1);
86
87 // total count of negatives
88 int m = read(neg, hi) - read(neg, lo - 1);
89
90 if(z > 0) cout << '0';
91 else if(m & 1) cout << '-';
92 else cout << '+';
93 }
94 }
95 cout << '\n';
96 }
97 return 0;
98 }
3 comments:
Surge said...
Thanks mate! you have been a great help :)
6:30 PM
Anand said...
Very nice tutorial. It finally helped me getting accepted my first Segment tree submission
in JAVA at UVA..
Thanks bro.. :)
11:42 AM
Post a Comment
Newer Post Home Older Post