码迷,mamicode.com
首页 > 其他好文 > 详细

P3038 [USACO11DEC]牧草种植Grass Planting

时间:2017-08-26 11:39:13      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:dep   open   hat   div   oid   reg   alt   counter   git   

                               P3038 [USACO11DEC]牧草种植Grass Planting

题目描述

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 <ctype.h>
  2 #include <cstdio>
  3 #include <queue>
  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]]<siz[to]) son[now]=to;
 50     }
 51     return;
 52 }
 53 
 54 void Dfs_2(int now,int tp) {
 55     top[now]=tp;
 56     id[now]=++inr;
 57     rank[inr]=now;
 58     if(son[now]==-1) return;
 59     Dfs_2(son[now],tp);
 60     for(int i=head[now];i;i=e[i].next) {
 61         int to=e[i].to;
 62         if(to==son[now]||to==fa[now]) continue;
 63         Dfs_2(to,to);
 64     } 
 65     return;
 66 }
 67 
 68 inline void swap(int&x,int&y) {
 69     int t=x;x=y;y=t;
 70     return;
 71 }
 72 
 73 inline void down(int now) {
 74     t[now<<1].tag+=t[now].tag;
 75     t[now<<1].sum+=(t[now<<1].r-t[now<<1].l+1)*t[now].tag;
 76     t[now<<1|1].tag+=t[now].tag;
 77     t[now<<1|1].sum+=(t[now<<1|1].r-t[now<<1|1].l+1)*t[now].tag;
 78     t[now].tag=0; 
 79 }
 80 
 81 void build_tree(int now,int l,int r) {
 82     t[now].l=l;t[now].r=r;
 83     if(l==r) return;
 84     int mid=(l+r)>>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[top[y]]) swap(x,y);
116         if(c==P) modify(1,id[top[x]],id[x]);
117         else ans+=query(1,id[top[x]],id[x]);
118         x=fa[top[x]];
119     } 
120     if(dep[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() {;}
代码

 

P3038 [USACO11DEC]牧草种植Grass Planting

标签:dep   open   hat   div   oid   reg   alt   counter   git   

原文地址:http://www.cnblogs.com/whistle13326/p/7434537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!