Professional Documents
Culture Documents
Sorted Subsegment A Project Work: in Information Technology Submitted by
Sorted Subsegment A Project Work: in Information Technology Submitted by
A Project work
In
Information Technology
Submitted by
M.BHARGAVI
Redg.No:171fa07073
VIGNAN’S UNIVERSITY
(Deemed to be University)
VADLAMUDI, GUNTUR, A.P,INDIA.
2019
DECLARATION
I here by declare that work described in this minor project entitled “SORTED
result of investigations carried out by us under the guidance of Dr.P.Subba rao sir and Balaji
Sir.
M.BHARGAVI
Date:
CERTIFICATE
This is to certify that the minor project entitled String SORTED SUBSEGMENT being
Andhra Pradesh, India is a bonified work carried out by us under guidance and supervision.
The result embodied in this minor project have not been submitted to any other university or
Institute.
Signature of HOD
ACKNOWLEDGEMENTS
authorities of Vignan’sUniversity in particular of Dr.Subba rao Sir and Dr.balaji Sir and
Task
following type on A :
Given A , can you find and print the value at index K (where 0<K<n ) after
performing q queries?
Input Format
The first line contains three positive space-separated integers describing the respective
values of (the number of integers in A ), q (the number of queries), and K (an index inA ).
The next line contains space-separated integers describing the respective values
of a0,a1,a2,a3..................an-1
Each line j of the q subsequent lines contain two space-separated integers describing the
Output Format
Print a single integer denoting the value of ak after processing all q queries
Sample Input :
311
321
01
Sample Output :
Explanation:
A=[3,2,1]
There is only one query to perform. When we sort the subarray ranging from index 0 to
index 1 , we get A’=[2,3,1] . We then print the element at index 1 , which is 3
Sample Input:
420
4321
02
13
Sample Output :
Explanation :
A=[4,3,2,1]
1. When we sort the subarray ranging from index 0 to index 2 , we get A’=[2,3,4,1] .
Having performed all of the queries, we print the element at index 0, which is 2.
2.INTRODUCTION
Operating system.\
Hacker rank
4.SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int offset;
int check;
int s[3];
} node;
void range_update(int v, int tl, int tr);
int sum(int v, int tl, int tr, int l, int r, int *s);
int min(int x, int y);
int max(int x, int y);
void push(int v, int tl, int tr);
void build(int x, int v, int tl, int tr);
void sort_a(int *a, int size);
void merge(int *a, int *left, int *right, int left_size,
int right_size);
int a[75000], b[75000], query[75000][2], x, pos1, pos2;
node t[300000];
int main() {
int n, q, k, l, r, m, s[3], i, j;
scanf("%d%d%d", &n, &q, &k);
for (i = 0; i < n; i++) {
scanf("%d", a + i);
b[i] = a[i];
}
for (i = 0; i < q; i++)
scanf("%d%d", &query[i][0], &query[i][1]);
for (i = j = q - 1, l = r = k; i >= 0; i--)
if (!(l > query[i][1] || r < query[i][0])) {
l = min(l, query[i][0]);
r = max(r, query[i][1]);
query[j][0] = query[i][0];
query[j][1] = query[i][1];
j--;
}
sort_a(b, n);
for (l = 0, r = n - 1, m = k; 1; m = (l + r) / 2) {
if (l == r) {
m = l;
break;
}
build(b[m], 1, 0, n - 1);
for (i = j + 1; i < q; i++) {
if (!sum(1, 0, n - 1, query[i][0], query[i][1], s))
continue;
if (s[0]) {
x = 1;
pos1 = query[i][0];
pos2 = query[i][0] + s[0] - 1;
range_update(1, 0, n - 1);
}
if (s[1]) {
x = 2;
pos1 = query[i][0] + s[0];
pos2 = query[i][0] + s[0] + s[1] - 1;
range_update(1, 0, n - 1);
}
if (s[2]) {
x = 3;
pos1 = query[i][0] + s[0] + s[1];
pos2 = query[i][1];
range_update(1, 0, n - 1);
}
}
sum(1, 0, n - 1, k, k, s);
if (s[0])
r = m - 1;
else if (s[1])
break;
else
l = m + 1;
}
printf("%d", b[m]);
return 0;
}
void range_update(int v, int tl, int tr) {
int tm, m1, m2, s1[3], s2[3];
push(v, tl, tr);
if (pos2 < tl || pos1 > tr)
return;
if (pos1 <= tl && pos2 >= tr)
t[v].offset = x;
else {
tm = (tl + tr) / 2;
range_update(v * 2, tl, tm);
range_update(v * 2 + 1, tm + 1, tr);
push(v * 2, tl, tm);
push(v * 2 + 1, tm + 1, tr);
t[v].s[0] = t[v * 2].s[0] + t[v * 2 + 1].s[0];
t[v].s[1] = t[v * 2].s[1] + t[v * 2 + 1].s[1];
t[v].s[2] = t[v * 2].s[2] + t[v * 2 + 1].s[2];
if (t[v * 2].s[2])
m1 = 2;
else if (t[v * 2].s[1])
m1 = 1;
else
m1 = 0;
if (t[v * 2 + 1].s[0])
m2 = 0;
else if (t[v * 2 + 1].s[1])
m2 = 1;
else
m2 = 2;
if (m1 <= m2 && !t[v * 2].check && !t[v * 2 +
1].check)
t[v].check = 0;
else
t[v].check = 1;
}
return;
}
int sum(int v, int tl, int tr, int l, int r, int *s) {
int tm, m1, m2, s1[3], s2[3], c1, c2;
push(v, tl, tr);
if (l == tl && r == tr) {
s[0] = t[v].s[0];
s[1] = t[v].s[1];
s[2] = t[v].s[2];
return t[v].check;
}
tm = (tl + tr) / 2;
push(v * 2, tl, tm);
push(v * 2 + 1, tm + 1, tr);
s1[0] = s2[0] = -1;
if (l <= min(r, tm))
c1 = sum(v * 2, tl, tm, l, min(r, tm), s1);
if (max(l, tm + 1) <= r)
c2 = sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r,
s2);
if (s1[0] == -1) {
s[0] = s2[0];
s[1] = s2[1];
s[2] = s2[2];
return c2;
}
if (s2[0] == -1) {
s[0] = s1[0];
s[1] = s1[1];
s[2] = s1[2];
return c1;
}
s[0] = s1[0] + s2[0];
s[1] = s1[1] + s2[1];
s[2] = s1[2] + s2[2];
if (s1[2])
m1 = 2;
else if (s1[1])
m1 = 1;
else
m1 = 0;
if (s2[0])
m2 = 0;
else if (s2[1])
m2 = 1;
else
m2 = 2;
if (m1 <= m2 && !c1 && !c2)
return 0;
return 1;
}
int min(int x, int y) { return (x < y) ? x : y; }
int max(int x, int y) { return (x > y) ? x : y; }
void push(int v, int tl, int tr) {
int tm;
if (!t[v].offset)
return;
t[v].check = 0;
switch (t[v].offset) {
case 1:
t[v].s[0] = tr - tl + 1;
t[v].s[1] = t[v].s[2] = 0;
break;
case 2:
t[v].s[1] = tr - tl + 1;
t[v].s[0] = t[v].s[2] = 0;
break;
case 3:
t[v].s[2] = tr - tl + 1;
t[v].s[0] = t[v].s[1] = 0;
break;
default:
break;
}
tm = (tl + tr) / 2;
if (t[v].offset && tl != tr)
t[v * 2].offset = t[v * 2 + 1].offset = t[v].offset;
t[v].offset = 0;
return;
}
void build(int x, int v, int tl, int tr) {
int tm, m1, m2;
t[v].offset = 0;
if (tl == tr) {
t[v].s[0] = t[v].s[1] = t[v].s[2] = t[v].check = 0;
if (x < a[tl])
t[v].s[2] = 1;
else if (x == a[tl])
t[v].s[1] = 1;
else
t[v].s[0] = 1;
} else {
tm = (tl + tr) / 2;
build(x, v * 2, tl, tm);
build(x, v * 2 + 1, tm + 1, tr);
t[v].s[0] = t[v * 2].s[0] + t[v * 2 + 1].s[0];
t[v].s[1] = t[v * 2].s[1] + t[v * 2 + 1].s[1];
t[v].s[2] = t[v * 2].s[2] + t[v * 2 + 1].s[2];
if (t[v * 2].s[2])
m1 = 2;
else if (t[v * 2].s[1])
m1 = 1;
else
m1 = 0;
if (t[v * 2 + 1].s[0])
m2 = 0;
else if (t[v * 2 + 1].s[1])
m2 = 1;
else
m2 = 2;
if (m1 <= m2 && !t[v * 2].check && !t[v * 2 +
1].check)
t[v].check = 0;
else
t[v].check = 1;
}
return;
}
void sort_a(int *a, int size) {
if (size < 2)
return;
int m = (size + 1) / 2, i;
int *left, *right;
left = (int *)malloc(m * sizeof(int));
right = (int *)malloc((size - m) * sizeof(int));
for (i = 0; i < m; i++)
left[i] = a[i];
for (i = 0; i < size - m; i++)
right[i] = a[i + m];
sort_a(left, m);
sort_a(right, size - m);
merge(a, left, right, m, size - m);
free(left);
free(right);
return;
}
void merge(int *a, int *left, int *right, int left_size,
int right_size) {
int i = 0, j = 0;
while (i < left_size || j < right_size) {
if (i == left_size) {
a[i + j] = right[j];
j++;
} else if (j == right_size) {
a[i + j] = left[i];
i++;
} else if (left[i] <= right[j]) {
a[i + j] = left[i];
i++;
} else {
a[i + j] = right[j];
j++;
}
}
return;
}
5.TESTCASES
https://www.hackerrank.com/challenges/sorted _subsegment/problem