Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 13102 – Tobby Stones
Source : UVa Online Judge
Category : Data Structure
Algorithm : Implicit Treap, Lazy Propagation
Verdict : Accepted
#include "bits/stdc++.h"
using namespace std;
#define FI freopen("in.txt", "r", stdin)
#define FO freopen("out.txt", "w", stdout)
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FOR(i, n) for (int i = 1; i <= n; i++)
#define For(i, n) for (int i = 0; i < n; i++)
#define ROF(i, n) for (int i = n; i >= 1; i--)
#define Rof(i, n) for (int i = n-1; i >= 0; i--)
#define FORI(i, n) for (auto i : n)
#define ll long long
#define ull unsigned long long
#define vi vector <int>
#define vl vector <ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define mk make_pair
#define ff first
#define ss second
#define eb emplace_back
#define em emplace
#define pb push_back
#define ppb pop_back
#define All(a) a.begin(), a.end()
#define memo(a, b) memset(a, b, sizeof a)
#define Sort(a) sort(All(a))
#define ED(a) Sort(a), a.erase(unique(All(a)), a.end())
#define rev(a) reverse(All(a))
//#define sz(a) (int)a.size()
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define maxAll(a) *max_element(All(a))
#define minAll(a) *min_element(All(a))
#define allUpper(a) transform(All(a), a.begin(), :: toupper)
#define allLower(a) transform(All(a), a.begin(), :: tolower)
#define endl '\n'
#define nl puts("")
#define ub upper_bound
#define lb lower_bound
#define Exp exp(1.0)
#define PIE 2*acos(0.0)
#define Sin(a) sin(((a)*PIE)/180.0)
#define EPS 1e-9
// int dr[] = {1, -1, 0, 0}; // 4 Direction
// int dc[] = {0, 0, 1, -1};
// int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction
// int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};
// int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves
// int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
inline int setbit(int mask, int pos) { return mask |= (1 << pos); }
inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); }
inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); }
inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
#define popcount(mask) __builtin_popcount(mask) // count set bit
#define popcountLL(mask) __builtin_popcountll(mask) // for long long
inline int nextINT() { int a; scanf("%d", &a); return a; }
inline ll nextLL() { ll a; scanf("%lld", &a); return a; }
inline double nextDD() { double a; scanf("%lf", &a); return a; }
template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; }
ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; }
#define inf 1e18
#define mod 1000000007
static const int maxn = 2e5 + 5;
static const int logn = 18;
struct node
{
int val, white, black;
int priority, sze;
int rev, sset, inv;
node *lft, *rgt;
node(int val = 0)
{
this->val = val;
this->white = this->black = 0;
val == 0 ? this->white++ : this->black++;
this->priority = rand();
this->sze = 1;
this->rev = this->sset = this->inv = 0;
this->lft = this->rgt = nullptr;
}
};
typedef node* pnode;
inline int sz(pnode t)
{
if (t) return t->sze;
return 0;
}
inline void updateSize(pnode t)
{
if (t) t->sze = sz(t->lft) + 1 + sz(t->rgt);
}
inline void push(pnode t)
{
if (!t) return;
if (t->rev)
{
t->rev = 0;
swap(t->lft, t->rgt);
if (t->lft) t->lft->rev ^= 1;
if (t->rgt) t->rgt->rev ^= 1;
}
if (t->sset)
{
if (t->lft) t->lft->sset = t->sset, t->lft->inv = 0;
if (t->rgt) t->rgt->sset = t->sset, t->rgt->inv = 0;
if (t->sset == 1) // all white
{
t->val = 0;
t->white = t->white + t->black;
t->black = 0;
}
else // all black
{
t->val = 1;
t->black = t->white + t->black;
t->white = 0;
}
t->sset = 0;
t->inv = 0;
}
if (t->inv)
{
if (t->lft)
{
if (t->lft->sset) t->lft->sset = 3 - t->lft->sset;
else t->lft->inv ^= 1;
}
if (t->rgt)
{
if (t->rgt->sset) t->rgt->sset = 3 - t->rgt->sset;
else t->rgt->inv ^= 1;
}
swap(t->white, t->black);
t->val = !t->val;
t->inv = 0;
}
}
inline void combine(pnode t)
{
if (!t) return;
push(t->lft);
push(t->rgt);
t->white = t->black = 0;
t->val == 0 ? t->white++ : t->black++;
if (t->lft)
{
t->white += t->lft->white;
t->black += t->lft->black;
}
if (t->rgt)
{
t->white += t->rgt->white;
t->black += t->rgt->black;
}
}
inline void Split(pnode t, pnode &l, pnode &r, int pos, int add = 0)
{
// 'pos' goes to left subtree
if (!t) return void(l = r = nullptr);
push(t);
int curr = sz(t->lft) + add;
if (curr <= pos) Split(t->rgt, t->rgt, r, pos, curr+1), l = t;
else Split(t->lft, l, t->lft, pos, add), r = t;
updateSize(t);
combine(t);
}
inline void Merge(pnode &t, pnode l, pnode r)
{
push(l), push(r);
if (!l || !r)
t = l ? l : r;
else if (l->priority < r->priority)
Merge(r->lft, l, r->lft), t = r;
else
Merge(l->rgt, l->rgt, r), t = l;
updateSize(t);
combine(t);
}
inline void Insert(pnode &t, pnode newnode, int pos)
{
pnode L, R;
Split(t, L, R, pos-1);
Merge(t, L, newnode);
Merge(t, t, R);
}
inline pnode Reverse(pnode t, int l, int r)
{
if (!t) return t;
pnode L, mid, R;
Split(t, L, mid, l-1);
Split(mid, t, R, r-l);
t->rev ^= 1;
Merge(mid, L, t);
Merge(t, mid, R);
return t;
}
inline pnode Set(pnode t, int l, int r, int val)
{
// val = 1, then all white
// val = 2, then all black
if (!t) return t;
pnode L, mid, R;
Split(t, L, mid, l-1);
Split(mid, t, R, r-l);
t->sset = val;
Merge(mid, L, t);
Merge(t, mid, R);
return t;
}
inline pnode Inverse(pnode t, int l, int r)
{
if (!t) return t;
pnode L, mid, R;
Split(t, L, mid, l-1);
Split(mid, t, R, r-l);
t->inv ^= 1;
Merge(mid, L, t);
Merge(t, mid, R);
return t;
}
inline pii query(pnode t, int l, int r)
{
if (!t) return pii(0, 0);
pnode L, mid, R;
Split(t, L, mid, l-1);
Split(mid, t, R, r-l);
pii ans = pii(t->white, t->black);
Merge(mid, L, t);
Merge(t, mid, R);
return ans;
}
inline void clean(pnode t)
{
if (!t) return;
clean(t->lft);
clean(t->rgt);
delete t;
}
int main()
{
// FI;
int N, M;
while (scanf("%d %d", &N, &M) == 2)
{
pnode treap = nullptr;
for (int i = 0; i < N; i++)
{
// pnode t;
// Merge(t, treap, new node(0));
// treap = t;
Insert(treap, new node(), i);
}
while (M--)
{
int type;
scanf("%d", &type);
if (type == 0)
{
int l, r;
scanf("%d %d", &l, &r);
pii ans = query(treap, l, r);
printf("%d %d\n", ans.ss, ans.ff);
}
else if (type == 1)
{
int l, r;
scanf("%d %d", &l, &r);
Reverse(treap, l, r);
}
else if (type == 2)
{
int l, r;
scanf("%d %d", &l, &r);
Inverse(treap, l, r);
}
else if (type == 3)
{
int l, r, val;
scanf("%d %d %d", &l, &r, &val);
if (val == 0) val = 2;
else val = 1;
Set(treap, l, r, val);
}
}
clean(treap);
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.