【BZOJ1036】【LCT版】树的统计Count

本文介绍了一种基于链接切割树(LCT)的算法实现,用于处理树状结构上的节点权值修改、路径最大权值及路径权值之和查询等问题。通过具体的代码示例,展示了如何使用LCT进行高效的树形数据结构操作。

Description

一 棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w。我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身

Input

输 入的第一行为一个整数n,表示节点的个数。接下来n – 1行,每行2个整数a和b,表示节点a和节点b之间有一条边相连。接下来n行,每行一个整数,第i行的整数wi表示节点i的权值。接下来1行,为一个整数 q,表示操作的总数。接下来q行,每行一个操作,以“CHANGE u t”或者“QMAX u v”或者“QSUM u v”的形式给出。 对于100%的数据,保证1<=n<=30000,0<=q<=200000;中途操作中保证每个节点的权值w在-30000到 30000之间。

Output

对于每个“QMAX”或者“QSUM”的操作,每行输出一个整数表示要求输出的结果。

Sample Input

4
1 2
2 3
4 1
4 2 1 3
12
QMAX 3 4
QMAX 3 3
QMAX 3 2
QMAX 2 3
QSUM 3 4
QSUM 2 1
CHANGE 1 5
QMAX 3 4
CHANGE 3 6
QMAX 3 4
QMAX 2 4
QSUM 3 4

Sample Output

4
1
2
2
10
6
5
6
5
16

Hint

【分析】

随便写了一个LCT的版本,比树链剖分慢...

