Advertisement
Guest User

396v2

a guest
Oct 26th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. /*
  2.  * main.c
  3.  *
  4.  *  Created on: 26 окт. 2014 г.
  5.  *      Author: ulgish
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. typedef unsigned char byte;
  13. #define P_OPEN 0
  14. #define P_QUERY 1
  15. #define P_CLOSE 2
  16.  
  17. typedef struct{
  18.     int x;
  19.     byte pr;
  20.     int index;
  21. }point;
  22.  
  23. int p_cmp(const void* key1, const void* key2){
  24.     point* p1 = (point*)key1;
  25.     point* p2 = (point*)key2;
  26.     if (p1->x < p2->x){
  27.         return -1;
  28.     }
  29.     if (p1->x > p2->x){
  30.         return 1;
  31.     }
  32.     if (p1->pr < p2->pr){
  33.         return -1;
  34.     }
  35.     if (p1->pr > p2->pr){
  36.         return 1;
  37.     }
  38.     return 0;
  39. }
  40.  
  41. int main(){
  42.     int N,M;
  43.     freopen("input.txt", "r", stdin);
  44.     freopen("output.txt", "w", stdout);
  45.     scanf("%d %d", &N, &M);
  46.     point* p = (point*) calloc(N*2+M, sizeof(point));
  47.     int* ans = (int*) calloc(M, sizeof(int));
  48.     //c - current count of points.
  49.     //l,r - first and second range of interval
  50.     int i,l,r,c = 0;
  51.     for (i = 0; i < N; i++){
  52.         scanf("%d %d", &l, &r);
  53.         p[c].x = l;
  54.         c++;
  55.         p[c].x = r;
  56.         if (l < r){
  57.             p[c].pr = P_CLOSE;
  58.             p[c-1].pr = P_OPEN;
  59.         }else{
  60.             p[c-1].pr = P_CLOSE;
  61.             p[c].pr = P_OPEN;
  62.         }
  63.         c++;
  64.     }
  65.     for (i = 0; i < M; i++){
  66.         scanf("%d", &l);
  67.         p[c].index = i;
  68.         p[c].pr = P_QUERY;
  69.         p[c].x = l;
  70.         c++;
  71.     }
  72.     qsort(p, c, sizeof(point), p_cmp);
  73.     int tmp = 0;
  74.     for (i = 0; i < c; i++){
  75.         switch (p[i].pr) {
  76.             case P_OPEN:
  77.                 tmp++;
  78.                 break;
  79.             case P_CLOSE:
  80.                 tmp--;
  81.                 break;
  82.  
  83.             default:
  84.                 ans[p[i].index] = tmp;
  85.                 break;
  86.         }
  87.     }
  88.     for (i = 0; i < M; i++){
  89.         printf("%d ", ans[i]);
  90.     }
  91.     fclose(stdout);
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement