/* Siano [A] * Marek Iwaniuk * O(nlog^2n) */ //#include "stdafx.h" #include <cstdio> #include <iostream> #include <algorithm> //#define scanf scanf_s #define MAX_ARR (1 << 19) + 100 #define MAX_TREE (1 << 20) + 100 using namespace std; struct Node { long long val, val_init, lazy, days_passed; bool change; }; class Field { private: const static int BASE = (1 << 19); int n, m, cut_from; long long day, cut, sum_query, last_day = 1; long long Input[MAX_ARR]; Node Tree[MAX_TREE]; void BuildTree(int v, int a, int b) { if (a > b) { return; } if (a == b) { if (b <= n) { Tree[v].val_init = Input[a]; Tree[v].val = Input[a]; } return; } BuildTree(v * 2, a, (a + b) / 2); BuildTree(v * 2 + 1, (a + b) / 2 + 1, b); Tree[v].val = Tree[v * 2].val + Tree[v * 2 + 1].val; Tree[v].val_init = Tree[v * 2].val_init + Tree[v * 2 + 1].val_init; } void UpdateNode(int &v, int &a, int &b) { if (Tree[v].change == true) { Tree[v].val = (b - a + 1) * Tree[v].lazy; if (a != b) { Tree[v * 2].change = true; Tree[v * 2 + 1].change = true; Tree[v * 2].lazy = Tree[v].lazy; Tree[v * 2 + 1].lazy = Tree[v].lazy; } Tree[v].change = false; } Tree[v].val += Tree[v].val_init * Tree[v].days_passed; if (a != b) { Tree[v * 2].days_passed += Tree[v].days_passed; Tree[v * 2 + 1].days_passed += Tree[v].days_passed; } Tree[v].days_passed = 0; } void Update(int v, int a, int b, int &i, int &j, long long &val) { UpdateNode(v, a, b); if (a > b || a > j || b < i) { return; } if (a >= i && b <= j) { Tree[v].val = (b - a + 1) * val; if (a != b) { Tree[v * 2].change = true; Tree[v * 2 + 1].change = true; Tree[v * 2].lazy = val; Tree[v * 2 + 1].lazy = val; } return; } Update(v * 2, a, (a + b) / 2, i, j, val); if ((a + b) / 2 + 1 <= j) { Update(v * 2 + 1, (a + b) / 2 + 1, b, i, j, val); } Tree[v].val = Tree[v * 2].val + Tree[v * 2 + 1].val; } long long QuerySum(int v, int a, int b, int &i, int &j) { UpdateNode(v, a, b); if (a > b || a > j || b < i) { return 0; } if (a >= i && b <= j) { return Tree[v].val; } return QuerySum(v * 2, a, (a + b) / 2, i, j) + QuerySum(v * 2 + 1, (a + b) / 2 + 1, b, i, j); } void Update2(int v, int a, int b, int &i, int &j) { UpdateNode(v, a, b); if (a >= b || a >= j || b <= i) { return; } Update2(v * 2, a, (a + b) / 2, i, j); if ((a + b) / 2 + 1 <= j) { Update2(v * 2 + 1, (a + b) / 2 + 1, b, i, j); } Tree[v].val = Tree[v * 2].val + Tree[v * 2 + 1].val; } long long QuerySum(int i, int j) { Update2(1, 1, BASE, i, j); return QuerySum(1, 1, BASE, i, j); } long long BinSearchForCut() { int L = 1, P = n; while (L != P) { int mid = (L + P) / 2; long long sum = QuerySum(mid, mid); if (sum >= cut) { P = mid; } else { L = mid + 1; } } cut_from = L; if (L == n && QuerySum(n, n) <= cut) { return 0; } return QuerySum(cut_from, n) - cut * (n - cut_from + 1); } public: Field() { for (int i = 0; i < MAX_TREE; i++) { Tree[i].val = Tree[i].val_init = Tree[i].lazy = Tree[i].days_passed = 0; Tree[i].change = false; } scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &Input[i]); } sort(Input + 1, Input + n + 1); BuildTree(1, 1, BASE); for (int i = 1; i <= m; i++) { scanf("%lld %lld", &day, &cut); Tree[1].days_passed += day - last_day; printf("%lld\n", BinSearchForCut()); if ((cut_from == n && QuerySum(n, n) > cut) || cut_from != n) { Update(1, 1, BASE, cut_from, n, cut); } last_day = day; } } }; int main() { Field *New = new Field(); delete New; return 0; }
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | /* Siano [A] * Marek Iwaniuk * O(nlog^2n) */ //#include "stdafx.h" #include <cstdio> #include <iostream> #include <algorithm> //#define scanf scanf_s #define MAX_ARR (1 << 19) + 100 #define MAX_TREE (1 << 20) + 100 using namespace std; struct Node { long long val, val_init, lazy, days_passed; bool change; }; class Field { private: const static int BASE = (1 << 19); int n, m, cut_from; long long day, cut, sum_query, last_day = 1; long long Input[MAX_ARR]; Node Tree[MAX_TREE]; void BuildTree(int v, int a, int b) { if (a > b) { return; } if (a == b) { if (b <= n) { Tree[v].val_init = Input[a]; Tree[v].val = Input[a]; } return; } BuildTree(v * 2, a, (a + b) / 2); BuildTree(v * 2 + 1, (a + b) / 2 + 1, b); Tree[v].val = Tree[v * 2].val + Tree[v * 2 + 1].val; Tree[v].val_init = Tree[v * 2].val_init + Tree[v * 2 + 1].val_init; } void UpdateNode(int &v, int &a, int &b) { if (Tree[v].change == true) { Tree[v].val = (b - a + 1) * Tree[v].lazy; if (a != b) { Tree[v * 2].change = true; Tree[v * 2 + 1].change = true; Tree[v * 2].lazy = Tree[v].lazy; Tree[v * 2 + 1].lazy = Tree[v].lazy; } Tree[v].change = false; } Tree[v].val += Tree[v].val_init * Tree[v].days_passed; if (a != b) { Tree[v * 2].days_passed += Tree[v].days_passed; Tree[v * 2 + 1].days_passed += Tree[v].days_passed; } Tree[v].days_passed = 0; } void Update(int v, int a, int b, int &i, int &j, long long &val) { UpdateNode(v, a, b); if (a > b || a > j || b < i) { return; } if (a >= i && b <= j) { Tree[v].val = (b - a + 1) * val; if (a != b) { Tree[v * 2].change = true; Tree[v * 2 + 1].change = true; Tree[v * 2].lazy = val; Tree[v * 2 + 1].lazy = val; } return; } Update(v * 2, a, (a + b) / 2, i, j, val); if ((a + b) / 2 + 1 <= j) { Update(v * 2 + 1, (a + b) / 2 + 1, b, i, j, val); } Tree[v].val = Tree[v * 2].val + Tree[v * 2 + 1].val; } long long QuerySum(int v, int a, int b, int &i, int &j) { UpdateNode(v, a, b); if (a > b || a > j || b < i) { return 0; } if (a >= i && b <= j) { return Tree[v].val; } return QuerySum(v * 2, a, (a + b) / 2, i, j) + QuerySum(v * 2 + 1, (a + b) / 2 + 1, b, i, j); } void Update2(int v, int a, int b, int &i, int &j) { UpdateNode(v, a, b); if (a >= b || a >= j || b <= i) { return; } Update2(v * 2, a, (a + b) / 2, i, j); if ((a + b) / 2 + 1 <= j) { Update2(v * 2 + 1, (a + b) / 2 + 1, b, i, j); } Tree[v].val = Tree[v * 2].val + Tree[v * 2 + 1].val; } long long QuerySum(int i, int j) { Update2(1, 1, BASE, i, j); return QuerySum(1, 1, BASE, i, j); } long long BinSearchForCut() { int L = 1, P = n; while (L != P) { int mid = (L + P) / 2; long long sum = QuerySum(mid, mid); if (sum >= cut) { P = mid; } else { L = mid + 1; } } cut_from = L; if (L == n && QuerySum(n, n) <= cut) { return 0; } return QuerySum(cut_from, n) - cut * (n - cut_from + 1); } public: Field() { for (int i = 0; i < MAX_TREE; i++) { Tree[i].val = Tree[i].val_init = Tree[i].lazy = Tree[i].days_passed = 0; Tree[i].change = false; } scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &Input[i]); } sort(Input + 1, Input + n + 1); BuildTree(1, 1, BASE); for (int i = 1; i <= m; i++) { scanf("%lld %lld", &day, &cut); Tree[1].days_passed += day - last_day; printf("%lld\n", BinSearchForCut()); if ((cut_from == n && QuerySum(n, n) > cut) || cut_from != n) { Update(1, 1, BASE, cut_from, n, cut); } last_day = day; } } }; int main() { Field *New = new Field(); delete New; return 0; } |