Description
维护一个W*W的矩阵,每次操作可以增加某格子的权值,或询问某子矩阵的总权值。 修改操作数M<=160000,询问数Q<=10000,W<=2000000。
Input
Output
Sample Input
0 4
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3
Sample Output
3
5
HINT

Input file Output file Meaning
0 4 Table size is , filled with zeroes.

1 2 3 3 Add 3 customers at (2, 3).
2 1 1 3 3 Query sum of rectangle , .

3 Answer.
1 2 2 2 Add 2 customers at (2, 2).
2 2 2 3 4 Query sum of rectangle , .

5 Answer
3 Exit your program.

题目大意:给你一个矩阵,每次可以增加一个点的权值,或者询问一个子矩阵的和

cdq分治

先做一个预处理,把每一个询问(x1,y1)(x2,y2)变成(1,y1)(x,y2)的形式,其实只要拆成两个相减的形式就行了

把询问(x1,y1)(x2,y2)改成(1,y1)(x1-1,y2)和(1,y1)(x2,y2)两个询问,回答的时候只要做差就行了

考虑什么操作才会对询问做出贡献,当这个操作time比询问早,操作的x<=询问的x才会有贡献

所以我们先按time排序,其实都不用排了

保证只有前面的才会影响后面的,然后对这些操作和询问进行(按x为关键字)归并排序

每次合并的时候计算出左边区间对右边区间造成的影响

所以slove函数就是这样的

 procedure slove(l,r:longint);
var
mid:longint;
begin
if l=r then exit;
slove(l,mid);
slove(mid+,r);
merge(l,mid,mid+,r);
end;

基本上就是这样了

 const
maxn=;
maxq=;
maxw=;
type
node=record
q:boolean;
x,l,r,k:longint;
ans:int64;
end; var
add,n,numq,m:longint;
a,b:array[..maxn*]of node;
ans:array[..maxq]of int64; procedure init;
var
k,x1,y1,x2,y2:longint;
begin
read(add,n);
while true do
begin
read(k);
if k= then exit;
if k= then
begin
inc(m);
with a[m] do
read(x,l,r);
end
else
begin
read(x1,y1,x2,y2);
inc(m);inc(numq);
with a[m] do
begin
k:=numq;
q:=true;
x:=x1-;
l:=y1;r:=y2;
end;
inc(m);inc(numq);
with a[m] do
begin
k:=numq;
q:=true;
x:=x2;
l:=y1;r:=y2;
end;
end;
end;
end; var
time:longint;
vis:array[..maxw]of longint;
c:array[..maxw]of int64; function lowbit(x:longint):longint;
begin
exit(x and -x);
end; procedure insert(x:longint;y:int64);
begin
while x<=n do
begin
if vis[x]<>time then
begin
vis[x]:=time;
c[x]:=;
end;
inc(c[x],y);
inc(x,lowbit(x));
end;
end; function sum(x:longint):int64;
begin
sum:=;
while x> do
begin
if vis[x]<>time then
begin
vis[x]:=time;
c[x]:=;
end;
inc(sum,c[x]);
dec(x,lowbit(x));
end;
end; procedure slove(l,r:longint);
var
mid,i,j,k,x:longint;
begin
if l=r then exit;
mid:=(l+r)>>;
slove(l,mid);
slove(mid+,r);
x:=l;
i:=l;
j:=mid+;
inc(time);
while (i<=mid) and (j<=r) do
begin
if a[i].x<=a[j].x then k:=i
else k:=j;
if (k<=mid) and (a[k].q=false) then insert(a[k].l,a[k].r);
if (k>mid) and (a[k].q) then inc(a[k].ans,sum(a[k].r)-sum(a[k].l-));
if k=i then inc(i)
else inc(j);
b[x]:=a[k];
inc(x);
end;
while i<=mid do
begin
b[x]:=a[i];
inc(i);
inc(x);
end;
while j<=r do
begin
if a[j].q then
inc(a[j].ans,sum(a[j].r)-sum(a[j].l-));
b[x]:=a[j];
inc(j);
inc(x);
end;
for i:=l to r do
a[i]:=b[i];
end; procedure work;
var
i:longint;
begin
slove(,m);
for i:= to m do
if a[i].q then
ans[a[i].k]:=a[i].ans+add*a[i].x*(a[i].r-a[i].l+);
for i:= to numq>> do
writeln(ans[i<<]-ans[i<<-]);
end; begin
init;
work;
end.

