题目描述

She says that any Pavarotti among the nightingales will serenade his mate while she sits on her eggs. She says that any excited database can answer the queries efficiently.
You are given the two dimensional database as a matrix A with n rows and n columns. In the beginning, A[i][j] = 0 for all 1 ≤ i, j ≤ n. Then q operations or queries will be given in turn.
You should maintain the database for two type of operations:
• 1 L R: for each element A[i][j] which satisfy L ≤ i + j ≤ R, increase the value to A[i][j] + 1, where 2 ≤ L ≤ R ≤ 2n.
• 2 L R: for each element A[i][j] which satisfy L ≤ i − j ≤ R, increase the value to A[i][j] + 1, where 1 − n ≤ L ≤ R ≤ n − 1.
Meanwhile, you should answer the queries:
• 3 x1 x2 y1 y2 : count the value of elements A[i][j] which satisfy x1 ≤ i ≤ x  and y1 ≤ j ≤ y2 , where 1 ≤ x1 < x2 ≤ n and 1 ≤ y1 < y2 ≤ n.

输入

The input contains several test cases. The first line of the input is a single integer t which is the number of test cases. Then t test cases follow.
Each test case contains several lines. The first line contains the integer n and q. The i-th line of the next q lines contains an operation “1 L R” or “2 L R”, or a query “3 x1 x2 y1 y2 ”.
The sum of n for all test cases would not be larger than 200000 and the sum of q would not be larger than 50000.

输出

For each test case, you should output answers to queries printed one per line.

样例输入

2
6 6
2 0 1
3 1 4 3 5
3 2 5 2 3
1 5 7
3 1 4 3 5
3 2 5 2 3
6 26
2 -4 -1
3 1 4 2 5
3 3 6 4 6
1 4 7
3 2 5 2 3
3 1 4 2 5
2 -3 -1
3 1 4 3 5
1 3 5
1 2 3
3 2 5 2 3
3 1 4 2 5
3 3 6 4 6
2 0 4
3 1 4 3 5
3 1 4 2 5
1 9 11
2 1 2
3 2 5 2 3
3 3 6 4 6
2 -2 2
1 7 12
3 1 4 3 5
3 2 5 2 3
3 1 4 2 5
3 3 6 4 6

样例输出

