Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 7890 - Emission Spectrum
Source : UVa Live Archive
Category : Data Structure
Algorithm : Persistent Segment Tree
Verdict : Accepted
- #include "bits/stdc++.h"
- #include "ext/pb_ds/assoc_container.hpp"
- #include "ext/pb_ds/tree_policy.hpp"
- #include "ext/rope"
-
- using namespace std;
- using namespace __gnu_pbds;
- using namespace __gnu_cxx;
-
- #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 FORAB(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
-
- template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
-
-
-
-
-
-
-
-
-
-
- #define here cerr << "Here" << endl;
- #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 ones(mask) __builtin_popcount(mask) // count set bit
- #define onesLL(mask) __builtin_popcountll(mask) // for long long
- #define lzeros(mask) __builtin_clz(mask) // no of leading zeros
- #define tzeros(mask) __builtin_ctz(mask) // no of trailing zeros
-
- 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 = 3e5 + 5;
- static const int logn = 18;
-
- struct node
- {
- int st;
- int lft, rgt;
- node(int st = 0, int lft = 0, int rgt = 0) : st(st), lft(lft), rgt(rgt) {}
- } Tree[maxn << 5];
-
- int version[maxn << 5];
- int NODES;
-
- int update(int root, int a, int b, int pos, int val)
- {
- int newNode = ++NODES;
- Tree[newNode] = Tree[root];
- if (a == b)
- {
- Tree[newNode].st += val;
- return newNode;
- }
- int mid = (a + b) >> 1;
- if (pos <= mid) Tree[newNode].lft = update(Tree[newNode].lft, a, mid, pos, val);
- else Tree[newNode].rgt = update(Tree[newNode].rgt, mid+1, b, pos, val);
- Tree[newNode].st = Tree[ Tree[newNode].lft ].st + Tree[ Tree[newNode].rgt ].st;
- return newNode;
- }
-
- int kth(int proot, int root, int a, int b, int k)
- {
- if (a == b) return a;
- int mid = (a + b) >> 1;
- int size_lft_child = Tree[ Tree[root].lft ].st - Tree[ Tree[proot].lft ].st;
- if (k <= size_lft_child) return kth(Tree[proot].lft, Tree[root].lft, a, mid, k);
- else return kth(Tree[proot].rgt, Tree[root].rgt, mid+1, b, k - size_lft_child);
- }
-
- void init()
- {
- for (int i = 0; i <= NODES; i++) Tree[i] = node();
- NODES = 0;
- version[0] = 0;
- }
-
- struct info
- {
- int v, p;
- info(int v = 0, int p = 0) : v(v), p(p) {}
- friend bool operator < (const info &A, const info &B)
- {
- if (A.v == B.v) return A.p < B.p;
- return A.v < B.v;
- }
- } temp[maxn];
-
- int arr[maxn], rmapper[maxn];
-
- int main()
- {
- FI;
- FAST;
-
- int N, Q;
- while (cin >> N >> Q)
- {
- init();
- for (int i = 1; i <= N; i++)
- {
- cin >> arr[i];
- temp[i] = info(arr[i], i);
- }
- sort(temp+1, temp+N+1);
- int n = 0;
- for (int i = 1; i <= N; i++)
- {
- ++n;
- arr[ temp[i].p ] = n;
- rmapper[ n ] = temp[i].v;
- }
- for (int i = 1; i <= N; i++)
- {
- version[i] = update(version[i-1], 1, n, arr[i], 1);
- }
- while (Q--)
- {
- char type;
- cin >> type;
- if (type == 'Q')
- {
- int l, r, k;
- cin >> l >> r >> k;
- if (l > r) swap(l, r);
- int ans = kth(version[l-1], version[r], 1, n, k);
- cout << rmapper[ans] << "\n";
- }
- else
- {
- int l;
- cin >> l;
- swap(arr[l], arr[l+1]);
- version[l] = update(version[l-1], 1, n, arr[l], 1);
- version[l+1] = update(version[l], 1, n, arr[l+1], 1);
- }
- }
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.