kZjPBD.jpg

Codeforces Round 791 (Div. 2)

 

C - Rooks Defenders

线段树模板,维护:1)v:个数 , 2)sum:v的个数是否大于0.

// #include"bits/stdc++.h"
#include"iostream"
using namespace std;
const int N = 2e5;
struct Node{
    int l, r, v, sum;
}tr1[N*4], tr2[N*4];
int n, q;


void build(Node tr[], int u, int l, int r){
    // cout<<"||| build :"<<u << ' '<<l<<" "<<r<<endl;
    // tr[u] = {l, r, 0, 0};
    tr[u].l = l;  tr[u].r = r;
    if (l == r)return ;
    int mid = l+r>>1;
    build(tr, u*2, l, mid);
    build(tr, u*2+1, mid+1, r);
}

void modify(Node tr[], int u, int x, int v){
    // cout<<u <<' '<<   << endl;
    if (tr[u].l == tr[u].r){
        tr[u].v += v;
        tr[u].sum = (tr[u].v > 0);
        return ;
    }
    int mid = (tr[u].l + tr[u].r) >> 1;
    if (x<=mid)
        modify(tr, u*2, x, v);
    else 
        modify(tr, u*2+1, x, v);

    //  pushup
    tr[u].sum = tr[u*2].sum + tr[u*2+1].sum;
}

int query(Node tr[], int u, int l, int r){
    // cout<<u<<' '<<tr[u].l<<' '<<tr[u].r<<endl;
    if (l <= tr[u].l && tr[u].r <= r) return tr[u].sum;
    int mid = (tr[u].l + tr[u].r)/2;
    int sum = 0;
    if (l <= mid)
        sum += query(tr, u*2, l, r);
    if (r > mid)
        sum += query(tr, u*2+1, l, r);
    return sum;
}

int main(){   
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    cin >> n >> q;
    build(tr1, 1, 1, n), build(tr2, 1, 1, n);
    while(q--){
        int op; 
        cin>>op;
        if (op == 1){
            int x, y;
            cin>>x>>y;
            modify(tr1, 1, x, 1);
            modify(tr2, 1, y, 1);
        }
        else if(op == 2){
            int x, y;
            cin>>x>>y;
            modify(tr1, 1, x, -1);
            modify(tr2, 1, y, -1);

        }
        else{
            int x0, y0, x1, y1;
            cin >> x0 >> y0 >> x1 >> y1;
            if (query(tr1, 1, x0, x1) == x1 - x0 + 1 || query(tr2, 1, y0, y1) == y1 - y0 + 1)
                cout << "Yes" << '\n';
            else cout << "No" << '\n';
        }
    }

    return 0;
}

 

 

D.D. Toss a Coin to Your Graph...

 

dfs找环+最长路

 

#include"iostream"
#include"vector"
#include"algorithm"

#define int long long 
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
vector<int> G[N];


int n,m;
ll k;


ll a[N];
ll b[N];

int mark[N];
int max_lem[N];
int in_loop[N];
int ok = 0;

void dfs(int x,int f, int mid){
    mark[x] = f;
    // cout<<"||| dfs(mid ="<<mid<<") "<< x<<endl;

    in_loop[x] = 1;
    int mx = 0;
    for(auto j:G[x]){
        // cout<<"||| son "<<j<<endl;

        if (a[j] > mid) continue;

        if (mark[j]!=-1){
            if (in_loop[j] == 1) {
                ok = 1;
            }
        }
        else{
            dfs(j, f, mid);
        }

        mx = max(mx, max_lem[j]);
    }

    max_lem[x] = mx + 1;
    in_loop[x] = 0;
    // cout<<"||| max_len "<<max_lem[x]<<endl;

}

signed main(){

    scanf("%lld %lld %lld", &n, &m, &k);
    // cout<<"|| k = "<<k<<endl;
    for(int i=1;i<=n;i++) scanf("%lld", &a[i]), b[i] = a[i];
    sort(b+1, b+1+n);
    for(int i=1;i<=m;i++){
        int u,v; scanf("%lld %lld", &u, &v);
        G[u].push_back(v);
    }
    // for(int i=1;i<=n;i++)cout<<a[i]<<" ";
    int l = 1, r = 1e9+10;
    // cout<<l <<" "<<r<<endl;
    int ans = -1;

    while(l <= r){
        int mid = (l+r)/2;
        // int mid = 4;
        ok = 0;
        for(int i=1;i<=n;i++) mark[i] = -1, max_lem[i]=0;

        int mid_max_len = 0; 
        for(int i=1;i<=n;i++){
            if (a[i] > mid) continue;
            if(mark[i] != -1) continue;

            dfs(i, i, mid);
            mid_max_len = max(mid_max_len, max_lem[i]);
        }

        for(int i=1;i<=n;i++){
            ok = ok | (max_lem[i] >= k);
        }

        // for(int i=1;i<=n;i++) 
            // cout<<i<<"-"<<max_lem[i]<<endl;
        // cout<<"|| ok "<<ok<<endl;
        // break;

        // if (1ll*mid_max_len >=k || ok == 1){
        if ( ok == 1){
            ans = mid;
            r=mid-1;
        }
        else{
            l = mid+1;
        }

        // break;
    }
    printf("%lld\n", ans);






    return 0;
}

 

posted @ 2024-02-25 22:27  Through_The_Night  阅读(2)  评论(0编辑  收藏  举报