社区讨论

求条(没过样例)

P1856[IOI 1998 / USACO5.5] 矩形周长 Picture参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mhjheu7b
此快照首次捕获于
2025/11/04 02:36
4 个月前
此快照最后确认于
2025/11/04 02:36
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>
#define N 50005
#define int long long 
using namespace std;

int n,ans;
int x[N],y[N],lenx,leny;
struct Line{
    int le,ri,h,tag;
} line1[N],line2[N];
struct Tree{
    int c,tag;
} tree1[N],tree2[N];
// 从上到下、从左到右

bool cmp(Line a,Line b){
    if (a.h == b.h) return a.tag > b.tag;
    return a.h < b.h;
}

void pushup1(int k,int l,int r){
    if (!tree1[k].tag){
        int lc = k*2,rc = k*2+1;
        if (l == r) tree1[k].c = 0;
        else tree1[k].c = tree1[lc].c + tree1[rc].c;
    }
}

void pushup2(int k,int l,int r){
    if (!tree2[k].tag){
        int lc = k*2,rc = k*2+1;
        if (l == r) tree2[k].c = 0;
        else tree2[k].c = tree2[lc].c + tree2[rc].c;
    }
}

void change1(int k,int l,int r,int ll,int rr,int tag){
    if (ll <= l && r <= rr){
        tree1[k].tag += tag;
        if (tree1[k].tag) tree1[k].c = x[rr+1] - x[ll];
        else tree1[k].c = 0;
        pushup1(k,l,r);
        return;
    }
    int mid,lc,rc;
    mid = (l+r)/2;
    lc = k*2;
    rc = k*2+1;
    if (ll <= mid) change1(lc,l,mid,ll,rr,tag);
    if (rr > mid) change1(rc,mid+1,r,ll,rr,tag);
    pushup1(k,l,r);
}

void change2(int k,int l,int r,int ll,int rr,int tag){
    if (ll <= l && r <= rr){
        tree2[k].tag += tag;
        if (tree2[k].tag) tree2[k].c = y[rr+1] - y[ll];
        else tree2[k].c = 0;
        pushup2(k,l,r);
        return;
    }
    int mid,lc,rc;
    mid = (l+r)/2;
    lc = k*2;
    rc = k*2+1;
    if (ll <= mid) change2(lc,l,mid,ll,rr,tag);
    if (rr > mid) change2(rc,mid+1,r,ll,rr,tag);
    pushup2(k,l,r);
}


signed main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
    for (int i=1;i<=n;++i){
        int x1,x2,y1,y2; cin >> x1 >> y1 >> x2 >> y2;
        x[i*2-1] = x1,x[i*2] = x2;
        y[i*2-1] = y1,y[i*2] = y2;
        line1[i*2-1] = {x1,x2,y1,1},line1[i*2] = {x1,x2,y2,-1};
        line2[i*2-1] = {y1,y2,x1,1},line2[i*2] = {y1,y2,x2,-1};
    }
    n <<= 1;
    sort(x+1,x+1+n); sort(y+1,y+1+n);
    sort(line1+1,line1+1+n,cmp); sort(line2+1,line2+1+n,cmp);
    lenx = unique(x+1,x+1+n) - (x + 1);
    leny = unique(y+1,y+1+n) - (y + 1);
    for (int i=1;i<=n;++i){
        int lastans = 0,x1,x2,y1,y2;
        lastans = tree1[1].c;
        x1 = lower_bound(x+1,x+1+lenx,line1[i].le) - x;
        x2 = lower_bound(x+1,x+1+lenx,line1[i].ri) - (x + 1);
        change1(1,1,lenx,x1,x2,line1[i].tag);
        ans += abs(tree1[i].c - lastans);

        lastans = tree2[1].c;
        y1 = lower_bound(y+1,y+1+leny,line2[i].le) - y;
        y2 = lower_bound(y+1,y+1+leny,line2[i].ri) - (y + 1);
        change2(1,1,leny,y1,y2,line2[i].tag);
        ans += abs(tree2[i].c - lastans);
    }
    cout << ans;
	
	
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...