[整体二分][树状数组] Bzoj P2527 Meteors

Description
Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The planet is unsuitable for colonisation due to strange meteor showers, which on the other hand make it an exceptionally interesting object of study.

The member states of BIU have already placed space stations close to the planet’s orbit. The stations’ goal is to take samples of the rocks flying by. The BIU Commission has partitioned the orbit into msectors, numbered from 1to m, where the sectors 1and mare adjacent. In each sector there is a single space station, belonging to one of the nmember states.

Each state has declared a number of meteor samples it intends to gather before the mission ends. Your task is to determine, for each state, when it can stop taking samples, based on the meter shower predictions for the years to come.

Byteotian Interstellar Union有N个成员国。现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站。

这个星球经常会下陨石雨。BIU已经预测了接下来K场陨石雨的情况。
BIU的第i个成员国希望能够收集Pi单位的陨石样本。你的任务是判断对于每个国家,它需要在第几次陨石雨之后,才能收集足够的陨石。
输入:
第一行是两个数N,M。
第二行有M个数,第i个数Oi表示第i段轨道上有第Oi个国家的太空站。
第三行有N个数,第i个数Pi表示第i个国家希望收集的陨石数量。
第四行有一个数K,表示BIU预测了接下来的K场陨石雨。
接下来K行,每行有三个数Li,Ri,Ai,表示第K场陨石雨的发生地点在从Li顺时针到Ri的区间中(如果Li<=Ri,就是Li,Li+1,…,Ri,否则就是Ri,Ri+1,…,m-1,m,1,…,Li),向区间中的每个太空站提供Ai单位的陨石样本。
输出:
N行。第i行的数Wi表示第i个国家在第Wi波陨石雨之后能够收集到足够的陨石样本。如果到第K波结束后仍然收集不到,输出NIE。
数据范围:

数据范围:
1<=n,m,k<=3*10^5
1<=Pi<=10^9
1<=Ai<10^9

 

题解

  • 这道就是道整体二分的裸题,单点修改时用树状数组维护就好了

代码

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #define N 300010
 5 #define ll long long 
 6 using namespace std;
 7 struct node{int id,head;ll v;}p[N],q[N<<1];
 8 struct edge{int to,from;}e[N];
 9 int n,m,k,cnt,L[N],R[N],ans[N],head[N];
10 ll A[N],sz[N<<1];
11 void insert(int x,int y) { e[++cnt].to=y,e[cnt].from=p[x].head,p[x].head=cnt; }
12 void change(int x,ll y) {for (;x<=2*m;x+=x&-x) sz[x]+=y;}
13 ll find(int x) {ll r=0; for (;x;x-=x&-x) r+=sz[x]; return r;}
14 void solve(int l,int r,int x,int y)
15 {
16     if (l==r) { for (int i=x;i<=y;i++) ans[p[i].id]=l; return; }
17     int mid=l+r>>1,nl=0,nr=n;
18     for (int i=l;i<=mid;i++) change(L[i],A[i]),change(R[i]+1,-A[i]);
19     for (int i=x;i<=y;i++)
20     {
21         ll tmp=0;
22         for (int j=p[i].head;j&&tmp<=p[i].v;j=e[j].from) tmp+=find(e[j].to+m)+find(e[j].to);
23         if (tmp>=p[i].v) q[++nl]=p[i]; else q[++nr]=p[i],q[nr].v-=tmp;    
24     }
25     for (int i=l;i<=mid;i++) change(L[i],-A[i]),change(R[i]+1,A[i]);
26     for (int i=1;i<=nl;i++) p[x+i-1]=q[i];
27     for (int i=n+1;i<=nr;i++) p[x+nl+i-n-1]=q[i];
28     solve(l,mid,x,x+nl-1),solve(mid+1,r,y-nr+n+1,y);
29 }
30 int main()
31 {
32     scanf("%d%d",&n,&m);
33     for (int i=1,x;i<=m;i++) scanf("%d",&x),insert(x,i);
34     for (int i=1;i<=n;i++) scanf("%lld",&p[i].v),p[i].id=i;
35     scanf("%d",&k); for (int i=1;i<=k;i++) scanf("%d%d%lld",&L[i],&R[i],&A[i]);
36     for (int i=1;i<=k;i++) if (R[i]<L[i]) R[i]+=m;
37     solve(1,k+1,1,n);
38     for (int i=1;i<=n;i++) (ans[i]==k+1)?puts("NIE"):printf("%d\n",ans[i]);
39 }

 

posted @ 2019-07-19 12:33  BEYang_Z  阅读(191)  评论(0编辑  收藏  举报