• hdu 4107当卡段树


    其核心思想是记录最大的节点值和最低值,假设max<p要么min>=p时间,在节点只变化add值,不要子树遍历;否则,就往子树递归。


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<vector>
    
    using namespace std;
    
    const int maxn = 2e5+50;
    int N, P;
    
    struct node{
    	int l, r, Min, Max, add;
    	int mid() { return (l+r)/2; }
    }tree[maxn<<2];
    
    int p, n;
    void buildTree(int l, int r, int rt)
    {
    	tree[rt].l = l;
    	tree[rt].r = r;
    	tree[rt].add = 0;
    	tree[rt].Min = 0;
    	tree[rt].Max = 0;
    	if(l == r) return ;
    	int mid = tree[rt].mid();
    	buildTree(l, mid, rt<<1);
    	buildTree(mid+1, r, rt<<1|1);
    }
    
    void update(int l, int r, int rt, int L, int R, int add)
    {
    	if(L <= l && R >= r)
    	{
    		if(tree[rt].Max < P)
    		{
    			tree[rt].add += add;
    			tree[rt].Min += add;
    			tree[rt].Max += add;
    			return ;
    		}
    		else if(tree[rt].Min >= P)
    		{
    			tree[rt].add += 2*add;
    			tree[rt].Min += 2*add;
    			tree[rt].Max += 2*add;
    			return ;
    		}
    	}
    	if(tree[rt].add){
    		tree[rt<<1].add += tree[rt].add;
    		tree[rt<<1].Min += tree[rt].add;
    		tree[rt<<1].Max += tree[rt].add; 
    		tree[rt<<1|1].add += tree[rt].add;
    		tree[rt<<1|1].Min += tree[rt].add;
    		tree[rt<<1|1].Max += tree[rt].add;
    		
    		tree[rt].add = 0;
    	}
    	if(l == r) return ;
    	int mid = tree[rt].mid();
    	if(L <= mid) update(l, mid, rt<<1, L, R, add);
    	if(R > mid) update(mid+1, r, rt<<1|1, L, R, add);
    	
    	tree[rt].Min = min(tree[rt<<1].Min, tree[rt<<1|1].Min);
    	tree[rt].Max = max(tree[rt<<1].Max, tree[rt<<1|1].Max);
    } 
    
    void query(int l, int r, int rt)
    {
    	if(tree[rt].Min == tree[rt].Max){
    		for(int i = l; i <= r; i ++)
    			printf( i == N ? "%d
    " : "%d ", tree[rt].Min );
    		return ;
    	}
    	if(tree[rt].add)
    	{
    		tree[rt<<1].add += tree[rt].add;
    		tree[rt<<1].Min += tree[rt].add;
    		tree[rt<<1].Max += tree[rt].add; 
    		tree[rt<<1|1].add += tree[rt].add;
    		tree[rt<<1|1].Min += tree[rt].add;
    		tree[rt<<1|1].Max += tree[rt].add;
    		
    		tree[rt].add = 0;
    	}	
    	if(l == r) return ;
    	int mid = tree[rt].mid();
    	query(l, mid, rt<<1);
    	query(mid+1, r, rt<<1|1);
    }
    
    int main()
    {
    	int n, m, p;
    	int a, b, c;
    	while(~scanf("%d%d%d", &n, &m, &p))
    	{
    		N = n;
    		P = p;
    		buildTree(1, n, 1);
    		for(int i = 0; i < m; i ++)
    		{
    			scanf("%d%d%d", &a, &b, &c);
    			update(1, n, 1, a, b, c);
    		}
    		query(1, n, 1);
    	}
    } 
    
    
    


  • 相关阅读:
    查看数据库错误日志的位置
    快速过滤出完整的SQL语句
    使用apidocJs快速生成在线文档
    vs开发nodejs系列之 修改新建js文件的模板
    Node.js 入门资料
    ECMAScript 6 学习资料
    NodeJS学习目录
    MongoDB学习笔记系列~目录
    sails ORM组件 Waterline v0.10 英文文档
    node.js---sails项目开发
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5047735.html
Copyright © 2020-2023  润新知