Case #1:
3
4
11
10
Case #2:
10
6
8
22
26
12
38
13
32
44
23
30
49
33
67
53
题意就是给你一个n×n的矩阵,有三种操作
1LR 给L<=i+j<=R的点加1
2LR 给L<=i- j<=R的点加1
x1 x2 y1 y2 询问x1<=i<=x2,y1<=j<=y2的点的和 建两棵线段树,一棵维护对角线(i-j相同的一斜列,不妨称i-j的值为斜率),一棵维护负对角线(i+j相同)
对每棵线段树维护sum:sum(l,r)为斜率在l和r之间的数的和
sumL:sumL(l,r)为斜率在l和r之间,l行取1个,l+1行取两个……所形成的三角形,即一个等差数列的和
sumR:sumR(l,r)为斜率在l和r之间,r行取1个,r-1行取两个……所形成的三角形,即一个等差数列的和 对每次询问,为一个矩形,拆成两个等腰直角三角形和一个平行四边形的和,对角线和负对角线单独计算
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=4e5+;
struct tree{
int l,r;
ll sum,sumL,sumR,lazy;
int length() {return r-l+;}
int maintain(ll a)
{
lazy+=a;
sum+=a*length();
ll add=a*length()*(length()+)/;
sumL+=add;
sumR+=add;
}
}t[][N*];
int n,q,op,T;
void Pushup(int s,int id)
{
t[id][s].sum=t[id][s<<].sum+t[id][s<<|].sum;
t[id][s].sumL=t[id][s<<].sumL+t[id][s<<|].sumL+t[id][s<<|].sum*(t[id][s<<].length());
t[id][s].sumR=t[id][s<<].sumR+t[id][s<<|].sumR+t[id][s<<].sum*(t[id][s<<|].length());
}
void Pushdown(int s,int id)
{
if (t[id][s].lazy)
{
t[id][s<<].maintain(t[id][s].lazy);
t[id][s<<|].maintain(t[id][s].lazy);
t[id][s].lazy=;
}
}
void build(int s,int l,int r,int id)
{
t[id][s].l=l; t[id][s].r=r;
t[id][s].sum=t[id][s].sumL=t[id][s].sumR=t[id][s].lazy=;
if (l==r) return ;
int mid=(t[id][s].l+t[id][s].r)>>;
build(s<<,l,mid,id);
build(s<<|,mid+,r,id);
Pushup(s,id);
}
void update(int s,int L,int R,int id)
{
int l=t[id][s].l,r=t[id][s].r;
if (L<=l&&r<=R)
{
t[id][s].maintain();
return ;
}
Pushdown(s,id);
int mid=(l+r)>>;
if (L<=mid) update(s<<,L,R,id);
if (R>mid) update(s<<|,L,R,id);
Pushup(s,id);
}
ll query(int s,int L,int R,int id)
{
int l=t[id][s].l,r=t[id][s].r;
if (L<=l&&r<=R) return t[id][s].sum;
Pushdown(s,id);
int mid=(l+r)>>;
ll ret=;
if (L<=mid) ret+=query(s<<,L,R,id);
if (R>mid) ret+=query(s<<|,L,R,id);
Pushup(s,id);
return ret;
}
ll queryL(int s,int L,int R,int id)
{
int l=t[id][s].l,r=t[id][s].r;
if (L<=l&&r<=R) return t[id][s].sumL+t[id][s].sum*(l-L);
Pushdown(s,id);
int mid=(l+r)>>;
ll ret=;
if (L<=mid) ret+=queryL(s<<,L,R,id);
if (R>mid) ret+=queryL(s<<|,L,R,id);
Pushup(s,id);
return ret;
}
ll queryR(int s,int L,int R,int id)
{
int l=t[id][s].l,r=t[id][s].r;
if (L<=l&&r<=R) return t[id][s].sumR+t[id][s].sum*(R-r);
Pushdown(s,id);
int mid=(l+r)>>;
ll ret=;
if (L<=mid) ret+=queryR(s<<,L,R,id);
if (R>mid) ret+=queryR(s<<|,L,R,id);
Pushup(s,id);
return ret;
} int main()
{
scanf("%d",&T);
for (int ca=;ca<=T;ca++)
{
printf("Case #%d:\n",ca);
int l,r;
int X1,X2,Y1,Y2;
scanf("%d%d",&n,&q);
build(,,n*,);
build(,,*n-,);
while (q--)
{
scanf("%d",&op);
if (op==)
{
scanf("%d%d",&l,&r);
update(,l,r,);
}
else if (op==)
{
scanf("%d%d",&l,&r);
update(,l+n,r+n,);
}
else
{
scanf("%d%d%d%d",&X1,&X2,&Y1,&Y2);
ll ans=;
ll wid=min(X2-X1,Y2-Y1)+; l=X2+Y1;r=X1+Y2;
if (l>r) swap(l,r);
ans+=query(,l,r,)*wid;
ans+=queryL(,X1+Y1,l-,);
ans+=queryR(,r+,X2+Y2,); l=X1-Y1+n,r=X2-Y2+n;
if (l>r) swap(l,r);
ans+=query(,l,r,)*wid;
ans+=queryL(,X1-Y2+n,l-,);
ans+=queryR(,r+,X2-Y1+n,);
printf("%lld\n",ans);
}
}
}
return ;
}

一个WA了无数发最终参考大佬博客才写成的代码

