From 8c2ff920efbb617869b5d4e8de819d59c06ae3a9 Mon Sep 17 00:00:00 2001
From: Bruno Freitas Tissei <bft15@inf.ufpr.br>
Date: Mon, 19 Feb 2018 20:39:18 -0300
Subject: [PATCH] Fix ternary search

Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br>
---
 paradigm/ternary_search.cpp  | 4 ++--
 problems/trees_partition.cpp | 2 +-
 structure/bit2d.cpp          | 7 +++----
 3 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/paradigm/ternary_search.cpp b/paradigm/ternary_search.cpp
index 55ee026..c512ef4 100644
--- a/paradigm/ternary_search.cpp
+++ b/paradigm/ternary_search.cpp
@@ -20,8 +20,8 @@ double ternary_search(double l, double r) {
     if (fabs(r - l) < EPS)
       return (l + r) / 2.0;
 
-    lt = l + (r - l) / 3.0;
-    rt = r - (r - l) / 3.0;
+    lt = (r - l) / 3.0 + l;
+    rt = ((r - l) * 2.0) / 3.0 + l;
 
     // < | minimum of f
     // > | maximum of f
diff --git a/problems/trees_partition.cpp b/problems/trees_partition.cpp
index fef0890..149cfe8 100644
--- a/problems/trees_partition.cpp
+++ b/problems/trees_partition.cpp
@@ -12,8 +12,8 @@ using namespace std;
 typedef unsigned long long ll;
 typedef pair<ll,ll> ii;
 
-vector<int> t1[MAX], t2[MAX];
 ll hsh[MAX];
+vector<int> t1[MAX], t2[MAX];
 
 unordered_map<ll, int> M;
 ll ans = 0, tot = 0;
diff --git a/structure/bit2d.cpp b/structure/bit2d.cpp
index 43aa801..211d4b2 100644
--- a/structure/bit2d.cpp
+++ b/structure/bit2d.cpp
@@ -11,9 +11,9 @@ int tree[MAXN][MAXM];
 
 // Perform query in array (tree) in the (idx,idy) position
 int query(int idx, int idy) {
-  int sum = 0, m;
+  int sum = 0;
   for (; idx > 0; idx -= (idx & -idx))
-    for (m = idy; m > 0; m -= (m & -m))
+    for (int m = idy; m > 0; m -= (m & -m))
       sum += tree[idx][m];
 
   return sum;
@@ -21,8 +21,7 @@ int query(int idx, int idy) {
 
 // Add a value (val) to a single position (idx,idy) in the array (tree).
 void update(int idx, int idy, int val) {
-  int m;
   for (; idx <= MAXN; idx += (idx & -idx))
-    for (m = idy; m <= MAXM; m += (m & -m))
+    for (int m = idy; m <= MAXM; m += (m & -m))
       tree[idx][m] += val;
 }
-- 
GitLab