非常无奈,模板重新无奈的打错了。。

只是,非常快便找到了。。

题意:给一些边,有一些操作,每次操作,都要在这些边上加上1,求每一个边的边权。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson id << 1
#define rson id << 1|1
const int M = 100008;
int top[M],siz[M],son[M],father[M],ti[M],dep[M];
int e[M][2],idx,tp;
struct {
int head;
}H[M];
struct{
int v,next;
}E[M*2];
void init(){
memset(H,-1,sizeof(H));
memset(E,-1,sizeof(E));
tp = 0,idx = 0;
}
void add(int u,int v){
E[tp].v =v;
E[tp].next = H[u].head;
H[u].head = tp++;
}
void dfs_1(int u,int fa){
son[u] = 0;siz[u] = 1;father[u] = fa;dep[u] = dep[fa] + 1;
for(int i=H[u].head;i!=-1;i=E[i].next){
int v = E[i].v;
if(v == fa)continue;
dfs_1(v,u);
siz[u] += siz[v];
if(siz[v] > siz[son[u]])son[u] = v;
}
}
void dfs_2(int u,int fa){
ti[u] = idx++;
top[u] = fa;
if(son[u])dfs_2(son[u],fa);
for(int i=H[u].head;i!=-1;i=E[i].next){
int v = E[i].v;
if(v == father[u]|| v == son[u])continue;
dfs_2(v,v);
}
}
struct M_tree{
int mark,l,r;
int mid(){
return (l+r) /2;
}
}node[M*4];
void build_tree(int id,int l,int r){
node[id].l = l;node[id].r = r;node[id].mark = 0; if(l == r)return;
int mid = node[id].mid();
build_tree(lson,l,mid);
build_tree(rson,mid+1,r);
}
void push_down(int id){
if(node[id].l == node[id].r)return;
if(node[id].mark){
node[lson].mark += node[id].mark ;
node[rson].mark += node[id].mark ;
node[id].mark = 0;
}
} void nede(int id,int l,int r){ if(node[id].l == l&&node[id].r == r){ node[id].mark += 1;
return;
}
push_down(id);
int mid = node[id].mid();
if(r <=mid)nede(lson,l,r);
else if(l > mid)nede(rson,l,r);
else{
nede(lson,l,mid);
nede(rson,mid+1,r);
}
}
int ans = 0;
int query(int id,int r){ if( node[id].l == r&&node[id].r == r)return node[id].mark;
push_down(id);
int mid= node[id].mid();
if(r <=mid)return query(lson,r);
else return query(rson,r);
}
void negat(int u,int v){
int f1 = top[u];
int f2 = top[v]; while(f1 != f2){
if(dep[f1] < dep[f2]){
swap(f1,f2);
swap(u,v);
}//cout << ti[f1] << "<---->" << ti[u]<<endl;
nede(1,ti[f1],ti[u]);
u = father[f1];f1 = top[u];
}
if(u == v)return;
if(dep[u] > dep[v])swap(u,v);
nede(1,ti[son[u]],ti[v]);
}
int main()
{
int u,v,n,m;
while(~scanf("%d",&n)){
init();
for(int i=0;i<n-1;i++){
scanf("%d%d",&e[i][0],&e[i][1]);
add(e[i][0],e[i][1]);
add(e[i][1],e[i][0]); }
dfs_1(1,1);
dfs_2(1,1);
build_tree(1,0,idx-1);
//cout <<idx<<endl;
scanf("%d",&m);
while(m--){
scanf("%d%d",&u,&v); negat(u,v);
}
for(int i=0;i<n-1;i++)
{
if(dep[e[i][0]] > dep[e[i][1]]){
swap(e[i][0],e[i][1]);
}
}
for(int i=0;i<n-1;i++){
printf("%d",query(1,ti[e[i][1]]));
if(i!=n-2)printf(" ");
}
printf("\n");
}
}

CodeForces 191C 树链剖分 第4遍的更多相关文章

  1. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  2. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  3. CodeForces - 343D 树链剖分

    题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的 ...

  4. Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)

    题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...

  5. Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分

    D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...

  6. 【Codeforces】【网络流】【树链剖分】【线段树】ALT (CodeForces - 786E)

    题意 现在有m个人,每一个人都特别喜欢狗.另外还有一棵n个节点的树. 现在每个人都想要从树上的某个节点走到另外一个节点,且满足要么这个人自带一条狗m,要么他经过的所有边h上都有一条狗. 2<=n ...

  7. Codeforces 704E Iron Man [树链剖分,计算几何]

    Codeforces 这题--真是搞死我了-- 好不容易下定了决心要不颓废,要写题,结果一调就调了十几个小时-- 思路 我们发现在树上做非常不舒服,于是树链剖分之后一次在重链上的移动就可以看做是在df ...

  8. D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】

    Codeforces Round #329 (Div. 2) D. Happy Tree Party time limit per test 3 seconds memory limit per te ...

  9. Codeforces 343D Water Tree & 树链剖分教程

    原题链接 题目大意 给定一棵根为1,初始时所有节点值为0的树,进行以下三个操作: 将以某点为根的子树节点值都变为1 将某个节点及其祖先的值都变为0 *询问某个节点的值 解题思路 这是一道裸的树链剖分题 ...

随机推荐

  1. HDU 1157 Who's in the Middle

    #include <cstdio> #include <algorithm> using namespace std; int main() { int n; while(sc ...

  2. C++模板:qsort

    void qsort(int l,int r){ int i,j,t,mid; mid=b[(l+r)>>1]; i=l; j=r; do{ while (b[i]<mid) i++ ...

  3. protubuf在cocos2dx的应用安装

    将protubuf放置在external文件夹 在vs工程里的luacocos2d里新建筛选器protubuf,将pb.c加入到该筛选器下. 将pb.c文件的属性里设置改文件编译为C文件. 将luas ...

  4. QQ聊天原理初识

    1:qq之间文件的传输是通过p2p通信进行的. 2:qq之间的表情发送实际上就是文字的发送,是client再接受到文字之后在本地自己进行转换 3:qq之间的通信既能够通过udp也能够通过Tcp 尽管u ...

  5. RGB与HSB之间的转换公式

    先来了解一些概念: 1.RGB是一种加色模型,就是将不同比例的Red/Green/Blue混合在一起得到新颜色.通常RGB颜色模型表示为: 2.HSB(HSV) 通过色相/饱和度/亮度三要素来表达颜色 ...

  6. DontDestroyOnLoad(Unity3D开发之五)

    Unity中我们从A场景切换到B场景的时候,A场景全部对象都会销毁,但有时候我不须要销毁某些东西. 比方一个简单的游戏的背景音乐,我不须要多次反复创建,多个场景播放这一个即可了.这个时候就须要用到Do ...

  7. UC/0S2之基础总结

    堆栈,就是在存储器中按数据“后进先出(LIFO)[类比杯子]”的原则组织的连续存储空间,为了满足任务切换和响应中断保存CPU寄存器中的内容及存储任务私有数据的需要,每个任务都应该配有自己的堆栈, 注意 ...

  8. sql日期转换格式

    Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE( ...

  9. HTML之表格与表单

    一.表格(table/tr/td) <table></table>表格 --不设高度 width:宽度.可以用像素或百分比表示. 常用960像素. border:边框,常用值为 ...

  10. html 7大知识点

    HTML是web前端开发的基础,学习前端的人都是先从html学起的. 关于HTML有一些必备的知识点,这些知识点都是HTML中最基本的内容,也是前端面试最常问的知识点. 1.网页结构网页结构一般都包含 ...