From d8cf3bd62c61450715f535c02f321870ad704655 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sun, 4 Sep 2016 17:53:12 +0800 Subject: [PATCH 01/22] Create 1957.cpp --- POJ/1957.cpp | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 POJ/1957.cpp diff --git a/POJ/1957.cpp b/POJ/1957.cpp new file mode 100644 index 0000000..e80e4ac --- /dev/null +++ b/POJ/1957.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +using namespace std; +struct Point2D +{ + int x,y; +}; +int nextvec[6][2]= +{ + { + 1,0 + }, + { + 1,1 + }, + { + 0,1 + }, + { + -1,0 + }, + { + -1,-1 + }, + { + 0,-1 + } +}; + +char stra[1024]; +char strb[1024]; +char buff[1024]; +Point2D pa[2000]; +Point2D pb[2000]; + +bool cmp(const Point2D& a,const Point2D& b) +{ + if(a.x==b.x) + { + return a.y < b.y; + } + return a.x < b.x; +} + +int main() +{ + int N; + scanf("%d%*c",&N); + while(N--) + { + memset(pa,0,sizeof(pa)); + memset(pb,0,sizeof(pb)); + gets(stra); + gets(strb); + gets(buff); + int lena=strlen(stra); + int lenb=strlen(strb); + if(lena==0&&lenb==0) + { + printf("true\n"); + continue; + } + else if(lena!=lenb) + { + printf("false\n"); + continue; + } + int x=0; + int y=0; + for(int i=0;i Date: Tue, 6 Sep 2016 13:44:01 +0800 Subject: [PATCH 02/22] Create Readme.md --- UESTC/Readme.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 UESTC/Readme.md diff --git a/UESTC/Readme.md b/UESTC/Readme.md new file mode 100644 index 0000000..755889f --- /dev/null +++ b/UESTC/Readme.md @@ -0,0 +1,6 @@ +#University Of Electronic Science And Technology Of China +#中国电子科技大学 + +*UESTC OJ no longer exist. Submit on CDOJ or vjudge instead.* + +[View On VJudge](http://acm.hust.edu.cn/vjudge/problem/#OJId=UESTC_old&probNum=&title=&source= "Problems - Virtual Judge") From 205aa6491f14e1bb566a4439b83ca839921e3b58 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 6 Sep 2016 13:46:57 +0800 Subject: [PATCH 03/22] Create Readme.md --- CDOJ/Readme.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 CDOJ/Readme.md diff --git a/CDOJ/Readme.md b/CDOJ/Readme.md new file mode 100644 index 0000000..7350ed6 --- /dev/null +++ b/CDOJ/Readme.md @@ -0,0 +1,4 @@ +#CDOJ +*CDOJ is a UESTC Online Judge* + +[CDOJ Home Page](http://acm.uestc.edu.cn/ "Welcome to CDOJ") From 399b1b45266aeaeac6f1050c9ccec5f8ee8cf8ce Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 6 Sep 2016 14:05:27 +0800 Subject: [PATCH 04/22] Create 594_ka200812.cpp From http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html --- CDOJ/594_ka200812.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 CDOJ/594_ka200812.cpp diff --git a/CDOJ/594_ka200812.cpp b/CDOJ/594_ka200812.cpp new file mode 100644 index 0000000..f25789f --- /dev/null +++ b/CDOJ/594_ka200812.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +using namespace std; +#define inf 0xfffffff +#define min(a,b) ab?a:b + +int dp[2][101]; +int n,c; +int q[101]; +int head,tail,cur; + +int main() +{ + int i,j,x,nowf; + //freopen("D:\\in.txt","r",stdin); + while(scanf("%d%d",&n,&c)==2) + { + scanf("%d",&x); + cur=0; + for(i=0;inowf) + tail--; + q[tail++]=nowf; + if(j=0;j--) //当身高为j时候,队列里便已经保存了100~j+1的信息,正写反写是有技巧的 + { + nowf=dp[1-cur][j]+j*c; + while(headnowf) + tail--; + q[tail++]=nowf; + if(j>=x) + dp[cur][j]=min(dp[cur][j],q[head]-j*c+(x-j)*(x-j)); + } + } + int ans=inf; + for(i=0;i<=100;i++) + ans=min(ans,dp[cur][i]); + printf("%d\n",ans); + } + return 0; +} From 45029e9fe7f309b921a727b9612641b2ae0322b9 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 6 Sep 2016 14:34:55 +0800 Subject: [PATCH 05/22] Create 594.cpp --- CDOJ/594.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 CDOJ/594.cpp diff --git a/CDOJ/594.cpp b/CDOJ/594.cpp new file mode 100644 index 0000000..9310c91 --- /dev/null +++ b/CDOJ/594.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +using namespace std; + +#define MAXN 50100 +#define MAXH 110 +#define MAXVAL_H 100 +#define INF 0x3f3f3f3f + +int dp[2][MAXH]; +int bus[MAXH]; + +int main() +{ + int N,C; + while(scanf("%d %d",&N,&C)==2) + { + /// Main Loop + int x; + scanf("%d",&x); + int index=0; + ///由于人无法变矮,因此这部分的花费记为INF + for(int i=0;i 大(N) + for(int k=0;k<=MAXVAL_H;k++) + { + int cost=dp[!index][k]-k*C; + /// Pop Back + while(begin cost) end--; + /// Push Back + bus[end++]=cost; + if(k=0;k--) + { + int cost=dp[!index][k]+k*C; + /// Pop Back + while(begin cost) end--; + /// Push Back + bus[end++]=cost; + if(k>=x) + { + dp[index][k]=min(dp[index][k],bus[begin]-k*C+(k-x)*(k-x)); + } + } + } + int ans=INF; + for(int i=0;i<=MAXVAL_H;i++) + { + ans=min(dp[index][i],ans); + } + printf("%d\n",ans); + } + return 0; +} From 928583813994a2ef3255b3162c7fcb13485f5e1b Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 6 Sep 2016 14:35:20 +0800 Subject: [PATCH 06/22] Create 1685.cpp --- UESTC/1685.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 UESTC/1685.cpp diff --git a/UESTC/1685.cpp b/UESTC/1685.cpp new file mode 100644 index 0000000..9310c91 --- /dev/null +++ b/UESTC/1685.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +using namespace std; + +#define MAXN 50100 +#define MAXH 110 +#define MAXVAL_H 100 +#define INF 0x3f3f3f3f + +int dp[2][MAXH]; +int bus[MAXH]; + +int main() +{ + int N,C; + while(scanf("%d %d",&N,&C)==2) + { + /// Main Loop + int x; + scanf("%d",&x); + int index=0; + ///由于人无法变矮,因此这部分的花费记为INF + for(int i=0;i 大(N) + for(int k=0;k<=MAXVAL_H;k++) + { + int cost=dp[!index][k]-k*C; + /// Pop Back + while(begin cost) end--; + /// Push Back + bus[end++]=cost; + if(k=0;k--) + { + int cost=dp[!index][k]+k*C; + /// Pop Back + while(begin cost) end--; + /// Push Back + bus[end++]=cost; + if(k>=x) + { + dp[index][k]=min(dp[index][k],bus[begin]-k*C+(k-x)*(k-x)); + } + } + } + int ans=INF; + for(int i=0;i<=MAXVAL_H;i++) + { + ans=min(dp[index][i],ans); + } + printf("%d\n",ans); + } + return 0; +} From 847e513b43082f9147b7a185440cf9e6ec619a98 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 6 Sep 2016 14:46:00 +0800 Subject: [PATCH 07/22] Create 3401_acm_ted.cpp From http://blog.csdn.net/acm_ted/article/details/7909742 --- HDOJ/3401_acm_ted.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 HDOJ/3401_acm_ted.cpp diff --git a/HDOJ/3401_acm_ted.cpp b/HDOJ/3401_acm_ted.cpp new file mode 100644 index 0000000..5f288b3 --- /dev/null +++ b/HDOJ/3401_acm_ted.cpp @@ -0,0 +1,66 @@ +#include +#include +using namespace std; +#define MAX 2005 +#define inf 0xfffff +#define max(a,b) ((a)>(b)?(a):(b)) +int T,MaxP,W; +int APi[MAX],BPi[MAX],ASi[MAX],BSi[MAX]; +int dp[MAX][MAX];//dp[i][j]第i天持有j股的最大值 +//dp[i][j]=max{dp[i-1][j],max{dp[r][k]-APi[i]*(j-k)}(0j)} +struct node +{ + int x;//存dp[i-w-1][k]+APi[i]*k或dp[i-w-1][k]+BPi[i]*k + int p;//当前持股数 +} q[2005],temp; +int front,back; +int main() +{ + int cas; + scanf("%d",&cas); + for(; cas--;) + { + scanf("%d%d%d",&T,&MaxP,&W); + for(int i=1; i<=T; ++i) + scanf("%d%d%d%d",APi+i,BPi+i,ASi+i,BSi+i); + for(int i=0; i<=T; ++i) + for(int j=0; j<=MaxP; ++j) + dp[i][j]=-inf; + for(int i=1; i<=W+1; ++i) + for(int j=0; j<=ASi[i]; ++j) + dp[i][j]=(-APi[i]*j); + for(int i=2; i<=T; ++i) + { + for(int j=0; j<=MaxP; ++j) + dp[i][j]=max(dp[i][j],dp[i-1][j]); + if(i<=W+1) continue; + //买入 + front=back=1; + for(int j=0; j<=MaxP; ++j) + { + temp.p=j; + temp.x=dp[i-W-1][j]+APi[i]*j; + for(;front=0; --j) + { + temp.p=j; + temp.x=dp[i-W-1][j]+BPi[i]*j; + for(;frontj;++front); + dp[i][j]=max(dp[i][j],q[front].x-BPi[i]*j); + } + } + int ans=0; + for(int i=0;i<=MaxP;++i) + ans=max(ans,dp[T][i]); + printf("%d\n",ans); + } + return 0; +} From ed8377f94d06f8cfb0117255f7c49be5fb98398a Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sat, 10 Sep 2016 11:51:02 +0800 Subject: [PATCH 08/22] Use C++ instead of G++ From http://blog.csdn.net/alongela/article/details/8229659 --- POJ/2823_alongela.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 POJ/2823_alongela.cpp diff --git a/POJ/2823_alongela.cpp b/POJ/2823_alongela.cpp new file mode 100644 index 0000000..85369fa --- /dev/null +++ b/POJ/2823_alongela.cpp @@ -0,0 +1,79 @@ +#include +#include +using namespace std; + +const int N = 1000005; + +struct Elem +{ + int val; + int pos; +}; + +Elem maxque[N]; +Elem minque[N]; +int maxhead, minhead, maxtail, mintail; +int maxans[N]; +int minans[N]; +int cur; + +int main() +{ + int n, w, num; + scanf("%d%d", &n, &w); + minhead = mintail = 0; + maxhead = maxtail = 0; + cur = 0; + for (int i = 0; i < w; ++i) + { + scanf("%d", &num); + + while (minhead < mintail && minque[mintail - 1].val >= num) --mintail; + minque[mintail].val = num; + minque[mintail].pos = i; + ++mintail; + + while (maxhead < maxtail && maxque[maxtail - 1].val <= num) --maxtail; + maxque[maxtail].val = num; + maxque[maxtail].pos = i; + ++maxtail; + } + for (int i = w; i < n; ++i) + { + minans[cur] = minque[minhead].val; + maxans[cur] = maxque[maxhead].val; + ++cur; + + scanf("%d", &num); + + while (minhead < mintail && i - minque[minhead].pos >= w) ++minhead; + while (minhead < mintail && minque[mintail - 1].val >= num) --mintail; + minque[mintail].val = num; + minque[mintail].pos = i; + ++mintail; + + while (maxhead < maxtail && i - maxque[maxhead].pos >= w) ++maxhead; + while (maxhead < maxtail && maxque[maxtail - 1].val <= num) --maxtail; + maxque[maxtail].val = num; + maxque[maxtail].pos = i; + ++maxtail; + } + minans[cur] = minque[minhead].val; + maxans[cur] = maxque[maxhead].val; + ++cur; + + for (int i = 0; i < cur; ++i) + { + if (i > 0) putchar(' '); + printf("%d", minans[i]); + } + printf("\n"); + for (int i = 0; i < cur; ++i) + { + if (i > 0) putchar(' '); + printf("%d", maxans[i]); + } + printf("\n"); + + return 0; +} From 2ee5748f15652f2c752d5d4e81968b2d52416640 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sat, 10 Sep 2016 15:42:30 +0800 Subject: [PATCH 09/22] Create Readme.md --- SCU/Readme.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 SCU/Readme.md diff --git a/SCU/Readme.md b/SCU/Readme.md new file mode 100644 index 0000000..9708041 --- /dev/null +++ b/SCU/Readme.md @@ -0,0 +1,3 @@ +#Sichuan University Online Judge 四川大学 +[Home Page](http://acm.scu.edu.cn/soj/ "Sichuan University Online Judge") +[ACM Home Page](http://acm.scu.edu.cn/ "四川大学 - ACM首页") From dad8ad9359f4119747c0fa7dfab83772fb5d70f1 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sat, 10 Sep 2016 15:43:47 +0800 Subject: [PATCH 10/22] Create 4444_woshinannan741.cpp From http://blog.csdn.net/woshinannan741/article/details/52235990 --- SCU/4444_woshinannan741.cpp | 102 ++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 SCU/4444_woshinannan741.cpp diff --git a/SCU/4444_woshinannan741.cpp b/SCU/4444_woshinannan741.cpp new file mode 100644 index 0000000..e48985f --- /dev/null +++ b/SCU/4444_woshinannan741.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +using namespace std; +const int MAXN = 5e5+5; +const int INF = 0x3f3f3f3f; +typedef long long LL; +int head[MAXN],e; +LL dis[MAXN]; +bool vis[MAXN]; +struct Edge{ + int v,nxt,w; +}E[MAXN<<1]; +void init() +{ + e=0; + memset(head,-1,sizeof head); + memset(vis,false,sizeof vis); +} +void add(int u,int v,int w) +{ + E[e].v = v; + E[e].w = w; + E[e].nxt = head[u]; + head[u] = e++; +} +struct QNode{ + int v,c; + QNode(int _v,int _c){ + v = _v;c=_c; + } + bool operator < (const QNode &a) const{ + return c>a.c; + } +}; +LL dijkstra(int n) +{ + memset(dis,INF,sizeof dis); + priority_queueQ; + dis[1] = 0; + Q.push(QNode(1,0)); + while(!Q.empty()) + { + QNode tmp = Q.top();Q.pop(); + int u = tmp.v; + if(vis[u]) continue; + vis[u] = true; + for(int i=head[u];~i;i=E[i].nxt){ + int v = E[i].v; + int w = E[i].w; + if(vis[v]) continue; + if(dis[u]+wta,tb; + queueQ;Q.push(1); + dis[1] = 0,dis[n] = INF; + for(int i=2;i<=n;i++) ta.insert(i); + while(!Q.empty()) + { + int u = Q.front();Q.pop(); + for(int i=head[u];~i;i=E[i].nxt){ + int v = E[i].v; + if(!ta.count(v)) continue; + ta.erase(v);tb.insert(v); + } + for(set::iterator it=ta.begin();it!=ta.end();it++){ + Q.push(*it); + dis[*it] = dis[u] + val; + } + ta.swap(tb);tb.clear(); + } + return dis[n]; +} +int main() +{ + int N,M; + LL A,B; + int u,v; + while(~scanf("%d%d%lld%lld",&N,&M,&A,&B)) + { + init(); + bool flag = false; + for(int i=0;i Date: Sun, 11 Sep 2016 12:13:51 +0800 Subject: [PATCH 11/22] Create 115_jianrenfang.cpp From http://www.cnblogs.com/jianrenfang/p/5861344.html --- hihoCoder/Weekly/115_jianrenfang.cpp | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 hihoCoder/Weekly/115_jianrenfang.cpp diff --git a/hihoCoder/Weekly/115_jianrenfang.cpp b/hihoCoder/Weekly/115_jianrenfang.cpp new file mode 100644 index 0000000..0a10100 --- /dev/null +++ b/hihoCoder/Weekly/115_jianrenfang.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define inf 0x3f3f3f3f +#define mod 10000 +typedef long long ll; +using namespace std; +const int N=505; +const int M=100005; +int n,m,k,ans,t; +int pre[N]; +struct man +{ + int c,f; +}w[N][N]; +bool bfs() +{ + queueq; + q.push(1); + memset(pre,0,sizeof(pre)); + pre[1]=1; + while(!q.empty()){ + int u=q.front();q.pop(); + for(int i=1;i<=t;i++){ + if( !pre[i] && w[u][i].c>w[u][i].f){ + pre[i]=pre[u]+1; + q.push(i); + } + } + } + return pre[t]!=0; +} +int dfs(int u,int sum) +{ + if(u==t||sum==0)return sum; + int tmp=sum,minn; + for(int i=1;i<=t;i++){ + if(pre[i]==pre[u]+1&&w[u][i].c>w[u][i].f){ + minn=dfs(i,min(tmp,w[u][i].c-w[u][i].f)); + w[u][i].f+=minn; + w[i][u].f -=minn; + tmp-=minn; + } + } + return sum-tmp; +} +void Dinic() +{ + ans=0; + while(bfs())ans+=dfs(1,inf); + cout< Date: Mon, 12 Sep 2016 20:02:39 +0800 Subject: [PATCH 12/22] Create 4444_lvshubao1314.cpp From http://blog.csdn.net/lvshubao1314/article/details/48848319 --- SCU/4444_lvshubao1314.cpp | 112 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 SCU/4444_lvshubao1314.cpp diff --git a/SCU/4444_lvshubao1314.cpp b/SCU/4444_lvshubao1314.cpp new file mode 100644 index 0000000..ee328ed --- /dev/null +++ b/SCU/4444_lvshubao1314.cpp @@ -0,0 +1,112 @@ +/** +SCU - 4444 别样最短路径-大数据完全图 +题目大意:给定一个完全图,其中有两种边,长度为a(不超过5e5)或长度为b(剩下的),求有1~n的最短路径(数据范围1e5) +解题思路:如果1和n之间连边为a那么答案一定为a和一条最短的全由b组成的路径的较小者,如果1和n之间连边为b,那么答案一定 + 为b和一条最短的全由a组成的路径的较小者。对于第1种情况直接跑spfa就可以了,第二种情况由于边数较多,不能直接spfa + 从1开始搜索与其相连的边权为b的边,用set维护一下,由于每个点只入队1次,复杂度还是允许的。这种处理方法还是第一 + 次做,感觉很巧妙 +*/ +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +const int INF=1e9+7; +const int maxn=100100; +int n,m,vis[maxn]; +LL a,b,dis[maxn]; +setst,ts; +set::iterator it; +int head[maxn],ip; +void init() +{ + memset(head,-1,sizeof(head)); + ip=0; +} +struct note +{ + int v,next; +}edge[maxn*10]; +void addedge(int u,int v) +{ + edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++; +} +void spfa() +{ + queueq; + for(int i=0;i<=n;i++)dis[i]=INF; + memset(vis,0,sizeof(vis)); + dis[1]=0; + q.push(1); + while(!q.empty()) + { + int u=q.front(); + q.pop(); + vis[u]=0; + for(int i=head[u];i!=-1;i=edge[i].next) + { + int v=edge[i].v; + if(dis[v]>dis[u]+1) + { + dis[v]=dis[u]+1; + if(!vis[v]) + { + vis[v]=1; + q.push(v); + } + } + } + } + printf("%lld\n",min(dis[n]*a,b)); +} +void bfs() +{ + dis[n]=INF; + st.clear(),ts.clear(); + for(int i=2;i<=n;i++)st.insert(i); + queueq; + q.push(1); + dis[1]=0; + while(!q.empty()) + { + int u=q.front(); + q.pop(); + for(int i=head[u];i!=-1;i=edge[i].next) + { + int v=edge[i].v; + if(st.count(v)==0)continue; + st.erase(v),ts.insert(v); + } + for(it=st.begin();it!=st.end();it++) + { + q.push(*it); + dis[*it]=dis[u]+1; + } + st.swap(ts); + ts.clear(); + } + printf("%lld\n",min(dis[n]*b,a)); +} +int main() +{ + while(~scanf("%d%d%lld%lld",&n,&m,&a,&b)) + { + init(); + int flag=0; + for(int i=0;iv)swap(u,v); + addedge(u,v); + addedge(v,u); + if(u==1&&v==n)flag=1; + } + if(flag)bfs(); + else spfa(); + } + return 0; +} From f0aac668d2c1aaf24a48a86d10a76cb9c0acf3b1 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Mon, 12 Sep 2016 20:10:13 +0800 Subject: [PATCH 13/22] Create 5869_zxhl.cpp From http://www.cnblogs.com/zxhl/p/5865948.html --- HDOJ/5869_zxhl.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 HDOJ/5869_zxhl.cpp diff --git a/HDOJ/5869_zxhl.cpp b/HDOJ/5869_zxhl.cpp new file mode 100644 index 0000000..2c1c62e --- /dev/null +++ b/HDOJ/5869_zxhl.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +#pragma comment(linker, "/STACK:102400000,102400000") +#define ls i<<1 +#define rs ls | 1 +#define mid ((ll+rr)>>1) +#define pii pair +#define MP make_pair + +typedef long long LL; +const long long INF = 1e18; +const double Pi = acos(-1.0); +const int N = 1e6+10, M = 1e2+11, mod = 1e9+7, inf = 2e9; + +int n,q,a[N],ans[N]; +int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b);} +vector G[N]; +struct QQ{ + int l,r,id; + bool operator < (const QQ &a) const { + return a.r > r; + } +}Q[N]; +int C[N],vis[N]; +void update(int x,int c) { + for(int i =x; i < N; i+=i&(-i)) C[i] += c; +} +int ask(int x) { + int s =0 ; + for(int i = x; i; i-= i & (-i))s += C[i]; + return s; +} +int main() { + while(scanf("%d%d",&n,&q)!=EOF) { + for(int i = 1; i <= n; ++i) scanf("%d",&a[i]); + for(int i = 0; i <= n; ++i) G[i].clear(); + for(int i = 1; i <= n; ++i) { + int x = a[i]; + int y = i; + for(int j = 0; j < G[i-1].size(); ++j) { + int res = gcd(x,G[i-1][j].first); + if(x != res) { + G[i].push_back(MP(x,y)); + x = res; + y = G[i-1][j].second; + } + } + G[i].push_back(MP(x,y)); + } + memset(C,0,sizeof(C)); + memset(vis,0,sizeof(vis)); + for(int i = 1; i <= q; ++i) {scanf("%d%d",&Q[i].l,&Q[i].r),Q[i].id = i;} + sort(Q+1,Q+q+1); + for(int R = 0, i = 1; i <= q; ++i) { + while(R < Q[i].r) { + R++; + for(int j = 0; j < G[R].size(); ++j) { + int res = G[R][j].first; + int ids = G[R][j].second; + if(vis[res]) update(vis[res],-1); + vis[res] = ids; + update(vis[res],1); + } + } + ans[Q[i].id] = ask(R) - ask(Q[i].l-1); + } + for(int i = 1; i <= q; ++i) cout< Date: Tue, 13 Sep 2016 04:32:04 +0800 Subject: [PATCH 14/22] Create 5868_zufezzt.cpp From http://www.cnblogs.com/zufezzt/p/5866141.html --- HDOJ/5868_zufezzt.cpp | 147 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 HDOJ/5868_zufezzt.cpp diff --git a/HDOJ/5868_zufezzt.cpp b/HDOJ/5868_zufezzt.cpp new file mode 100644 index 0000000..59038d9 --- /dev/null +++ b/HDOJ/5868_zufezzt.cpp @@ -0,0 +1,147 @@ +#pragma comment(linker, "/STACK:1024000000,1024000000") +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +const double pi=acos(-1.0),eps=1e-6; +void File() +{ + freopen("D:\\in.txt","r",stdin); + freopen("D:\\out.txt","w",stdout); +} +template +inline void read(T &x) +{ + char c=getchar(); x=0; + while(!isdigit(c)) c=getchar(); + while(isdigit(c)) {x=x*10+c-'0'; c=getchar();} +} + +LL n,mod=1e9+7; + +LL extend_gcd(LL a,LL b,LL &x,LL &y) +{ + if(a==0&&b==0) return -1; + if(b==0){x=1;y=0;return a;} + LL d=extend_gcd(b,a%b,y,x); + y-=a/b*x; + return d; +} + +LL mod_reverse(LL a,LL n) +{ + LL x,y; + LL d=extend_gcd(a,n,x,y); + if(d==1) return (x%n+n)%n; + else return -1; +} + +LL phi(LL n) +{ + LL res=n,a=n; + for(int i=2;i*i<=a;i++) + { + if(a%i==0) + { + res=res/i*(i-1); + while(a%i==0) a/=i; + } + } + if(a>1) res=res/a*(a-1); + return res; +} + +struct Matrix +{ + LL A[3][3]; + int R, C; + Matrix operator*(Matrix b); +}; + +Matrix X, Y, Z; + +Matrix Matrix::operator*(Matrix b) +{ + Matrix c; + memset(c.A, 0, sizeof(c.A)); + int i, j, k; + for (i = 1; i <= R; i++) + for (j = 1; j <= b.C; j++) + for (k = 1; k <= C; k++) + c.A[i][j] = (c.A[i][j] + (A[i][k] * b.A[k][j]) % mod) % mod; + c.R = R; c.C = b.C; + return c; +} + +void init() +{ + memset(X.A, 0, sizeof X.A); + memset(Y.A, 0, sizeof Y.A); + memset(Z.A, 0, sizeof Z.A); + + Y.R = 2; Y.C = 2; + for (int i = 1; i <= 2; i++) Y.A[i][i] = 1; + + X.R = 2; X.C = 2; + X.A[1][1]=0; X.A[1][2]=1; + X.A[2][1]=1; X.A[2][2]=1; + + Z.R = 1; Z.C = 2; + Z.A[1][1]=0; Z.A[1][2]=1; +} + +LL work(int x) +{ + x--; + while (x) + { + if (x % 2 == 1) Y = Y*X; + x = x >> 1; + X = X*X; + } + Z = Z*Y; + return Z.A[1][2]; +} + +LL fib(int x) +{ + if(x==0) return 0; + init(); + return work(x); +} + +int main() +{ + while(~scanf("%lld",&n)) + { + if(n==1) { printf("2\n"); continue; } + LL ans=0; + + for(LL i=1;i*i<=n;i++) + { + if(n%i!=0) continue; + + LL t=phi(n/i)*((fib(i-1)+fib(i+1))%mod)%mod; + ans=(ans+t)%mod; + + if(n/i!=i) + { + t=phi(i)*((fib(n/i-1)+fib(n/i+1))%mod)%mod; + ans=(ans+t)%mod; + } + } + + LL ni=mod_reverse(n,mod); + ans=ans*ni%mod; + printf("%lld\n",ans); + } + return 0; +} From 3d94c049e0b6c71032ee267a7db0f5099f8b3baa Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 13 Sep 2016 04:34:16 +0800 Subject: [PATCH 15/22] Create 5869_kopyh.cpp From http://blog.csdn.net/kopyh/article/details/52496525 --- HDOJ/5869_kopyh.cpp | 136 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 HDOJ/5869_kopyh.cpp diff --git a/HDOJ/5869_kopyh.cpp b/HDOJ/5869_kopyh.cpp new file mode 100644 index 0000000..6904932 --- /dev/null +++ b/HDOJ/5869_kopyh.cpp @@ -0,0 +1,136 @@ +//kopyh +#include +#define INF 0x3f3f3f3f +#define MOD 1000000007 +#define N 112345 +using namespace std; +int n,m,sum,res,flag; +#define root 1 , n , 1 +#define lson l , m , rt << 1 +#define rson m + 1 , r , rt << 1 | 1 +struct node +{ + int pos,val; + node(int x=0,int y=0){pos=x,val=y;} + friend bool operator < (node a, node b) + { + return a.val < b.val; + } + friend node operator + (node a, node b) + { + return node(a.pos,a.val+b.val); + } +}arr[N<<2]; +int add[N<<2],tot; +void pushUp(int rt) +{ + arr[rt] = arr[rt<<1]+arr[rt<<1|1]; +} +void pushDown(int l,int r,int rt) +{ + if(add[rt]) + { + int m = (l+r)>>1; + add[rt<<1] += add[rt]; + add[rt<<1|1] += add[rt]; + arr[rt<<1].val += (m-l+1)*add[rt]; + arr[rt<<1|1].val += (r-m)*add[rt]; + add[rt] = 0; + } +} +void updata(int l,int r,int rt,int ql,int qr,int val) +{ + if(l>qr||ql>r)return; + if(l>=ql&&r<=qr) + { + arr[rt].val += (r-l+1)*val; + add[rt] += val; + return; + } + pushDown(l,r,rt); + int m = (l+r)>>1; + if(ql<=m)updata(lson,ql,qr,val); + if(qr>m)updata(rson,ql,qr,val); + pushUp(rt); +} +void build(int l,int r,int rt) +{ + add[rt]=0; + if(l == r) + { + arr[rt].val = 0; + arr[rt].pos = tot; + return; + } + int m = (l+r)>>1; + build(lson); + build(rson); + pushUp(rt); +} +node query(int l,int r,int rt,int ql,int qr) +{ + if(l>qr||ql>r) + return node(0,0); + if(l>=ql&&r<=qr) + return arr[rt]; + pushDown(l,r,rt); + int m = (l+r)>>1; + return query(lson,ql,qr)+query(rson,ql,qr); +} +int a[N],pos[N*10],ans[N]; +vector >vc[N],qu[N]; +int gcd(int x,int y) +{ return y?gcd(y,x%y):x; } +int main() +{ + #ifndef ONLINE_JUDGE + freopen("test.txt","r",stdin); + #endif + int i,j,k,cas,T,t,x,y,z,now; + while(scanf("%d%d",&n,&m)!=EOF) + { + for(i=1;i<=n;i++) + { + scanf("%d",&a[i]); + vc[i].clear(); + qu[i].clear(); + } + for(i=1;i<=n;i++) + { + x=a[i]; y=i; + for(j=0;j Date: Tue, 13 Sep 2016 04:35:25 +0800 Subject: [PATCH 16/22] Create 5870_fzmh.cpp From http://www.cnblogs.com/fzmh/p/5861485.html --- HDOJ/5870_fzmh.cpp | 152 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 HDOJ/5870_fzmh.cpp diff --git a/HDOJ/5870_fzmh.cpp b/HDOJ/5870_fzmh.cpp new file mode 100644 index 0000000..68207fb --- /dev/null +++ b/HDOJ/5870_fzmh.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include +#define mp make_pair +#define pb push_back +#define fi first +#define sc second +using namespace std; +const int N = 201010; +const int M = 2010101; +const int inf = 2100000000; +typedef pair P; +priority_queue,greater

> Q; +int n,m,i,a,b,c,dis[N],cnt[N],dis1[N],cnt1[N],dis2[N],cnt2[N],X; +int id[N],ID[N],Id[N]; +vector

e[N]; +void gao(int x) +{ + int i; + for (i=1;i<=n;i++) + dis[i]=inf,cnt[i]=0; + dis[x]=0; + cnt[x]=1; + Q.push(mp(0,x)); + while (!Q.empty()) + { + P tmp=Q.top(); + Q.pop(); + x=tmp.sc; + if (dis[x]!=tmp.fi) continue; + for (i=0;iX) cnt[e[x][i].fi]=X; + } + } +} + +int ls[M],rs[M],s[M],tot,root[N]; +void build(int &x,int a,int b) +{ + x=++tot; + ls[x]=rs[x]=s[x]=0; + if (b-a>1) + { + int m=(a+b)>>1; + build(ls[x],a,m); + build(rs[x],m,b); + } +} +void insert(int y,int &x,int a,int b,int l,int r) +{ + x=++tot; + ls[x]=ls[y];rs[x]=rs[y];s[x]=s[y]+1; + if ((a<=l)&&(r<=b)) + return; + int m=(l+r)>>1; + if (a>1,ans=0; + if (acnt2[b]; +} +int main() +{ + while (~scanf("%d%d",&n,&m)) + { + if (n+m==0) return 0; + for (i=1;i<=n;i++) e[i].clear(); + scanf("%d",&X); + for (i=1;i<=m;i++) + { + scanf("%d%d%d",&a,&b,&c); + e[a].pb(mp(b,c)); + e[b].pb(mp(a,c)); + } + gao(1); + for (i=1;i<=n;i++) + dis1[i]=dis[i],cnt1[i]=cnt[i]; + gao(n); + for (i=1;i<=n;i++) + dis2[i]=dis[i],cnt2[i]=cnt[i]; + + tot=0; + build(root[0],0,n); + for (i=1;i<=n;i++) + id[i]=i; + sort(id+1,id+1+n,cmp); + for (i=1;i<=n;i++) + ID[id[i]]=i; + + for (i=1;i<=n;i++) + Id[i]=i; + sort(Id+1,Id+1+n,CMP); + for (i=1;i<=n;i++) + insert(root[i-1],root[i],ID[Id[i]]-1,ID[Id[i]],0,n); + long long ans=0; + for (i=1;i<=n;i++) + { + int l=1,r=n; + while (l<=r) + { + m=(l+r)>>1; + if (dis2[id[m]]+dis1[i]+1<=dis1[n]) l=m+1;else r=m-1; + } + int j=r; + + l=1;r=n; + while (l<=r) + { + m=(l+r)>>1; + if (1LL*cnt1[i]*cnt2[Id[m]]>=X) l=m+1;else r=m-1; + } + + if (j) ans=ans+query(root[r],0,j,0,n); + + + for (int k=0;k=X) ans--; + + if (dis1[i]+dis2[i]+1<=dis1[N]) + if (1LL*cnt1[i]*cnt2[i]>=X) ans--; + + } + printf("%lld\n",ans); + } +} From 038880cfb54e7c7e7d90b097641d8cf8f1f7871a Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 13 Sep 2016 04:37:39 +0800 Subject: [PATCH 17/22] Create 5872_kopyh.cpp From http://blog.csdn.net/kopyh/article/details/52496579 --- HDOJ/5872_kopyh.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 HDOJ/5872_kopyh.cpp diff --git a/HDOJ/5872_kopyh.cpp b/HDOJ/5872_kopyh.cpp new file mode 100644 index 0000000..2bfc47e --- /dev/null +++ b/HDOJ/5872_kopyh.cpp @@ -0,0 +1,22 @@ +//kopyh +#include +#define INF 0x3f3f3f3f +#define MOD 1000000007 +#define N 1123456 +using namespace std; +long long n,m,sum,res,flag; +int main() +{ + #ifndef ONLINE_JUDGE + freopen("test.txt","r",stdin); + #endif + long long i,j,cas,T,t,x,y,z,h,l,k; + while(scanf("%I64d%I64d%I64d",&h,&l,&k)!=EOF) + { + x=k/h;//最大人数时一排几个部门 + y=k/(x+1);//多放一个部门时每个部门最多的人数 + res=y+1;//加一个人保证不能多放一个部门 + printf("%I64d\n",l/(k-k%res)+bool(l%(k-k%res)));//ok + } + return 0; +} From 038d6e08195135480005560ec9020c8c6115d137 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 13 Sep 2016 04:38:42 +0800 Subject: [PATCH 18/22] Create 5873_kopyh.cpp From http://blog.csdn.net/kopyh/article/details/52496667 --- HDOJ/5873_kopyh.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 HDOJ/5873_kopyh.cpp diff --git a/HDOJ/5873_kopyh.cpp b/HDOJ/5873_kopyh.cpp new file mode 100644 index 0000000..b41b042 --- /dev/null +++ b/HDOJ/5873_kopyh.cpp @@ -0,0 +1,38 @@ +//kopyh +#include +#define INF 0x3f3f3f3f +#define MOD 1000000007 +#define N 1123456 +using namespace std; +long long n,m,sum,res,flag,a[N]; +int main() +{ + #ifndef ONLINE_JUDGE + freopen("test.txt","r",stdin); + #endif + int i,j,k,cas,T,t,x,y,z; + while(scanf("%I64d",&n)!=EOF) + while(n--) + { + flag=1; sum=0; + scanf("%I64d",&m); + for(i=0;i Date: Tue, 13 Sep 2016 04:40:54 +0800 Subject: [PATCH 19/22] Create 5874_qq_33184171.cpp From http://blog.csdn.net/qq_33184171/article/details/52496677 --- HDOJ/5874_qq_33184171.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 HDOJ/5874_qq_33184171.cpp diff --git a/HDOJ/5874_qq_33184171.cpp b/HDOJ/5874_qq_33184171.cpp new file mode 100644 index 0000000..b5e2f0f --- /dev/null +++ b/HDOJ/5874_qq_33184171.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; +using LL = long long; +int main() +{ + LL m,n; + while(~scanf("%I64d %I64d",&m,&n)) + { + LL ans = m/2*(m-m/2); + + if( ans <= n) puts("T"); + else puts("F"); + } + return 0; +} From 9fccc93b736f1bb1f93d1150ba019170bd78de34 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 13 Sep 2016 04:42:29 +0800 Subject: [PATCH 20/22] Create 5875_chen9510.cpp From http://www.cnblogs.com/chen9510/p/5862834.html --- HDOJ/5875_chen9510.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 HDOJ/5875_chen9510.cpp diff --git a/HDOJ/5875_chen9510.cpp b/HDOJ/5875_chen9510.cpp new file mode 100644 index 0000000..780de3d --- /dev/null +++ b/HDOJ/5875_chen9510.cpp @@ -0,0 +1,48 @@ +// From http://www.cnblogs.com/chen9510/p/5862834.html +// ONLY For Test +#include +#include +#include +#include +#include +using namespace std; +int A[100005]; +int pos[100005]; +int s[100005]; + +int main() +{ + int T,N,M; + cin>>T; + while(T--) + { + scanf("%d",&N); + for(int i=1;i<=N;i++) + scanf("%d",&A[i]); + int num=1; + A[N+1]=-1; + s[1]=N+1; + for(int i=N;i>=1;i--)///单调栈; + { + while(A[i]r) break; + tmp=tmp%A[pos[l]]; + l=pos[l]; + } + printf("%d\n",tmp); + } + } + return 0; +} From 33ff3e8cc97fea214a0cb34d4f5c7ab7397156b6 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 13 Sep 2016 04:44:13 +0800 Subject: [PATCH 21/22] Create 5876_yuanjunlai141.cpp From http://blog.csdn.net/yuanjunlai141/article/details/52496658 --- HDOJ/5876_yuanjunlai141.cpp | 98 +++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 HDOJ/5876_yuanjunlai141.cpp diff --git a/HDOJ/5876_yuanjunlai141.cpp b/HDOJ/5876_yuanjunlai141.cpp new file mode 100644 index 0000000..12ee4eb --- /dev/null +++ b/HDOJ/5876_yuanjunlai141.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +const int maxn=200000*5,inf=0x3f3f3f3f; +struct Edge +{ + int v,next,cap; + +} edge[maxn]; +int tot,head[maxn]; +int vis[maxn],du[maxn],dis[maxn]; +void init() +{ + tot=0; + memset(head,-1,sizeof(head)); +} +void add_edge(int u,int v,int cap) +{ + edge[tot].v=v; + edge[tot].cap=cap; + edge[tot].next=head[u]; + head[u]=tot++; +} +void bfs(int start,int n) +{ + sets1; + sets2; + set::iterator it; + for(int i=1;i<=n;i++) + { + if(i!=start) + s1.insert(i); + } + + queueque; + que.push(start); + dis[start]=0; + while(!que.empty()) + { + int u=que.front(); + que.pop(); + for(int i=head[u];i!=-1;i=edge[i].next) + { + int v=edge[i].v; + if(!s1.count(v)) + continue; + s1.erase(v); + s2.insert(v); + + } + for(it=s1.begin();it!=s1.end();it++) + { + que.push(*it); + dis[*it]=dis[u]+1; + } + s1.swap(s2); + s2.clear(); + } +} +int main() +{ + int s,n,t,m; + int u,v; + scanf("%d",&t); + while(t--) + { + scanf("%d%d",&n,&m); + init(); + memset(du,0,sizeof(du)); + for(int i=0; i Date: Tue, 13 Sep 2016 04:46:30 +0800 Subject: [PATCH 22/22] Create 5877_angon823.cpp From http://blog.csdn.net/angon823/article/details/52496420 --- HDOJ/5877_angon823.cpp | 319 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 319 insertions(+) create mode 100644 HDOJ/5877_angon823.cpp diff --git a/HDOJ/5877_angon823.cpp b/HDOJ/5877_angon823.cpp new file mode 100644 index 0000000..9e9b88c --- /dev/null +++ b/HDOJ/5877_angon823.cpp @@ -0,0 +1,319 @@ +#include +#include +#define MAXN 200005 +int cnt = 1, rt = 0; +int Add[MAXN]; + +struct Tree{ + long long key; + int num, size, fa, son[2]; +}T[MAXN]; + +inline void PushUp(int x) +{ + T[x].size = T[T[x].son[0]].size + T[T[x].son[1]].size + T[x].num; +} + +inline void PushDown(int x) +{ + if (Add[x]) + { + if (T[x].son[0]) + { + T[T[x].son[0]].key += Add[x]; + Add[T[x].son[0]] += Add[x]; + } + if (T[x].son[1]) + { + T[T[x].son[1]].key += Add[x]; + Add[T[x].son[1]] += Add[x]; + } + Add[x] = 0; + } +} + +inline int Newnode(long long key, int fa) //新建一个节点并返回 +{ + ++cnt; + T[cnt].key = key; + T[cnt].num = T[cnt].size = 1; + T[cnt].fa = fa; + T[cnt].son[0] = T[cnt].son[1] = 0; + return cnt; +} + +inline void Rotate(int x, int p) //0左旋 1右旋 +{ + int y = T[x].fa; + PushDown(y); + PushDown(x); + T[y].son[!p] = T[x].son[p]; + T[T[x].son[p]].fa = y; + T[x].fa = T[y].fa; + if (T[x].fa) + T[T[x].fa].son[T[T[x].fa].son[1] == y] = x; + T[x].son[p] = y; + T[y].fa = x; + PushUp(y); + PushUp(x); +} + +void Splay(int x, int To) //将x节点移动到To的子节点中 +{ + while (T[x].fa != To) + { + if (T[T[x].fa].fa == To) + Rotate(x, T[T[x].fa].son[0] == x); + else + { + int y = T[x].fa, z = T[y].fa; + int p = (T[z].son[0] == y); + if (T[y].son[p] == x) + Rotate(x, !p), Rotate(x, p); //之字旋 + else + Rotate(y, p), Rotate(x, p); //一字旋 + } + } + if (To == 0) rt = x; +} + +int GetPth(int p, int To) //返回第p小的节点 并移动到To的子节点中 +{ + if (!rt || p > T[rt].size) return 0; + int x = rt; + while (x) + { + PushDown(x); + if (p >= T[T[x].son[0]].size + 1 && p <= T[T[x].son[0]].size + T[x].num) + break; + if (p > T[T[x].son[0]].size + T[x].num) + { + p -= T[T[x].son[0]].size + T[x].num; + x = T[x].son[1]; + } + else + x = T[x].son[0]; + } + Splay(x, 0); + return x; +} + +int Find(long long key) //返回值为key的节点 若无返回0 若有将其转移到根处 +{ + if (!rt) return 0; + int x = rt; + while (x) + { + PushDown(x); + if (T[x].key == key) break; + x = T[x].son[key > T[x].key]; + } + if (x) Splay(x, 0); + return x; +} + +int Prev() //返回根节点的前驱 非重点 +{ + if (!rt || !T[rt].son[0]) return 0; + int x = T[rt].son[0]; + while (T[x].son[1]) + { + PushDown(x); + x = T[x].son[1]; + } + Splay(x, 0); + return x; +} + +int Succ() //返回根结点的后继 非重点 +{ + if (!rt || !T[rt].son[1]) return 0; + int x = T[rt].son[1]; + while (T[x].son[0]) + { + PushDown(x); + x = T[x].son[0]; + } + Splay(x, 0); + return x; +} + +void Insert(long long key) //插入key值 +{ + if (!rt) + rt = Newnode(key, 0); + else + { + int x = rt, y = 0; + while (x) + { + PushDown(x); + y = x; + if (T[x].key == key) + { + T[x].num++; + T[x].size++; + break; + } + T[x].size++; + x = T[x].son[key > T[x].key]; + } + if (!x) + x = T[y].son[key > T[y].key] = Newnode(key, y); + Splay(x, 0); + } +} + +void Delete(long long key) //删除值为key的节点1个 +{ + int x = Find(key); + if (!x) return; + if (T[x].num>1) + { + T[x].num--; + PushUp(x); + return; + } + int y = T[x].son[0]; + while (T[y].son[1]) + y = T[y].son[1]; + int z = T[x].son[1]; + while (T[z].son[0]) + z = T[z].son[0]; + if (!y && !z) + { + rt = 0; + return; + } + if (!y) + { + Splay(z, 0); + T[z].son[0] = 0; + PushUp(z); + return; + } + if (!z) + { + Splay(y, 0); + T[y].son[1] = 0; + PushUp(y); + return; + } + Splay(y, 0); + Splay(z, y); + T[z].son[0] = 0; + PushUp(z); + PushUp(y); +} + +int GetRank(long long key) //获得值<=key的节点个数 +{ + if (!Find(key)) + { + Insert(key); + int tmp = T[T[rt].son[0]].size; + Delete(key); + return tmp; + } + else + return T[T[rt].son[0]].size + T[rt].num; +} + +void Delete(int l, int r) //删除值在[l, r]中的所有节点 l!=r +{ + if (!Find(l)) Insert(l); + int p = Prev(); + if (!Find(r)) Insert(r); + int q = Succ(); + if (!p && !q) + { + rt = 0; + return; + } + if (!p) + { + T[rt].son[0] = 0; + PushUp(rt); + return; + } + if (!q) + { + Splay(p, 0); + T[rt].son[1] = 0; + PushUp(rt); + return; + } + Splay(p, q); + T[p].son[1] = 0; + PushUp(p); + PushUp(q); +} +#define MAX 100005 +#include +#include +using namespace std; +int v[MAX]; +int N; +long long K; +long long res = 0; +struct Edge +{ + int b; + int next; +}edge[MAX]; +int edgeCnt = 1; +int adj[MAX]; +void AddEdge(int a, int b) +{ + edge[edgeCnt].b = b; + edge[edgeCnt].next = adj[a]; + adj[a] = edgeCnt++; +} +void Search(int now) +{ + res += GetRank(K / v[now]); + Insert(v[now]); + for(int p = adj[now]; p; p = edge[p].next) + { + Search(edge[p].b); + } + Delete(v[now]); +} +int in[MAX]; +int main() { + int T; + while(~scanf("%d", &T)) + { + int i; + int a, b; + for (; T; T--) + { + scanf("%d%lld", &N, &K); + memset(adj, 0, (N + 1) * sizeof(int)); + memset(in, 0, (N + 1) * sizeof(int)); + edgeCnt = 1; + for (i = 1; i <= N; i++) + { + scanf("%d", &v[i]); + } + for (i = 1; i < N; i++) + { + scanf("%d%d", &a, &b); + in[b]++; + AddEdge(a, b); + } + for (i = 1; i <= N; i++) + { + if (in[i] == 0) + { + break; + } + } + cnt = 1, rt = 0; + res = 0; + Search(i); + printf("%lld\n", res); + } + } + return 0; +}