训练网址
if (r <= mid) return query(2 * u, l, r);
,递归的时候不是 query(2 * u, l, mid)
!#include
#include
#include
typedef unsigned long long ll;
using namespace std;
const int maxn = 1000010;
const ll M = 1e12;
int p[maxn];
ll sum[maxn];
int find(int x){
if (p[x] == x) return x;
return p[x] = find(p[x]);
}
void unite(int a, int b) {
if (find(a) == find(b)) return;
p[a] = find(b);
}
unsigned long long k1, k2;
int N;
long long a[1000001];
unsigned long long xorShift128Plus() {
unsigned long long k3 = k1, k4 = k2;
k1 = k4;
k3 ^= k3 << 23;
k2 = k3 ^ k4 ^ (k3 >> 17) ^ (k4 >> 26);
return k2 + k4;
}
void gen() {
scanf("%d %llu %llu", &N, &k1, &k2);
for (int i = 1; i <= N; i++) {
a[i] = xorShift128Plus() % 999999999999 + 1;
}
}
struct node {
int l, r;
ll sum;
}tr[maxn * 4];
void pushup(int u) {
tr[u].sum = tr[2 * u].sum + tr[2 * u + 1].sum;
}
void build(int u, int l, int r) {
if (l == r) tr[u] = { l, l, 0 };
else {
tr[u].l = l, tr[u].r = r;
int mid = (l + r) / 2;
build(2 * u, l, mid), build(2 * u + 1, mid + 1, r);
//pushup(u);
}
}
node query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) return tr[u];
int mid = (tr[u].l + tr[u].r) / 2;
if (r <= mid) return query(2 * u, l, r);
else if (l > mid) return query(2 * u + 1, l, r);
else {
node left = query(2 * u, l, r);
node right = query(2 * u + 1, l, r);
node res;
res.sum = left.sum + right.sum;
return res;
}
}
void modify(int u, int x, ll v) { // a[x] = v;
if (tr[u].l == x && tr[u].r == x) tr[u].sum = v;
else {
int mid = (tr[u].l + tr[u].r) / 2;
if (x <= mid) modify(2 * u, x, v);
else modify(2 * u + 1, x, v);
pushup(u);
}
}
int main() {
gen();
sort(a + 1, a + N + 1);
//for (int i = 1; i <= N; i++) printf("%llu ", a[i]);
for (int i = 1; i <= N; i++) {
sum[i] = sum[i - 1] + a[i];
}
int Q;
scanf("%d", &Q);
build(1, 1, N);
for (int i = 1; i <= N + 1; i++) {
p[i] = i;
}
while (Q--) {
char op[5];
ll x;
scanf("%s%llu", op, &x);
if (op[0] == 'D') { // 标记>=x的第一个未被标记的a[i]
int id = lower_bound(a + 1, a + N + 1, x) - a;
int pos = find(id);
if (pos == N + 1) continue;
unite(pos, pos + 1);
modify(1, pos, a[pos]);
}
if (op[0] == 'F') { // 查询>=x的第一个未被标记的a[i]
int id = lower_bound(a + 1, a + N + 1, x) - a;
int pos = find(id);
if (pos == N + 1) printf("%llu\n", M);
else {
printf("%llu\n", a[pos]);
}
}
if (op[0] == 'R') { // <=x的标记全部清零
int id = upper_bound(a + 1, a + N + 1, x) - a - 1;
if (id == 0) continue;
for (int i = 1; i <= id; i++) {
p[i] = i;
modify(1, i, 0);
}
}
if (op[0] == 'C') { //查询小于等于 x 的所有未标记数之和;若没有,则输出0。
int id = upper_bound(a + 1, a + N + 1, x) - a - 1;
if (id == 0) printf("0\n");
else {
printf("%llu\n", sum[id] - query(1, 1, id).sum);
}
}
}
return 0;
}
#include
#include
#include
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
ll mod_pow(ll x, ll n) {
ll res = 1;
while (n) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll p[7];
void calc(ll a, ll b, ll c) {
//相当于把八个点带入直线方程,求D的值
P Ds[8] = { P(0, 0), P(a, 1), P(b, 1), P(c, 1),
P(a + b, -1), P(b + c, -1), P(a + c, -1), P(a + b + c, 0) };
sort(Ds, Ds + 8);
ll edges = 3, last_D = Ds[0].first;
for (int i = 1; i < 8; i++) {
p[edges] += (Ds[i].first - last_D);
last_D = Ds[i].first;
edges += Ds[i].second;
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
memset(p, 0, sizeof p);
ll a, b, c, A, B, C;
scanf("%lld%lld%lld%lld%lld%lld", &a, &b, &c, &A, &B, &C);
A = abs(A), B = abs(B), C = abs(C);
if (A && B && C) calc(a * A, b * B, c * C);
//只要 A, B, C 有一个0,那么一定是与四个棱交点(与某个棱重合的概率可以认为是0,因为点的长度是0嘛)。
else {
p[4] = 1;
}
ll sum = 0;
for (int i = 3; i <= 6; i++) {
sum += p[i];
}
for (int i = 3; i <= 6; i++) {
ll d = gcd(p[i], sum);
ll ans = (p[i] / d) * mod_pow(sum / d, mod - 2) % mod;
printf("%lld%c", ans, i == 6 ? '\n' : ' ');
}
}
return 0;
}
#include
#include
#include
#include
using namespace std;
const int maxn = 1010, maxm = 1000010;
int h[maxn], e[maxm], ne[maxm], idx;
int x[maxn], y[maxn], N, d[maxn];
int din[maxn];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void toposort() {
d[0] = 0;
queue<int> que;
for (int i = 0; i <= N; i++) {
if (din[i] == 0) que.push(i);
}
while (que.size()) {
int u = que.front(); que.pop();
for (int i = h[u]; i != -1; i = ne[i]) {
int v = e[i];
d[v] = max(d[v], d[u] + 1);
if (--din[v] == 0) que.push(v);
//printf("### %d %d\n", u, v);
}
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
memset(h, -1, sizeof h);
memset(d, -0x3f, sizeof d);
memset(din, 0, sizeof d);
idx = 0;
scanf("%d%d", &x[0], &y[0]);
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%d%d", &x[i], &y[i]);
}
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= N; j++) {
if (i == j) continue;
if (abs(x[i] - x[j]) <= y[i] - y[j] + 1) {
add(j, i);
din[i]++;
}
}
}
toposort();
int ans = 0;
for (int i = 0; i <= N; i++) {
ans = max(ans, d[i]);
}
printf("%d\n", ans);
}
return 0;
}
#include
#include
#include
using namespace std;
const int maxn = 1010, maxm = 1000010;
int h[maxn], e[maxm], ne[maxm], idx;
int x[maxn], y[maxn], N, d[maxn];
int din[maxn];
bool vis[maxn];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int dp(int u) {
if (vis[u]) return d[u];
d[u] = 0;
vis[u] = true;
for (int i = h[u]; i != -1; i = ne[i]) {
int v = e[i];
d[u] = max(d[u], dp(v) + 1);
}
return d[u];
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
memset(vis, false, sizeof vis);
memset(h, -1, sizeof h);
memset(d, -0x3f, sizeof d);
memset(din, 0, sizeof d);
idx = 0;
scanf("%d%d", &x[0], &y[0]);
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%d%d", &x[i], &y[i]);
}
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= N; j++) {
if (i == j) continue;
if (abs(x[i] - x[j]) <= y[i] - y[j] + 1) {
add(j, i);
din[i]++;
}
}
}
d[0] = 0;
printf("%d\n", dp(0));
}
return 0;
}
rt = find(fa[v.idx]);
ans += rt.b * v.a;
nodes[rt].b += v.b, nodes[rt].a += v.a;
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 100010;
struct node {
int idx;
ll a, b;
bool operator <(const node& rhn)const {
return b * rhn.a < rhn.b * a;
}
}nodes[maxn];
int p[maxn], fa[maxn];
bool vis[maxn];
int find(int x) {
if (p[x] == x) return x;
return p[x] = find(p[x]);
}
void unite(int a, int b) {
if (find(a) == find(b)) return;
p[a] = find(b);
}
int main() {
int N;
scanf("%d", &N);
for (int i = 1; i <= N; i++) p[i] = i;
for (int i = 2; i <= N; i++) {
scanf("%d", &fa[i]);
}
priority_queue<node> que;
for (int i = 1; i <= N; i++) {
scanf("%lld%lld", &nodes[i].a, &nodes[i].b);
nodes[i].idx = i;
if (i != 1) que.push(nodes[i]);
}
ll ans = 0;
while (que.size()) {
auto v = que.top(); que.pop();
if (vis[v.idx]) continue;
vis[v.idx] = true;
int rt = find(fa[v.idx]);
ans += nodes[rt].b * v.a;
nodes[rt].b += v.b, nodes[rt].a += v.a;
unite(v.idx, rt);
if (rt != 1 && !vis[rt]) que.push(nodes[rt]);
}
printf("%lld\n", ans);
return 0;
}