CF961E Tufurama 主席树

#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
namespace remoon {
#define ri register int
#define ll long long
#define tpr template <typename ra>
#define rep(iu, st, ed) for(ri iu = st; iu <= ed; iu ++)
#define drep(iu, ed, st) for(ri iu = ed; iu >= st; iu --)
#define gc getchar
int p = 0, w = 1; char c = gc();
while(c > ‘9‘ || c < ‘0‘) { if(c == ‘-‘) w = -1; c = gc(); }
while(c >= ‘0‘ && c <= ‘9‘) p = p * 10 + c - ‘0‘, c = gc();
return p * w;
}
int wr[50], rw;
#define pc(iw) putchar(iw)
tpr inline void write(ra o, char c = ‘\n‘) {
if(!o) pc(‘0‘);
if(o < 0) o = -o, pc(‘-‘);
while(o) wr[++ rw] = o % 10, o /= 10;
while(rw) pc(wr[rw --] + ‘0‘);
pc(c);
}
tpr inline void cmin(ra &a, ra b) { if(a > b) a = b; }
tpr inline void cmax(ra &a, ra b) { if(a < b) a = b; }
tpr inline bool ckmin(ra &a, ra b) { return (a > b) ? a = b, 1 : 0; }
tpr inline bool ckmax(ra &a, ra b) { return (a < b) ? a = b, 1 : 0; }
}
using namespace std;
using namespace remoon;
#define sid 10000050
#define xid 200050

ll ans;
int n, id;
int a[xid], rt[xid];
int ls[sid], rs[sid], sz[sid];

inline void insert(int &now, int pre, int l, int r, int v) {
now = ++ id;
ls[now] = ls[pre]; rs[now] = rs[pre];
sz[now] = sz[pre] + 1;
if(l == r) return;
int mid = (l + r) >> 1;
if(v <= mid) insert(ls[now], ls[pre], l, mid, v);
else insert(rs[now], rs[pre], mid + 1, r, v);
}

inline int qry(int l, int r, int ql, int qr, int ml, int mr) {
if(ml > r || mr < l) return 0;
if(ml <= l && mr >= r) return sz[qr] - sz[ql];
int mid = (l + r) >> 1;
int ret = qry(l, mid, ls[ql], ls[qr], ml, mr);
ret += qry(mid + 1, r, rs[ql], rs[qr], ml, mr);
return ret;
}

int main() {
rep(i, 1, n) a[i] = min(read(), n);
rep(i, 1, n) insert(rt[i], rt[i - 1], 1, n, a[i]);
rep(i, 1, n) if(i + 1 <= a[i])
ans += qry(1, n, rt[i], rt[a[i]], i, n);
write(ans);
return 0;
}

CF961E Tufurama 主席树

(0)
(0)

0条