Senior's Fish

Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 242    Accepted Submission(s): 50

Problem Description
Xuejiejie loves to eat fish. One day, she goes to a pond for fishing. The pond can be seen as a two-dimensional surface, and her fishing net can be seen as a rectangle. One of the edges of the rectangle is parallel to the x-axis ,and another is parallel to
y-axis. The fishes can be seen as points. Sometimes the fishes will enter the net, and sometimes they will leave the net. So, Xuejiejie doesn't know the appropriate time to draw the net in when she will get as many fishes as possible. 



Xuejiejie assigns each fish with a number, from 1 to n, n being
the total number. There are 2 types of movements of the fishes:



1 l r d : the fishes index between the interval [l,r] moved towards the x-axis for length d (For example , if a fish's current position is (x,y) ,
after moving , its position will change to (x+d,y).
)



2 l r d : the fishes index between the interval [l,r] moved towards the y-axis for length d (For example , if a fish's current position is (x,y),
after moving , its position will change to (x,y+d).
)



And sometimes Xuejiejie will ask you some questions.
 
Input
In the first line there is an integer T,
indicates the number of test cases.



In each case:



The first line includes an integer n indicating
the total number of fish.



The second line includes 4 integers x1,y1。x2,y2,indicating
the position of the fishing net. (x1,y1) means
the lower-left position, and (x2,y2) means
the top-right position.



The next n lines: each line includes x[i],y[i];
means the fish i's initial position.



The next line includes an integer m.



The next m lines
describe the events you have to deal with.



In each line the first integer is c (1≤c≤3),
which indicates the type of events.



1 l r d : the fish index between the interval [l。r] moved towards the x-axis for length d



2 l r d : the fish index between the interval [l,r] moved towards the y-axis for length d



3 l r : query the number of the fish index between the interval [l,r] which are in the net(including the one in the border)



1≤n,m≤100000, 1≤l≤r≤n. 1≤d≤109, x1≤x2, y1≤y2。
Ensure that any time all involved coordinate values in the range of[−109,109]。
 
Output
In each case:



For each type 3 events, output a integer which means the answer.
 
Sample Input
1
5
1 1 5 5
1 1
2 2
3 3
4 4
5 5
3
3 1 5
1 2 4 2
3 1 5
 
Sample Output
5
4
 
Source
 
Recommend

hujie   |   We have carefully selected several similar problems for you:  5287 

pid=5286" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5286 

pid=5284" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5284 5279 

pid=5278" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5278

题解:用线段树维护每一个点左下角内鱼的数目。由于渔网有四条边,依据限制条件的不同,须要维护四颗线段树,其限制条件分别不能从x or y 轴越过,否则该鱼不參与计数,设ans[0 ~ 3]分别为渔网右上角、左上角、右下角、左下角四个点左下方鱼的数目,则答案为: Ans = ans[0] - ans[1] - ans[2] + ans[3].

