Cerinta completa
Consider an array of integers. We perform queries of the following type on :
- Sort all the elements in the subsegment .
Given , can you find and print the value at index (where ) after performing queries?
Input Format
The first line contains three positive space-separated integers describing the respective values of (the number of integers in ), (the number of queries), and (an index in ).
The next line contains space-separated integers describing the respective values of .
Each line of the subsequent lines contain two space-separated integers describing the respective and values for query .
Constraints
Output Format
Print a single integer denoting the value of after processing all queries.
Sample Input 0
3 1 1
3 2 1
0 1
Sample Output 0
3
Explanation 0
There is only one query to perform. When we sort the subarray ranging from index to index , we get . We then print the element at index , which is .
Sample Input 1
4 2 0
4 3 2 1
0 2
1 3
Sample Output 1
2
Explanation 1
There are queries:
- When we sort the subarray ranging from index to index , we get .
- When we sort the subarray of from index to index , we get .
Having performed all of the queries, we print the element at index , which is .
Limbajul de programare folosit: cpp14
Cod:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
struct Query{
int l, r;
int ignore;
};
int ar1[75000];
int ar2[75000];
struct Query queries[75000];
struct Query sarea[75000];
int cmp(const void *a, const void *b){
return (*(int *)a - *(int *)b);
}
void insertionsort(int a[], int N){
int i, j;
int v;
for (i = 1; i < N; i++){
v = a[i];
for (j = i; j>0 && a[j - 1] > v; j--){
a[j] = a[j - 1];
}
a[j] = v;
}
}
int main() {
int n, q, k1, i, l, r, ign, j,mi,hr,nr,k,changed;
int si, sj;
int *a = ar1;
int *b = ar2;
scanf("%d %d %d", &n, &q, &k1);
for (i = 0; i<n; i++){
scanf("%d", &a[i]);
}
for (i = 0; i<q; i++){
scanf("%d %d", &(queries[i].l), &(queries[i].r));
queries[i].ignore = 0;
}
i = q ;
do{
i = i - 1;
} while (i >= 0 && (k1 < queries[i].l || k1 > queries[i].r));
if (i >= 0){
l = queries[i].l;
r = queries[i].r;
ign = i;
for (i = i-1; i >= 0; i--){
if (queries[i].r < l || queries[i].l > r){
queries[i].ignore = 1;
}
else{
if (queries[i].r > r && queries[i].l >= l)
r = queries[i].r;
else if (queries[i].l < l && queries[i].r <= r)
l = queries[i].l;
else if (queries[i].l < l && queries[i].r > r){
ign = i;
r = queries[i].r;
l = queries[i].l;
}
}
}
l = 0;
r = 0;
si = 0;
for (i = 0; i <= ign; i++){
if (!queries[i].ignore){
for (sj = si - 1; sj >= 0; sj--){
if (sarea[sj].l < queries[i].l && queries[i].l < sarea[sj].r) break;
if (sarea[sj].l < queries[i].r && queries[i].r < sarea[sj].r) break;
if (sarea[sj].l >= queries[i].l && queries[i].r >= sarea[sj].r) break;
}
if (sj == -1){
qsort(a + queries[i].l, queries[i].r - queries[i].l + 1, sizeof(int), cmp);
sarea[si] = queries[i];
si++;
}
else{
changed =0;
l = sarea[sj].l;
r = sarea[sj].r;
if (queries[i].l < l){
changed=1;
hr = l - queries[i].l;
memcpy(b, a + queries[i].l, hr*sizeof(int));
//qsort(b, hr, sizeof(int), cmp);
insertionsort(b,hr);
mi = 0;
j = l;
k = queries[i].l;
nr = (r < queries[i].r ? r : queries[i].r);
while (mi < hr && j <= nr)
{
a[k++] = (b[mi] < a[j] ? b[mi++] : a[j++]);
}
while (mi < hr) a[k++] = b[mi++];
}
if (queries[i].r > r){
changed+=2;
hr = queries[i].r - r;
memcpy(b, a + r + 1, hr*sizeof(int));
//qsort(b, hr, sizeof(int), cmp);
insertionsort(b,hr);
mi = hr - 1;
j = r;
k = queries[i].r;
while (mi >= 0 && j >= queries[i].l)
{
a[k--] = (b[mi] > a[j] ? b[mi--] : a[j--]);
}
while (mi >= 0) a[k--] = b[mi--];
r = queries[i].r;
}
if (changed){
sarea[sj].l = queries[i].l;
sarea[sj].r = queries[i].r;
}
}
}
}
}
printf("%d", a[k1]);
return 0;
}
Scor obtinut: 1.0
Submission ID: 464649456
Link challenge: https://www.hackerrank.com/challenges/sorted-subsegments/problem
