fork download
  1. // ~~ icebear love attttttt ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define mp make_pair
  27. #define pb push_back
  28. #define fi first
  29. #define se second
  30. #define all(x) x.begin(), x.end()
  31. #define task "icebearat"
  32.  
  33. const int MOD = 1e9 + 7;
  34. const int inf = 1e9 + 27092008;
  35. const ll INF = 1e18 + 27092008;
  36. const int N = 2e5 + 5;
  37. int n, m, k;
  38. array<int, 3> a[N];
  39. int sfmin[N], sfans[N], ans[N], pfsum[N];
  40. deque<int> dq;
  41.  
  42. void init(void) {
  43. cin >> n >> m >> k;
  44. FOR(i, 1, k) cin >> a[i][0] >> a[i][1], a[i][1]--, a[i][2] = i;
  45. a[++k] = {1, 1, 0};
  46. a[++k] = {n, m, 0};
  47. }
  48.  
  49. int bs(int x) {
  50. int l = 0, r = (int)dq.size() - 1, res = 0;
  51. while(l <= r) {
  52. int mid = (l + r) >> 1;
  53. if (a[dq[mid]][1] < x) res = mid, l = mid + 1;
  54. else r = mid - 1;
  55. }
  56. return res;
  57. }
  58.  
  59. void process(void) {
  60. sort(a + 1, a + k + 1);
  61. sfmin[k] = a[k][1];
  62. FORR(i, k - 1, 0) {
  63. sfans[i] = sfans[i + 1] + (a[i + 1][0] - a[i][0]) * sfmin[i + 1];
  64. sfmin[i] = min(sfmin[i + 1], a[i][1]);
  65. }
  66. FOR(i, 1, k) {
  67. while(!dq.empty() && a[dq.back()][1] >= a[i - 1][1]) dq.pop_back();
  68. if (!dq.empty()) pfsum[dq.size()] = pfsum[dq.size() - 1] + (a[i - 1][0] - a[dq.back()][0]) * a[i - 1][1];
  69. dq.pb(i - 1);
  70. int x = bs(sfmin[i + 1]);
  71. ans[a[i][2]] = (a[i + 1][0] - a[dq[x]][0]) * sfmin[i + 1] + sfans[i + 1] + pfsum[x];
  72. }
  73.  
  74. FOR(i, 1, k - 2) cout << ans[i] << ' ';
  75. }
  76.  
  77. signed main() {
  78. ios_base::sync_with_stdio(0);
  79. cin.tie(0); cout.tie(0);
  80. if (fopen(task".inp", "r")) {
  81. freopen(task".inp", "r", stdin);
  82. freopen(task".out", "w", stdout);
  83. }
  84. int tc = 1;
  85. // cin >> tc;
  86. while(tc--) {
  87. init();
  88. process();
  89. }
  90. return 0;
  91. }
  92.  
  93.  
Success #stdin #stdout 0.01s 7844KB
stdin
Standard input is empty
stdout
Standard output is empty