uva 11992 线段树对矩阵进行更新查询

来源:互联网 发布:中国博士与英国知乎 编辑:程序博客网 时间:2024/05/10 17:54

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3143


把矩阵变成一行,然后计算位置,lrj给了线段树数组做法 但是我做的线段树空间过大,直接爆掉,所以换方法了

主要还是测试自己的线段树区间更新的模板

各种RE+WA之后AC,,,,,


做的时候出现的几个错误:

1、行和列弄错

2、build初始化的时候,mmin mmax 都初始化为0才对



[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<algorithm>  
  4. using namespace std;  
  5.   
  6. #define lson(i) l , mid , (i)*2  
  7. #define rson(i) mid + 1 , r , ((i)*2 +1)  
  8. #define ll rt*2  
  9. #define rr (rt*2+1)  
  10.   
  11. const int INFMIN = 0xffffffff;  
  12. const int INFMAX = 1000000009;//0x80000000;  
  13. const int MAXN = 30001000;  
  14. struct Node{  
  15.     int l,r;  
  16.     int mmax,mmin,sum,add,s;/*s去标记是不是被set*/  
  17. };  
  18. Node nodes[MAXN];  
  19.   
  20.     int mmax,mmin,sum;  
  21.     void PushUp(int rt)  
  22.     {  
  23.         nodes[rt].mmax = max(nodes[ll].mmax,nodes[rr].mmax);  
  24.         nodes[rt].mmin = min(nodes[ll].mmin,nodes[rr].mmin);  
  25.         nodes[rt].sum = nodes[ll].sum + nodes[rr].sum;  
  26.     }  
  27.     void PushDown(int rt)  
  28.     {  
  29.         //if(nodes[rt].add && flag == 1)  
  30.         if(nodes[rt].add)  
  31.         {  
  32.             nodes[ll].add += nodes[rt].add;  
  33.             nodes[ll].mmin += nodes[rt].add;  
  34.             nodes[ll].mmax += nodes[rt].add;  
  35.             nodes[rr].add += nodes[rt].add;  
  36.             nodes[rr].mmin += nodes[rt].add;  
  37.             nodes[rr].mmax += nodes[rt].add;  
  38.   
  39.             nodes[ll].sum += nodes[rt].add*(nodes[ll].r-nodes[ll].l+1);  
  40.             nodes[rr].sum += nodes[rt].add*(nodes[rr].r-nodes[rr].l+1);  
  41.             nodes[rt].add = 0;  
  42.         }  
  43.         //if(nodes[rt].s && flag == 2)  
  44.         if(nodes[rt].s)  
  45.         {  
  46.             nodes[ll].s = nodes[rr].s=nodes[rt].s;  
  47.             nodes[ll].mmin = nodes[ll].mmax = nodes[rr].mmax = nodes[rr].mmin = nodes[rt].mmax;  
  48.             nodes[ll].sum = nodes[rt].mmin*(nodes[ll].r-nodes[ll].l+1);  
  49.             nodes[rr].sum = nodes[rt].mmin*(nodes[rr].r-nodes[rr].l+1);  
  50.             nodes[ll].add = nodes[rr].add = 0;//////////////  
  51.             nodes[rt].s=0;  
  52.         }  
  53.     }  
  54.     void Build(int l,int r,int rt)  
  55.     {  
  56.         nodes[rt].l=l;  
  57.         nodes[rt].r=r;  
  58.         nodes[rt].add =0;  
  59.         nodes[rt].s=0;  
  60.         nodes[rt].sum =0;  
  61.         nodes[rt].mmin=0;  
  62.         nodes[rt].mmax=0;  
  63.         if(l == r)  
  64.         {  
  65.             //nodes[rt].mmin = nodes[rt].mmax = nodes[rt].sum =a[l];  
  66.             nodes[rt].mmin = nodes[rt].mmax = nodes[rt].sum =0;////////////////  
  67.             return ;  
  68.         }  
  69.         int mid = (nodes[rt].l+nodes[rt].r)/2;  
  70.         Build(lson(rt));  
  71.         Build(rson(rt));  
  72.         //PushUp(rt);  
  73.     }  
  74.   
  75.     void Update(int l,int r,int add,int rt,int flag)  
  76.     {  
  77. /////////////////////////////////////////////////////////////////  
  78. //printf("rt=%d l=%d r=%d add=%d flag =%d nodes[rt].l=%d nodes[rt].r=%d\n",rt,l,r,add,flag,nodes[rt].l,nodes[rt].r);  
  79.         if(l<=nodes[rt].l && nodes[rt].r<=r)  
  80.         {  
  81.             if(flag == 1)/*increase*/  
  82.             {  
  83.                 nodes[rt].mmax += add;  
  84.                 nodes[rt].mmin += add;  
  85.                 nodes[rt].add += add;  
  86.                 nodes[rt].sum += add*(nodes[rt].r-nodes[rt].l+1);  
  87.   
  88.             }  
  89.             else  
  90.             {  
  91.                 nodes[rt].mmax = add;  
  92.                 nodes[rt].mmin = add;  
  93.                 nodes[rt].add=0;  
  94.                 nodes[rt].s=1;  
  95.                 nodes[rt].sum = add*(nodes[rt].r-nodes[rt].l+1);  
  96.             }  
  97.             return;  
  98.         }  
  99.         PushDown(rt);  
  100.         int mid = (nodes[rt].l+nodes[rt].r)/2;  
  101.         if(l<=mid)Update(l,r,add,ll,flag);  
  102.         if(r>mid)Update(l,r,add,rr,flag);  
  103.         PushUp(rt);  
  104.     }  
  105.     void Query(int l,int r,int rt)/*1表示mmin 2--mmax 3-sum*/  
  106.     {  
  107.     /////////////////////////////////////////////////////////////////  
  108. //printf("rt=%d l=%d r=%d  nodes[rt].l=%d nodes[rt].r=%d\n",rt,l,r,nodes[rt].l,nodes[rt].r);  
  109.  ///////////////////////////////////////////////////////////////////////////  
  110.         if(l<=nodes[rt].l && nodes[rt].r<=r)  
  111.         {  
  112.             mmin = min(mmin,nodes[rt].mmin);  
  113.             mmax = max(mmax,nodes[rt].mmax);  
  114.             sum += nodes[rt].sum;  
  115.             return ;  
  116.         }  
  117.         PushDown(rt);  
  118.         int mid = (nodes[rt].l+nodes[rt].r)/2;  
  119.         if(l<=mid)Query(l,r,ll);  
  120.         if(r>mid)Query(l,r,rr);  
  121.         PushUp(rt);  
  122.     }  
  123.     void clr()/*每次查询之前使用*/  
  124.     {  
  125.         sum =0;  
  126.         mmin=INFMAX;  
  127.         mmax=INFMIN;  
  128.     }  
  129.   
  130. int main()  
  131. {  
  132.     //freopen("uva11992.txt","r",stdin);  
  133.     int r,c,m,v,flag,x1,y1,x2,y2;  
  134.   
  135.     while(scanf("%d%d%d",&r,&c,&m)!=EOF)  
  136.     {  
  137.         Build(1,r*c,1);  
  138.         while(m--)  
  139.         {  
  140.             scanf("%d%d%d%d%d",&flag,&x1,&y1,&x2,&y2);  
  141.             if(flag<3)  
  142.             {  
  143.                 scanf("%d",&v);  
  144.                 for(int i=x1;i<=x2;i++)/*此处注意*/  
  145.                 {  
  146. ///////////////////////////////////////////////////////////////  
  147. //printf("i=%d\n",i);  
  148.   
  149.                     Update(y1+(i-1)*c,y2+(i-1)*c,v,1,flag);  
  150.                 }  
  151.             }  
  152.             else  
  153.             {  
  154.                 int anssum=0,ansmin=INFMAX,ansmax=INFMIN;  
  155.                 for(int i=x1;i<=x2;i++)  
  156.                 {  
  157.                     clr();  
  158.                     Query(y1+(i-1)*c,y2+(i-1)*c,1);  
  159.                     ////////////////////////  
  160.                    // printf("**min=%d\n",mmin);  
  161.                     /////////////////  
  162.                     anssum+=sum;  
  163.                     ansmax=max(ansmax,mmax);  
  164.                     ansmin=min(ansmin,mmin);  
  165.                 }  
  166.                 printf("%d %d %d\n",anssum,ansmin,ansmax);  
  167.             }  
  168.         }  
  169.     }  
  170.   
  171.     return 0;  
  172. }  
0 0
原创粉丝点击