20210809 Merchant,Equation,Rectangle
做过,但当时咕了 T3
Merchant
先特判 \(t=0\),之后斜率一定会起作用。
考虑最终选择的物品集合,它们的斜率和一定大于 \(0\),因此答案具有单调性,可以二分。
实现的时候注意细节
const int N = 1e6+5;
int n,m;
LL s,k[N],b[N];
LL y[N];
bool check(int x) {
For(i,1,n) y[i] = k[i] * x + b[i];
nth_element(y+1,y+m,y+n+1,greater<LL>()); // O(n)找前m大
LL sum = 0;
For(i,1,m) {
if( y[i] >= 0 ) sum += y[i]; // 可能选不满m个
if( sum >= s ) return 1; // 如果不在这里判会爆LL
}
return 0;
}
signed main() {
freopen("merchant.in","r",stdin);
freopen("merchant.out","w",stdout);
read(n,m,s);
For(i,1,n) read(k[i],b[i]);
if( check(0) ) { puts("0"); return 0; }
int l = 1, r = 1e9;
while( l < r ) {
int mid = l+r>>1;
if( check(mid) ) r = mid;
else l = mid+1;
}
write(l);
return iocl();
}
Equation
乍一看不好做,但题目只求 \(x_1\) 的值,可以把每个方程改写成 \(x_i=k_ix_1+b_i\) 的形式,修改时用 BIT 维护 \(b\) 即可。注意分深度的奇偶来判断正负。
const int N = 1e6+5;
int n,q,fa[N],val[N];
int ind,dfn[N],siz[N],k[N];
vector<int> to[N];
struct BIT {
LL t[N];
void add(int i,LL x) { for(;i<=n;i+=i&-i)t[i]+=x; }
void add(int l,int r,LL x) { add(l,x), add(r+1,-x); }
LL operator [] (int i) { LL res=0; for(;i;i-=i&-i)res+=t[i]; return res; }
} b;
void none() { putchar('n'),putchar('o'),putchar('n'),putchar('e'),putchar(10); }
void inf() { putchar('i'),putchar('n'),putchar('f'),putchar(10); }
void dfs(int u) {
dfn[u] = ++ind, siz[u] = 1;
for(int v : to[u]) k[v] = -k[u], dfs(v), siz[u] += siz[v];
b.add(dfn[u],dfn[u]+siz[u]-1,k[u]*val[u]);
}
signed main() {
freopen("equation.in","r",stdin);
freopen("equation.out","w",stdout);
read(n,q);
For(i,2,n) {
read(fa[i]), read(val[i]);
to[fa[i]].pb(i);
}
k[1] = 1, dfs(1);
while( q-- ) {
int op; read(op);
if( op == 1 ) {
int u,v,s; read(u,v,s);
int kk = k[u]+k[v]; LL bb = s-k[u]*b[dfn[u]]-k[v]*b[dfn[v]];
if( !kk ) !bb ? inf() : none();
else if( bb % kk ) none();
else !bb ? none() : write(bb/kk);
} else {
int u,w; read(u,w);
b.add(dfn[u],dfn[u]+siz[u]-1,k[u]*(w-val[u])), val[u] = w;
}
}
return iocl();
}
rectangle
发现 \(n\) 很大而值域很小,考虑直接在值域上找矩形。
枚举列来确定矩形左右边界,根据左右边界上的点的纵坐标来确定合法的上下边界。
具体做法
时间复杂度 \(O(2500n\log2500)\)
const int N = 1e4+5, X = 2500, mod = 1e9+7;
int n,cnt[N],mp[X+5][X+5];
bool vis[X+5][X+5];
int ans;
struct BIT {
int t[X+5];
void add(int i,int x) { for(;i<=X;i+=i&-i)t[i]+=x; }
int query(int l,int r) {
int res=0; for(--l;r>l;r-=r&-r)res+=t[r];
for(;l>r;l-=l&-l)res-=t[l]; return res; }
} siz[X+5],sum[X+5];
signed main() {
freopen("rectangle.in","r",stdin);
freopen("rectangle.out","w",stdout);
read(n);
For(i,1,n) {
int x; read(x);
read(mp[x][++cnt[x]]);
}
For(i,1,X) {
sort(mp[i]+1,mp[i]+cnt[i]+1);
mp[i][cnt[i]+1] = X+1;
}
For(r,1,X) if( cnt[r] ) {
For(i,1,cnt[r]) if( !vis[r][mp[r][i]] ) vis[r][mp[r][i]] = 1,
siz[r].add(mp[r][i],1), sum[r].add(mp[r][i],mp[r][i]);
rFor(l,r-1,1) if( cnt[l] ) {
For(i,1,cnt[l]) if( !vis[r][mp[l][i]] ) vis[r][mp[l][i]] = 1,
siz[r].add(mp[l][i],1), sum[r].add(mp[l][i],mp[l][i]);
int i = 1, j = 1, now = max(mp[l][1],mp[r][1]);
while( mp[l][i+1] <= now ) ++i;
while( mp[r][j+1] <= now ) ++j;
while( i <= cnt[l] && j <= cnt[r] ) {
int up = min(mp[l][i+1],mp[r][j+1]), down = min(mp[l][i],mp[r][j]);
ans += (r-l) *
(((LL)sum[r].query(now,up-1) * siz[r].query(1,down) -
(LL)siz[r].query(now,up-1) * sum[r].query(1,down)) %mod) %mod;
if( ans >= mod ) ans -= mod;
now = up;
if( mp[l][i+1] <= now ) ++i;
if( mp[r][j+1] <= now ) ++j;
}
}
}
write(ans);
return iocl();
}
20210809 Merchant,Equation,Rectangle的更多相关文章
- csp-s模拟测试56Merchant, Equation,Rectangle题解
题面:https://www.cnblogs.com/Juve/articles/11619002.html merchant: 二分答案,贪心选前m大的 但是用sort复杂度不优,会T掉 我们只是找 ...
- csp-s模拟测试56(10.2)Merchant「二分」·Equation「树状数组」
又死了......T1 Merchant 因为每个集合都可以写成一次函数的形式,所以假设是单调升的函数,那么随着t越大就越佳 而单调减的函数,随着t的增大结果越小,所以不是单调的??? 但是我们的单调 ...
- 2D Rotated Rectangle Collision
Introduction While working on a project for school, I found it necessary to perform a collision chec ...
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- Redis挂了,流量把数据库也打挂了,怎么办?
你好呀,我是歪歪. 是这样的,前几天有个读者给我发消息,说面试的时候遇到一个场景题: 他说他当时,一时间竟然找不到回答问题的角度,感觉自己没有回答到点子上. 我仔细想了一下,确实是感到这个问题有一丝丝 ...
- Samba 远程命令执行漏洞(CVE-2017-7494)
该漏洞影响Samba 3.5.0之后的所有版本,在4.6.4/4.5.10/4.4.14修复了这个漏洞 use exploit/linux/samba/is_known_pipename set rh ...
- Linux账号管理(入门)
引言 对用户的操作要在root权限下执行 创建用户 useradd useradd -m username:创建一个用户名为:username的用户 -m参数代表自动创建这个用户的主目录 删除用户 u ...
- [源码解析]机器学习参数服务器ps-lite(4) ----- 应用节点实现
[源码解析]机器学习参数服务器ps-lite(4) ----- 应用节点实现 目录 [源码解析]机器学习参数服务器ps-lite(4) ----- 应用节点实现 0x00 摘要 0x01 基础类 1. ...
- P6855「EZEC-4.5」走方格 TJ
目录 前言 题意简述 法一:时间复杂度 $Θ(m2n2)$ (TLE) $Code$ 法二:正解,时间复杂度 $Θ(mn)$ $Code$ 写在最后 洛谷 前言 题目传送门 正解:动态规划 挺 dul ...
- 文件上传之WAF绕过及相安全防护
文件上传在数据包中可修改的地方 Content-Disposition:一般可更改 name:表单参数值,不能更改 filename:文件名,可以更改 Content-Type:文件 MIME,视情况 ...
- SWAP的罪与罚&&NUMA的取舍
说个案例:一台Apache服务器,由于其MaxClients参数设置过大,并且恰好又碰到访问量激增,结果内存被耗光,从而引发SWAP,进而负载攀升,最终导致宕机. 正所谓:SWAP,性能之大事,死生之 ...
- Java HashMap【笔记】
Java HashMap[笔记] HashMap HashMap 基本结构 HashMap 底层的数据结构主要是数组 + 链表 + 红黑树 其中当链表的长度大于等于 8 时,链表会转化成红黑树,当红黑 ...
- SQL 练习3
查询存在" 01 "课程,可能不存在" 02 "课程的情况(不存在时显示为 null ) SELECT * FROM (SELECT * FROM SC WHE ...
- Java社区——个人项目开发笔记(二)
1.B\S架构通信原理 浏览器,服务器之间产生通信,浏览器访问服务器,服务器返回一个HTML,浏览器会对HTML进行解析,并渲染相关的内容. 在解析过程中,会发现HTML里引用了css文件,js文件, ...