代码 I : (第一个写的。为超时版本号, 正在努力改动)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
#define INF 0x7ffffff
#define eps 1e-8
#define maxn 100000 + 10
#define lson L, mid, rt<<1
#define rson mid+1, R, rt<<1|1 int X[4], Y[4], x[2], y[2];
int n, m;
int xx[maxn], yy[maxn]; struct Node
{
int sum, mx, my, addx, addy; ///addx addy 相当于lazy标记
}T[4][maxn<<2]; void Pushup(int k, int rt)
{
int l = rt<<1, r = rt<<1|1;
T[k][rt].sum = T[k][l].sum + T[k][r].sum;
T[k][rt].mx = max(T[k][l].mx, T[k][r].mx);
T[k][rt].my = max(T[k][l].my, T[k][r].my);
} void Pushdown(int k, int rt)
{
int l = rt<<1, r = rt<<1|1;
int t1 = T[k][rt].addx, t2 = T[k][rt].addy; T[k][l].addx += t1, T[k][r].addx += t1;
T[k][l].addy += t2, T[k][r].addy += t2;
T[k][l].mx += t1, T[k][r].mx += t1;
T[k][l].my += t2, T[k][r].my += t2; T[k][rt].addx = T[k][rt].addy = 0;
} void Bulid(int k, int L, int R, int rt) ///递归构造线段树
{
T[k][rt].addx = T[k][rt].addy = 0;
if(L == R)
{
if(xx[L]<=X[k] && yy[L]<=Y[k])
{
T[k][rt].sum = 1;
T[k][rt].mx = xx[L], T[k][rt].my = yy[L];
}
else
{
T[k][rt].sum = 0;
T[k][rt].mx = T[k][rt].my = -INF;
}
return ;
}
int mid = (L+R)>>1;
Bulid(k, lson);
Bulid(k, rson);
Pushup(k, rt);
} void Update(int k, int flag, int l, int r, int d, int L, int R, int rt)
{
if(l<=L && r>=R)
{
if(flag) /// 1 -- x
{
T[k][rt].addx += d, T[k][rt].mx += d;
if(T[k][rt].mx <= X[k])
return ;
}
else /// 0 -- y
{
T[k][rt].addy += d, T[k][rt].my += d;
if(T[k][rt].my <= Y[k])
return ;
}
if(L == R)
{
T[k][rt].sum = 0;
T[k][rt].mx = T[k][rt].my = -INF;
return ;
}
}
int mid = (L+R)>>1;
if(T[k][rt].addx || T[k][rt].addy) Pushdown(k, rt);
if(r <= mid) Update(k, flag, l, r, d, lson);
else if(l > mid) Update(k, flag, l, r, d, rson);
else
{
Update(k, flag, l, mid, d, lson);
Update(k, flag, mid+1, r, d, rson);
}
Pushup(k, rt);
} int Query(int k, int l, int r, int L, int R, int rt)
{
if(l<=L && r>=R)
return T[k][rt].sum;
int mid = (L+R)>>1;
if(T[k][rt].addx || T[k][rt].addy) Pushdown(k, rt);
if(r <= mid) Query(k, l, r, lson);
else if(l > mid) Query(k, l, r, rson);
else
return Query(k, l, mid, lson) + Query(k, mid+1, r, rson);
} int ans[4]; int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);///
scanf("%d%d%d%d", &x[0], &y[0], &x[1], &y[1]);
X[0] = x[1], Y[0] = y[1];
X[1] = x[0]-1, Y[1] = y[1];
X[2] = x[1], Y[2] = y[0]-1;
X[3] = x[0]-1, Y[3] = y[0]-1;
for(int i=1; i<=n; i++)
scanf("%d%d", &xx[i], &yy[i]);/// for(int i=0; i<4; i++)
Bulid(i, 1, n, 1); scanf("%d", &m);///
for(int i=0; i<m; i++)
{
int t, l, r, d;
scanf("%d%d%d", &t, &l, &r);
if(t < 3) scanf("%d", &d);
if(t == 1)
{
for(int i=0; i<4; i++)
Update(i, 1, l, r, d, 1, n, 1);
}
else if(t == 2)
{
for(int i=0; i<4; i++)
Update(i, 0, l, r, d, 1, n, 1);
}
else
{
for(int i=0; i<4; i++)
ans[i] = Query(i, l, r, 1, n, 1);
printf("%d\n", ans[0] - ans[1] - ans[2] + ans[3]);
}
}
}
return 0;
} /* 2 5
1 1 5 5
1 1
2 2
3 3
4 4
5 5
3
3 1 5
1 2 4 2
3 1 5 3
1 1 5 5
1 5
2 5
3 5
4
2 2 5 1
3 1 1
3 1 2
3 1 5 */

Bestcoders的更多相关文章

随机推荐

  1. 给Input type='date'赋值

    (如有错敬请指点,以下是我工作中遇到并且解决的问题) 需要使用AngularJS动态给<input type="date" />赋值. 我使用的是ng-bind=&qu ...

  2. Mac下安装npm,http-server,安装虚拟服务器

    http-server是一个简单的,不需要配置的命令行下使用的http服务器.类似的还有Xampp等. 针对前端开发工程的代码不需要编译的特点,使用这种简单的服务器十分的便利. 1.安装这个首先要安装 ...

  3. HTML5面向对象的游戏开发简单实例总结

    在阅读一本HTML5游戏开发相关书籍时发现一个很好的例子,通过这个例子可以对面向对象的开发进行更深入的理解.这个对象要实现的是:将一个CSS sprite中的图像绘制到canvas中.首先创建一个Sp ...

  4. (3)C#工具箱-容器

    容器特点:把控件放到容器里,移动容器控件也会跟着移动. 1.flowLayoutPanel(流布局控件) 放入控件后,会自动垂直或水平排列 拉长布局,控件自动跑到一行 2.GroupBox(组合框) ...

  5. HDU 17新生赛 正品的概率【数论-概率论】

    正品的概率 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. Tomcat服务器与HTTP协议

    Tomcat服务器与HTTP协议 一.  Tomcat服务器 1.tomcat服务器 1.web :网页,它代表的是网络上的资源.(java技术开发动态的web资源,即动态web页面,在Java中,动 ...

  7. #424 Div2 C

    #424 Div2 C 题意 给出 k 个分数,初始分数未知,按顺序把这 k 个分数加到初始分数上,已知 n 个加入分数后的结果(无序),问初始分数有多少种可能. 分析 也就是说这 n 个结果,它们之 ...

  8. 网络监控工具ntopng

    网络监控工具ntopng   ntopng是Kali提供的一个网络监控软件,用于显示当前网络的使用情况.它能列出当前使用网络的主机,并且显示每台主机发送和接受的数据包.同时,它提供强大的数据处理功能, ...

  9. Ubuntu 16.04通过网络配置工具NetworkManager设置IP、网关、DNS和查看IP、网关、DNS

    说明: 1.NetworkManager工具是Ubuntu桌面版的GUI设置工具. 2.这个工具推荐直接在GUI上操作,不建议用命令行进行管理,比如Wifi这些配置等. 3.当然,这个工具能带有命令行 ...

  10. MVC中的Controller中返回一个JsonResult在弹出一个下载框?

    public JsonResult ReturnTest() { return Json(new {myMsg ="hello world"}, "text/html; ...