zoukankan      html  css  js  c++  java
  • poj 2155 Matrix

    题意:有一个n*n(n<=1000) 的矩阵,开始时全为零,现在有两种操作

    (1)"C x1 y1 x2 y2“表示将以(x1,y1)为左上顶点,以(x2,y2)为右下顶点的长方形区域内的值全部取反。

    (2)”Q x y“表示点(x,y)的值。

    具体思路参考国家集训队2009论文集浅谈信息学竞赛中的0和1“;

    我自己用的是二维线段树。

    View Code
      1 #include <cstdio>
      2 #include <algorithm>
      3 using namespace std;
      4 #define maxn 1002
      5 #define lson l,m,rt<<1
      6 #define rson m+1,r,rt<<1|1
      7 int n;
      8 struct node
      9 {
     10     int subsetree[maxn*3];
     11 }setree[maxn*3];
     12 void subbuild(int num,int l,int r,int rt)
     13 {
     14     setree[num].subsetree[rt]=0;
     15     if(l==r)
     16     return;
     17     int m=(l+r)>>1;
     18     subbuild(num,lson);
     19     subbuild(num,rson);
     20 }
     21 void build(int l,int r,int rt)
     22 {
     23     subbuild(rt,1,n,1);
     24     if(l==r)
     25     return;
     26     int m=(l+r)>>1;
     27     build(lson);
     28     build(rson);
     29 }
     30 void subupdate(int num,int l,int r,int rt,int x1)
     31 {
     32     setree[num].subsetree[rt]++;
     33     if(l==r)
     34     return;
     35     int m=(l+r)>>1;
     36     if(x1<=m)
     37     subupdate(num,lson,x1);
     38     else if(x1>m)
     39     subupdate(num,rson,x1);
     40 }
     41 void update(int l,int r,int rt,int x1,int y1)
     42 {
     43     subupdate(rt,1,n,1,y1);
     44     if(l==r)
     45         return;
     46     int m=(l+r)>>1;
     47     if(x1<=m)
     48     update(lson,x1,y1);
     49     else if(x1>m)
     50     update(rson,x1,y1);
     51 }
     52 int subquery(int num,int l,int r,int rt,int l1,int r1)
     53 {
     54     if(l1<=l&&r<=r1)
     55     return setree[num].subsetree[rt];
     56     int m=(l+r)>>1;
     57     int ans=0;
     58     if(l1<=m)
     59     ans+=subquery(num,lson,l1,r1);
     60     if(r1>m)
     61     ans+=subquery(num,rson,l1,r1);
     62     return ans;
     63     
     64 }
     65 int query(int l,int r,int rt,int l1,int r1,int l2,int r2)
     66 {
     67     if(l1<=l&&r<=r1)
     68     return subquery(rt,1,n,1,l2,r2);
     69     int m=(l+r)>>1;
     70     int ans=0;
     71     if(l1<=m)
     72     ans+=query(lson,l1,r1,l2,r2);
     73     if(r1>m)
     74     ans+=query(rson,l1,r1,l2,r2);
     75     return ans;
     76 }
     77 int main()
     78 {
     79     int t;
     80     scanf("%d",&t);
     81     while(t--){
     82         int m;
     83         scanf("%d%d",&n,&m);
     84         n++;
     85         build(1,n,1);
     86         while(m--){
     87             char s[5];
     88             int x1,y1,x2,y2;
     89             scanf("%s",s);
     90             if(s[0]=='C'){
     91             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
     92             update(1,n,1,x1,y1);
     93             update(1,n,1,x2+1,y1);
     94             update(1,n,1,x1,y2+1);
     95             update(1,n,1,x2+1,y2+1);
     96             }
     97             else if(s[0]=='Q'){
     98                 scanf("%d%d",&x1,&y1);
     99                 int ans=query(1,n,1,1,x1,1,y1);
    100                 printf("%d\n",ans%2);
    101             }
    102         }
    103         putchar('\n');
    104     }
    105     return 0;
    106 }
  • 相关阅读:
    UART中RTS、CTS
    Verdi:内存不足
    SV学习之interface
    perl学习之:@_ $_
    perl学习之:package and module
    代码变成可执行程序期间,编译器做了那些事?
    perl学习之:use & require
    perl学习之:use and require
    8位二进制补码表示整数的最小值是什么,最大值是什么
    深入理解计算机系统
  • 原文地址:https://www.cnblogs.com/kim888168/p/2867794.html
Copyright © 2011-2022 走看看