#include <stdio.h> #include <stdlib.h> #include <stdint.h> #define YELLOW_START 0x01 #define YELLOW_END 0x02 #define BLUE_START 0x04 #define BLUE_END 0x08 #define RED_START 0x10 #define RED_END 0x20 int main(void) { uint32_t bucketSize, orderSize; scanf("%u %u", &bucketSize, &orderSize); uint8_t* buckets = (uint8_t*) malloc(sizeof(uint8_t) * bucketSize); uint32_t begin, end, colorId, colorFlag; while (orderSize--) { scanf("%u %u %u", &begin, &end, &colorId); switch (colorId) { case 1: colorFlag = YELLOW_START; break; case 2: colorFlag = BLUE_START; break; case 3: colorFlag = RED_START; break; } buckets[begin-1] |= colorFlag; buckets[end-1] |= 2*colorFlag; } uint32_t greens = 0; uint32_t yellows = 0; uint32_t blues = 0; uint32_t reds = 0; for (uint32_t i = 0; i < bucketSize; ++i) { if (buckets[i]) { if (buckets[i] & BLUE_START) blues++; if (buckets[i] & YELLOW_START) yellows++; if (buckets[i] & RED_START) reds++; } if (yellows && blues && !reds) greens++; if (buckets[i]) { if (buckets[i] & BLUE_END) blues--; if (buckets[i] & YELLOW_END) yellows--; if (buckets[i] & RED_END) reds--; } } printf("%u", greens); free(buckets); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #define YELLOW_START 0x01 #define YELLOW_END 0x02 #define BLUE_START 0x04 #define BLUE_END 0x08 #define RED_START 0x10 #define RED_END 0x20 int main(void) { uint32_t bucketSize, orderSize; scanf("%u %u", &bucketSize, &orderSize); uint8_t* buckets = (uint8_t*) malloc(sizeof(uint8_t) * bucketSize); uint32_t begin, end, colorId, colorFlag; while (orderSize--) { scanf("%u %u %u", &begin, &end, &colorId); switch (colorId) { case 1: colorFlag = YELLOW_START; break; case 2: colorFlag = BLUE_START; break; case 3: colorFlag = RED_START; break; } buckets[begin-1] |= colorFlag; buckets[end-1] |= 2*colorFlag; } uint32_t greens = 0; uint32_t yellows = 0; uint32_t blues = 0; uint32_t reds = 0; for (uint32_t i = 0; i < bucketSize; ++i) { if (buckets[i]) { if (buckets[i] & BLUE_START) blues++; if (buckets[i] & YELLOW_START) yellows++; if (buckets[i] & RED_START) reds++; } if (yellows && blues && !reds) greens++; if (buckets[i]) { if (buckets[i] & BLUE_END) blues--; if (buckets[i] & YELLOW_END) yellows--; if (buckets[i] & RED_END) reds--; } } printf("%u", greens); free(buckets); } |