2018 Hunan Fourteenth students computer programming contest

Topic Link

Portal

topic

A title

Thinking

Sign.

Code

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define fi first
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
 
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 200000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
int n;
 
int main() {
    scanf("%d", &n);
    printf("ooo");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("ooo");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("ooo");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("ooo\n");
    printf("..o");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("o.o");
    for(int i = 1; i <= n; ++i) printf(".");
    printf(".o.");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("o.o\n");
    printf("ooo");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("o.o");
    for(int i = 1; i <= n; ++i) printf(".");
    printf(".o.");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("ooo\n");
    printf("o..");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("o.o");
    for(int i = 1; i <= n; ++i) printf(".");
    printf(".o.");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("o.o\n");
    printf("ooo");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("ooo");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("ooo");
    for(int i = 1; i <= n; ++i) printf(".");
    printf("ooo\n");
    return 0;
}

B title

Thinking

Hit the table to find the law.

Code

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define fi first
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
 
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 200000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
int n, m;
int C[4007][2007];
 
int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif
    for(int i = 0; i <= 4000; ++i) {
        C[i][0] = 1;
        for(int j = 1; j <= min(i, 2000); ++j) {
            C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod;
        }
    }
    while(~scanf("%d%d", &n, &m)) {
        printf("%lld\n", 1LL * (C[n+m][m] - 1) * (C[n+m][m] - 1) % mod);
    }
    return 0;
}

C title

Thinking

See examples guess.

Code

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define fi first
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
 
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 200000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
int h, c;
 
int main() {
    while(~scanf("%d%d", &h, &c)) {
        printf("%d\n", h > c ? h : c + 1);
    }
    return 0;
}

E title

Thinking

When there is an initial rapid Unicom \ (n \ times m \) a, since the number of re-operation after each block Unicom complex, so we can look at the shift in thinking, after each operation becomes reduced block Unicom \ (X \) , the answer is \ (the n-\ Times MX \) .

After each increase we can find a new horizontal line (one row in front of no operation) reduces the Unicom block \ (m-1 \) a, after each block to increase a reduced vertical Unicom \ (n-1 \) months.

We assumed that the number of horizontal lines \ (H \) , the number of vertical lines \ (W \) , then only the horizontal when the answer is \ (n-\ Times MH \ Times (. 1-m) \) , when only the vertical when the answer is \ (the n-\ Times mw \ Times (the n--1) \) . When there are both horizontal vertical repellent capacity needs little bit, as we repeat for crosspoint lost, requiring added back, is repeated for the lost portion \ ((h-1) \ times (w-1 ) \) , then the answer is \ (n-\ Times MH \ Times (. 1-m) -w \ Times (. 1-n-) + (. 1-H) \ Times (. 1-W) \) .

Note that the same line is operated many times can only be counted once, so we can consider using the left and right to open and close to maintain the tree line.

Code

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define fi first
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
 
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 200000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
bool lazy[3][maxn<<2];
int n, m, q, cnt;
int op[maxn], l[maxn], r[maxn], sum[3][maxn<<2], num[maxn*2];
 
void push_down(int rt, int l, int r, int id) {
    if(!lazy[id][rt]) return;
    lazy[id][rt] = 0;
    lazy[id][lson] = lazy[id][rson] = 1;
    int mid = (l + r) >> 1;
    sum[id][lson] = num[mid+1] - num[l];
    sum[id][rson] = num[r+1] - num[mid+1];
}
 
void push_up(int rt, int id) {
    sum[id][rt] = sum[id][lson] + sum[id][rson];
}
 
void build(int rt, int l, int r) {
    for(int i = 0; i < 2; ++i) sum[i][rt] = 0, lazy[i][rt] = false;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(lson, l, mid), build(rson, mid + 1, r);
}
 
void update(int rt, int l, int r, int L, int R, int id) {
    if(l == L && r == R) {
        sum[id][rt] = num[R+1] - num[L];
        lazy[id][rt] = true;
        return;
    }
    push_down(rt, l, r, id);
    int mid = (l + r) >> 1;
    if(R <= mid) update(lson, l, mid, L, R, id);
    else if(L > mid) update(rson, mid + 1, r, L, R, id);
    else {
        update(lson, l, mid, L, mid, id);
        update(rson, mid + 1, r, mid + 1, R, id);
    }
    push_up(rt, id);
}
 
int getnum(int x) {
    return lower_bound(num + 1, num + cnt + 1, x) - num;
}
 
int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif
    while(~scanf("%d%d%d", &n, &m, &q)) {
        cnt = 0;
        for(int i = 1; i <= q; ++i) {
            scanf("%d%d%d", &op[i], &l[i], &r[i]);
            num[++cnt] = l[i], num[++cnt] = r[i] + 1;
        }
        sort(num + 1, num + cnt + 1);
        cnt = unique(num + 1, num + cnt + 1) - num - 1;
        build(1, 1, cnt);
        for(int i = 1; i <= q; ++i) {
            l[i] = getnum(l[i]), r[i] = getnum(r[i]+1);
            if(op[i] == 1) {
                update(1, 1, cnt, l[i], r[i]-1, 0);
            } else {
                update(1, 1, cnt, l[i], r[i]-1, 1);
            }
            // printf("[%d %d]\n", sum[0][1], sum[1][1]);
            LL ans = 1LL * n * m - 1LL * sum[0][1] * (m - 1) - 1LL * sum[1][1] * (n - 1);
            if(sum[0][1] && sum[1][1]) ans += 1LL * (sum[0][1] - 1) * (sum[1][1] - 1);
            printf("%lld\n", ans);
        }
    }
    return 0;
}

