(CodeForces) F - Rectangles Gym - 101982F (线段树+扫描线)

本文深入探讨了扫描线算法在计算几何中的应用,特别是在处理多个矩形重叠问题上。通过异或操作,算法能准确计算出所有重叠区域中被奇数个矩形覆盖的总面积。文章提供了详细的代码实现,包括线段树的更新和查询过程,以及如何通过反转操作来高效处理区间覆盖问题。

题意:给出n个矩形的位置,问重叠奇数个矩形的面积之和

解:求面积并的时候是只要线段被覆盖的次数大于0,那就是算进去的,而这个只要奇数覆盖,所以只要异或就可以了,只有奇数次覆盖的线段,我们才考虑了。(把扫描线的知识都忘了,那时候也没学好

学习扫描线

#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define SZ(a) int((a).size())
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int N=1e5+5;
//il int Add(int &x,ll y) {return x=x+y>=mod?x+y-mod:x+y;}
//il int Mul(int &x,ll y) {return x=x*y>=mod?x*y%mod:x*y;}
struct node{
    ll l,r,h;
    bool operator<(const node &tp )const{
        return h<tp.h;
    }
}line[N<<1];
ll s[N<<1],sz[N<<3];
bool lz[N<<3];
il void cg(int rt,int l,int r){//反转
    int all=s[r]-s[l];
    sz[rt]=all-sz[rt];
}
il void pushdown(int rt,int l,int r){
    if(lz[rt]){
        int mid=(l+r)>>1;
        cg(rt<<1,l,mid),cg(rt<<1|1,mid,r);
        lz[rt<<1]^=1,lz[rt<<1|1]^=1;
        lz[rt]=0;
    }
}
il void update(int l,int r,int rt,int L,int R){
    if(L<=s[l] && R>=s[r]){
        cg(rt,l,r);
        lz[rt]^=1;
        return ;
    }
    int mid=(l+r)>>1;
    pushdown(rt,l,r);
    if(L<s[mid]) update(l,mid,rt<<1,L,R);
    if(R>s[mid]) update(mid,r,rt<<1|1,L,R);
    sz[rt]=sz[rt<<1]+sz[rt<<1|1];
}
int n;
int main(){
//    std::ios::sync_with_stdio(0);cin.tie(0);
    scanf("%d",&n);
    int x1,y1,x2,y2,cnt=0,tot=0;
    for(int i=1;i<=n;++i){
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        line[++cnt]={x1,x2,y1};
        line[++cnt]={x1,x2,y2};
        s[++tot]=x1,s[++tot]=x2;
    }
    sort(line+1,line+cnt+1);
    sort(s+1,s+tot+1);
    int all=unique(s+1,s+tot+1)-(s+1);
    ll ans=0;
    for(int i=1;i<=cnt-1;++i){
        update(1,all,1,line[i].l,line[i].r);
        ans+=sz[1]*(line[i+1].h-line[i].h);
    }
    printf("%lld\n",ans);
    return 0;
}








 

 

 

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值