做过,但当时咕了 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的更多相关文章

  1. csp-s模拟测试56Merchant, Equation,Rectangle题解

    题面:https://www.cnblogs.com/Juve/articles/11619002.html merchant: 二分答案,贪心选前m大的 但是用sort复杂度不优,会T掉 我们只是找 ...

  2. csp-s模拟测试56(10.2)Merchant「二分」·Equation「树状数组」

    又死了......T1 Merchant 因为每个集合都可以写成一次函数的形式,所以假设是单调升的函数,那么随着t越大就越佳 而单调减的函数,随着t的增大结果越小,所以不是单调的??? 但是我们的单调 ...

  3. 2D Rotated Rectangle Collision

    Introduction While working on a project for school, I found it necessary to perform a collision chec ...

  4. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  5. [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 ...

  6. [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 ...

  7. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  8. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  9. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

随机推荐

  1. 如何给html元素的onclick事件传递参数即如何获取html标签的data

    某些非text元素,如a.button等用于触发时间的标签可已将要传的数据放在一个属性中,如data,这个属性必须是不影响样式的,可以使用任意非html定义的名字命名属性,然后将数据传到属性中, &l ...

  2. 如何根治慢SQL?

    本文摘自:CodeSheep 今天和大家聊一个常见的问题:慢SQL. 包括以下内容: 慢SQL的危害 SQL语句的执行过程 存储引擎和索引的那些事儿 慢SQL解决之道 后续均以MySQL默认存储引擎I ...

  3. 【网站公告】避免反对百度的限制措施:百度搜索过来的访问会自动禁用js权限

    今天下午百度联系我们,发现通过百度搜索访问我们网站的博文时会出现下面反对百度的画面,让我们今天彻底处理好,保证不再出现这种情况. 我们排查后发现是这位博主申请了js权限,添加了下面的反对百度的脚本: ...

  4. C++//菱形继承 //俩个派生类继承同一个基类 //又有某个类同时继承俩个派生类 //成为 菱形继承 或者 钻石 继承//+解决

    1 //菱形继承 2 //俩个派生类继承同一个基类 3 //又有某个类同时继承俩个派生类 4 //成为 菱形继承 或者 钻石 继承 5 6 #include <iostream> 7 #i ...

  5. Java 执行控制流程

    1.带标签的break会中断并跳出标签所指的循环: 2.带标签的continue会中断本次循环,并开始标签所指处循环的下一轮循环.

  6. Servlet基本知识

    Servlet基本知识 1.IDEA创建第一个Servlet程序xing 这里说明如何使用 IDEA Ultimate 2020.1.3版本来新建第一个web程序.参考 MoonChasing 1.1 ...

  7. Vue系列-02-axios

    vue.js和aixos.js # https://blog-static.cnblogs.com/files/lichengguo/vue.js # 下载该文件,保存的路径为代码同级目录 js/vu ...

  8. 题解 e

    传送门 第一眼看貌似可以树剖,然而那个绝对值不知怎么维护 求最小连通块我只会\(k^2\) 主席树貌似可以用来查询区间内与某个数差的绝对值的最小值? 确实,每次查大于等于该数的最小数和小于等于该数的最 ...

  9. centos7-同步时间

    yum install -y ntp ntpdate ntpdate -u cn.pool.ntp.org # 阿里云ntp ntpdate ntp1.aliyun.com 但这样的同步,只是强制性的 ...

  10. 如何选择Spring cloud和 Spring Boot对应的版本

    如何选择Spring cloud和 Spring Boot对应的版本 首先,我们进入Spring Cloud官网,查询Spring cloud的版本和对应的Spring Boot版本 打开Spring ...