202009

4. 星际旅行

  历年第四题比较简单的一道,明确几何关系,最后的答案不管多少维都可以转化到二维上求解。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;

#define debug(x) cout << #x << " is " << x << endl
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int N = 105, M = 2005;

int n, m, r;
double o[N], p[M][N], d[M], rd[M];
double ans[M];

inline double sqr(double x) { return x * x; }

int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(12);
    cin >> n >> m >> r;
    for (int i = 1; i <= n; ++i) cin >> o[i];
    for (int i = 1; i <= m; ++i) {
        double s = 0;
        for (int j = 1; j <= n; ++j) {
            cin >> p[i][j];
            s += sqr(p[i][j] - o[j]);
        }
        d[i] = sqrt(s);
        rd[i] = sqrt(s - sqr(r));
    }
    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j < i; ++j) {
            double a = 0, b = d[i], c = d[j];
            for (int k = 1; k <= n; ++k) a += sqr(p[i][k] - p[j][k]);
            a = sqrt(a);
            double p = (a + b + c) / 2;
            double area = sqrt(p * (p - a) * (p - b) * (p - c));
            double h = 2 * area / a;
            if (h >= r || sqr(a) + sqr(b) <= sqr(c) || sqr(a) + sqr(c) <= sqr(b)) {
                ans[i] += a; ans[j] += a;
                continue;
            } 
            double alpha1 = acos(r / b);
            double alpha2 = acos(r / c);
            double beta = acos((sqr(b) + sqr(c) - sqr(a)) / (2 * b * c));
            double t = (beta - alpha1 - alpha2) * r + rd[i] + rd[j];
            ans[i] += t; ans[j] += t;
        }
    }
    for (int i = 1; i <= m; ++i)
        cout << ans[i] << '\n';
    return 0;
}

202012

  惨败,不到 300 分,前两题是我考试时交的,最后一题线段树调了 1 个小时样例过了但爆 0 了。

1. 期末预测之安全指数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

#define debug(x) cout << #x << " is " << endl
#define ll long long
const int INF = 0x3f3f3f3f;

int n;

int main()
{
    scanf("%d", &n);
    int sum = 0, x, y;
    for (int i = 1; i <= n; ++i) {
        scanf("%d%d", &x, &y);
        sum += x * y;
    }
    if (sum > 0) printf("%d\n", sum);
    else puts("0");
    return 0;
}

2. 期末预测之最佳阈值

5. 星际旅行

  题意:给出 $n(n\le1e9)$ 个初始为 0 的三元组,$m(m\le4e4)$ 次操作,区间乘、加、三元置换和求平方和操作。

  进行离散化,将操作[l,r]转换成(l-1,r],更新优先级:置换>乘法>加法。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <bits/stdc++.h>
using namespace std;

#define debug(x) cout << #x << " is " << x << endl
typedef pair<int, int> P;
typedef long long ll;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int N = 4e5 + 5;

struct Q {
    int op, l, r, k, a, b, c;
}q[N];

vector<int> sca;

struct Node {
    int l, r, k;
    int a[3], b[3], s[3];
    int get_len() { return sca[r] - sca[l-1]; }
}tr[N<<2];

int n, m;

int get_id(int x) { 
    return lower_bound(sca.begin(), sca.end(), x) - sca.begin(); 
}

void push_up(int u) {
    auto ls = tr[u<<1].s, rs = tr[u<<1|1].s;
    for (int i = 0; i < 3; ++i) 
        tr[u].s[i] = (ls[i] + rs[i]) % MOD;
}

void rotate(int a[]) {
    int t = a[0];
    a[0] = a[1], a[1] = a[2], a[2] = t;
}

void eval(int u, int k) {
    k %= 3;
    for (int i = 0; i < k; ++i) {
        rotate(tr[u].a); rotate(tr[u].b); rotate(tr[u].s);
    }
    tr[u].k += k;
}

void eval(int u, int a[], int b[]) {
    for (int i = 0; i < 3; ++i) {
        tr[u].s[i] = (1ll * tr[u].s[i] * a[i] + 1ll * tr[u].get_len() * b[i]) % MOD;
        int c = 1ll * tr[u].a[i] * a[i] % MOD;
        int d = (1ll * tr[u].b[i] * a[i] + b[i]) % MOD;
        tr[u].a[i] = c; tr[u].b[i] = d;
    }
}

void push_down(int u) {
    eval(u<<1, tr[u].k); eval(u<<1|1, tr[u].k);
    tr[u].k = 0;
    eval(u<<1, tr[u].a, tr[u].b); eval(u<<1|1, tr[u].a, tr[u].b);
    for (int i = 0; i < 3; ++i)
        tr[u].a[i] = 1, tr[u].b[i] = 0;
}

void build(int u, int l, int r) {
    tr[u].l = l, tr[u].r = r;
    for (int i = 0; i < 3; ++i) tr[i].a[i] = 1;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(u<<1, l, mid);
    build(u<<1|1, mid + 1, r); 
}

void update(int u, int l, int r, int k, int a[], int b[]) {
    if (tr[u].l >= l && tr[u].r <= r) {
        eval(u, k), eval(u, a, b);
        return;
    }
    push_down(u);
    int mid = (tr[u].l + tr[u].r) >> 1;
    if (l <= mid) update(u<<1, l, r, k, a, b);
    if (r > mid) update(u<<1|1, l, r, k, a, b);
    push_up(u);
}

vector<int> query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return { tr[u].s[0], tr[u].s[1], tr[u].s[2] };
    push_down(u);
    int mid = (tr[u].l + tr[u].r) >> 1;
    vector<int> res(3);
    if (l <= mid) res = query(u<<1, l, r);
    if (r > mid) {
        auto t = query(u<<1|1, l, r);
        for (int i = 0; i < 3; ++i)
            res[i] = (res[i] + t[i]) % MOD;
    }
    return res;
}

int main() 
{
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        q[i].k = 1;
        cin >> q[i].op >> q[i].l >> q[i].r;
        if (q[i].op == 1) cin >> q[i].a >> q[i].b >> q[i].c;
        else if (q[i].op == 2) cin >> q[i].k;
        sca.push_back(q[i].l - 1); sca.push_back(q[i].r);
    }
    sort(sca.begin(), sca.end());
    sca.erase(unique(sca.begin(), sca.end()), sca.end());
    build(1, 0, sca.size() - 1);
    for (int i = 1; i <= m; ++i) {
        int op = q[i].op, l = get_id(q[i].l - 1) + 1, r = get_id(q[i].r);
        int a[] = {q[i].k, q[i].k, q[i].k}, b[] = {q[i].a, q[i].b, q[i].c};
        if (op == 1 || op == 2) update(1, l, r, 0, a, b);
        else if (op == 3) update(1, l, r, 1, a, b);
        else {
            auto t = query(1, l, r);
            ll sum = 0;
            for (int j = 0; j < 3; ++j)
                sum += 1ll * t[j] * t[j];
            cout << sum % MOD << '\n';
        }
    }
    return 0;
}