Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 5306 - Gorgeous Sequence
Source : HDU
Category : Data Structure
Algorithm : Segment Tree Beats
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 REP(i, a, b) for (int i = a; i <= b; i++)
-
- #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
-
- #include "ext/pb_ds/assoc_container.hpp"
- #include "ext/pb_ds/tree_policy.hpp"
- using namespace __gnu_pbds;
-
- template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
-
- #include "ext/rope"
- using namespace __gnu_cxx;
-
-
-
-
-
-
-
-
-
-
- #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 read() { int a; scanf("%d", &a); return a; }
- inline ll readLL() { ll a; scanf("%lld", &a); return a; }
- inline double readDD() { 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 1e17
- #define mod 1000000007
-
- static const int maxn = 1e6 + 5;
- static const int logn = 18;
-
-
- struct segmentTreeBeats
- {
- ll max1, max2, cnt_max1, sum;
- segmentTreeBeats(ll max1 = 0, ll max2 = 0, ll cnt_max1 = 0, ll sum = 0) :
- max1(max1), max2(max2), cnt_max1(cnt_max1), sum(sum) {}
- } Tree[maxn << 2];
-
- ll arr[maxn];
-
- inline void push_up(int node)
- {
- int lft = node << 1;
- int rgt = lft | 1;
- segmentTreeBeats &par = Tree[node];
- segmentTreeBeats &lchild = Tree[lft];
- segmentTreeBeats &rchild = Tree[rgt];
- par.max1 = max(lchild.max1, rchild.max1);
- par.sum = lchild.sum + rchild.sum;
- if (lchild.max1 == rchild.max1)
- {
- par.cnt_max1 = lchild.cnt_max1 + rchild.cnt_max1;
- par.max2 = max(lchild.max2, rchild.max2);
- }
- else
- {
- if (lchild.max1 > rchild.max1) par.cnt_max1 = lchild.cnt_max1;
- else par.cnt_max1 = rchild.cnt_max1;
- par.max2 = max(min(lchild.max1, rchild.max1), max(lchild.max2, rchild.max2));
- }
- }
-
- inline void modify(int node, ll val)
- {
- segmentTreeBeats &par = Tree[node];
- if (val >= par.max1) return;
- par.sum -= (par.max1 - val) * par.cnt_max1;
- par.max1 = val;
- }
-
- inline void push_down(int node)
- {
- int lft = node << 1;
- int rgt = lft | 1;
- modify(lft, Tree[node].max1);
- modify(rgt, Tree[node].max1);
- }
-
- inline void build(int node, int a, int b)
- {
- if (a == b)
- {
- Tree[node] = segmentTreeBeats(arr[a], -1, 1, arr[a]);
- return;
- }
- int lft = node << 1;
- int rgt = lft | 1;
- int mid = (a + b) >> 1;
- build(lft, a, mid);
- build(rgt, mid+1, b);
- push_up(node);
- }
-
- inline void update(int node, int a, int b, int i, int j, ll val)
- {
- if (a > j || b < i || Tree[node].max1 <= val) return;
- if (a >= i && b <= j && Tree[node].max2 < val)
- {
- modify(node, val);
- return;
- }
- push_down(node);
- int lft = node << 1;
- int rgt = lft | 1;
- int mid = (a + b) >> 1;
- update(lft, a, mid, i, j, val);
- update(rgt, mid+1, b, i, j, val);
- push_up(node);
- }
-
- inline ll get_max(int node, int a, int b, int i, int j)
- {
- if (a == i && b == j) return Tree[node].max1;
- push_down(node);
- int lft = node << 1;
- int rgt = lft | 1;
- int mid = (a + b) >> 1;
- ll res = -1;
- if (j <= mid)
- {
- res = max(res, get_max(lft, a, mid, i, j));
- }
- else if (i > mid)
- {
- res = max(res, get_max(rgt, mid+1, b, i, j));
- }
- else
- {
- ll p = get_max(lft, a, mid, i, mid);
- ll q = get_max(rgt, mid+1, b, mid+1, j);
- res = max(res, max(p, q));
- }
- push_up(node);
- return res;
- }
-
- inline ll get_sum(int node, int a, int b, int i, int j)
- {
- if (a == i && b == j) return Tree[node].sum;
- push_down(node);
- int lft = node << 1;
- int rgt = lft | 1;
- int mid = (a + b) >> 1;
- ll res = 0;
- if (j <= mid)
- {
- res += get_sum(lft, a, mid, i, j);
- }
- else if (i > mid)
- {
- res += get_sum(rgt, mid+1, b, i, j);
- }
- else
- {
- res += get_sum(lft, a, mid, i, mid);
- res += get_sum(rgt, mid+1, b, mid+1, j);
- }
- push_up(node);
- return res;
- }
-
- int main()
- {
- FI;
- int tc = read();
- FOR(tcase, tc)
- {
- int n = read();
- int q = read();
- FOR(i, n) arr[i] = read();
- build(1, 1, n);
- FOR(_q, q)
- {
- int type = read();
- int l = read();
- int r = read();
- if (l > r) swap(l, r);
- if (type == 0)
- {
- ll x = readLL();
- update(1, 1, n, l, r, x);
- }
- else if (type == 1)
- {
- ll maxi = get_max(1, 1, n, l, r);
- printf("%lld\n", maxi);
- }
- else
- {
- ll sum = get_sum(1, 1, n, l, r);
- printf("%lld\n", sum);
- }
- }
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.