ZOJ4100 浙江省赛16th Problem A
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4100
比赛的时候封榜开始开这题,因为我的愚蠢没有开出来。
查询的最小值不用多说,维护一个当前的联通快数量num, max(num - K,1LL)就是答案,问题在于最大值。
仔细一看就会发现,首先需要将所有的联通快填满,变成一个个完全子图,这些被填满的边被称为白给的边,然后再开始计算答案,每次将两个联通快联通的时候,只用一条有效边就又会产生一堆白给的边,所以说,为了使联通快尽可能地多,我们需要每次不得不联通两个块之后尽量产生更多的白给的边。显而易见的是,联通需要从大的联通快开始合并,可以贪心的产生更多的白给边。
-----------------------------比赛想到了这一步,开始set暴力,最后十分钟发现时间复杂度很假,开始挂机-------------------------------------------
被合并的联通快的数量可以二分,二分的位置往右的所有联通快将会被变成一个大联通快,单调性显然。
问题在于维护这个过程,考虑到权值线段树,思路就明了了。
一开始先二分然后权值线段树check的操作写挂了,后面采用先权值线段树,最后一个点二分的操作就过了。
意识到先二分的话时间复杂度是nlognlogn,先权值线段树的话时间复杂度是Nlognlog(最后一个叶子节点的点数量)
#include <map> #include <set> #include <ctime> #include <cmath> #include <queue> #include <stack> #include <vector> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <sstream> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define For(i, x, y) for(int i=x;i<=y;i++) #define _For(i, x, y) for(int i=x;i>=y;i--) #define Mem(f, x) memset(f,x,sizeof(f)) #define Sca(x) scanf("%d", &x) #define Sca2(x,y) scanf("%d%d",&x,&y) #define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define Scl(x) scanf("%lld",&x); #define Pri(x) printf("%d\n", x) #define Prl(x) printf("%lld\n",x); #define CLR(u) for(int i=0;i<=N;i++)u[i].clear(); #define LL long long #define ULL unsigned long long #define mp make_pair #define PII pair<int,int> #define PIL pair<int,long long> #define PLL pair<long long,long long> #define pb push_back #define fi first #define se second typedef vector<int> VI; int read(){int x = 0,f = 1;char c = getchar();while (c<'0' || c>'9'){if (c == '-') f = -1;c = getchar();} while (c >= '0'&&c <= '9'){x = x * 10 + c - '0';c = getchar();}return x*f;} const double eps = 1e-9; const int maxn = 1e5 + 10; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; int N,M,K; int fa[maxn]; LL size[maxn],edge[maxn],comp[maxn]; LL bg,num; void init(){for(int i = 0 ; i <= N ; i ++){fa[i] = i;size[i] = 1;edge[i] = 0;}num = N;bg = 0; } inline LL cul(LL x){return x * (x - 1) / 2; }struct Tree{int l,r;LL sum,num,edge; }tree[maxn << 2]; void Pushup(int t){tree[t].edge = tree[t << 1].edge + tree[t << 1 | 1].edge;tree[t].num = tree[t << 1].num + tree[t << 1 | 1].num;tree[t].sum = tree[t << 1].sum + tree[t << 1 | 1].sum; } void Build(int t,int l,int r){tree[t].l = l; tree[t].r = r;if(l == r){tree[t].sum = tree[t].num = tree[t].edge = 0;if(l == 1){tree[t].sum = tree[t].num = N;tree[t].edge = N * comp[l];}return; }int m = l + r >> 1;Build(t << 1,l,m); Build(t << 1 | 1,m + 1,r);Pushup(t); } void update(int t,int p,int v){if(tree[t].l == tree[t].r){tree[t].sum += v * tree[t].l;tree[t].num += v;tree[t].edge += comp[tree[t].l] * v;return;}int m = tree[t].l + tree[t].r >> 1;if(p <= m) update(t << 1,p,v);else update(t << 1 | 1,p,v);Pushup(t); } int find(int p){if(fa[p] == p) return p;return fa[p] = find(fa[p]); } void add(int u,int v){u = find(u); v = find(v);if(u == v){edge[u]++;bg--;return;} num--;bg -= comp[size[u]] - edge[u];bg -= comp[size[v]] - edge[v];update(1,size[u],-1);update(1,size[v],-1);fa[u] = v;size[v] += size[u];edge[v] += edge[u] + 1;bg += comp[size[v]] - edge[v];update(1,size[v],1); } LL cnt,E; int query(int t,LL k,LL v){if(tree[t].l == tree[t].r){int l = 0,r = tree[t].num;int ans = 0;while(l <= r){int m = l + r >> 1;if(comp[v + m * tree[t].l] < comp[tree[t].l] * m + k){ans = m + 1;l = m + 1; }else{r = m - 1;}}return ans;}if(comp[v + tree[t << 1 | 1].sum] >= k + tree[t << 1 | 1].edge) return query(t << 1 | 1,k,v);else return query(t << 1,k + tree[t << 1 | 1].edge,v + tree[t << 1 | 1].sum) + tree[t << 1 | 1].num; }int main(){int T; Sca(T);for(int i = 0 ; i < maxn; i ++) comp[i] = cul(i);while(T--){Sca2(N,M); init();Build(1,1,N);for(int i = 1; i <= M ; i ++){int q = read();if(q == 1){int u,v; u = read(); v = read(); add(u,v); }else{LL k; Scl(k);int MIN,MAX;MIN = max(num - k,1LL);if(k <= bg){MAX = num;}else{k -= bg;MAX = num - query(1,k,0LL) + 1;}printf("%d %d\n",MIN,MAX);}}} return 0; }
转载于:https://www.cnblogs.com/Hugh-Locke/p/10844117.html
ZOJ4100 浙江省赛16th Problem A相关推荐
- 2017浙江省赛 B - Problem Preparation ZOJ - 3959
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 题目: It's time to prepare the pr ...
- 【2016浙江省赛:区间取模】E : Modulo Query | ZOJ - 3940
2016浙江省赛:E 题 Modulo Query [难度] 4.5/104.5/104.5/10 据说是卡银题?感觉有点难 [题意] F(i,X)={XmodA1i=1F(i−1,X)modAi2≤ ...
- 2021浙江省赛题解(A,C,F,G,J,L,M)
2021浙江省赛题解(A,C,F,G,J,L,M) A.League of Legends 题解 签到题 直接求和判断一下 注意会爆 i n t int int以及相等的情况. 代码 #include ...
- 2022浙江省赛、ICPC昆明区域赛 游·寄
前夜 周六打的浙江省赛.周日昆明区域赛,周五平常作息,早上和爸妈聊天 我说道 金华疫情情况还好,还能出校吃饭,没想到 噩耗马上就来了.金华突然有了几例阳性,其中有一位还是滴滴司机.线上教学的消息已发出 ...
- 【ZJCPC2019 第16届 浙江省赛】The 16th Zhejiang Provincial Collegiate Programming Contest(GFHIJ 5题)
补题地址:https://zoj.pintia.cn/home/news 搜索16th 本文按照通过率补的题 G .Lucky 7 in the Pocket 题意:给出T个数,对于每个数,找出一个能 ...
- 2019 浙江省赛部分题解(The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)
签到题 GLucky 7 in the Pocket Time Limit: 1 Second Memory Limit: 65536 KB BaoBao loves number 7 bu ...
- 多说都是泪 GDUT 广东工业大学2016校赛决赛-网络赛 1170 Problem B Sward Art Online
题目:点我 http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=1 今天打网络个人赛,一开始就不太顺利,到了这个题,刚看到的时候,认为是手 ...
- 中石油训练赛 - Bee Problem(dfs+连通块)
题目描述 You are a busy little bee, and you have a problem. After collecting nectar all day long, you ar ...
- 2019年ICPC银川区域赛 Easy Problem(简单莫比乌斯函数 + 欧拉降幂)
Easy Problem ∑a1=1m∑a2=1m∑a3=1m⋯∑an−1m∑anm[gcd(a1,a2,a3,-,an−1,an)==d](a1,a2,a3,-,an−1,an)k=dkd∑a1=1 ...
最新文章
- 智源发布!《人工智能的认知神经基础白皮书》
- oracle创建函数和调用存储过程和调用函数的例子(区别)
- 如何在Ruby中获得随机数
- ChineseGLUE(CLUE):针对中文自然语言理解任务的基准平台
- mysql数据库txt备份linux_linux备份mysql数据库
- 【APICloud系列|11】使用APPuploader申请ios开发证书及ios发布证书教程
- 【Python】time内置模块处理时间信息
- 更高效地刷OJ——Java中常用的排序方法,Array.sort(),Arrays.parallelSort(), Collections.sort()
- STL 算法罗列 (转)
- TCP/IP - ARP的作用、RARP协议
- 【目标检测大集合】R-FCN、SSD、YOLO2、faster-rcnn和labelImg实验笔记
- 测量MATLAB安装哪些产品,matlab需要安装哪些工具箱
- 微信支付全流程对接文档
- 不良资产证券化信披 担任怎样的角色
- 常用ES6语法归纳总结!
- VUE项目中高德地图选择坐标和输入搜索功能
- 运营笔记:一个新公众号怎么吸粉?看看这位大神怎么做的!
- React Native + react-native-camera 实现扫描二维码「安卓」
- vue项目中引入阿里云滑动验证
- 快速填充空单元格-快速填充上一行或者下一行数据
热门文章
- 00-02.PHP 网站假设 之 学习PHP语法 [James建站]
- Effective Java 学习笔记 1
- HttpWebRequest与HttpWebResponse进行数据采集时的注意点
- Flink数据清洗(Kafka事实表+Redis维度表)
- Java spark中的各种范型接口Function的区别(持续更新中)
- Remarkable启动遇到Spellchecking not enabled(没有解决)
- django项目更新图片后,页面图片不更新
- 数据仓库相关书籍调研
- Exception in thread main java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FSDataInputStream
- 强制apt使用ipv4来更新