博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P3038 [USACO11DEC]牧草种植Grass Planting
阅读量:4331 次
发布时间:2019-06-07

本文共 4143 字,大约阅读时间需要 13 分钟。

                               

题目描述

Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional roads, such that there is exactly one path between any two pastures. Bessie, a cow who loves her grazing time, often complains about how there is no grass on the roads between pastures. Farmer John loves Bessie very much, and today he is finally going to plant grass on the roads. He will do so using a procedure consisting of M steps (1 <= M <= 100,000).

At each step one of two things will happen:

  • FJ will choose two pastures, and plant a patch of grass along each road in between the two pastures, or,

  • Bessie will ask about how many patches of grass on a particular road, and Farmer John must answer her question.

Farmer John is a very poor counter -- help him answer Bessie's questions!

给出一棵n个节点的树,有m个操作,操作为将一条路径上的边权加一或询问某条边的权值。

输入输出格式

输入格式:

 

  • Line 1: Two space-separated integers N and M

  • Lines 2..N: Two space-separated integers describing the endpoints of a road.

  • Lines N+1..N+M: Line i+1 describes step i. The first character of the line is either P or Q, which describes whether or not FJ is planting grass or simply querying. This is followed by two space-separated integers A_i and B_i (1 <= A_i, B_i <= N) which describe FJ's action or query.

 

输出格式:

 

  • Lines 1..???: Each line has the answer to a query, appearing in the same order as the queries appear in the input.

 

输入输出样例

输入样例#1:
4 6 1 4 2 4 3 4 P 2 3 P 1 3 Q 3 4 P 1 4 Q 2 4 Q 1 4
输出样例#1:
2 1 2

 

树剖。。

1 #include 
2 #include
3 #include
4 5 const int MAXN=100010; 6 7 int n,m,inr; 8 9 int dfn[MAXN],dep[MAXN],id[MAXN],fa[MAXN]; 10 int top[MAXN],son[MAXN],siz[MAXN],rank[MAXN]; 11 12 struct SegmentTree { 13 int l,r; 14 int tag; 15 int sum; 16 }; 17 SegmentTree t[MAXN<<2]; 18 19 struct node { 20 int to; 21 int next; 22 }; 23 node e[MAXN<<1]; 24 25 int head[MAXN],tot; 26 27 inline void read(int&x) { 28 int f=1;register char c=getchar(); 29 for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar()); 30 for(;isdigit(c);x=x*10+c-48,c=getchar()); 31 x=x*f; 32 } 33 34 inline void add(int x,int y) { 35 e[++tot].to=y; 36 e[tot].next=head[x]; 37 head[x]=tot; 38 } 39 40 void Dfs_1(int now,int f) { 41 dep[now]=dep[f]+1; 42 siz[now]=1; 43 fa[now]=f; 44 for(int i=head[now];i;i=e[i].next) { 45 int to=e[i].to; 46 if(to==f) continue; 47 Dfs_1(to,now); 48 siz[now]+=siz[to]; 49 if(son[now]==-1||siz[son[now]]
>1; 85 build_tree(now<<1,l,mid); 86 build_tree(now<<1|1,mid+1,r); 87 } 88 89 void modify(int now,int l,int r) { 90 if(l<=t[now].l&&r>=t[now].r) { 91 ++t[now].tag; 92 t[now].sum+=t[now].r-t[now].l+1; 93 return; 94 } 95 if(t[now].tag) down(now); 96 int mid=(t[now].l+t[now].r)>>1; 97 if(l<=mid) modify(now<<1,l,r); 98 if(r>mid) modify(now<<1|1,l,r); 99 t[now].sum=t[now<<1].sum+t[now<<1|1].sum;100 }101 102 int query(int now,int l,int r) {103 int ans=0;104 if(l<=t[now].l&&r>=t[now].r) return t[now].sum;105 if(t[now].tag) down(now);106 int mid=(t[now].l+t[now].r)>>1;107 if(l<=mid) ans+=query(now<<1,l,r);108 if(r>mid) ans+=query(now<<1|1,l,r);109 return ans;110 }111 112 inline void Pre_query(char c,int x,int y) {113 int ans=0;114 while(top[x]!=top[y]) {115 if(dep[top[x]]
dep[y]) swap(x,y);121 if(c=='P') modify(1,id[x]+1,id[y]);122 else ans+=query(1,id[x]+1,id[y]),printf("%d\n",ans);123 return;124 }125 126 int hh() {127 char s[5];128 read(n);read(m);129 for(int i=1;i<=n;++i) son[i]=-1;130 int t=n-1;131 for(int x,y;t--;) {132 read(x);read(y);133 add(x,y);add(y,x);134 }135 Dfs_1(1,0);136 Dfs_2(1,1);137 build_tree(1,1,inr);138 for(int x,y;m--;) {139 scanf("%s",s);read(x);read(y);140 Pre_query(s[0],x,y);141 }142 return 0;143 }144 145 int sb=hh();146 int main() {;}
代码

 

转载于:https://www.cnblogs.com/whistle13326/p/7434537.html

你可能感兴趣的文章
KnockoutJs学习笔记(十一)
查看>>
访问修饰符public、private、protect、default范围
查看>>
jQuery实现布局高宽自适应
查看>>
《学习》10函数的建立与使用
查看>>
去除TB二合一页面弹窗
查看>>
算法第四章实践报告
查看>>
牛客练习赛29 B
查看>>
数字校园项目-学生失联预警系统(三)----数据库设计
查看>>
C# 6.0部分新特性
查看>>
Docker命令之 exec
查看>>
centos yum源配置 与yum配置文件
查看>>
Spring核心技术之IOC容器(一):IOC容器与Bean简介
查看>>
c# 文件IO操作 StreamReader StreamWriter Split 使用
查看>>
The Best Hacking Tools
查看>>
清结算系统的一些思考
查看>>
图的深度优先遍历和广度优先遍历
查看>>
vuex实践之路——笔记本应用(三)
查看>>
Ubuntu中Vim使用技巧
查看>>
Angular cli 发布自定义组件
查看>>
jstl标签的用法 fn标签
查看>>