CF816B Karen and Coffee
阿新 • • 發佈:2017-06-25
數組 main 樹狀數組 cnblogs print div log space printf
思路:
有點類似於區間修改點查詢的樹狀數組。
實現:
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int MAXN = 200005; 6 int a[MAXN], n, k, q; 7 8 int main() 9 { 10 cin >> n >> k >> q; 11 for (int i = 0; i < n; i++) 12 { 13 intl, r; 14 scanf("%d %d", &l, &r); 15 a[l]++; a[r + 1]--; 16 } 17 for (int i = 1; i < MAXN; i++) a[i] += a[i - 1]; 18 for (int i = 1; i < MAXN; i++) 19 if (a[i] >= k) a[i] = 1; 20 else a[i] = 0; 21 for(int i = 1; i < MAXN; i++) a[i] += a[i - 1]; 22 for (int i = 0; i < q; i++) 23 { 24 int l, r; 25 scanf("%d %d", &l, &r); 26 printf("%d\n", a[r] - a[l - 1]); 27 } 28 return 0; 29 }
CF816B Karen and Coffee