【dfs序】【二分】【主席树】【分块】bzoj3351 [ioi2009]Regions
http://dzy493941464.sinaapp.com/archives/96
那个SIZE貌似必须设成R*R/Q?不知为啥,自己算的不是这个的说。
本机AC,线上TLE。
#include<cstdio>
#include<set>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
int f,C;
void R(int &x){
C=0;f=1;
for(;C<'0'||C>'9';C=getchar())if(C=='-')f=-1;
for(x=0;C>='0'&&C<='9';C=getchar())(x*=10)+=(C-'0');
x*=f;
}
void P(int x){
if(x<10)putchar(x+'0');
else{P(x/10);putchar(x%10+'0');}
}
typedef double db;
#define N 200001
vector<int>Over,Nodes[N],col_in_sub[N];
typedef vector<int>::iterator ER;
int v[N],next[N],first[N],en,sz;
void AddEdge(int U,int V)
{
v[++en]=V;
next[en]=first[U];
first[U]=en;
}
multiset<int>S;
int anss[701][701];
int n,K,m,a[N],Ls[N],Rs[N],pl[N];
int root[N],e;
struct Node{int v,lc,rc;}T[N*24];
void Insert(int pre,int cur,int p,int l,int r)
{
if(l==r)
{
T[cur].v=T[pre].v+1;
return;
}
int m=(l+r>>1);
if(p<=m)
{
T[cur].lc=++e; T[cur].rc=T[pre].rc;
Insert(T[pre].lc,T[cur].lc,p,l,m);
}
else
{
T[cur].rc=++e; T[cur].lc=T[pre].lc;
Insert(T[pre].rc,T[cur].rc,p,m+1,r);
}
T[cur].v=T[T[cur].lc].v+T[T[cur].rc].v;
}
int Query(int cur,int p,int l,int r)
{
if(l==r) return T[cur].v;
int m=(l+r>>1);
if(p<=m) return Query(T[cur].lc,p,l,m);
else return Query(T[cur].rc,p,m+1,r);
}
int fa[N],id[N];
void dfs(int U)
{
root[U]=++e;
Insert(root[fa[U]],root[U],a[U],1,K);
Ls[U]=++en;
if(pl[a[U]]>sz)
for(ER it=Over.begin();it!=Over.end();++it)
anss[id[*it]][id[a[U]]]+=S.count(*it);
S.insert(a[U]);
for(int i=first[U];i;i=next[i])
dfs(v[i]);
Rs[U]=en;
S.erase(S.find(a[U]));
}
int main()
{
int x,y;
R(n); R(K); R(m);
sz=K*K/m;
R(a[1]); ++pl[a[1]];
for(int i=2;i<=n;++i)
{
R(x); R(a[i]);
fa[i]=x;
++pl[a[i]];
AddEdge(x,i);
}
en=0;
for(int i=1;i<=n;++i)
{
if(pl[i]>sz)
{
if(!id[i]) id[i]=++en;
Over.push_back(i);
}
if(pl[a[i]]<=sz) Nodes[a[i]].push_back(i);
}
en=0; dfs(1);
for(int i=1;i<=n;++i) col_in_sub[a[i]].push_back(Ls[i]);
for(int i=1;i<=n;++i) sort(col_in_sub[i].begin(),col_in_sub[i].end());
for(;m;--m)
{
R(x); R(y);
if(pl[x]>sz&&pl[y]>sz) P(anss[id[x]][id[y]]),puts("");
else if(pl[x]<=sz)
{
int ans=0;
for(ER it=Nodes[x].begin();it!=Nodes[x].end();++it) if(Ls[*it]!=Rs[*it])
ans+=(upper_bound(col_in_sub[y].begin(),col_in_sub[y].end(),Rs[*it])-
lower_bound(col_in_sub[y].begin(),col_in_sub[y].end(),Ls[*it]+1));
P(ans); puts("");
}
else
{
int ans=0;
for(ER it=Nodes[y].begin();it!=Nodes[y].end();++it)
ans+=Query(root[fa[*it]],x,1,K);
P(ans); puts("");
}
}
return 0;
}
【dfs序】【二分】【主席树】【分块】bzoj3351 [ioi2009]Regions的更多相关文章
- 【BZOJ 4556】[Tjoi2016&Heoi2016]字符串 SAM+二分+主席树
这道题市面上就两种法:一种是SA+二分+主席树,一种是SAM+二分+主席树(有不少人打线段树合并???)(除此之外还有一种利用炒鸡水的数据的暴力SA,贼快.....)(当时学SA的时候没做这道题,现在 ...
- BZOJ2653 middle 【二分 + 主席树】
题目 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个 长度为n的序列s.回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[c ...
- 洛谷P4559 [JSOI2018]列队 【70分二分 + 主席树】
题目链接 洛谷P4559 题解 只会做\(70\)分的\(O(nlog^2n)\) 如果本来就在区间内的人是不用动的,区间右边的人往区间最右的那些空位跑,区间左边的人往区间最左的那些空位跑 找到这些空 ...
- bzoj3306: 树(dfs序+倍增+线段树)
比较傻逼的一道题... 显然求子树最小值就是求出dfs序用线段树维护嘛 换根的时候树的形态不会改变,所以我们可以根据相对于根的位置分类讨论. 如果询问的x是根就直接输出整棵树的最小值. 如果询问的x是 ...
- P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]
题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...
- [bzoj2653][middle] (二分 + 主席树)
Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...
- LOJ#3097 [SNOI2019]通信 最小费用最大流+cdq分治/主席树/分块优化建图
瞎扯 我们网络流模拟赛(其实是数据结构模拟赛)的T2. 考场上写主席树写自闭了,直接交了\(80pts\)的暴力,考完出来突然发现: woc这个题一个cdq几行就搞定了! 题意简述 有\(n\)个哨站 ...
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)
思路: dfs序其实是很水的东西. 和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...
- Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树
E. Sign on Fence Bizon the Champion has recently finished painting his wood fence. The fence consi ...
- bzoj 3744: Gty的妹子序列 主席树+分块
3744: Gty的妹子序列 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 101 Solved: 34[Submit][Status] Descr ...
随机推荐
- BZOJ1202:狡猾的商人(带权并查集)
1202: [HNOI2005]狡猾的商人 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1202 Description: 刁姹接到一个 ...
- 获得edittext的图片大小
1.在布局文件中编写控件,有2张图片 <EditText android:id="@+id/edit" android:background="@drawable/ ...
- Patch Windows with SSM on AWS
ec2ssmupdate https://docs.amazonaws.cn/systems-manager/latest/userguide/systems-manager-patch.htmlht ...
- 修改select样式,vue select
<style> .selectbox{ width: 200px; display: inline-block; overflow-x: hidden; height: 28px; lin ...
- SpringMVC学习 -- @RequestParam , @RequestHeader , @CookieValue 的使用
使用 @RequestParam 绑定请求参数值: value:参数名 , 仅有一个 value 属性时 , value 可以省略不写. required:是否必须.默认为 true , 表示请求参数 ...
- Nim博弈(nim游戏)
http://blog.csdn.net/qiankun1993/article/details/6765688 NIM 游戏 重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P- ...
- Liberty中应用的contextroot
参考:http://www-01.ibm.com/support/knowledgecenter/api/content/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ ...
- 【Foreign】冒泡排序 [暴力]
冒泡排序 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 4 5 ...
- 【洛谷 P2485】 [SDOI2011]计算器 (BSGS)
题目链接 第一问:快速幂 第二问:扩欧解线性同余方程 第三问:\(BSGS\) 三个模板 #include <cstdio> #include <cmath> #include ...
- hdu 1509 Windows Message Queue (优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1509 题目大意:每一次输入都有序号和优先级,优先级小的先输出,优先级相同的话则序号小的先输出!第一次用 ...