现在发现LCT真是个好东西,好写又有用。

  1 /*
  2 唐代白居易
  3 《白云泉》
  4 天平山上白云泉,云自无心水自闲。
  5 何必奔冲山下去,更添波浪向人间。 
  6 */
  7 #include <iostream>
  8 #include <cstdio>
  9 #include <algorithm>
 10 #include <cstring>
 11 #include <vector>
 12 #include <utility>
 13 #include <iomanip>
 14 #include <string>
 15 #include <cmath>
 16 #include <queue>
 17 #include <assert.h>
 18 #include <map>
 19 #include <ctime>
 20 #include <cstdlib>
 21 #include <stack>
 22 #define LOCAL
 23 const int INF = 0x7fffffff;
 24 const int MAXN = 30000  + 10;
 25 const int maxnode = 1000000;
 26 const int maxm= 30000 * 2 + 10;
 27 using namespace std;
 28 
 29 struct Link_Cut_Tree{
 30        struct Node{
 31               int val, sum, Max;//用来debug 
 32               int delta;
 33               Node *parent;
 34               Node *ch[2];
 35        }node[MAXN], *cur, _null, *null;
 36        Node *tmp[MAXN];
 37        
 38        bool is_root(Node* t){//判断是否是splay的根 
 39             if (t == null || (t->parent->ch[0] != t && t->parent->ch[1] != t)) return 1;
 40             return 0;
 41        }
 42        //本来以为不要pushdown了,结果还是要 
 43        void pushdown(Node *t){
 44             if (t == null) return;
 45             if (t->delta){
 46                t->delta = 0;
 47                if (t->ch[0] != null) t->ch[0]->delta ^= 1;
 48                if (t->ch[1] != null) t->ch[1]->delta ^= 1;
 49                swap(t->ch[0], t->ch[1]); 
 50             } 
 51        }
 52        //更新 
 53        void update(Node *t){
 54             if (t == null) return;
 55             t->sum = t->val;
 56             if (t->ch[0] != null) t->sum += t->ch[0]->sum;
 57             if (t->ch[1] != null) t->sum += t->ch[1]->sum;
 58             t->Max = max(t->val, max(t->ch[0]->Max, t->ch[1]->Max));
 59             return;
 60        }
 61        void init(){
 62             null = &_null;
 63             _null.val = _null.sum = _null.Max = -INF;
 64             _null.delta = 0;
 65             _null.parent = _null.ch[0] = _null.ch[1] = null;
 66             
 67             cur = node + 1;
 68        }
 69        Node* NEW(int val){
 70             cur->parent = cur->ch[0] = cur->ch[1] = null;
 71             cur->val = cur->Max = cur->sum = val;
 72             cur->delta = 0;
 73             return cur++;
 74        } 
 75        void rotate(Node *t, int d){
 76             if (is_root(t)) return;
 77             Node *p = t->parent;
 78             p->ch[d ^ 1] = t->ch[d];
 79             if (t->ch[d] != null) t->ch[d]->parent = p;
 80             t->parent = p->parent;
 81             if (p != null){
 82                if (p->parent->ch[0] == p) p->parent->ch[0] = t;
 83                else if (p->parent->ch[1] == p) p->parent->ch[1] = t;
 84             }
 85             t->ch[d] = p;
 86             p->parent = t;
 87             update(t);
 88             update(p);//真逗,什么都不要 
 89        } 
 90        //将t旋转到根 
 91        void splay(Node *t){
 92             //标记下传 
 93             int cnt = 1;
 94             tmp[0] = t;
 95             for (Node *y = t; !is_root(y); y = y->parent) tmp[cnt++] = y->parent;
 96             while (cnt) pushdown(tmp[--cnt]);
 97             
 98             while (!is_root(t)){
 99                   Node *y = t->parent;
100                   if (is_root(y)) rotate(t, (y->ch[0] == t));
101                   else {
102                        int d = (y->parent->ch[0] == y);
103                        if (y->ch[d] == t) rotate(t, d ^ 1);
104                        else rotate(y, d);
105                        rotate(t, d);
106                   }
107                   update(t);
108             }
109             update(t);
110        }
111        Node* access(Node *t){
112             Node *p = null;
113             while (t != null){
114                   splay(t);
115                   if (p != null) p->parent = t;
116                   t->ch[1] = p;
117                   update(t);
118                   p = t;
119                   t = t->parent;
120             }
121             return p;
122        }
123        //合并u,v所在的树 
124        void merge(Node *u, Node *v){
125             access(u);
126             splay(u);
127             u->delta = 1;
128             u->parent = v;
129             return; 
130        }
131        void cut(Node *u, Node *v){
132             access(u)->delta ^= 1;
133             //splay(u);//转到顶上就切断v就可以了
134             access(v);
135             splay(v);
136             
137             v->ch[0]->parent = null;
138             v->ch[0] = null;
139             return;
140        }
141        //找根 
142        Node *find(Node *t){
143             access(t);
144             splay(t);
145             while (t->parent != null) t = t->parent;
146             return t; 
147        }
148        bool check(Node *u, Node *v){
149             while (u->parent != null) u = u->parent;
150             while (v->parent != null) v = v->parent;
151             return (u == v);
152        }
153        void change(Node *l, int x){
154             access(l);
155             splay(l);
156             l->val = x;
157             update(l);
158        }
159 }splay;
160 int n, m;
161 int u[MAXN] , v[MAXN];
162  
163 void init(){
164      splay.init();
165      scanf("%d", &n);
166      for (int i = 1; i < n; i++) scanf("%d%d", &u[i], &v[i]);
167      for (int i = 1; i <= n; i++){
168          int a;
169          scanf("%d", &a);
170          splay.NEW(a);
171      }
172      for (int i = 1; i < n; i++) 
173      splay.merge(splay.node + u[i], splay.node + v[i]);
174 }
175 void work(){
176      int m ;
177      scanf("%d", &m);
178      for (int i = 1; i <= m; i++){
179          char str[10];
180          scanf("%s", str);
181          if (str[0] == 'C'){
182             int l, x;
183             scanf("%d%d", &l, &x);//将l的值改为x
184             splay.change(splay.node + l, x);
185          }else if (str[0] == 'Q'){
186                int l, r;
187                scanf("%d%d", &l, &r);
188                if (str[1] == 'S'){
189                   splay.access(splay.node + l)->delta ^= 1;//换根 
190                   splay.access(splay.node + r);
191                   Link_Cut_Tree::Node *p = splay.find(splay.node + r);
192                   printf("%d\n", p->sum);
193                }else{//求最大值 
194                   splay.access(splay.node + l)->delta ^= 1;//换根 
195                   splay.access(splay.node + r);
196                   Link_Cut_Tree::Node *p = splay.find(splay.node + r);
197                   printf("%d\n", p->Max);
198                }
199          }
200      }
201 }
202 
203 int main (){
204     
205     init();
206     work();
207     return 0;
208 }
View Code

 

转载于:https://www.cnblogs.com/hoskey/p/4342812.html

