codeforcesRound378C-dfs+树状数组
分成K个块,每个块内部dfs解决,然后用树状数组统计第i个元素前面有多少怪物已经消失,来计算当前的下标
#include<bits/stdc++.h> #define inf 0x3f3f3f3f const int maxn=; using namespace std; typedef pair<int,char> P; vector<P> G[maxn+]; int n; int k; int a[maxn+]; int b[maxn+]; int c[maxn+]; int d[maxn+]; int si[maxn+]; int lowbit(int x){
return x&(-x);
} void add(int x,int val){
while(x<=n){
d[x]+=val;
x+=lowbit(x);
}
} int sum(int x){
int res=;
while(x){
res+=d[x];
x-=lowbit(x);
}
return res;
} int flag; int solve(int x,int y,int s){
// printf("%d\n",s);
if(s==b[x]){
return ;
}
//printf("%d %d\n",y,s);
for(int i=y+;i<=c[x];i++){
if(a[i]==inf) continue;
if(a[i]!=a[y]){
//printf("%d %d\n",i,y);
if(a[y]>a[i]){
// printf("%d %d\n",y,i);
int temp=a[i];
int temp1=a[y];
a[i]+=a[y];
a[y]=inf;
add(y,);
//printf("%d %d %d\n",y,y-sum(y)+1,sum(y));
G[x].push_back(P(y-sum(y)+,'R'));
//printf("%d %d %d %d\n",x,i,y,s+temp);
if(solve(x,i,s+temp)){
// printf("dsasd\n");
return ;
} else {
a[i]=temp;
a[y]=temp1;
add(y,-);
G[x].erase(G[x].end()-);
return ;
}
}
else {
// printf("%d %d\n",i,y);
int temp=a[i];
int temp1=a[y];
a[y]+=a[i];
a[i]=inf;
add(i,);
G[x].push_back(P(i-sum(i)+,'L'));
// printf("%d %d\n",y,s+temp);
if(solve(x,y,s+temp)){
return ;
} else {
a[i]=temp;
a[y]=temp1;
add(i,-);
G[x].erase(G[x].end()-);
return ;
}
} } else break;
}
for(int i=y-;i>c[x-];i--){
if(a[i]==inf) continue;
// printf("%d %d\n",i,y);
if(a[i]!=a[y]){
if(a[y]>a[i]){
int temp=a[i];
int temp1=a[y];
a[i]+=a[y];
a[y]=inf;
add(y,);
G[x].push_back(P(y-sum(y)+,'L'));
if(solve(x,i,s+temp)){
return ;
} else {
a[i]=temp;
a[y]=temp1;
add(y,-);
G[x].erase(G[x].end()-);
return ;
}
}
else {
int temp=a[i];
int temp1=a[y];
a[y]+=a[i];
a[i]=inf;
add(i,);
G[x].push_back(P(i-sum(i)+,'R'));
if(solve(x,y,s+temp)){
return ;
} else {
a[i]=temp;
a[y]=temp1;
add(i,-);
G[x].erase(G[x].end()-);
return ;
}
} } else break;
}
for(int i=c[x-]+;i<=c[x];i++){
if(a[i]!=inf&&i!=y&&a[i]!=a[y]){
//printf("%d %d\n",i,s);
if(solve(x,i,a[i])) return ;
}
}
return ;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&k);
for(int i=;i<=k;i++){
scanf("%d",&b[i]);
}
int cnt=;
int temp=;
for(int i=;i<=n;i++){
if(temp<b[cnt]){
temp+=a[i];
si[cnt]++;
}
if(temp==b[cnt]){
c[cnt]=i;
cnt++;
temp=;
} else if(temp>b[cnt]) {
flag=;
break;
}
}
if(flag||cnt!=k+){
printf("NO\n");
} else {
for(int i=;i<=k;i++){
if(si[i]==) continue;
int f=;
for(int j=c[i-]+;j<c[i];j++){
if(a[j]!=a[j+]){
if(solve(i,j,a[j])){
f=;
break;
} else {
f=;
break;
}
}
}
if(!f){
if(solve(i,c[i],a[c[i]])){
f=;
}
}
if(!f){
printf("NO\n");
return ;
}
}
printf("YES\n");
for(int i=;i<=k;i++){
for(int j=;j<G[i].size();j++){
printf("%d %c\n",G[i][j].first,G[i][j].second);
}
}
}
return ;
}
codeforcesRound378C-dfs+树状数组的更多相关文章
- Weak Pair (dfs+树状数组)
Weak Pair (dfs+树状数组) 题意 这个题目是要求:一颗树上,有n个节点,给出每个节点的权值.另外给出一个值k,问有多少对节点满足: \(power[u]*power[v]<=k\) ...
- codeforces 1076E Vasya and a Tree 【dfs+树状数组】
题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...
- hdu 5877 (dfs+树状数组) Weak Pair
题目:这里 题意: 给出一个n个结点的树和一个数k,每个结点都有一个权值,问有多少对点(u,v)满足u是v的祖先结点且二者的权值之积小于等于k. 从根结点开始dfs,假设搜的的点的权值是v,我们需要的 ...
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
- POJ 3321 Apple Tree (DFS + 树状数组)
题意: 一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果. 现在有两种操作: 1.某个分叉上的苹果从有变无或者从无边有. 2.需要统计以某个分叉为根节点时,它的 ...
- hdu_5877_Weak Pair(离散+DFS+树状数组)
题目链接:hdu_5877_Weak Pair 题意: 给你一棵树,让你找有多少对满足那两个条件的weak pair 题解: 有人用Treap,我不会,然后我用树状数组+离散来替代Treap,用DFS ...
- HDU - 5877 Weak Pair (dfs+树状数组)
题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...
- HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V ...
- CF E. Vasya and a Tree】 dfs+树状数组(给你一棵n个节点的树,每个点有一个权值,初始全为0,m次操作,每次三个数(v, d, x)表示只考虑以v为根的子树,将所有与v点距离小于等于d的点权值全部加上x,求所有操作完毕后,所有节点的值)
题意: 给你一棵n个节点的树,每个点有一个权值,初始全为0,m次操作,每次三个数(v, d, x)表示只考虑以v为根的子树,将所有与v点距离小于等于d的点权值全部加上x,求所有操作完毕后,所有节点的值 ...
- HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化
http://acm.hdu.edu.cn/listproblem.php?vol=49 给定一颗树,然后对于每一个节点,找到它的任何一个祖先u,如果num[u] * num[v] <= k.则 ...
随机推荐
- 查 101.201.62.30 IP信誉方法
查 101.201.62.30 IP信誉方法https://www.virustotal.com/#/ip-address/101.201.62.30https://talosintelligence ...
- Go丨语言学习笔记--switch
Java语言与Go语言的switch对比 Go语言 switch str { case "yes" : do something ... case "no" d ...
- 剑指OFFER18 判断一个二叉树的子树
public class a18_IsSubTree { public static boolean hasSubTree(TreeNode treeRoot1, TreeNode treeRoot2 ...
- 详解C/C++ 编译 g++ gcc 的区别
我们在编译c/c++代码的时候,有人用gcc,有人用g++,于是各种说法都来了,譬如c代码用gcc,而c++代码用g++, 或者说编译用gcc,链 接用g++,一时也不知哪个说法正确,如果再遇上个ex ...
- linux C++ scandir 的使用
() 头文件 #include <dirent.h> () 函数定义 int scandir(const char *dir,struct dirent **namelist,int (* ...
- MySQL-计算7月重新激活客户第二种方法_20161022
上周日休假去广西玩了一周,回来继续. (一)上次用的是取当月首单日期,往前推30天 代码比较繁琐 这次方法是借助变量 错行进行判断 一个用户上次最后一次下单时间和下次下单时间的时间差 也就是形成一个相 ...
- 关于qwerta
性别女 爱好男 有时喜欢装成男孩子混迹于OI圈. 就读于长沙市MD中学 是个剧毒蒻蒻蒻. 以 qwerta['kwɜ:rtɑ] 的ID混迹于各大OJ,但是在其它地方通常用qwertaya(重名率太高了 ...
- Linux命令汇总(二)
1.登录用户设置 新创建了一个用户,用useradd指令,但是发现通过终端无法登陆: echo password | passwd --stdin username 或者 passwd --st ...
- POJ2182:Lost Cows
浅谈线段树和树状数组:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:http://poj.org/problem?id=2182 线段树,倒着确 ...
- 如何使用代码美化器Uncrustify (How to use code beautifier Uncrustify)
1.下载NodePad++, 2.选择菜单Plugins/Plugin Manager/Show Plugin Manager 3.勾选UniversalIndentGUI,点击Install 4.下 ...