【upc】2020年秋季组队训练赛第十五场 Black and White | 扫描线 + 线段树

本文主要是介绍【upc】2020年秋季组队训练赛第十五场 Black and White | 扫描线 + 线段树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题 B: Black and White

时间限制: 1 Sec  内存限制: 128 MB
提交 状态

题目描述

Consider a square map with N × N cells. We indicate the coordinate of a cell by (i, j), where 1 ≤ i, j ≤ N . Each cell has a color either white or black. The color of each cell is initialized to white. The map supports the operation
flip([xlow , xhigh], [ylow , yhigh]), which flips the color of each cell in the rectangle [xlow , xhigh] × [ylow , yhigh]. Given
a sequence of flip operations, our problem is to count the number of black cells in the final map. We illustrate this in the following example. Figure (a) shows the initial map. Next, we call flip([2, 4], [1, 3]) and obtain Figure (b). Then, we call flip([1, 5], [3, 5]) and obtain Figure (c). This map contains 18 black cells.

输入

The first line contains the number of test cases T (T < 10). Each test case begins with a line containing two integers N and K (1 < N, K < 10000), where N is the parameter of the map size and K is the number of flip operations. Each subsequent line corresponds to a flip operation, with four integers: xlow , xhigh, ylow , yhigh.

输出

For each test case, output the answer in a line.

样例输入 Copy

1   
5 2  
2 4 1 3
1 5 3 5

样例输出 Copy

18

题目大意:

每次翻转一个矩形内的灯的状态,最后询问矩形内有多少灯亮着

题目思路:

扫描线,扫描当前一行内有多少灯是亮着的

用线段树维护区间0变1 , 1变0即可

因为无论是增加覆盖次数,或者减少覆盖次数 都会使得奇偶性发生改变

所以用线段树维护1的数量即可

Code:

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
const ll INF= 1e18;
const int maxn = 1e5;
const int mod= 998244353;
inline bool read(ll &num)
{char in;bool IsN=false;in=getchar();if(in==EOF) return false;while(in!='-'&&(in<'0'||in>'9')) in=getchar();if(in=='-'){ IsN=true;num=0;}else num=in-'0';while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}if(IsN) num=-num;return true;}
ll n,m,p;
vector<pair<int,int>>v[maxn];
ll sum[maxn];
int lazy[maxn];
void push(int k){sum[k] = sum[k<<1] + sum[k<<1|1];
}
void build(int k,int l,int r){sum[k] = lazy[k] = 0;if(l == r) return ;int mid = (l+r)/2;build(k<<1,l,mid);build(k<<1|1,mid+1,r);
}
void down(int k,int l,int r){int mid = (l+r)/2;if(lazy[k]){lazy[k<<1] ^=1;lazy[k<<1|1] ^= 1;sum[k<<1] = (mid-l+1)-sum[k<<1];sum[k<<1|1] = (r-mid) - sum[k<<1|1];lazy[k] = 0;}
}
void Modify(int k,int l,int r,int x,int y){if(x<=l && y>=r){sum[k] = r-l+1 - sum[k];lazy[k] ^= 1;return;}down(k,l,r);int mid = (l+r)/2;if(x<=mid) Modify(k<<1,l,mid,x,y);if(y>mid) Modify(k<<1|1,mid+1,r,x,y);push(k);
}
int main(){int T;scanf("%d",&T);while(T--){read(n);read(m);for(int i=0;i<=10000;i++) v[i].clear();for(int i=1;i<=m;i++){int ax,ay,bx,by;scanf("%d%d%d%d",&ax,&bx,&ay,&by);v[ax].push_back({ay,by});v[bx+1].push_back({ay,by});}ll ans = 0;build(1,1,10000);for(int i=1;i<=10000;i++){for(auto x:v[i]) Modify(1,1,n,x.first,x.second);ans += sum[1];}printf("%lld\n",ans);}return 0;
}
/***
1
4 2
1 3 1 3
2 4 2 4
***/

 

这篇关于【upc】2020年秋季组队训练赛第十五场 Black and White | 扫描线 + 线段树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/411644

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

HDU4737线段树

题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i,j)<=m的总数。 const int Max_N = 100008 ;int sum[Max_N<<2] , x[Max_N] ;int n , m ;void push_up(int t){sum[t] = sum[t<<1] | sum[t<<1|1] ;}void upd

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

圆与线段的交点

poj 3819  给出一条线段的两个端点,再给出n个圆,求出这条线段被所有圆覆盖的部分占了整条线段的百分比。 圆与线段的交点 : 向量AB 的参数方程  P = A + t * (B - A)      0<=t<=1 ; 将点带入圆的方程即可。  注意: 有交点 0 <= t <= 1 ; 此题求覆盖的部分。 则 若求得 t  满足 ; double ask(d