BZOJ2527: [Poi2011]Meteors

2527: [Poi2011]Meteors

Time Limit: 60 Sec  Memory Limit: 128 MB
Submit: 230  Solved: 92
[Submit][Status]

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

Input

The first line of the standard input gives two integers, n and m(1<=n,m<=3*10^5) separated by a single space, that denote, respectively, the number of BIU member states and the number of sectors the orbit has been partitioned into.
In the second line there are mintegers Oi(1<=Oi<=n) separated by single spaces, that denote the states owning stations in successive sectors.
In the third line there are nintegers Pi(1<=Pi<=10^9) separated by single spaces, that denote the numbers of meteor samples that the successive states intend to gather.
In the fourth line there is a single integer k(1<=k<=3*10^5) that denotes the number of meteor showers predictions. The following klines specify the (predicted) meteor showers chronologically. The i-th of these lines holds three integers Li, Ri, Ai(separated by single spaces), which denote that a meteor shower is expected in sectors Li,Li+1,…Ri (if Li<=Ri) or sectors Li,Li+1,…,m,1,…Ri (if Li>Ri), which should provide each station in those sectors with Aimeteor samples (1<=Ai<10^9).
In tests worth at least 20% of the points it additionally holds that .

Output

 
Your program should print nlines on the standard output. The i-th of them should contain a single integer Wi, denoting the number of shower after which the stations belonging to the i-th state are expected to gather at least Pi samples, or the word NIE (Polish for no) if that state is not expected to gather enough samples in the foreseeable future.

Sample Input

3 5
1 3 2 1 3
10 5 7
3
4 2 4
1 3 1
3 5 2

Sample Output

3
NIE
1

HINT

Source

鸣谢 Object022

题解:

为何我觉得整体二分就是CDQ分治呢?计算左边对答案的影响,然后递归两边。。。

继续搬运题解。。。

首先根据读入的信息处理出第 i 个国家的位置集合,
用个 vector 或者链表存一下。
然后用分治解决就可以了。
我们分治陨石雨的位置,记当前等待处理的国家集合为 S,分治的陨石雨区间为[L,R],我们取 mid=(L+R)/2,
对于一次陨石雨,
如果 l<=r 我们可以看成是把 l 到 r 都加 d,
否则可以看出把 l 到 m 都加 d,
再把 1 到 r 都加 d。
因此我们需要一个能支持区间加减,单点询问的数据结构,使用树状数组即可。我们首先处理[L,mid]的陨石雨,
对于一个国家,如果当前获得的数量达到或者超过了它的需求,则把它分进 A 集合,同时把需求的数量也放进
去,如果没达到要求,记需求为 x,当前获得为 y,则把它放入 B 集合,同时把 x-y 作为新的需求数量也放入 B
集合。这样 A 集合中的答案就都在[l,mid]中,B 集合中的答案就都在[mid+1,r]中,递归处理两边即可。由于每个
国家最多被处理 logk 次,因此时间复杂度最大为 O(mlogmlogk)。

 

刚开始想到了枚举i,然后二分时间,后来发现复杂度太高了

而整体二分就是把所有的询问一块儿二分,然后就避免的相当多的重复计算,所以复杂度比较好

 

WA了几发,不知道哪错了。。。

代码:

  1 #include<cstdio>
  2 
  3 #include<cstdlib>
  4 
  5 #include<cmath>
  6 
  7 #include<cstring>
  8 
  9 #include<algorithm>
 10 
 11 #include<iostream>
 12 
 13 #include<vector>
 14 
 15 #include<map>
 16 
 17 #include<set>
 18 
 19 #include<queue>
 20 
 21 #include<string>
 22 
 23 #define inf 1100000000
 24 
 25 #define maxn 500000+100
 26 
 27 #define maxm 500+100
 28 
 29 #define eps 1e-10
 30 
 31 #define ll long long
 32 
 33 #define pa pair<int,int>
 34 
 35 #define for0(i,n) for(int i=0;i<=(n);i++)
 36 
 37 #define for1(i,n) for(int i=1;i<=(n);i++)
 38 
 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 40 
 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 42 
 43 #define mod 1000000007
 44 
 45 using namespace std;
 46 
 47 inline int read()
 48 
 49 {
 50 
 51     int x=0,f=1;char ch=getchar();
 52 
 53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 54 
 55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 56 
 57     return x*f;
 58 
 59 }
 60 int n,m,k,ans[maxn],head[maxn],next[maxn];
 61 ll s[maxn];
 62 struct rec1{int l,r;ll w;}b[maxn];
 63 struct rec2{int pos;ll ned,cur;}a[maxn],q[maxn];
 64 inline void change(int x,ll w)
 65 {
 66     for(;x<=m;x+=x&(-x))s[x]+=w;
 67 }
 68 inline ll sum(int x)
 69 {
 70     ll t=0;
 71     for(;x;x-=x&(-x))t+=s[x];
 72     return t;
 73 }
 74 inline void update(int l,int r,ll w)
 75 {
 76     change(l,w);change(r+1,-w);
 77 }
 78 void solve(int l,int r,int h,int t)
 79 {
 80     if(h>t)return;
 81     if(l==r)
 82     {
 83         for2(i,h,t)ans[a[i].pos]=l;
 84         return;
 85     }
 86     int mid=(l+r)>>1,t1=h-1,t2=t+1;
 87     for2(i,l,mid)
 88     if(b[i].l<=b[i].r)update(b[i].l,b[i].r,b[i].w);else update(b[i].l,m,b[i].w),update(1,b[i].r,b[i].w);
 89     for2(i,h,t)
 90     {
 91         ll tmp=0;
 92         for(int j=head[a[i].pos];j;j=next[j])tmp+=sum(j);
 93         if(a[i].cur+tmp>=a[i].ned)q[++t1]=a[i];else a[i].cur+=tmp,q[--t2]=a[i];
 94     }
 95     for2(i,h,t)a[i]=q[i];
 96     for2(i,l,mid)
 97     if(b[i].l<=b[i].r)update(b[i].l,b[i].r,-b[i].w);else update(b[i].l,m,-b[i].w),update(1,b[i].r,-b[i].w);
 98     solve(l,mid,h,t1);
 99     solve(mid+1,r,t2,t);
100 }
101 
102 int main()
103 
104 {
105 
106     freopen("input.txt","r",stdin);
107 
108     freopen("output.txt","w",stdout);
109 
110     n=read();m=read();
111     for1(i,m)
112     {
113         int x=read();
114         next[i]=head[x];head[x]=i;
115     }
116     for1(i,n)a[i].ned=read(),a[i].pos=i;
117     k=read();
118     for1(i,k)b[i].l=read(),b[i].r=read(),b[i].w=read();
119     b[++k].l=1;b[k].r=m;b[k].w=inf;
120     solve(1,k,1,n);
121     for1(i,n)
122      if(ans[i]==k||ans[i]==0)printf("NIE\n");else printf("%d\n",ans[i]);
123 
124     return 0;
125 
126 }
View Code

 实在调不出来T_T

