• 1452: [JSOI2009]Count


    1452: [JSOI2009]Count

    Time Limit: 10 Sec  Memory Limit: 64 MB
    Submit: 2083  Solved: 1228
    [Submit][Status][Discuss]

    Description

    Input

    Output

    Sample Input



    Sample Output

    1
    2

    HINT

    思路:二维树状数组;
    将数组开三维的然后,每一维维护一个值,然后直接二维树状数组维护即可;
     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<stdlib.h>
     5 #include<queue>
     6 #include<string.h>
     7 using namespace std;
     8 int bit[105][305][305];
     9 int ans[305][305];
    10 int lowbit(int x)
    11 {
    12     return x&(-x);
    13 }
    14 void add(int x,int y,int val,int op)
    15 {
    16     int i,j;
    17     for(i = x; i < 305; i += lowbit(i))
    18     {
    19         for(j = y; j < 305; j += lowbit(j))
    20         {
    21             if(op)
    22                 bit[val][i][j]++;
    23             else bit[val][i][j]--;
    24         }
    25     }
    26 }
    27 int ask(int x,int y,int val)
    28 {
    29     int i,j;
    30     int sum = 0;
    31     for(i = x; i > 0; i -= lowbit(i))
    32     {
    33         for(j = y; j > 0; j -= lowbit(j))
    34         {
    35             sum += bit[val][i][j];
    36         }
    37     }
    38     return sum;
    39 }
    40 int main(void)
    41 {
    42     int n,m;
    43     scanf("%d %d",&n,&m);
    44     int i,j;
    45     for(i = 1; i <= n; i++)
    46     {
    47         for(j = 1; j <= m; j++)
    48         {
    49             int x;
    50             scanf("%d",&x);
    51             ans[i][j] = x;
    52             add(i,j,x,1);
    53         }
    54     }
    55     int T;
    56     scanf("%d",&T);
    57     while(T--)
    58     {
    59         int val;
    60         scanf("%d",&val);
    61         if(val == 1)
    62         {
    63             int xx,yy,c;
    64             scanf("%d %d %d",&xx,&yy,&c);
    65             add(xx,yy,ans[xx][yy],0);
    66             ans[xx][yy] = c;
    67             add(xx,yy,ans[xx][yy],1);
    68         }
    69         else
    70         {
    71             int x,y,x1,y1,c;
    72             scanf("%d %d %d %d %d",&x,&x1,&y,&y1,&c);
    73             int sum = ask(x1,y1,c);
    74             sum -= ask(x-1,y1,c);
    75             sum -= ask(x1,y-1,c);
    76             sum += ask(x-1,y-1,c);
    77             printf("%d
    ",sum);
    78         }
    79     }
    80     return 0;
    81 }
    油!油!you@
  • 相关阅读:
    二叉排序树和平衡二叉树
    博客首页特效整理2
    博客首页特效整理
    19-20下学期思维导图
    19-20下学期第一次作业问卷调查回答
    c++实现五子棋游戏
    c++实现扫雷游戏
    c++实现2048游戏
    c++实现推箱子游戏
    C++实现贪吃蛇小游戏
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5894367.html
Copyright © 2020-2023  润新知