您的位置:首页 > 其它

POJ 2155 Matrix 3468 A Simple Problem with Integers

2017-10-04 20:44 344 查看
简单的挑了几个基础题

Problem 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. <br> <br>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. <br>
 

Output

For each querying output one line, which has an integer representing A[x, y]. <br> <br>There is a blank line between every two continuous test cases. <br>
 

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
题意:类似上一篇中的意思,只不过这个题是二维的,上一个是三维的(暴力了~~),1变0 0变1
看了多个题解,都没有点到,始终不理解,最后终于让我找了一篇好文章,http://blog.csdn.net/zxy_snow/article/details/6264135
这里接他一张图,其实两种方法都是一样的,翻转的是整片区域,他的文章里很明确的都写了,就不在复述了
/*x1++;y1++;x2++;y2++;
                update(x2,y2);
                update(x1-1,y1-1);
                update(x1-1,y2);
                update(x2,y1-1);*/
                update(x1,y1);
                update(x2+1,y2+1);
                update(x1,y2+1);
                update(x2+1,y1);

这样的话,上一个三维的也可以理解了,三维树状数组,
update(x1,y1,z1);
update(x1,y1,z2+1);
update(x1,y2+1,z1);
update(x1,y2+1,z2+1);
update(x2+1,y1,z1);
update(x2+1,y1,z2+1);
update(x2+1,y2+1,z1);
update(x2+1,y2+1,z2+1);



#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int c[1010][1010];
int n;
int lowbit(int x)
{
return x &(-x);
}
void update(int x,int y)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
c[i][j]++;
}
int sum(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+=c[i][j];
return sum;
}
int main()
{
int t,m;
int x1,y1,x2,y2;
char ch[2];
cin>>t;
while(t--)
{
memset(c,0,sizeof(c));
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%s",ch);
if(ch[0]=='C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
/*x1++;y1++;x2++;y2++;
update(x2,y2);
update(x1-1,y1-1);
update(x1-1,y2);
update(x2,y1-1);*/
update(x1,y1);
update(x2+1,y2+1);
update(x1,y2+1);
update(x2+1,y1);
}
else
{
scanf("%d%d",&x1,&y1);
printf("%d\n",sum(x1,y1)%2);
}
}
cout<<endl;
}
return 0;
}

[align=left]Problem Description[/align]You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. 
[align=left]Input[/align]<p>The first line contains two numbers <i>N</i> and <i>Q</i>. 1 ≤ <i>N</i>,<i>Q</i> ≤ 100000.<br>The second line contains <i>N</i> numbers, the initial values of <i>A</i><sub>1</sub>, <i>A</i><sub>2</sub>, ... , <i>A<sub>N</sub></i>. -1000000000 ≤ <i>A<sub>i</sub></i> ≤ 1000000000.<br>Each of the next <i>Q</i> lines represents an operation.<br>"C <i>a</i> <i>b</i> <i>c</i>" means adding <i>c</i> to each of <i>A<sub>a</sub></i>, <i>A<sub>a</sub></i><sub>+1</sub>, ... , <i>A<sub>b</sub></i>. -10000 ≤ <i>c</i> ≤ 10000.<br>"Q <i>a</i> <i>b</i>" means querying the sum of <i>A<sub>a</sub></i>, <i>A<sub>a</sub></i><sub>+1</sub>, ... , <i>A<sub>b</sub></i>.</p> 
[align=left]Output[/align]<p>You need to answer all <i>Q</i> commands in order. One answer in a line.</p> 
[align=left]Sample Input[/align]
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
 
[align=left]Sample Output[/align]
4
55
9
15题意:成段更新 求和
思路:线段树好像更好用一点,套个版
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
long long add[500000];
long long sum[500000];
void pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
add[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void pushdown(int rt,int m)
{
if(add[rt])
{
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=(m-(m>>1))*add[rt];
sum[rt<<1|1]+=(m>>1)*add[rt];
add[rt]=0;
}
}
void Update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
add[rt]+=c;
sum[rt]+=c*(r-l+1);
return;
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
Update(L,R,c,lson);
if(R>m)
Update(L,R,c,rson);
pushup(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
return sum[rt];
pushdown(rt,r-l+1);
int m=(l+r)>>1;
long long ret=0;
if(L<=m)
ret+=query(L,R,lson);
if(R>m)
ret+=query(L,R,rson);
return ret;
}
int main()
{
int n,m;
char ch[2];
while(cin>>n>>m)
{
memset(sum,0,sizeof(sum));
build(1,n,1);
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%s",ch);
if(ch[0]=='Q')
{
scanf("%d%d",&a,&b);
cout<<query(a,b,1,n,1)<<endl;
}
else
{
scanf("%d%d%d",&a,&b,&c);
Update(a,b,c,1,n,1);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: