zoukankan      html  css  js  c++  java
  • poj_1195Mobile phones&&poj_2155Matrix

    Mobile phones
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 12399   Accepted: 5714

    Description

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 

    Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

    Input

    The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 

    The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 

    Table size: 1 * 1 <= S * S <= 1024 * 1024 
    Cell value V at any time: 0 <= V <= 32767 
    Update amount: -32768 <= A <= 32767 
    No of instructions in input: 3 <= U <= 60002 
    Maximum number of phones in the whole table: M= 2^30 

    Output

    Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

    Sample Input

    0 4
    1 1 2 3
    2 0 0 2 2 
    1 1 1 2
    1 1 2 -1
    2 1 1 2 3 
    3
    

    Sample Output

    3
    4
    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    const int MAXN = 1030;
    using namespace std;
    
    int tree[MAXN][MAXN];
    int n, m;
    int LowBit(int x)
    {
    	return x & (-x);  
    }
    
    void Update(int x, int y, int val)
    {
    	for(int i = x; i <= m; i += LowBit(i))
    	{
    		for(int j = y; j <= m; j += LowBit(j))
    		{
    			tree[i][j] += val;
    		}
    	}
    }
    
    int GetSum(int x,int y)
    {
    	int sum = 0;
    	for(int i = x; i > 0; i -= LowBit(i))
    	{
    		for(int j = y; j > 0; j -= LowBit(j))
    		{
    			sum += tree[i][j];
    		}
    	}
    	return sum;
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int x1, x2, y1, y2, x, y, z;
    	while(scanf("%d",&n) != EOF)
    	{
    		if(n == 3)
    		{
    			break;
    		}
    		else if(n == 0)
    		{
    			scanf("%d", &m);
    			for(int i = 1;i <= m; i++)
    			{
    				for(int j = 1; j <= m; j++)
    				{
    					tree[i][j] = 0;
    				}
    			}
    		}
    		else if(n == 1)
    		{
    			scanf("%d %d %d", &x, &y, &z);
    			Update(x + 1, y + 1, z);
    		}
    		else if(n == 2)
    		{
    			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    			printf("%d\n", GetSum(x2+1,y2+1) - GetSum(x2+1,y1) - GetSum(x1,y2+1) + GetSum(x1,y1));
    		}
    	}
    	return 0;
    }
    
    Matrix
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 14344   Accepted: 5415

    Description

    Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N). 

    We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using "not" operation (if it is a '0' then change it into '1' otherwise change it into '0'). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions. 

    1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2). 
    2. Q x y (1 <= x, y <= n) querys A[x, y]. 

    Input

    The first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case. 

    The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2 y2", which has been described above. 

    Output

    For each querying output one line, which has an integer representing A[x, y]. 

    There is a blank line between every two continuous test cases. 

    Sample Input

    1
    2 10
    C 2 1 2 2
    Q 2 2
    C 2 1 2 1
    Q 1 1
    C 1 1 2 1
    C 1 2 1 2
    C 1 1 2 2
    Q 1 1
    C 1 1 2 1
    Q 2 1
    

    Sample Output

    1
    0
    0
    1

    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 1030;
    
    int tree[MAXN][MAXN];
    int n, m;
    int LowBit(int x)
    {
    	return x & (-x);  
    }
    
    void Update(int x, int y, int val)
    {
    	for(int i = x; i <= m; i += LowBit(i))
    	{
    		for(int j = y; j <= m; j += LowBit(j))
    		{
    			tree[i][j] += val;
    		}
    	}
    }
    
    int GetSum(int x,int y)
    {
    	int sum = 0;
    	for(int i = x; i > 0; i -= LowBit(i))
    	{
    		for(int j = y; j > 0; j -= LowBit(j))
    		{
    			sum += tree[i][j];
    		}
    	}
    	return sum;
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int x1, x2, y1, y2;
    	int t;
    	char ch;
    	scanf("%d", &t);
    	while (t--)
    	{
    		memset(tree, 0, sizeof(tree));
    		scanf("%d %d", &m, &n);
    		while (n--)
    		{
    			getchar();
    			scanf("%c", &ch);
    			if(ch == 'C')
    			{
    				scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    				x1++; y1++; x2++; y2++;
    				Update(x2, y2, 1);  
    				Update(x1-1, y1-1, 1);  
    				Update(x1-1, y2, 1);  
    				Update(x2, y1-1, 1); 
    			}
    			else
    			{
    				scanf("%d %d", &x1, &y1);
    				printf("%d\n", GetSum(x1, y1) % 2);
    			}
    		}
    		printf("\n");
    	}
    	return 0;
    }
    
    另一种方法:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int Max = 1005;
     
    int n, m;
    bool ar[Max][Max];
     
    int lowbit(int x){
        return x & (-x);
    }
     
    void updata(int i, int j){
        int tmpj;
        while(i > 0){
            tmpj = j;
            while(tmpj > 0){
                ar[i][tmpj] ^= 1;
                tmpj -= lowbit(tmpj);
            }
            i -= lowbit(i);
        }
    }
     
    int query(int i, int j){
        int tmpj, ans = 0;
        while(i <= n){
            tmpj = j;
            while(tmpj <= n){
                ans ^= ar[i][tmpj];
                tmpj += lowbit(tmpj);
            }
            i += lowbit(i);
        }
        return ans;
    }
     
    int main(){
        int t, x, xx, y, yy;
        scanf("%d", &t);
        while(t --){
            memset(ar, 0, sizeof(ar));
            scanf("%d%d", &n, &m);
            while(m --){
                char ord;
                cin >> ord;
                if(ord == 'C'){
                    scanf("%d%d%d%d", &x, &y, &xx, &yy);
                    updata(xx, yy);
                    updata(x-1, yy);
                    updata(xx, y-1);
                    updata(x-1, y-1);
                }else{
                    scanf("%d%d", &x, &y);
                    printf("%d\n", query(x, y));
                }
            }
            printf("\n");
        }
        return 0;
    }


    
    




  • 相关阅读:
    Python画图代码
    关于meshgrid和numpy.c_以及numpy.r_
    Reshape以及向量机分类学习和等高线绘制代码
    Python中的数组和list
    关于透视图和等高线
    Iris花逻辑回归与实现
    Crowd安装和破解
    Confluence搭建
    基于搜狗微信搜索获取公众号文章的阅读量及点赞量
    PHP中使用cURL实现Get和Post请求的方法
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834985.html
Copyright © 2011-2022 走看看