# 左闭右开线段树 2019牛客多校（第七场）E_Find the median（点代表区间

@

## AC_Code

#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include <ctime>
#include <iostream>
#include <assert.h>
#include <vector>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define fi first
#define se second
#define endl '\n'
#define o2(x) (x)*(x)
#define BASE_MAX 31
#define mk make_pair
#define eb push_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset((a),(b),sizeof((a)))
#define iis std::ios::sync_with_stdio(false); cin.tie(0)
#define my_unique(x) sort(all(x)),x.erase(unique(all(x)),x.end())
using namespace std;
#pragma optimize("-O3")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
LL x = 0;int f = 0;
char ch = getchar();
while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x = f ? -x : x;
}
inline void write(LL x, bool f) {
if (x == 0) {putchar('0'); if(f)putchar('\n');else putchar(' ');return;}
if (x < 0) {putchar('-');x = -x;}
static char s[23];
int l = 0;
while (x != 0)s[l++] = x % 10 + 48, x /= 10;
while (l)putchar(s[--l]);
if(f)putchar('\n');else putchar(' ');
}
int lowbit(int x) { return x & (-x); }
template<class T>T big(const T &a1, const T &a2) { return a1 > a2 ? a1 : a2; }
template<class T>T sml(const T &a1, const T &a2) { return a1 < a2 ? a1 : a2; }
template<typename T, typename ...R>T big(const T &f, const R &...r) { return big(f, big(r...)); }
template<typename T, typename ...R>T sml(const T &f, const R &...r) { return sml(f, sml(r...)); }
void debug_out() { cerr << '\n'; }
template<typename T, typename ...R>void debug_out(const T &f, const R &...r) {cerr << f << " ";debug_out(r...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__);

const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int HMOD[] = {1000000009, 1004535809};
const LL BASE[] = {1572872831, 1971536491};
const int mod = 1e9 + 0;//998244353
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MXN = 1e6 + 7;
const int MXE = 1e6 + 7;

int n, m;
int xs[MXN], ys[MXN], ls[MXN], rs[MXN];
LL a1, a2, b1, b2, c1, c2, m1, m2;
vector<int> vs;
int lazy[MXN<<2];
LL sum[MXN<<2];
void push_down(int rt, int l, int mid, int r) {
if(lazy[rt] == 0) return;
lazy[rt<<1] += lazy[rt], lazy[rt<<1|1] += lazy[rt];
sum[rt<<1] += (LL)lazy[rt] * (vs[mid + 1] - 1 - (vs[l] - 1));
sum[rt<<1|1] += (LL)lazy[rt] * (vs[r + 1] - 1 - (vs[mid + 1] - 1));
lazy[rt] = 0;
}
void update(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
sum[rt] += vs[R + 1] - 1 - (vs[L] - 1);
//        debug(L, R, vs[R+1] - 1, vs[L] - 1)
++ lazy[rt];
return;
}
int mid = (l + r) >> 1;
push_down(rt, l, mid, r);
if(L > mid) update(L, R, mid + 1, r, rt<<1|1);
else if(R <= mid) update(L, R, l, mid, rt<<1);
else {
update(L, mid, l, mid, rt<<1), update(mid + 1, R, mid + 1, r, rt<<1|1);
}
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
int query(LL p, int l, int r, int rt) {
if(l == r) {
//        debug(l, vs[l], p, sum[rt])
LL len = sum[rt]/(vs[l+1] - vs[l]);
return (p + len - 1)/len + vs[l] - 1;
}
int mid = (l + r) >> 1;
push_down(rt, l, mid, r);
//    debug(rt, sum[rt], sum[rt<<1], sum[rt<<1|1])
if(sum[rt<<1] >= p) return query(p, l, mid, rt<<1);
else return query(p - sum[rt<<1], mid + 1, r, rt<<1|1);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("/home/cwolf9/CLionProjects/ccc/in.txt", "r", stdin);
//    freopen("/home/cwolf9/CLionProjects/ccc/out.txt", "w", stdout);
#endif
vs.eb(0);
vs.eb(ls[1] = sml(xs[1], ys[1]) + 1), vs.eb((rs[1] = big(xs[1], ys[1]) + 1)+1);
vs.eb(ls[2] = sml(xs[2], ys[2]) + 1), vs.eb((rs[2] = big(xs[2], ys[2]) + 1)+1);
for(int i = 3; i <= n; ++i) {
xs[i] = (xs[i-1] * a1 + xs[i-2] * b1 + c1)%m1, ys[i] = (ys[i-1] * a2 + ys[i-2] * b2 + c2)%m2;
vs.eb(ls[i] = sml(xs[i], ys[i]) + 1), vs.eb((rs[i] = big(xs[i], ys[i]) + 1)+1);
}
my_unique(vs);
for(auto x: vs) printf("%d ", x); printf("\n");
for(int i = 1, tx, ty; i <= n; ++i) {
tx = lower_bound(all(vs), ls[i]) - vs.begin();
ty = upper_bound(all(vs), rs[i]) - vs.begin();
//        debug(tx, ty, vs.size())
update(tx, ty - 1                                           , 1, vs.size(), 1);
//        debug(sum[1], sum[1]/2+(sum[1]%2))
printf("%d\n", query(sum[1]/2+(sum[1]%2), 1, vs.size(), 1));
//        debug(sum[1])
//        if(i == 2) break;
}
return 0;
}

posted @ 2019-08-08 21:55  Cwolf9  阅读(...)  评论(...编辑  收藏