专题突破一之分块——Untitled Problem II,Balanced Lineup,[ioi2009]Regions

文章目录

  • SP2940 UNTITLE1 - Untitled Problem II
    • source
    • solution
    • code
  • Balanced Lineup
    • source
    • code
  • Count on a tree II
  • [ioi2009]Regions

SP2940 UNTITLE1 - Untitled Problem II

source

solution

分块
si={si+k×(i−l+1)=si+k×i+k×(1−l)l≤i≤rsi+k×(r−l+1)r<is_i=\begin{cases} s_i+k\times (i-l+1)=s_i+k\times i+k\times (1-l)&&l\le i\le r\\ s_i+k\times (r-l+1)&&r<i \end{cases} si={si+k×(il+1)=si+k×i+k×(1l)si+k×(rl+1)lirr<i
对于整块而言k×(1−l)k\times(1-l)k×(1l)k×(r−l+1)k\times (r-l+1)k×(rl+1)都可以看作常数,用区间标记tag记录,再单独记录一下k
ans=max⁡{si+kblocki×i+tagblocki}l≤i≤rans=\max\bigg\{s_i+k_{block_i}\times i+tag_{block_i}\bigg\}\qquad l\le i\le r ans=max{si+kblocki×i+tagblocki}lir
对于l,rl,rl,r在的散块,暴力下放标记到块区间内,然后暴力查询

对于整块,发现max的方程类似dpdpdp​斜率优化转移,维护块内的上凸壳,二分求解

  • 假设块内i<ji<ji<j,且ansi<ansjans_i<ans_jansi<ansj

    • ansi=si+kblockpi+tagblockpans_i=s_i+k_{block_p}i+tag_{block_p} ansi=si+kblockpi+tagblockp

    • ansj=sj+kblockpj+tagblockpans_j=s_j+k_{block_p}j+tag_{block_p} ansj=sj+kblockpj+tagblockp

  • ansi<ansj⇔si+kblockpi<sj+kblockpj⇔si−sj<kblockp(j−i)⇒sj−sij−i>−kblockpans_i<ans_j\Leftrightarrow s_i+k_{block_p}i<s_j+k_{block_p}j\Leftrightarrow s_i-s_j<k_{block_p}(j-i)\\\Rightarrow \frac{s_j-s_i}{j-i}>-k_{block_p} ansi<ansjsi+kblockpi<sj+kblockpjsisj<kblockp(ji)jisjsi>kblockp

code

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define int long long
#define maxn 50005
#define maxB 255
int n, B;
int s[maxn], block[maxn];struct node {int l, r, tag, k, top;struct point {int x, y;point(){}point( int X, int Y ) { x = X, y = Y; }friend double slope( point s, point t ) { return 1.0 * ( s.y - t.y ) / ( s.x - t.x ); }}sta[maxB];void build_hall() {top = k = tag = 0;for( int i = l;i <= r;i ++ ) {point now( i, s[i] );while( top > 1 && slope( sta[top], now ) >= slope( sta[top - 1], now ) ) top --;sta[++ top] = now;}sta[top + 1] = point( r + 1, -1e18 );}int calc() {int l = 1, r = top, pos = 0;while( l <= r ) {int mid = ( l + r ) >> 1;if( slope( sta[mid + 1], sta[mid] ) > -k ) l = mid + 1;else r = mid - 1, pos = mid;}return tag + sta[pos].x * k + sta[pos].y;}void init( int L, int R ) { l = L, r = R, build_hall(); }void pushdown() { for( int i = l;i <= r;i ++ ) s[i] += tag + k * i; }}t[maxB];void modify( int l, int r, int k ) {for( int i = block[r] + 1;i <= block[n];i ++ )t[i].tag += ( r - l + 1 ) * k;if( block[l] == block[r] ) {t[block[l]].pushdown();for( int i = l;i <= r;i ++ )s[i] += ( i - l + 1 ) * k;for( int i = r + 1;i <= t[block[l]].r;i ++ )s[i] += ( r - l + 1 ) * k;t[block[l]].build_hall();}else {for( int i = block[l] + 1;i < block[r];i ++ ) t[i].k += k, t[i].tag -= ( l - 1 ) * k;t[block[l]].pushdown();t[block[r]].pushdown();for( int i = l;i <= t[block[l]].r;i ++ )s[i] += ( i - l + 1 ) * k;for( int i = t[block[r]].l;i < r;i ++ )s[i] += ( i - l + 1 ) * k;for( int i = r;i <= t[block[r]].r;i ++ ) s[i] += ( r - l + 1 ) * k;t[block[l]].build_hall();t[block[r]].build_hall();}
}int query( int l, int r ) {int ans = -1e18;if( block[l] == block[r] ) {t[block[l]].pushdown();for( int i = l;i <= r;i ++ )ans = max( ans, s[i] );t[block[l]].build_hall();}else {t[block[l]].pushdown();t[block[r]].pushdown();for( int i = l;i <= t[block[l]].r;i ++ ) ans = max( ans, s[i] );for( int i = t[block[r]].l;i <= r;i ++ ) ans = max( ans, s[i] );for( int i = block[l] + 1;i < block[r];i ++ ) ans = max( ans, t[i].calc() );t[block[l]].build_hall();t[block[r]].build_hall();}return ans;
}signed main() {scanf( "%lld", &n );B = sqrt( n );for( int i = 1;i <= n;i ++ ) {scanf( "%lld", &s[i] );s[i] += s[i - 1];block[i] = ( i - 1 ) / B + 1;}for( int i = 1;i <= block[n];i ++ )t[i].init( B * ( i - 1 ) + 1, min( n, B * i ) );int Q, opt, x, y, k;scanf( "%lld", &Q );while( Q -- ) {scanf( "%lld %lld %lld", &opt, &x, &y );if( opt ) printf( "%lld\n", query( x, y ) );else scanf( "%lld", &k ), modify( x, y, k );}return 0;
}

Balanced Lineup

source

code

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 50005
#define maxB 250
int n, Q, B;
int block[maxn], h[maxn], Max[maxB], Min[maxB];void solve( int l, int r ) {int maxx = -inf, minn = inf;for( int i = l;i <= min( block[l] * B, r );i ++ )maxx = max( maxx, h[i] ), minn = min( minn, h[i] );for( int i = block[l] + 1;i < block[r];i ++ )maxx = max( maxx, Max[i] ), minn = min( minn, Min[i] );for( int i = max( l, ( block[r] - 1 ) * B + 1 );i <= r;i ++ )maxx = max( maxx, h[i] ), minn = min( minn, h[i] );printf( "%d\n", maxx - minn );
}int main() {scanf( "%d %d", &n, &Q );B = sqrt( n );for( int i = 1;i <= n;i ++ ) {scanf( "%d", &h[i] );block[i] = ( i - 1 ) / B + 1;}memset( Min, 0x3f, sizeof( Min ) );for( int i = 1;i <= n;i ++ ) {Max[block[i]] = max( Max[block[i]], h[i] );Min[block[i]] = min( Min[block[i]], h[i] );}while( Q -- ) {int l, r;scanf( "%d %d", &l, &r );solve( l, r );}return 0;
}

Count on a tree II

已经合并到莫队算法里面去了

[ioi2009]Regions

source
将询问按颜色出现次数分块

小于等于n\sqrt nn的就挂在e2e2e2上,统计从根到该点颜色的出现次数

大于n\sqrt nn的就挂在e1e1e1上,统计该点子树内颜色的出现次数

复杂度就是nnn\sqrt nnn

#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
#define maxn 200005
#define maxR 25005
struct node {int x, y;node(){}node( int X, int Y ) { x = X, y = Y; }bool operator < ( node t ) const {return x == t.x ? y < t.y : x < t.x;}bool operator == ( node t ) {return x == t.x && y == t.y;}
}MS[maxn];
map < node, int > mp;
vector < int > G[maxn], f[maxR], g[maxR];
int n, R, Q, block;
int r[maxn], cnt[maxR], ans[maxn], id[maxn];void dfs1( int u ) {cnt[r[u]] ++;for( auto v : g[r[u]] ) ans[v] += cnt[MS[v].x];for( auto v : G[u] ) dfs1( v );cnt[r[u]] --;
}void dfs2( int u ) {	for( auto v : f[r[u]] ) ans[v] -= cnt[MS[v].y];cnt[r[u]] ++;for( auto v : G[u] ) dfs2( v );for( auto v : f[r[u]] ) ans[v] += cnt[MS[v].y];
}int main() {scanf( "%d %d %d %d", &n, &R, &Q, &r[1] );for( int i = 2, x;i <= n;i ++ ) {scanf( "%d %d", &x, &r[i] );G[x].push_back( i );}for( int i = 1;i <= n;i ++ )cnt[r[i]] ++;block = sqrt( n );for( int i = 1, x, y;i <= Q;i ++ ) {scanf( "%d %d", &x, &y );MS[i] = node( x, y );if( mp.count( node( x, y ) ) ) id[i] = mp[node( x, y )];else {id[i] = mp[node( x, y )] = i;if( cnt[y] <= block ) g[y].push_back( i );else f[x].push_back( i );}}memset( cnt, 0, sizeof( cnt ) );dfs1( 1 );dfs2( 1 );for( int i = 1;i <= Q;i ++ )printf( "%d\n", ans[id[i]] );return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/317273.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

.NET Core实战项目之CMS 第十七章 CMS网站系统的部署

目前我们的.NET Core实战项目之CMS系列教程基本走到尾声了&#xff0c;通过这一系列的学习你应该能够轻松应对.NET Core的日常开发了&#xff01;当然这个CMS系统的一些逻辑处理还需要优化&#xff0c;如没有引入日志组件以及缓存功能&#xff0c;权限目前只支持控制到菜单&…

ICPC2019南昌区域赛

ICPC2019南昌区域赛 题号题目知识点难度A9102BA Funny Bipartite Graph状压dp思维稳银快金CAnd and Pair二项式定理快铜DBitwise TreeEBob’s Problem思维&#xff0c;生成树签到FDynamic Suffix ArrayGEating Plan思维题稳铜快银HPowers of TwoIResistanceJSummonKTreeLWho i…

Loj#510-「LibreOJ NOI Round #1」北校门外的回忆【线段树】

正题 题目链接:https://loj.ac/p/510 题目大意 给出一个代码 function add(x,v)while x < n dos[x] s[x] xor vx x lowbit(x) //注意&#xff0c;这里是 lowbit&#xff0c;这也是两份代码唯一的区别end while end functionfunction query(x)ans 0while x > 0 doa…

如何用EFCore Lazy Loading实现Entity Split

α角 与 β角支持 现实生活 的 计算机系统&#xff0c;总有着两大偏差&#xff0c;第一个是 现实生活 与 计算机系统 的α角&#xff0c;另外一个是计算机系统的 逻辑设计 与 物理设计 的β角。举个栗子&#xff1a;α角&#xff1a;假设某个公司的商业流程&#xff0c;我们在做…

CodeForces:372(div1)div373(div2)

文章目录前言CF373A Collecting Beats is FunDescription\text{Description}DescriptionSolution\text{Solution}SolutionCode\text{Code}CodeCF373B Making Sequences is FunDescription\text{Description}DescriptionSolution\text{Solution}SolutionCF372A Counting Kangaro…

一二三系列之CodeChef分块——Chef and Churu,Chef and Problems,Children Trips

文章目录Chef and ChurusourcesolutioncodeChef and ProblemssourcesolutioncodeChildren TripssourcesolutioncodeChef and Churu source solution 对于单独的iii&#xff0c;查询可以用线段树/树状数组O(nlog⁡n)O(n\log n)O(nlogn)&#xff0c;这暗示可以平衡查询修改次数…

Bob‘s Problem

Bob’s Problem 题意&#xff1a; 一个有n个点的图&#xff0c;其中边分为白色和黑色&#xff0c;每个边都有边权&#xff0c;所选白边的数量的数量不能超过k&#xff0c;问现在选择一些边&#xff0c;使得所有点连通&#xff0c;问最大权值是多少&#xff1f; 题解&#xf…

.NET Core 开源项目 Anet 在路上

今天给大家介绍我刚开源的一个 .NET Core 项目&#xff1a;Anet。Anet 的目标是实现一个 .NET Core 通用库、通用框架和通用模板。我给它的定义是&#xff1a;A .NET Core Common Lib, Framework and Boilerplate.它的取名正是来自于这句话的前面四个字母&#xff1a;ANET。Ane…

Loj#2324-「清华集训 2017」小 Y 和二叉树

正题 题目链接:https://loj.ac/p/2324 题目大意 给出nnn个点的一棵树&#xff0c;每个点的度数不超过333。 你要求它的一个二叉树结构&#xff08;根任意选择&#xff09;使得其中序遍历的字典序最小。 1≤n≤1061\leq n\leq 10^61≤n≤106 解题思路 直接找根感觉比较麻烦&…

模板:后缀自动机(SAM)

所谓后缀自动机&#xff0c;就是通过后缀建立的自动机 &#xff08;逃&#xff09; 请允许我先介绍一下后缀家族&#xff1a; &#xff08;又逃&#xff09; 前言 OI生涯目前为止学习的最为难以理解的算法&#xff0c;没有之一。 到现在也没有完全的理解。 qwq 概念 定义&…

.NET 开源项目 Anet 介绍

使用 Anet 有一段时间了&#xff0c;已经在我的个人网站&#xff08;如 bookist.cc&#xff09;投入使用&#xff0c;目前没有发现什么大问题&#xff0c;所以才敢写篇文章向大家介绍。GitHub 地址&#xff1a;https://github.com/anet-team/anetAnet 是一个 .NET Core 通用框架…

线性代数问卷调查反馈——Find The Determinant III,Takahashi‘s Basics in Education and Learning

文章目录Find The Determinant IIIsourcecodeTakahashis Basics in Education and LearningsourcecodeFind The Determinant III source 高斯消元求行列式的模板题 code #include <cstdio> #include <iostream> using namespace std; #define maxn 205 #define…

L - Who is the Champion

L - Who is the Champion 计蒜客 - 42587 题意&#xff1a; 给出一个N阶矩阵&#xff0c;&#xff08; i , j &#xff09; &#xff08;i, j&#xff09;&#xff08;i,j&#xff09;处的数字表示这场比赛球队i ii踢进球队j jj多少球。两支球队平局则各加一分&#xff0c;一…

CF1037H Security(SAM)

解析 算是一个比较高级的SAM的应用了 对fail树的dfs序建立维护右端点最大值的线段树 考虑把所有的询问离线&#xff0c;按照右端点排序 每次动态把当前询问右端点左侧的前缀插入线段树 处理询问时&#xff0c;先贪心的尝试和询问串填法一样&#xff0c;如果不行就往下一个字母…

AT5147-[AGC036D]Negative Cycle【dp,模型转换】

正题 题目链接:https://www.luogu.com.cn/problem/AT5147 题目大意 有nnn个点的一张图&#xff0c;其中i→i1(i<n)i\rightarrow i1(i< n)i→i1(i<n)有一条边权值为000。 对于其他i,j(i≠j)i,j(i\neq j)i,j(ij)存在一条边i→ji\rightarrow ji→j&#xff0c;若i&l…

栈/队列/分块问卷调查反馈——Weak in the Middle,Cutting Plants,最小公倍数

文章目录Weak in the MiddlesourcesolutioncodeCutting Plantssourcesolutioncode[HNOI2016]最小公倍数sourcesolutioncodeWeak in the Middle source solution 栈模拟。 天数的计算&#xff0c;可以发现与其参与三元比较的次数有关 对于栈顶&#xff0c;如果被弹出&#…

我的十年创业路

记十年创业的心路历程和我的创业思辨导读1 为什么写这篇文章2 详细的总结和思辨 2.01 感恩 2.02 为什么创业 2.03 十年流水账 2.04 经历了哪些失败 2.05 重要的职场基础 2.06 持续的学习和进步 2.07 创业与兴趣 2.08 价值观的碰撞和选择 2.09 合作与…

C - And and Pair

C - And and Pair 题意&#xff1a; 问有多少组(i,j)满足要求。 要求为&#xff1a; 0<j<i<n i&ni i&j0 答案mod 1e97 题解&#xff1a; 这个和我的思路时一样的&#xff0c;且讲的更清楚 i&ni说明n为0的地方&#xff0c;i必须为0&#xff1b;n为1的地…

CF850F Rainbow Balls(数学、期望)

解析 二倍相邻项&#xff0c;就要想裂项 纯数学题 一道黑色的小凯的疑惑 设总球数为 sss 我们先钦定一种颜色留到最后&#xff0c;那么其他颜色可以等价考虑 设 fif_{i}fi​ 为钦定的颜色的球有 iii 个&#xff0c;涂完需要的期望次数 则有&#xff1a; fi(fi−1fi1)pfi(1−2p…

pjudge#21652-[PR #4]到底有没有九【数位dp】

正题 题目链接:http://pjudge.ac/problem/21652 题目大意 给出一个正整数kkk&#xff0c;求第nnn个xxx满足x(10k−1)x\times (10^k-1)x(10k−1)中没有一个数位为999。 1≤n≤1018,1≤k≤181\leq n\leq 10^{18},1\leq k\leq 181≤n≤1018,1≤k≤18 解题思路 首先是从高位到低…