您的位置:首页 > 其它

BNU20410 UVA11992 线段树区间更新

2016-09-19 23:40 381 查看


Fast Matrix Operations

Time Limit: 5000ms

Memory Limit: 131072KB
This problem will be judged on UVA.
Original ID: 11992

64-bit integer IO format: %lld     
Java class name: Main

Prev 

Submit Status Statistics Discuss
 Next

Type: 

None

NoneGraph Theory
    2-SAT    Articulation/Bridge/Biconnected Component
    Cycles/Topological Sorting/Strongly Connected Component
    Shortest Path
        Bellman Ford        Dijkstra/Floyd Warshall
    Euler Trail/Circuit
    Heavy-Light Decomposition    Minimum Spanning Tree
    Stable Marriage Problem
    Trees    Directed Minimum Spanning Tree
    Flow/Matching
        Graph Matching            Bipartite Matching
            Hopcroft–Karp Bipartite Matching
            Weighted Bipartite Matching/Hungarian Algorithm
        Flow
            Max Flow/Min Cut            Min Cost Max Flow
DFS-like
    Backtracking with Pruning/Branch and Bound
    Basic Recursion    IDA* Search
    Parsing/Grammar    Breadth First Search/Depth First Search
    Advanced Search Techniques
        Binary Search/Bisection        Ternary Search
Geometry
    Basic Geometry    Computational Geometry
    Convex Hull
    Pick's TheoremGame Theory
    Green Hackenbush/Colon Principle/Fusion Principle
    Nim    Sprague-Grundy Number
Matrix    Gaussian Elimination
    Matrix ExponentiationData Structures
    Basic Data Structures    Binary Indexed Tree
    Binary Search Tree
    Hashing    Orthogonal Range Search
    Range Minimum Query/Lowest Common Ancestor
    Segment Tree/Interval Tree    Trie Tree
    Sorting
    Disjoint SetString
    Aho Corasick    Knuth-Morris-Pratt
    Suffix Array/Suffix TreeMath
    Basic Math    Big Integer Arithmetic
    Number Theory        Chinese Remainder Theorem
        Extended Euclid
        Inclusion/Exclusion        Modular Arithmetic
    Combinatorics
        Group Theory/Burnside's lemma        Counting
    Probability/Expected Value
Others    Tricky
    Hardest    Unusual
    Brute Force    Implementation
    Constructive Algorithms    Two Pointer
    Bitmask    Beginner
    Discrete Logarithm/Shank's Baby-step Giant-step Algorithm
    Greedy    Divide and Conquer
Dynamic Programming 
Tag it!

[PDF Link]


Problem F


Fast Matrix Operations

There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has
a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operations:
1 x1 y1 x2 y2 v

Increment each element (x,y) in submatrix (x1,y1,x2,y2) by v (v>0)
2 x1 y1 x2 y2 v

Set each element (x,y) in submatrix (x1,y1,x2,y2) to v
3 x1 y1 x2 y2

Output the summation, min value and max value of submatrix (x1,y1,x2,y2)
In the above descriptions, submatrix (x1,y1,x2,y2) means all the elements (x,y) satisfying x1<=x<=x2 and y1<=x<=y2. It is guaranteed that 1<=x1<=x2<=r, 1<=y1<=y2<=c. After any operation, the sum of all the elements
in the matrix does not exceed 109.


Input

There are several test cases. The first line of each case contains three positive integers r, c, m, where m (1<=m<=20,000) is the number of operations. Each of the next m lines contains a query. There will be at
most twenty rows in the matrix. The input is terminated by end-of-file (EOF). The size of input file does not exceed 500KB.


Output

For each type-3 query, print the summation, min and max.


Sample Input

4 4 8
1 1 2 4 4 5
3 2 1 4 4
1 1 1 3 4 2
3 1 2 4 4
3 1 1 3 4
2 2 1 4 4 2
3 1 2 4 4
1 1 1 4 3 3


Output for the Sample Input

45 0 5
78 5 7
69 2 7
39 2 7

https://www.bnuoj.com/v3/problem_show.php?pid=20410

