題目:Matrix
大體內容
題意:
初始化的矩陣爲0;
C->給出矩形範圍,範圍內的進行一次翻轉操作
Q->查詢某個位置0或者1
解法:
二維線段樹,或者樹狀數組
樹狀數組:同一維
詳細分析
樹狀數組無;
線段樹,靜等填坑
二維線段樹
//由於只有0,1所以只需異或操作即可
// * 成段更新,單點查詢
> * 更新的時候,找到所在的段,更新即可
> * 一個點[x,y],x屬於固定的一些段中
如:
[1,5]
[1,3] [4,5]
[1,2] 3 4 5
1 2
對於橫座標爲3的點,只可能在更新[1,5][1,3][3,3]時會更新到,所以查詢時都要進行y的查詢。
同樣的對於y周也是同樣的,包含的那些段中都要進行異或操作。
樹狀數組
- /*************************************************************************
- > File Name: poj2155.cpp
- > Author: cy
- > Mail: [email protected]
- > Created Time: 14/10/18 12:30:44
- ************************************************************************/
- #include<iostream>
- #include<cstring>
- #include <algorithm>
- #include<cstdlib>
- #include<vector>
- #include<cmath>
- #include<stdlib.h>
- #include<iomanip>
- #include<list>
- #include<deque>
- #include<map>
- #include <stdio.h>
- #include <queue>
- #define maxn 1400+5
- #define inf 0x3f3f3f3f
- #define INF 0x3FFFFFFFFFFFFFFFLL
- #define rep(i,n) for(i=0;i<n;i++)
- #define reP(i,n) for(i=1;i<=n;i++)
- #define ull unsigned long long
- #define ll long long
- #define cle(a) memset(a,0,sizeof(a))
- using namespace std;
- int val[1500][1500];
- int bit(int x){
- return x&(-x);
- }
- void update(int x,int y){
- int i,j;
- for(i=x;i<=maxn;i+=bit(i)){
- for(j=y;j<=maxn;j+=bit(j)){
- val[i][j]++;
- }
- }
- }
- int getsum(int x,int y){
- int i,j;int sum=0;
- for(i=x;i>0;i-=bit(i)){
- for(j=y;j>0;j-=bit(j)){
- sum+=val[i][j];
- }
- }
- return sum;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- #endif
- int T;cin>>T;
- while(T--){
- int n,m;
- cin>>n>>m;
- cle(val);
- for(int tt=1;tt<=m;tt++){
- char c;cin>>c;
- if(c=='C'){
- int x1,y1,x2,y2;
- cin>>x1>>y1>>x2>>y2;
- update(x1,y1);update(x2+1,y2+1);update(x2+1,y1);update(x1,y2+1);
- }
- if(c=='Q'){
- int x,y;
- cin>>x>>y;
- cout<<getsum(x,y)%2<<endl;
- }
- }
- cout<<endl;
- }
- return 0;
- }
二維線段樹
- /*************************************************************************
- > File Name: poj12155.cpp
- > Author: cy
- > Mail: [email protected]
- > Created Time: 14/10/18 14:33:19
- ************************************************************************/
- #include<iostream>
- #include<cstring>
- #include <algorithm>
- #include<cstdlib>
- #include<vector>
- #include<cmath>
- #include<stdlib.h>
- #include<iomanip>
- #include<list>
- #include<deque>
- #include<map>
- #include <stdio.h>
- #include <queue>
- #define maxn 5000+5
- #define inf 0x3f3f3f3f
- #define INF 0x3FFFFFFFFFFFFFFFLL
- #define rep(i,n) for(i=0;i<n;i++)
- #define reP(i,n) for(i=1;i<=n;i++)
- #define ull unsigned long long
- #define ll long long
- #define cle(a) memset(a,0,sizeof(a))
- using namespace std;
- bool tree[maxn][maxn];
- int ans=0;
- int n;
- void update_y(int i,int rt,int l,int r,int l1,int rr){
- if(l==l1&&r==rr){
- tree[i][rt]^=1;
- return;
- }
- int mid=(l+r)>>1;
- if(rr<=mid){
- update_y(i,rt<<1,l,mid,l1,rr);
- }
- else if(l1>mid){
- update_y(i,rt<<1|1,mid+1,r,l1,rr);
- }
- else{
- update_y(i,rt<<1,l,mid,l1,mid);
- update_y(i,rt<<1|1,mid+1,r,mid+1,rr);
- }
- }
- void update_x(int rt,int l,int r,int x1,int y1,int x2, int y2){
- if(l==x1&&r==y1){
- update_y(rt,1,1,n,x2,y2);
- return;
- }
- int mid=(l+r)>>1;
- if(y1<=mid){
- update_x(rt<<1,l,mid,x1,y1,x2,y2);
- }
- else if(x1>mid){
- update_x(rt<<1|1,mid+1,r,x1,y1,x2,y2);
- }
- else{
- update_x(rt<<1,l,mid,x1,mid,x2,y2);
- update_x(rt<<1|1,mid+1,r,mid+1,y1,x2,y2);
- }
- }
- void query_y(int i,int rt,int l,int r,int y){
- ans^=tree[i][rt];
- if(l==r)return;
- int mid=(l+r)>>1;
- if(y<=mid){
- query_y(i,rt<<1,l,mid,y);
- }
- else if(y>mid){
- query_y(i,rt<<1|1,mid+1,r,y);
- }
- }
- void query_x(int rt,int l,int r,int x,int y){
- query_y(rt,1,1,n,y);
- if(l==r)return;
- int mid=(l+r)>>1;
- if(x<=mid){
- query_x(rt<<1,l,mid,x,y);
- }
- else {
- query_x(rt<<1|1,mid+1,r,x,y);
- }
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- #endif
- int T;cin>>T;
- while(T--)
- {
- int m;
- cin>>n>>m;
- cle(tree);
- int i;
- rep(i,m){
- ans=0;
- char c;
- cin>>c;
- if(c=='C')
- {
- int x1,y1,x2,y2;
- cin>>x1>>y1>>x2>>y2;
- update_x(1,1,n,x1,x2,y1,y2);
- }
- else{
- int x,y;
- cin>>x>>y;
- query_x(1,1,n,x,y);
- cout<<ans<<endl;
- }
- }
- cout<<endl;
- }
- return 0;
- }