ACcode

1176: [Balkan2007]Mokia - BZOJ的更多相关文章

  1. BZOJ 1176: [Balkan2007]Mokia

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 2012  Solved: 896[Submit][St ...

  2. BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )

    考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...

  3. BZOJ 1176[Balkan2007]Mokia(CDQ分治)

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 3381  Solved: 1520[Submit][S ...

  4. 1176: [Balkan2007]Mokia

    1176: [Balkan2007]Mokia 链接 分析 三维偏序问题,CDQ分治论文题. 代码 #include<bits/stdc++.h> using namespace std; ...

  5. 【BZOJ】1176: [Balkan2007]Mokia(cdq分治)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 在写这题的时候思维非常逗啊........2333................... 最后 ...

  6. BZOJ 1176 [Balkan2007]Mokia ——CDQ分治

    [题目分析] 同BZOJ2683,只需要提前处理s对结果的影响即可. CDQ的思路还是很清晰的. 排序解决一维, 分治时间, 树状数组解决一维. 复杂度是两个log [代码] #include < ...

  7. BZOJ 1176: [Balkan2007]Mokia [CDQ分治]

    题意: 有一个n * n的棋盘,每个格子内有一个数,初始的时候全部为0.现在要求维护两种操作: 1)Add:将格子(x, y)内的数加上A. 2)Query:询问矩阵(x0, y0, x1, y1)内 ...

  8. bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要 ...

  9. 【BZOJ】1176: [Balkan2007]Mokia

    [题意]n*n的矩阵,初始值为0(题面有误),m次操作,增加一个格子的权值,或查询子矩阵和.n<=2*10^6.(m应该较题面所述偏大). [算法]CDQ分治(算法知识见数据结构) [题解]三维 ...

随机推荐

  1. html5 js os build

    1.NodeJS6.6 install   https://nodejs.org/en/ c:\nodejs>npm install -g grunt-cliC:\Users\police\Ap ...

  2. Nginx - Additional Modules, Content and Encoding

    The following set of modules provides functionalities having an effect on the contents served to the ...

  3. 自己封装的SqlHelper

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  4. VS2012报表(RDLC)系列应用之单据批量打印

    一.前言 最近做的项目需要单据批量打印的功能,优先想到用RDLC来实现.经过Visual Studio几个版本的发展后,RDLC愈发成熟,操作方式也变得简洁,相比vs2005的版本,有质的提升,不过仍 ...

  5. 第八篇、微信小程序-progress组件

    主要属性: 效果图: ml: <View > <!--百分比是30,并在进度条右侧显示百分比--> <Text class="text-style"& ...

  6. Swift常量和变量以及命名规范

    我们在上一章中介绍了如何使用Swift编写一个HelloWorld小程序,其中就用到了变量.常量和变量是构成表达式的重要组成部分.常量在声明和初始化变量时,在标识符的前面加上关键字let,就可以把该变 ...

  7. using System.Reflection;

    基础代码: public interface IDBHelper { void Query(); } public class DBHelper : IDBHelper { public int Id ...

  8. [zz]安装PostgreSQL数据库(Linux篇)

    0.编译环境 Linux: CentOS 5.5 gcc: 4.1.2 1. 安装PostgreSQL 1) 解压postgresql-9.1.7.tar.bz2 #tar jxvf postgres ...

  9. Codevs 2296 仪仗队 2008年省队选拔赛山东

    2296 仪仗队 2008年省队选拔赛山东 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 题目描述 Description 作为体育委员,C君负责这次运动 ...

  10. From MSI to WiX, Part 8 - Major Upgrade, by Alex Shevchuk

    Following content is reprinted from here, please go to the original website for more information. Au ...