给你一个初始全是0的矩阵r*c    三个操作1把满足 x1<=x<=x2  y1<=y<y2    (x,y)的子矩阵都加V 

2就把子矩阵变成V  3就是查询子矩阵和,最小值,最大值;

多维转换成一维  注意set的操作放下去的时候add标记得清零

#include<stdio.h>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int SUM,MIN,MAX;
struct node
{
int minn,maxn,sum,add,st;
} tree[1000010*4];

void pushup(int rt)
{
int lc=rt<<1,rc=rt<<1|1;
tree[rt].sum=tree[lc].sum+tree[rc].sum;
tree[rt].minn=min(tree[lc].minn,tree[rc].minn);
tree[rt].maxn=max(tree[lc].maxn,tree[rc].maxn);
}

void build(int l,int r,int rt)
{
tree[rt].add=0;
tree[rt].st=-1;
tree[rt].sum=0;
tree[rt].minn=0;
tree[rt].maxn=0;
if(l==r) return ;
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}

void pushdown(int rt,int len)
{
int lc=(rt<<1);
int rc=(rt<<1|1);
if(tree[rt].st>=0)
{
tree[lc].st=tree[rc].st=tree[rt].st;
tree[lc].add=tree[rc].add=0;//add清除
tree[rt].st=-1;
tree[lc].sum=tree[lc].st*(len-(len>>1));
tree[rc].sum=tree[rc].st*(len>>1);
tree[lc].minn=tree[lc].st;
tree[lc].maxn=tree[lc].st;
tree[rc].minn=tree[rc].st;
tree[rc].maxn=tree[rc].st;
}
if(tree[rt].add)
{
tree[lc].add+=tree[rt].add;
tree[rc].add+=tree[rt].add;
tree[lc].sum+=tree[rt].add*(len-(len>>1));//放下去个一半
tree[rc].sum+=tree[rt].add*(len>>1);//只要加上上层传下来的
tree[lc].minn+=tree[rt].add;
tree[lc].maxn+=tree[rt].add;
tree[rc].minn+=tree[rt].add;
tree[rc].maxn+=tree[rt].add;
tree[rt].add=0;
}
}

void update1(int L,int R,int l,int r,int rt,int p)
{
if(L<=l&&R>=r)
{
tree[rt].add+=p; //本层更新
tree[rt].sum+=(r-l+1)*p;
tree[rt].minn+=p;
tree[rt].maxn+=p;
return ;
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m) update1(L,R,lson,p);
if(R>m) update1(L,R,rson,p);
pushup(rt); //更新返回
}

void update2(int L,int R,int l,int r,int rt,int p)
{
if(L<=l&&R>=r)
{
tree[rt].add=0;
tree[rt].st=p;
tree[rt].sum=(r-l+1)*p;
tree[rt].minn=p;
tree[rt].maxn=p;
return ;
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m) update2(L,R,lson,p);
if(R>m) update2(L,R,rson,p);
pushup(rt);
}

int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
MIN=min(MIN,tree[rt].minn);
MAX=max(MAX,tree[rt].maxn);
return tree[rt].sum;
}
pushdown(rt,r-l+1);
int ret=0;
int m=(l+r)>>1;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson);
return ret;
}

int main()
{
int r,c,q,s,i,x1,x2,y1,y2,v,n;
while(scanf("%d%d%d",&r,&c,&q)!=EOF)
{
n=r*c;
build(1,n,1);
while(q--)
{
scanf("%d",&s);
if(s!=3)
{
if(s==1)
{
scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&v);
for(i=x1-1; i<x2; i++)
update1(i*c+y1,i*c+y2,1,n,1,v);
}
else
{
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v);
for(i=x1-1; i<x2; i++)
update2(i*c+y1,i*c+y2,1,n,1,v);
}
}
else
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
SUM=0;
MIN=0x7f7f7f7f;
MAX=0;
for(i=x1-1; i<x2; i++)
SUM+=query(i*c+y1,i*c+y2,1,n,1);
printf("%d %d %d\n",SUM,MIN,MAX);

}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UVA BNU