zoukankan      html  css  js  c++  java
  • POJ2155 Matrix 二维线段树 | 树状数组

      题目链接:http://poj.org/problem?id=2155

      比较典型的二维线段树题目,直接永久更新即可,在询问的时候,要询问每个x区间的子树,复杂度O(log(n)^2)。

      也可以用树状数组搞,推荐看NOI 2009 武森《浅谈信息学竞赛中的“0”和“1”》。

     1 //STATUS:C++_AC_907MS_16144KB
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 //#include<math.h>
     6 #include<iostream>
     7 #include<string>
     8 #include<algorithm>
     9 #include<vector>
    10 #include<queue>
    11 #include<stack>
    12 using namespace std;
    13 #define LL __int64
    14 #define pii pair<int,int>
    15 #define Max(a,b) ((a)>(b)?(a):(b))
    16 #define Min(a,b) ((a)<(b)?(a):(b))
    17 #define mem(a,b) memset(a,b,sizeof(a))
    18 #define lson l,mid,rt<<1
    19 #define rson mid+1,r,rt<<1|1
    20 const int N=1010,INF=0x3f3f3f3f,MOD=100000000;
    21 const double DNF=100000000000;
    22 
    23 bool f[N<<2][N<<2];
    24 int T,n,m,a,b,x1,y1,x2,y2,ans;
    25 
    26 void updatey(int l,int r,int rt,int fa)
    27 {
    28     if(y1<=l && r<=y2){
    29         f[fa][rt]^=1;
    30  //   printf(" %d %d %d\n",l,r,f[fa][rt]);//
    31         return;
    32     }
    33     int mid=(l+r)>>1;
    34     if(y1<=mid)updatey(lson,fa);
    35     if(y2>mid)updatey(rson,fa);
    36 }
    37 
    38 void updatex(int l,int r,int rt)
    39 {
    40     if(x1<=l && r<=x2){
    41  //       printf("%d %d %d\n",l,r,f[rt][0]);//
    42         updatey(1,n,1,rt);
    43         return;
    44     }
    45     int mid=(l+r)>>1;
    46     if(x1<=mid)updatex(lson);
    47     if(x2>mid)updatex(rson);
    48 }
    49 
    50 void queryy(int l,int r,int rt,int fa)
    51 {
    52     ans^=f[fa][rt];
    53  //   printf("  %d\n",f[fa][rt]);
    54     if(l==r)return;
    55     int mid=(l+r)>>1;
    56     if(y1<=mid)queryy(lson,fa);
    57     else queryy(rson,fa);
    58 }
    59 
    60 void queryx(int l,int r,int rt)
    61 {
    62     queryy(1,n,1,rt);
    63     if(l==r)return;
    64     int mid=(l+r)>>1;
    65     if(x1<=mid)queryx(lson);
    66     else queryx(rson);
    67 }
    68 
    69 int main()
    70 {
    71  //   freopen("in.txt","r",stdin);
    72     char c;
    73     scanf("%d",&T);
    74     while(T--)
    75     {
    76         mem(f,0);
    77         scanf("%d%d",&n,&m);
    78         while(m--){
    79             getchar();
    80             scanf("%c",&c);
    81             if(c=='C'){
    82                 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    83                 updatex(1,n,1);
    84             }
    85             else {
    86                 scanf("%d%d",&x1,&y1);
    87                 ans=0;
    88                 queryx(1,n,1);
    89                 printf("%d\n",ans);
    90             }
    91         }
    92         putchar('\n');
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    简明Python3教程 12.问题解决
    简明Python3教程 11.数据结构
    【SPOJ 694】Distinct Substrings
    【codeforces Manthan, Codefest 17 C】Helga Hufflepuff's Cup
    【CF Manthan, Codefest 17 B】Marvolo Gaunt's Ring
    【CF Manthan, Codefest 17 A】Tom Riddle's Diary
    【SPOJ 220】 PHRASES
    【POJ 3261】Milk Patterns
    【POJ 3294】Life Forms
    【POJ 1226】Substrings
  • 原文地址:https://www.cnblogs.com/zhsl/p/2958402.html
Copyright © 2011-2022 走看看