Codeforces Round 909 (Div. 3) A~G个人题解

A:签到

void solve(){
    cin >> n;
    if(n%3==0){
        cout << "Second" << endl;
    }else{
        cout << "First" << endl;
    }
}

B:签到

 cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int ans = 0;
 
    for (int i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            int ma = 0, mi = LNF, now = 0;
            for (int j = 1; j <= n; j += i)
            {
                now = 0;
                for (int k = j; k <= j + i - 1; k++)
                {
                    now += a[k];
                }
                ma = max(ma, now);
                mi = min(mi, now);
            }
            ans = max(ans, ma - mi);
             ma = 0, mi = LNF, now = 0;
            for (int j = 1; j <= n; j += n/i)
            {
                now = 0;
                for (int k = j; k <= j + n/i - 1; k++)
                {
                    now += a[k];
                }
                ma = max(ma, now);
                mi = min(mi, now);
            }
            ans = max(ans, ma - mi);
        }
    }
    cout << ans << endl;

C:贪心

 cin >> n;
    int ans = -LNF, now = 0;
    for (int i = 1; i <= n; i++)
        cin >> a[i], ans = max(ans, a[i]);
    for (int i = 1; i <= n; i++)
    {   
        
        if (now>=0&&abs(a[i]) % 2 != abs(a[i - 1]) % 2)
        {
            now += a[i];
        }
        else
        {
            now = a[i];
        }
        
        ans = max(ans, now);
    }
    cout << ans << endl;

D:手玩可得只有1,2或者相同的情况

cin >> n;
    map<int, int> mp;
    int ans = 0;
    for (int i = 1; i <= n;i++){
        cin >> a[i];
       if(a[i]==2)
           a[i]--;
       mp[a[i]]++;
    }
   for(auto i:mp){
       ans += (i.second) * (i.second - 1) / 2;
   }
    cout << ans << endl;

E:当最小的数在第一个时,它和后面的数就无法有效移动,所以最小的数之后的数都无法归位,若出现逆序输出-1,否则统计第一个最小的数之前出现了多少个数。

 cin >> n;
    int mi = LNF;
    for (int i = 1; i <= n;i++){
        cin >> a[i],mi=min(mi,a[i]);
    }
    vector<pair<int, int>> v;
    bool is = 0;
    int num = 0;
    for (int i = 1; i <= n;i++){
       if(!is&&a[i]==mi){
           is = 1;
           num = i-1;
           continue;
       }
       if(is){
        if(a[i]<a[i-1]){
            cout << -1 << endl;
            return;
        }
       }
    }
    cout << num << endl;

F:开始时构造一条链,之后只拿n号节点进行操作即可,记得记录上次操作的位置。

  cin >> n>>q;
    for (int i = 1; i < n;i++){
        cout << i << " " << i + 1 << endl;
    }
    int fore = n - 1;
    while(q--){
        int d;
        cin >> d;
        if(fore==d){
            cout << "-1 -1 -1" << endl;
        }else{
            cout << n << " " << fore << " " << d << endl;
            fore = d;
        }
    }

G:

题意:给一颗树,一个序列p,每次问序列p下标l到r区间有是否有p[i]节点是x的子节点

赛时做法:先dfs打出各个点的dfs序(id数组),并记录回溯时的当前dfs编号(lim数组),则dfs序在id[i]与lim[i]之间的点为子树上的点,建立用id[p[i]]值域主席树,每次查询第l到r两个版本的主席树中间有多少个值在id[x]到lim[x]的点,如果数量不为0则存在。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define LNF 1e18
#define INF 0x7fffffff
#define int long long
#define lowbit(x) ((x) & (-x))
#define abs(x) llabs(x)
#define endl '\n'
#define Y() cout << "Yes" << endl
#define N() cout << "No" << endl
const db eps = 1e-9;
const int mod = 998244353;
const int MAXN = 2e5 + 5;

int n,q,head[100005];
struct segment_tree{
    int cnt=0;
    struct node{
        int l, r, val;
    } tr[MAXN << 5];
    int copy(node &p){
        tr[++cnt] = p;
        return cnt;
    }
    int build(int l,int r){
        int p = ++cnt;
        tr[p].val = 0;
        if(l<r){
            int mid = l + r >> 1;
            tr[p].l = build(l, mid);
            tr[p].r = build(mid + 1, r);
        }
        return p;
    }
    int query(int ql,int qr,int l,int r,int qql,int qqr){
        if(l>r)
            return 0;
        
        if(l>=qql&&r<=qqr){
            return tr[qr].val - tr[ql].val;
        }
        int mid = l + r >> 1;
        int ans = 0;
        if(qql<=mid){
            ans += query(tr[ql].l, tr[qr].l, l, mid, qql, qqr);
        }
        if(qqr>mid){
            ans += query(tr[ql].r, tr[qr].r, mid + 1, r, qql, qqr);
        }
        return ans;
    }
    int add(int p,int l,int r,int addpos,int x){
        int t = copy(tr[p]),mid=(l+r>>1);
        tr[t].val += x;
        if(l<r){
            if(addpos<=mid)
                tr[t].l = add(tr[p].l, l, mid, addpos, x);
            else
                tr[t].r = add(tr[p].r, mid + 1, r, addpos, x);
        }
        return t;
    }
    int query_kth(int ql,int qr,int l,int r,int k){
        if(l>=r)
            return l;
        int value = tr[tr[qr].l].val - tr[tr[ql].l].val, mid = l + r >> 1;
        if(value>=k)
            return query_kth(tr[ql].l, tr[qr].l, l, mid, k);
        else
            return query_kth(tr[ql].r, tr[qr].r, mid + 1, r, k - value);
    }
} SGT;
void solve()
{
    cin >> n>>q;
    vector<vector<int>> pth(n + 1);
    vector<int> p(n + 1),f(n+1),id(n+1),lim(n+1);
    for (int i = 1; i < n;i++){
        int u, v;
        cin >> u >> v;
        pth[u].push_back(v);
        pth[v].push_back(u);
    }
    for (int i = 1; i <= n;i++)
        cin >> p[i],f[p[i]]=i;
    //vector<tuple<int, int, int>> ask;
    
    int cnt = 1;
    auto dfs = [&](int pos, int fa, auto dfs) -> void {
        for(auto i:pth[pos]){
            if(i==fa)
                continue;
            id[i] = ++cnt;
            dfs(i, pos, dfs);
            lim[i] = cnt;
        }
        
    };
    id[1] = 1;
    dfs(1, 0, dfs);
    lim[1] = cnt;
    SGT.cnt = 0;
    head[0]=SGT.build(1, n);
    for (int i = 1; i <= n;i++){
        //cout << id[p[i]] << " "<<lim[p[i]]<<" ";
        head[i] = SGT.add(head[i-1], 1, n, id[p[i]], 1);
    }
    //cout << 1 << endl;
    while(q--){
        int l, r, x;
        cin >> l >> r >> x;
        if(SGT.query(head[l-1],head[r],1,n,id[x],lim[x])!=0){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
        //ask.push_back({l, r, x});
    }
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值