ICPC 2018 Xuzhou Division game network

ACM-ICPC 2018 Xuzhou Division game network

 Blog last year recorded over the game experience: damn water problem

 One year later, the water problem is not a card, but the problem did not do a few. Level a little bit of progress.

 
 

F. Features Track

Meaning of the questions:

 Given \ (n-\) state in time (frame) cat, with each state \ (<a, b> \ ) FIG. If the same state occurs in a plurality of successive instants, constitute a movement. Seeking the longest such movement.

 

Ideas:

 Attendance problems, SLT pair the Map * use. Note that state to heavy! ! !
 

AC Code:

Click View Code

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

typedef pair<int, int> pii;
map<pii, int> S;

int id;
const int maxn = 100100;
vector<int> arr[maxn];

int ID(pii a) {
    if(S.find(a)!=S.end()) return S[a];
    return S[a]=++id;
}

int main() {
    int t; cin>>t;
    while(t--) {
        int n, k, maxid = 0;
        scanf("%d", &n);
        for(int i=1;i<=n;i++) {
            scanf("%d", &k);
            while(k--) {
                int a, b;
                scanf("%d %d", &a, &b);
                int id = ID(make_pair(a, b));
                arr[id].push_back(i);
          //      cout<<id<<' '<<i<<endl;
                maxid = max(maxid, id);
            }
        }
        int ans = 0;
        for(int i=1;i<=maxid;i++) {
            if(arr[i].size()==0) continue;
            else if(arr[i].size()==1) {
                ans = max(ans, 1);
                continue;
            }
            sort(arr[i].begin(), arr[i].end());
            unique(arr[i].begin(), arr[i].end());  // 去重!!!

            int now = 1;
            for(int j=1;j<arr[i].size();j++) {
                if(arr[i][j]==arr[i][j-1]+1) {
                    ans = max(ans, ++now);
                } else {
                    now = 1; 
                }

            }
        }
        printf("%d\n", ans);
        
        id = 0;
        S.clear();
        for(int i=1;i<=maxid;i++)
            arr[i].clear();
    }
    
    return 0;
}

H. Ryuji doesn't want to study

Meaning of the questions:

 There \ (n-\) book, respectively, each book \ (a [i] \) of knowledge. Reading from \ (L \) to \ (R & lt \) knowledge can be obtained as \ (a [l] × L + a [l + 1] × (L-1) + ⋯ + a [r-1] × A + 2 [R & lt] \) , where \ (R & lt L = - L +. 1 \) . There are times to ask, ask two types of inquiry 1. \ ([l, r] \ ) knowledge point range. 2. the first \ (b \) book knowledge points instead \ (c \) .
 

Ideas:

Little bit derived formulas can be found and can maintain a two prefix \ (sum1 [n] = \ sum_ {1} ^ {n} a_i \) and \ (sum2 [n] = \ sum_ {1} ^ {n} ia_i \) , then the \ (ANS [L, R & lt] = (SUM2 [R & lt] -sum2 [-L. 1]) - (NR) (SUM2 [R & lt] -sum2 [-L. 1]) \) , then a tree like array to write out soon.
Note that addthe function should work for the \ (the X-\ the n-Le \) , the situation got started writing less than WA. . .
Into a tree line, burst ll once they WA

 

AC Code:

Fenwick tree writing

#include<iostream>
#include<cstdio>
#define lowbit(x) ((x)&(-x))
using namespace std;
typedef long long ll;
const int maxn = 100100;

ll C1[maxn];
ll C2[maxn];
int n, q;
ll arr[maxn];

void add(ll C[], int x, ll val) {
    while(x<=n) {
        C[x] += val;
        x += lowbit(x);
    }
}

ll sum(ll C[], int x) {
    ll res = 0;
    while(x) {
        res += C[x];
        x -= lowbit(x);
    }
    return res;
}

int main() {
    cin>>n>>q;
    for(int i=1;i<=n;i++) {
        ll val;
        scanf("%lld", &val);
        arr[i] = val;
        add(C1, i, val);
        add(C2, i, val*(n+1-i));
    }
    
    while(q--) {
        int op;
        ll l, r;
        scanf("%d %lld %lld", &op, &l, &r);
        if(op==1) {
            ll Sum1 = sum(C2, r) - sum(C2, l-1);
            ll Sum2 = sum(C1, r) - sum(C1, l-1);
            printf("%lld\n", Sum1 - Sum2*(n-r));
        } else {
            add(C1, l, r-arr[l]);
            add(C2, l, (r-arr[l])*(n+1-l));
            arr[l] = r;
        }
    }
    return 0;
}

 

Segment tree writing

Click View Code

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define MID (l+r)>>1
const int maxn = 100010;
typedef long long ll;

ll t1[maxn<<2];
ll t2[maxn<<2];
ll arr[maxn];
int n;

void build(ll t[], bool f, int rt, int l, int r) {
    if(l==r) {
        if(f)
            t[rt] = arr[l];
        else
            t[rt] = arr[l]*(n+1-l);
        return;
    }
    int mid = MID;
    build(t, f, lson);
    build(t, f, rson);
    t[rt] = t[rt<<1] + t[rt<<1|1];
}

void update(ll t[], int pos, ll val, int rt, int l, int r) {
    if(l==r) {
        t[rt] += val;
        return;
    }

    int mid = MID;
    if(pos<=mid)
        update(t, pos, val, lson);
    else
        update(t, pos, val, rson);

    t[rt] = t[rt<<1] + t[rt<<1|1];
}


ll query(ll t[], int L, int R, int rt, int l, int r) {
    if(L<=l && R>=r) {
        return t[rt];
    }

    ll ans = 0;
    int mid = MID;
    if(L<=mid)
        ans += query(t, L, R, lson);
    if(mid<R)
        ans += query(t, L, R, rson);
    return ans;
}


int main() {
    int q;
    scanf("%d %d", &n, &q);
    for(int i=1;i<=n;i++) {
        scanf("%lld", &arr[i]);
    }

    build(t1, 1, 1, 1, n);
    build(t2, 0, 1, 1, n);

    while(q--) {
        int op;
        ll l, r;
        scanf("%d %d %d", &op, &l, &r);
        if(op==1) {
            ll sum1 = query(t1, l, r, 1, 1, n);
            ll sum2 = query(t2, l, r, 1, 1, n);
            sum2 -= sum1 * (n-r);
            printf("%lld\n", sum2);
        } else {
            update(t1, l, r-arr[l], 1, 1, n);
            update(t2, l, (r-arr[l])*(n+1-l), 1, 1, n);

            arr[l] = r;
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/izcat/p/11441093.html