H title

Thinking

Since it is guaranteed query \ (rl is an \ Leq 2 \) , so we can use the tree three arrays are able to save to \ (I \) is the left end point of the interval length of \ (0,1,2 \) interval is What interval coverage too.

For insertion \ ([l, r] \ ) for each point number as a left interval length of \ (0 \) is covered both, \ ([L, R & lt-. 1] \) is the interval length of the left end point \ (1 \) is covered both, \ ([L, R & lt-2] \) for the left endpoint of interval length \ (2 \) to be covered are, respectively, \ (Update \) can.

Code

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define fi first
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
 
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 100000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
inline int read() {
    int f = 0, x = 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;
}
 
int n, q, op, l, r;
int tree[5][maxn];
 
void bit_add(int x, int id, int val) {
    while(x <= n) {
        tree[id][x] += val;
        x += lowbit(x);
    }
}
 
int bit_query(int x, int id) {
    int ans = 0;
    while(x) {
        ans += tree[id][x];
        x -= lowbit(x);
    }
    return ans;
}
 
int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif
    while(~scanf("%d%d", &n, &q)) {
        for(int i = 0; i <= 2; ++i) {
            for(int j = 0; j <= n; ++j) tree[i][j] = 0;
        }
        while(q--) {
            op = read(), l = read(), r = read();
            if(op == 1) {
                bit_add(l, 0, 1);
                bit_add(r + 1, 0, -1);
                if(l <= r - 1) {
                    bit_add(l, 1, 1);
                    bit_add(r, 1, -1);
                }
                if(l <= r - 2) {
                    bit_add(l, 2, 1);
                    bit_add(r - 1, 2, -1);
                }
            } else {
                printf("%d\n", bit_query(l, r-l));
            }
        }
    }
    return 0;
}

J title

Thinking

We consider the current \ (dfs \) to the node \ (u \) , from \ (1 \) to \ (u \) father nodes, the answer is \ (SUM \) , so \ (f_u = sum + u \) contribution.

When \ (a [u] \) in \ (1 \) to \ (U \) when this is the first time on the strand, at this time \ (U \) contribution to this different numbers of strands number;

When \ (a [u] \) is the second time, this time \ (U \) contribution to the last \ (a [u] \) appears to position \ (U \) on a number of different paths the number \ (1 + \) , the \ (1 \) represented by an ordered pair is \ ((a [U], a [U]) \), .

When \ (a [u] \) that appear more than \ (2 \) , the case \ (U \) contribution to the last \ (a [u] \) appears to position \ (U \) on the path the number of different numbers.

I remember clear back when the \ (u \) the impact.

Code

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define fi first
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
 
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 200000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
int ans[maxn], sum;
int n, u, tot, cnt;
int head[maxn], a[maxn], vis[maxn], lst[maxn];
 
struct edge {
    int v, next;
}ed[maxn*2];
 
void add(int u, int v) {
    ed[tot].v = v;
    ed[tot].next = head[u];
    head[u] = tot++;
}
 
void dfs(int u) {
    int tmp = 0;
    if(!vis[a[u]]) tmp = cnt, ++cnt;
    else if(vis[a[u]] == 1) tmp = cnt - lst[a[u]] + 1;
    else tmp = cnt - lst[a[u]];
    ans[u] = sum + tmp;
    int pp = lst[a[u]];
    lst[a[u]] = cnt;
    sum += tmp;
    ++vis[a[u]];
    for(int i = head[u]; ~i; i = ed[i].next) {
        int v = ed[i].v;
        dfs(v);
    }
    sum -= tmp;
    --vis[a[u]];
    if(vis[a[u]] == 0) --cnt;
    lst[a[u]] = pp;
}
 
int main() {
    while(~scanf("%d", &n)) {
        tot = cnt = sum = 0;
        assert(n >= 1 && n <= 100000);
        for(int i = 1; i <= n; ++i) vis[i] = ans[i] = lst[i] = 0, head[i] = -1;
        for(int i = 2; i <= n; ++i) {
            scanf("%d", &u);
            assert(u >= 1 && u <= n && u != i);
            add(u, i);
        }
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            assert(a[i] >= 1 && a[i] <= n);
        }
        dfs(1);
        for(int i = 2; i <= n; ++i) printf("%d\n", ans[i]);
    }
    return 0;
}

K title

Thinking

We \ (a \) be a prefix and then consider each (b \) \ contribution can be.

Code

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define fi first
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)

const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 500000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int n, m, L, R;
LL sum[maxn], a[maxn], b[maxn];
 
int main() {
    while (~scanf("%d%d%d%d", &n, &m, &L, &R)) {
        L += 2, R += 2;
        sum[0] = 0;
        for (int i = 1; i <= n + 1; i++) scanf("%lld", &a[i]), sum[i] = sum[i - 1] + a[i];
        for (int i = 1; i <= m + 1; i++) scanf("%lld", &b[i]);
        LL ans = 0;
        for (int i = 1; i <= m + 1; i++) {
            int l = max(1, L - i), r = min(n + 1, R - i);
            if (l > n + 1 || r < 1) continue;
            ans = (ans + 1LL * b[i] * ((sum[r] - sum[l - 1] + mod) % mod) % mod) % mod;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Dillonh/p/11621180.html