ICPC 2015 Shenyang Online-E-EXCITED DATAbase的更多相关文章

  1. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  2. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  3. ACM-ICPC 2015 Shenyang Preliminary Contest B. Best Solver

    The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. ...

  4. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 I. Illegal or Not?

    I. Illegal or Not? time limit per test 1 second memory limit per test 512 megabytes input standard i ...

  5. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout

    K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...

  6. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing

    H. Hashing time limit per test 1 second memory limit per test 512 megabytes input standard input out ...

  7. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 C. Colder-Hotter

    C. Colder-Hotter time limit per test 1 second memory limit per test 512 megabytes input standard inp ...

  8. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 A. Anagrams

    A. Anagrams time limit per test 1 second memory limit per test 512 megabytes input standard input ou ...

  9. The Preliminary Contest for ICPC Asia Shenyang 2019

    传送门 B. Dudu's maze 题意: 是什么鬼东西???我读题可以读半小时QAQ 给出一张无向图,一个人在里面收集糖果,每个点都有一个糖果,特殊点除外.当他第一次进入特殊点时,会随机往一条边走 ...

随机推荐

  1. Tensorflow踩坑之tf.nn.bidirectional_dynamic_rnn()报错 “ValueError: None values not supported.”

    详细解决方法见链接:https://stackoverflow.com/questions/39808336/tensorflow-bidirectional-dynamic-rnn-none-val ...

  2. 在Delphi中通过OLE方式写Excel文件

    报表的打印是每个项目都会遇到的问题.由于报表格式要求五花八门,往往又同时要求打印格式可方便调整.作为一种替代方法,可以将需要打印的报表导出到Excel/Word,打印交给Office去吧.由于Offi ...

  3. 学习laravel源码之中间件原理

    刨析laravel源码之中间件原理 在看了laravel关于中间件的源码和参考了相关的书籍之后,写了一个比较简陋的管道和闭包实现,代码比较简单,但是却不好理解所以还是需要多写多思考才能想明白其中的意义 ...

  4. Ubuntu下面 PHPSTORM2017.2破解方法

    Ubuntu下面 PHPSTORM2017.2破解方法 下载破解文件 在 http://idea.lanyus.com/上面新下载一个破解文件. 破解步骤 将JetbrainsCrack-2.6.3_ ...

  5. Asp.net MVC area

    妈的,今天去携程面试,技术面了三轮,竟然让我走了,没有然后了,你不要老子,干嘛还面那么多轮,害的老子一上午的时间没了,气死我了. 好了,总结下面试中的问题吧, 1.GC 2.设计模式 3.做过的项目的 ...

  6. 深入理解JAVA虚拟机阅读笔记5——Java内存模型与线程

    Java内存模型是定义线程共享的变量的访问规则(实例字段.静态字段和构成数组对象的元素),但不包括线程私有的局部变量和方法参数. 1.主内存与工作内存 Java内存模型规定,所有的变量都必须存储在主内 ...

  7. Java中split的对象被特殊字符(.或|)分隔

    在Java中,一个String对象被一些特殊字符分隔时,可以使用split()方法,生成一个String[],然后进行其他的操作,就像下面这样: String str = "a1_b1_c1 ...

  8. FileZilla Server ftp 服务器下通过alias别名设置虚拟目录(多个分区)

    最近检查服务器的时候发现磁盘空间不够用了,正好有两个硬盘正好,一个硬盘还空着,正好通过ftp服务器的别名功能实现添加空间了,这样就不用重新弄机器了 说明:FileZilla Server 的虚拟目录设 ...

  9. python参数传递方式

    原文地址:http://www.cnblogs.com/zhaopengcheng/p/5492183.html python中一切皆对象,函数中参数传递的是对象的引用. 1在函数中改变变量指向的对象 ...

  10. linux 实践到的命令 collection

    查看文件夹/文件 大小:du   :(disk usage) 要通过 1024 字节块概述一个目录树及其每个子树的磁盘使用情况,请输入: du -k /home/fran/filename 这在/ho ...