답안 #1022243

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1022243 2024-07-13T11:32:02 Z NeroZein 사탕 분배 (IOI21_candies) C++17
0 / 100
170 ms 20076 KB
#include "candies.h"
#include <bits/stdc++.h>
 
using namespace std; 

const int N = 2e5 + 5;

struct node {
  int mn = 0, mx = 0;
  int lazy_add = 0, lazy_set = -1; 
};

node seg[N * 2];
pair<int, int> c[N];

node merge(node lx, node rx) {
  node ret;
  ret.mn = min(lx.mn, rx.mn);
  ret.mx = max(lx.mx, rx.mx); 
  return ret;
}

void push(int nd, int l, int r) {
  if (seg[nd].lazy_set != -1) {
    //cout << "push: " << l << ' ' << r << ' ' << seg[nd].lazy_set << '\n';
    if (seg[nd].lazy_set == 0) {
      seg[nd].mn = seg[nd].mx = 0;
    } else {
      seg[nd].mn = c[l].first;
      seg[nd].mx = c[r].first; 
    }
    if (l != r) {
      int mid = (l + r) >> 1;
      int rs = nd + ((mid - l + 1) << 1);
      seg[nd + 1].lazy_set = seg[nd].lazy_set;
      seg[rs].lazy_set = seg[nd].lazy_set;
      seg[nd + 1].lazy_add = seg[rs].lazy_add = 0;
    }
    seg[nd].lazy_set = -1; 
  }
  seg[nd].mn += seg[nd].lazy_add;
  seg[nd].mx += seg[nd].lazy_add;
  if (l != r) {
    int mid = (l + r) >> 1;
    int rs = nd + ((mid - l + 1) << 1);
    seg[nd + 1].lazy_add += seg[nd].lazy_add;
    seg[rs].lazy_add += seg[nd].lazy_add;
  }
  seg[nd].lazy_add = 0;
}

void upd(int nd, int l, int r, int v) {
  push(nd, l, r);
  if (l == r) {
    //cout << "l, v: " << l << ' ' << v << endl; 
    seg[nd].mx = max(0, seg[nd].mx + v);
    seg[nd].mn = max(0, seg[nd].mn + v);
    seg[nd].mx = min(seg[nd].mx, c[l].first);
    seg[nd].mn = min(seg[nd].mn, c[l].first);
    return;
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  push(nd + 1, l, mid);
  push(rs, mid + 1, r); 
  if (seg[nd + 1].mx + v <= 0) {
    seg[nd + 1].lazy_add = 0;
    seg[nd + 1].lazy_set = 0; 
    upd(rs, mid + 1, r, v); 
  } else if (seg[nd + 1].mx + v >= c[mid].first) {
    seg[nd + 1].lazy_add = 0;
    seg[nd + 1].lazy_set = 1; 
    upd(rs, mid + 1, r, v); 
  } else {
    seg[rs].lazy_add += v;
    upd(nd + 1, l, mid, v); 
  }
  seg[nd] = merge(seg[nd + 1], seg[rs]);
}

int qry(int nd, int l, int r, int p) {
  push(nd, l, r); 
  if (l == r) {
    assert(seg[nd].mn == seg[nd].mx);
    return seg[nd].mn;
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  if (p <= mid) {
    return qry(nd + 1, l, mid, p);
  }
  return qry(rs, mid + 1, r, p); 
}
 
vector<int> distribute_candies(vector<int> c_, vector<int> ql, vector<int> qr, vector<int> v) {
  int n = (int) c_.size();
  int q = (int) ql.size(); 
  for (int i = 0; i < n; ++i) {
    c[i].first = c_[i];
    c[i].second = i; 
  }
  sort(c, c + n);
  for (int i = 0; i < q; ++i) {
    upd(0, 0, n - 1, v[i]);
  }
  vector<int> s(n);
  for (int i = 0; i < n; ++i) {
    s[c[i].second] = qry(0, 0, n - 1, i);
  }
  return s;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 170 ms 20076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 80 ms 7776 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -