不常用的板子
ayzhr435619 · · 个人记录
::::success[QOJ 964 Excluded Min]
#include<bits/stdc++.h>
using namespace std;
namespace zhr{
#define AC 0
#define Debug 1
#define D(...) print(#__VA_ARGS__, __VA_ARGS__)
template<typename... Args>
void print(const char* names, Args&&... args){if(!Debug) return;auto print_one = [&](const char*& p, auto&& v, bool is_first) {if (!is_first) std::cout << ", ";while (*p == ' ') p++;while (*p && *p != ',') std::cout << *p++;std::cout << ": " << v;if (*p == ',') p++;};bool first = true;((print_one(names, forward<Args>(args), first), first = false), ...);cout << '\n';}
#define tab cout << " "
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define RE register
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define uint unsigned int
#define vi vector<int>
#define vp vector<pair<int, int> >
#define All(x) x.begin(), x.end()
#define db double
#define fst first
#define scd second
#define mkp make_pair
#define pii pair<int,int>
#define inl inline
#define TTr int T;T=read();while(T--)
#define TTc int T;cin>>T;while(T--)
#define up(l,r,i) for(int i=(l);i<=(r);++i)
#define dn(r,l,i) for(int i=(r);i>=(l);--i)
#define upp(l,r,i,k) for(int i=(l);i<=(r);i+=(k))
#define dnn(r,l,i,k) for(int i=(r);i>=(l);i-=(k))
#define mems(a, x) memset((a), (x), sizeof(a))
#define Open(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout);
#define ctz __builtin_ctz
#define popcount __builtin_popcount
#define ctzll __builtin_ctzll
#define popcountll __builtin_popcountll
#define ctn continue
#define brk break
const int inf = 0x3fffffff, pyl = 1e4 + 7;
const ll infll = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
template <class T> constexpr inline T abs(T a){return a < 0 ? -a : a;}
inline int min(int a, int b){return a < b ? a : b;}
inline int max(int a, int b){return a > b ? a : b;}
template <class T> constexpr inline void ckmin(T &a, T b){a = a < b ? a : b;}
template <class T> constexpr inline void ckmax(T &a, T b){a = a > b ? a : b;}
template <class T> constexpr inline T gcd(T a, T b) {int az = ctz(a), bz = ctz(b), z = min(az, bz);for (b >>= bz; a; ) {a >>= az; int d = a - b;az = ctz(a - b), b = min(a, b), a = (d < 0) ? -d : d;} return b << z;}
template <class T> constexpr inline T lcm(T a, T b){return a / gcd(a, b) * b;}
inline int ksm(int a, int m){int ans = 1;while(m) {if (m & 1) ans *= a;a *= a;m >>= 1;}return ans;}
inline int ksm(int a, int m, int p){int ans = 1;while(m) { if(m & 1) ans = (ans * a) % p;a = (a * a) % p;m >>= 1;}return ans;}
inline bool Isprime(int x){if(x <= 1) return false;for(int i = 2; i * i <= x; i = -~i) if(x % i == 0) return false;return true;}
inline int lowbit(int x){return x & -x;}
mt19937_64 rng(time(0));
inline ll Rand(ll l, ll r){return rng() % (r - l + 1) + l;}
inline long long power(long long a, long long b, long long p) {long long res = 1; for (a %= p; b; b >>= 1) {if (b & 1) res = (__int128) res * a % p;a = (__int128) a * a % p;} return res;}
const long long BASE[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
inline bool miller_rabin(long long n) {if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;long long s = __builtin_ctzll(n - 1), d = n >> s;for (long long a : BASE) {long long p = power(a, d, n), i = s;while (p != 1 && p != n - 1 && a % n && i--) p = (__int128) p * p % n;if (p != n - 1 && i != s) return false;} return true;}
inline int TIME(){return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();}
constexpr int __mint_global_mod = 998244353;
#define mod __mint_global_mod
struct mint {
int val;
explicit operator int() const {return val;}
static int norm(int x) {return x<0?x+mod:x;}
mint():val(0){}
mint(signed m):val(norm(m%mod)){}
mint(long long m):val(norm(m%mod)){}
mint operator- () const {return -val;}
bool operator== (const mint& x) const {return val==x.val;}
bool operator!= (const mint& x) const {return val!=x.val;}
bool operator< (const mint& x) const {return val<x.val;}
bool operator<= (const mint& x) const {return val<=x.val;}
bool operator> (const mint& x) const {return val>x.val;}
bool operator>= (const mint& x) const {return val>=x.val;}
mint& operator+= (const mint& x) {return val=(val+x.val>=mod?val+x.val-mod:val+x.val),*this;}
mint& operator-= (const mint& x) {return val=(val-x.val<0?val-x.val+mod:val-x.val),*this;}
mint& operator*= (const mint& x) {return val=(static_cast<long long>(val)*x.val%mod),*this;}
mint pow(long long b) const {mint a(*this),res(1);for(;b;a*=a,b>>=1)if(b&1)res*=a;return res;}
// static void exgcd(long long a,long long b,long long& x,long long& y)
// {return b==0?(x=1,y=0):(exgcd(b,a%b,y,x),y-=a/b*x),void();}
// mint operator~ () const {long long a,b;return exgcd(val,mod,a,b),a;}
mint operator~ () const {return pow(mod-2);}
mint& operator/= (const mint& x) {return *this*=~x;}
mint operator+ (const mint& x) const {return mint(*this)+=x;}
mint operator- (const mint& x) const {return mint(*this)-=x;}
mint operator* (const mint& x) const {return mint(*this)*=x;}
mint operator/ (const mint& x) const {return mint(*this)/=x;}
friend std::istream& operator>> (std::istream& in, mint& x) {long long v;return in>>v,x.val=norm(v%mod),in;}
friend std::ostream& operator<< (std::ostream& out, const mint& x) {return out<<x.val;}
};
#undef mod
#define Z mint
#define vz vector<Z>
}
using namespace zhr;
namespace IO{
const int MAXSIZE = 1 << 19;
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *P1 = buf, *P2 = buf;
char pbuf[MAXSIZE], *PP = pbuf;
#define faster 0
inline char gc(){
#if faster
if (P1 == P2) P2 = (P1 = buf) + fread(buf, 1, MAXSIZE, stdin);
return P1 == P2 ? ' ' : *P1++;
#endif
return getchar();
}
inline void pc(const char &c){
#if faster
if (PP - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), PP = pbuf;
*PP++ = c;
#else
putchar(c);
#endif
}
inline void Flush(){
#if faster
fwrite(pbuf, 1, PP - pbuf, stdout);
PP = pbuf;
#endif
}
#define enter pc('\n')
#define space pc(' ')
inline void read(double& rdx) {double t = 0;int x = 0, s = 0, f = 1;char c;do c = gc();while (!isdigit(c) && c != '-' && c != '.');if (c == '-')f = -1, c = gc();while (isdigit(c) && c != '.') x = (c ^ 48) + (x << 1) + (x << 3), c = gc();if (c == '.') c = gc();else {rdx = x * f;return;}while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s = -~s, c = gc();while (s--) t /= 10;rdx = (x + t) * f;}
inline void read(char& ch) {ch = gc();while (ch == ' ' || ch == '\n') ch = gc();}
inline int read() {int x = 0;bool f = 1;char ch = gc();while (!isdigit(ch)) f = ch == '-' ? 0 : 1, ch = gc();while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();return f ? x : -x;}
inline void outin2(int x){if(x < 0){pc('-'), x = -x;}static int sta[130];int top = 0;do {sta[top++] = x & 1, x >>= 1;} while (x);while (top) pc(sta[--top] + 48);}
inline void outin2(int x, int y){if(x < 0){pc('-'), x = -x;}static int sta[130];int top = 0;do {sta[top++] = x & 1, x >>= 1;} while (x);while(top < y) sta[top++] = 0;while (top) pc(sta[--top] + 48);}
inline void write(int x) {if(x < 0){pc('-'), x = -x;}static int sta[50];int top = 0;do {sta[top++] = x % 10, x /= 10;} while (x);while (top) pc(sta[--top] + 48);}
inline void writesp(int x) {if(x < 0){pc('-'), x = -x;}static int sta[50];int top = 0;do {sta[top++] = x % 10, x /= 10;} while (x);while (top) pc(sta[--top] + 48);pc(' ');}
inline void writeln(int x) {if(x < 0){pc('-'), x = -x;}static int sta[50];int top = 0;do {sta[top++] = x % 10, x /= 10;} while (x);while (top) pc(sta[--top] + 48);pc('\n');}
}
using namespace IO;
const int N = 5e5 + 7, mod = 1e9 + 7;
int n, m;
int a[N];
vector<int> vec[N];
struct line{
int l, r, id;
bool operator <(const line&o) const{
return r == o.r ? l < o.l : r < o.r;
}
}b[N];
map<int, int> ans[N];
#define lc p << 1
#define rc p << 1 | 1
int c[N];
inline void add(int x, int k){for(; x <= n; x += lowbit(x)) c[x] += k;}
inline int ask(int x){int res = 0;for(; x > 0; x -= lowbit(x)) res += c[x];return res;}
inline int ask(int l, int r){return ask(r) - ask(l - 1);}
struct sgt{
int tr[N << 2], tag[N << 2];
sgt(){mems(tr, 0xc0);mems(tag, 0);}
inline void update(int p, int l, int r, int x, int k){
if(l == r){tr[p] = k;return;}
int mid = l + r >> 1;
tr[lc] += tag[p], tr[rc] += tag[p], tag[lc] += tag[p], tag[rc] += tag[p], tag[p] = 0;
if(x <= mid) update(lc, l, mid, x, k);
else update(rc, mid + 1, r, x, k);
tr[p] = max(tr[lc], tr[rc]);
}
inline void update(int p, int l, int r, int L, int R, int k){
if(L <= l && r <= R){tr[p] += k;tag[p] += k;return;}
int mid = l + r >> 1;
if(tag[p]) tr[lc] += tag[p], tr[rc] += tag[p], tag[lc] += tag[p], tag[rc] += tag[p], tag[p] = 0;
if(L <= mid) update(lc, l, mid, L, R, k);
if(R > mid) update(rc, mid + 1, r, L, R, k);
tr[p] = max(tr[lc], tr[rc]);
}
inline int query(int p, int l, int r, int L, int R, int k){
if(L > r || R < l) return -1;
if(tr[p] < k) return -1;
if(l == r) return l;
int mid = l + r >> 1;
if(tag[p]) tr[lc] += tag[p], tr[rc] += tag[p], tag[lc] += tag[p], tag[rc] += tag[p], tag[p] = 0;
int res = query(lc, l, mid, L, R, k);
if(~res) return res;
return query(rc, mid + 1, r, L, R, k);
}
}t[2];
#define all 1, 1, n
set<int> s[N], S;
int pos[N];//t[1]目前右端点在pos
inline void init(){
up(1, m, i) s[b[i].l].insert(b[i].r);
up(1, n, i) if(!s[i].empty()) t[0].update(all, i, *s[i].rbegin());
up(1, n, i) add(i, 1);
}
inline void del(int x){
if(s[x].empty()) return;
S.erase(*s[x].rbegin());
s[x].erase(--s[x].end());
t[1].update(all, x, -inf);
if(s[x].empty()) t[0].update(all, x, -inf);
else t[0].update(all, x, *s[x].rbegin());
}
inline void ins(int x){
if(s[x].empty()) return;
int y = *s[x].rbegin();
t[1].update(all, x, ask(x, y));
pos[y] = x;
S.insert(y);
}
inline void update(int i){
int x = a[i];
add(i, -1);
int l = pos[*S.lower_bound(i)];
// D(x, i, l);
if(l <= i && l) t[1].update(all, l, i, -1);
}
inline void upd(int l, int r, int mn){//左端点在[l,r]区间里,右端点>mn的
if(l > r) return;
int p = t[0].query(all, l, r, mn + 1);
// D(l, r, mn, p);
if(!~p) return;
int q = *s[p].rbegin();
ins(p);
upd(p, r, q);
}
signed main() {
n = read(), m = read();
int mx = n;
up(1, n, i) a[i] = read(), vec[a[i]].emplace_back(i), mx = max(mx, a[i] + 1);
up(1, m, i) b[i].l = read(), b[i].r = read(), b[i].id = i;
init();
upd(1, n, 0);
S.insert(0);
S.insert(n + 1);
pos[n + 1] = n + 1;
dn(mx, 0, x){
// D(x);
if(!vec[x].empty()) for(int i : vec[x]) update(i);
while(1){
int p = t[1].query(all, 1, n, x);
// D(p);
if(!~p) brk;
int q = *s[p].rbegin();
ans[p][q] = x;
del(p);
int l = pos[*prev(S.lower_bound(q))] + 1;
int r = pos[*S.lower_bound(q)] - 1;
// D(l, r);
upd(l, r, *prev(S.lower_bound(q)));
}
}
up(1, m, i) writeln(ans[b[i].l][b[i].r]);
Flush();
return AC;
}
/*
<x有x个
从大到小删除。
两颗线段树。
一颗维护比右端点最大值
一颗维护左端点答案,然后都要区间max
都是求max,线段树上二分。
3 3
0 0 2
1 3
2 3
3 3
*/
注释一下,t[0]表示当前状态的左端点对应的右端点,t[1]表示答案。
ins是插入一个新的区间到t[1],t[0]的"状态"不会改变。del 会更新t[0]。这个upd可以更新所有的新区间。这里的实现是把区间去重了。s[l] 是 r 的集合。S 表示当前"状态"的左端点集合(t[0] 里有值的东西)。pos[r] = l。
你要改的是 update 和 init 和 ask 和t[1].query。
我怎么写出这种东西的。
::::