UPD:无聊来调程序,才发现是long long爆了,怒开unsigned long long,然后就过了。。。其实当tmp>inf的时候直接退出也行,就不用unsigned long long了

代码:

  1 #include<cstdio>
  2  
  3 #include<cstdlib>
  4  
  5 #include<cmath>
  6  
  7 #include<cstring>
  8  
  9 #include<algorithm>
 10  
 11 #include<iostream>
 12  
 13 #include<vector>
 14  
 15 #include<map>
 16  
 17 #include<set>
 18  
 19 #include<queue>
 20  
 21 #include<string>
 22  
 23 #define inf 110000000000000ll
 24  
 25 #define maxn 500000+100
 26  
 27 #define maxm 500+100
 28  
 29 #define eps 1e-10
 30  
 31 #define ll unsigned long long
 32  
 33 #define pa pair<int,int>
 34  
 35 #define for0(i,n) for(int i=0;i<=(n);i++)
 36  
 37 #define for1(i,n) for(int i=1;i<=(n);i++)
 38  
 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 40  
 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 42  
 43 #define mod 1000000007
 44  
 45 using namespace std;
 46  
 47 inline ll read()
 48  
 49 {
 50  
 51     ll x=0,f=1;char ch=getchar();
 52  
 53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 54  
 55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 56  
 57     return x*f;
 58  
 59 }
 60 int n,m,k,ans[maxn],head[maxn],next[maxn];
 61 ll s[maxn];
 62 struct rec1{int l,r;ll w;}b[maxn];
 63 struct rec2{int pos;ll ned,cur;}a[maxn],q[maxn];
 64 inline void change(int x,ll w)
 65 {
 66     for(;x<=m+1;x+=x&(-x))s[x]+=w;
 67 }
 68 inline ll sum(int x)
 69 {
 70     ll t=0;
 71     for(;x;x-=x&(-x))t+=s[x];
 72     return t;
 73 }
 74 inline void update(int l,int r,ll w)
 75 {
 76     change(l,w);change(r+1,-w);
 77 }
 78 void solve(int l,int r,int h,int t)
 79 {
 80     if(h>t)return;
 81     if(l==r)
 82     {
 83         for2(i,h,t)ans[a[i].pos]=l;
 84         return;
 85     }
 86     int mid=(l+r)>>1,t1=h-1,t2=t+1;
 87     for2(i,l,mid)
 88     if(b[i].l<=b[i].r)update(b[i].l,b[i].r,b[i].w);else update(b[i].l,m,b[i].w),update(1,b[i].r,b[i].w);
 89     for2(i,h,t)
 90     {
 91         ll tmp=0;
 92         for(int j=head[a[i].pos];j;j=next[j])tmp+=sum(j);
 93         if(a[i].cur+tmp>=a[i].ned)q[++t1]=a[i];else a[i].cur+=tmp,q[--t2]=a[i];
 94     }
 95     for2(i,h,t)a[i]=q[i];
 96     for2(i,l,mid)
 97     if(b[i].l<=b[i].r)update(b[i].l,b[i].r,-b[i].w);else update(b[i].l,m,-b[i].w),update(1,b[i].r,-b[i].w);
 98     solve(l,mid,h,t1);
 99     solve(mid+1,r,t2,t);
100 }
101  
102 int main()
103  
104 {
105    freopen("input.txt","r",stdin);
106    freopen("output.txt","w",stdout);
107     n=read();m=read();
108     for1(i,m)
109     {
110         int x=read();
111         next[i]=head[x];head[x]=i;
112     }
113     for1(i,n)a[i].ned=read(),a[i].pos=i;
114     k=read();
115     for1(i,k)b[i].l=read(),b[i].r=read(),b[i].w=read();
116     b[++k].l=1;b[k].r=m;b[k].w=inf;
117     solve(1,k,1,n);
118     for1(i,n)
119      if(ans[i]==k||ans[i]==0)printf("NIE\n");else printf("%d\n",ans[i]);
120  
121     return 0;
122  
123 }
View Code

 

posted @ 2014-10-15 17:44  ZYF-ZYF  Views(463)  Comments(0Edit  收藏  举报