80分求优化

P2692 覆盖

我的做法是标记行和列,三重循环肯定超时,输出靠判断两个数组来判断整张地图 ```cpp #include<bits/stdc++.h> using namespace std; int h[5005],l[5005]; int main(){ int n,m,b,g; cin >> n >> m >> b >> g; for(int i=1;i<=b;i++){ int x,y; cin >> x >> y; for(int j=x;j<=y;j++){ h[j] = 1; } } for(int i=1;i<=g;i++){ int x,y; cin >> x >> y; for(int j=x;j<=y;j++){ l[j] = 1; } } int cnt = 0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(h[i] == 1||l[j] == 1) cnt++; } } cout << cnt; return 0; } ```
by Li_Yichen @ 2024-02-01 20:41:02


|