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. 浅谈call和apply的联系&区别&应用匹配

    call和apply的联系和区别在之前查过资料了解了一番,昨天晚上睡不着觉忽然想到了这个问题,发现对于他们的联系和区别理解的还是很模糊.看来还是欠缺整理,知识没有连贯起来.反思一二,详情如下: 1作用 ...

  2. Quartz Scheduler(2.2.1) - Working with SchedulerListeners

    SchedulerListeners SchedulerListeners are much like TriggerListeners and JobListeners, except they r ...

  3. Linux 命令 - curl: transfer a URL

    命令格式 curl [options] [URL...] 命令参数 -0, --http1.0 强制使用 HTTP/1.0 发送请求 -A, --user-agent 指定用户代理 -b/--cook ...

  4. C#数组比较取值

    string strs = string.Empty;            string[] strArray1 = { "a", "b", "c& ...

  5. 安装安卓模拟器和unity3d插件EZGUI

    一.安装安卓模拟器 1.下载安卓模拟器http://www.pc6.com/softview/SoftView_64923.html: 2.安装安卓模拟器. 3.下载安卓apk,然后右键用BlueSt ...

  6. 【Cocos2d入门教程一】Cocos2d-x环境搭建

    在进行Cocos2d游戏开发前 我们先来配置一下环境,我们先来准备一下工具,我们所需要的工具分别为: 1.Cocos2d引擎 2.JDK 3.SDK 4.NDK 5.ANT 6.ADT 1.下载Coc ...

  7. sql常识-SQL 通配符

    在搜索数据库中的数据时,您可以使用 SQL 通配符. SQL 通配符 在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符. SQL 通配符必须与 LIKE 运算符一起使用. 在 SQL 中, ...

  8. jQuery API 3.1.0 速查表-打印版

    jQuery API 3.1.0 速查表-打印图,(API来自:http://jquery.cuishifeng.cn/index.html)

  9. ListView onItemClick(AdapterView<?> parent, View view, int position, long id)参数详解

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { parent.getA ...

  10. asterisk 语音文件转换

    Centos wav to sln sox foo-in.wav -t raw -r 8000 -s -2 -c 1 foo-out.sln 当前目录下所有语音wav文件 转换成sln for a i ...