传送带损坏与对象检测数据集 一、基础信息 • 数据集名称:传送带损坏与对象检测数据集 • 图片数量: 训练集:645张图片 验证集:185张图片 测试集:92张图片 总计:922张工业监控图片 • 训练集:645张图片 • 验证集:185张图片 • 测试集:92张图片 • 总计:922张工业监控图片 • 分类类别: Hole(孔洞):传送带表面的孔洞损坏。 Human(人类):工作区域中的人类,用于安全监控。 Other Objects(其他对象):非预期对象,可能引起故障。 Puncture(刺穿):传送带被刺穿的损坏。 Roller(滚筒):传送带滚筒部件。 Tear(撕裂):传送带撕裂损坏。 impact damage(冲击损坏):由于冲击导致的损坏。 patch work(修补工作):已修补的区域。 • Hole(孔洞):传送带表面的孔洞损坏。 • Human(人类):工作区域中的人类,用于安全监控。 • Other Objects(其他对象):非预期对象,可能引起故障。 • Puncture(刺穿):传送带被刺穿的损坏。 • Roller(滚筒):传送带滚筒部件。 • Tear(撕裂):传送带撕裂损坏。 • impact damage(冲击损坏):由于冲击导致的损坏。 • patch work(修补工作):已修补的区域。 • 标注格式:YOLO格式,包含边界框和类别标签,适用于目标检测任务。 • 数据格式:图像数据来源于工业监控系统,适用于计算机视觉分析。 二、适用场景 • 工业自动化检测系统开发:用于构建自动检测传送带损坏和异物的AI模型,实现实时监控和预防性维护,减少停机时间。 • 安全监控应用:识别人类和其他对象,提升工业环境的安全性,避免事故和人员伤害。 • 学术研究与创新:支持计算机视觉在制造业、物流和自动化领域的应用研究,促进AI技术与工业实践的融合。 • 教育与培训:可用于培训AI模型或作为工业工程和自动化教育的案例数据,帮助学习者理解实际应用场景。 三、数据集优势 • 多样化的类别覆盖:包含8个关键类别,涵盖多种损坏类型和对象,确保模型能够处理各种实际工业场景,提升泛化能力。 • 精准的标注质量:采用YOLO格式,边界框标注准确,由专业标注人员完成,保证数据可靠性和模型训练效果。 • 强大的任务适配性:兼容主流深度学习框架(如YOLO、TensorFlow、PyTorch),可直接用于目标检测任务,并支持扩展至其他视觉任务需求。 • 突出的工业价值:专注于工业传送带系统的实际需求,帮助提升生产效率、降低维护成本,并增强工作场所安全,具有较高的实际应用价值。
一、基础信息 • 数据集名称:垃圾废弃物目标检测数据集 • 图片数量: 训练集:1124张图片 验证集:375张图片 总计:1499张图片 • 训练集:1124张图片 • 验证集:375张图片 • 总计:1499张图片 • 分类类别:包含60多个垃圾和废弃物类别,如气溶胶、铝泡罩包装、电池、破碎玻璃、卡片泡罩包装、香烟、透明塑料瓶、瓦楞纸箱、薯片袋、一次性食品容器、一次性塑料杯、饮料罐、饮料纸盒、鸡蛋盒、泡沫杯、泡沫食品容器、食品罐、食物垃圾、垃圾袋、玻璃瓶、玻璃杯、玻璃罐、杂志纸、餐盒、金属瓶盖、金属盖、普通纸、其他纸箱、其他塑料、其他塑料瓶、其他塑料容器、其他塑料杯、其他塑料包装、纸袋、纸杯、纸吸管、披萨盒、塑料瓶盖、塑料薄膜、塑料手套、塑料盖、塑料吸管、塑料餐具、聚丙烯袋、拉环、绳子、废金属、鞋子、一次性购物袋、六罐环、涂抹管、可挤压管、泡沫塑料片、纸巾、厕纸管、特百惠、未标记垃圾、包装纸等。 • 标注格式:YOLO格式,包含边界框和类别标签,适用于目标检测任务。 • 数据格式:图片来源于实际场景,细节清晰。 二、适用场景 • 垃圾自动分类系统开发:数据集支持目标检测任务,帮助构建能够自动识别和分类垃圾物品的AI模型,用于智能垃圾桶或回收系统,提升废弃物管理效率。 • 环保应用研发:集成至环保和废弃物管理应用,提供实时垃圾识别功能,促进回收和环境保护,支持可持续发展倡议。 • 学术研究与创新:支持计算机视觉与环保领域的交叉研究,助力发表垃圾识别和AI技术相关学术论文,推动技术创新。 • 教育与培训:可用于学校或培训机构,作为垃圾分类和AI目标检测教学的重要资源,培养环保意识和技术能力。 三、数据集优势 • 精准标注与多样性:每张图片经过准确标注,确保边界框定位精确;包含多种垃圾类别,覆盖常见废弃物,提升模型的泛化能力和鲁棒性。 • 任务适配性强:标注兼容主流深度学习框架(如YOLO等),可直接用于目标检测任务,并支持扩展到其他视觉任务,如分类或分割。 • 实际应用价值:专注于垃圾识别,为环保、废弃物管理和回收提供重要数据支撑,有助于减少污染和促进循环经济。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值