Skip to content

Instantly share code, notes, and snippets.

@LifeMoroz
Created December 4, 2013 14:38
Show Gist options
  • Select an option

  • Save LifeMoroz/7788460 to your computer and use it in GitHub Desktop.

Select an option

Save LifeMoroz/7788460 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include "math.h"
using namespace std;
#define INT_MAX1 2147483647
class Elem{
public:
int color;
int modify;
};
class MySegTree {
public:
MySegTree(const vector<int>& source, int n)
{
tree = vector<Elem>(source.size());
for (unsigned long i = source.size() - 1; i >= source.size()/2; i--)
{
tree[i].modify = -1;
tree[i].color = source[i];
}
for (unsigned long i = source.size()/2 - 1; i >= 0; i--)
{
tree[i].color = min(tree[2 * i + 1].color, tree[2*i + 2].color);
tree[i].modify = -1;
if (i == 0) break;
}
size = n;
};
int RMQ(int l, int r);
void Modify(int l, int r, int val);
private:
vector<Elem>tree;
int size;
int getRMQ(int pos, int posL, int posR, int l, int r);
void Modification(int pos, int posL, int posR, int l, int r, int val);
};
void MySegTree::Modify(int l, int r, int val)
{
Modification(0, 0, size-1, l, r, val);
}
int MySegTree::RMQ(int l, int r)
{
return getRMQ(0, 0, size-1, l, r);
}
int logarifm(unsigned int x)
{
int logar = 1;
int k = 0;
while (logar < x) {
logar = logar * 2;
k++;
}
return k;
};
void MySegTree::Modification(int pos, int posL, int posR, int l, int r, int val)
{
if (l == posL && r == posR)
{
tree[pos].modify = val;
int i = (pos - 1)/2;
while (i >= 0)
{
if (tree[2 * i + 1].modify != -1 && tree[ 2*i +2].modify == -1)
tree[i].color = min(tree[2 * i + 1].modify, tree[2* i + 2].color);
if (tree[2 * i + 1].modify == -1 && tree[ 2*i +2].modify != -1)
tree[i].color = min(tree[2 * i + 1].color, tree[2* i + 2].modify);
if (tree[2 * i + 1].modify != -1 && tree[ 2*i +2].modify != -1)
tree[i].color = min(tree[2 * i + 1].modify, tree[2* i + 2].modify);
if (tree[2 * i + 1].modify == -1 && tree[ 2*i +2].modify == -1)
tree[i].color = min(tree[2 * i + 1].color, tree[2* i + 2].color);
if (i == 0) break;
i = (i - 1)/2;
}
return;
}
if (tree[pos].modify != -1)
{
tree[pos * 2 + 1].modify = tree[pos].modify;
tree[pos * 2 + 2].modify = tree[pos].modify;
tree[pos].color = tree[pos].modify;
tree[pos].modify = -1;
}
const int posM = (posL + posR)/2;
if (l <= posM)
Modification(2 * pos + 1, posL, posM, l, min(r, posM), val);
if (r > posM)
Modification(2 * pos + 2, posM + 1, posR, max(l, posM + 1), r, val);
};
int MySegTree::getRMQ(int pos, int posL, int posR, int l, int r)
{
if (tree[pos].modify != -1) return tree[pos].modify;
if( l == posL && r == posR ) // Весь диапазон.
{
return tree[pos].color;
}
const int posM = ( posL + posR ) / 2;
int result = INT_MAX1;
if( l <= posM ) // Затронуто левое поддерево.
result = getRMQ( 2 * pos + 1, posL, posM, l, min( r, posM ) );
if( r > posM ) // Затронуто правое поддерево.
result = min( result, getRMQ( 2 * pos + 2, posM + 1, posR, max( l, posM + 1 ), r ) );
return result;
}
int main() {
int n, r, g, b;
cin >> n;
int size = pow(2, logarifm(n) + 1) - 1;
vector <int > v(size);
for (int i = size/2; i < size/2 + n; i++)
{
cin >> r >> g >> b;
v[i] = r + g + b;
}
for (int i = size/2 + n; i < size; i ++)
v[i] = INT_MAX1;
n = pow(2, logarifm(n));
MySegTree a(v, n);
int k, c, d, e, f;
cin >> k;
for (int i = 0; i < k; i ++)
{
cin >> c >> d >> r >> g >> b >> e >> f;
a.Modify(c, d, r + g + b);
cout << a.RMQ(e, f) << " ";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment