Codeforces 1181D Irrigation

7924 단어
Irrigation
질문을 오프라인으로 하고 어릴 때부터 큰 문제에서 큰 문제를 구하는 문제로 바꾸어 균형 트리를 만들면 된다.
#include
#include 
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair
#define PLI pair
#define PII pair
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;
using namespace __gnu_pbds;

const int N = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

template <class T>
using Tree = tree, rb_tree_tag,tree_order_statistics_node_update>;
Tree<int> bst;

int n, m, q;
PLL qus[N];
PLL now[N];

int a[N];
int cnt[N];
int ans[N];

int main() {
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        now[a[i]].fi++;
    }
    for(int i = 1; i <= m; i++) now[i].se = i;
    sort(now + 1, now + 1 + m);

    for(int i = 1; i <= q; i++) {
        scanf("%lld", &qus[i].fi);
        qus[i].se = i;
    }
    sort(qus + 1, qus + 1 + q);

    LL pre = 0;
    LL cnt = 0;
    LL tmp = n;

    for(int i = 1, j = 1; i <= q; i++) {
        LL k = qus[i].fi;
        while(j <= m && (now[j].fi - pre) * cnt + tmp < k) {
            tmp += (now[j].fi - pre) * cnt;
            cnt++;
            bst.insert(now[j].se);
            pre = now[j].fi;
            j++;
        }
        LL need = k - tmp;
        LL pos = need % cnt;

        if(pos) pos = *bst.find_by_order(pos - 1);
        else pos = *bst.find_by_order(cnt - 1);

        ans[qus[i].se] = pos;
    }
    for(int i = 1; i <= q; i++) printf("%d
", ans[i]); return 0; } /* */

 
전재 대상:https://www.cnblogs.com/CJLHY/p/11041549.html

좋은 웹페이지 즐겨찾기