Bzoj2527 [Poi2011]Meteors

 

Time Limit: 60 Sec  Memory Limit: 128 MB
Submit: 1618  Solved: 580

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

 

分治 整体二分

注意累加一个国家所有辖区的值的时候,可能会爆long long,需要中途break

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<map>
  6 #include<cstring>
  7 #include<vector>
  8 #define LL long long
  9 #define LU unsigned long long
 10 using namespace std;
 11 const int mxn=300010;
 12 int read(){
 13     int x=0,f=1;char ch=getchar();
 14     while(ch<'0' || ch>'9'){if(ch=='-')f=-f;ch=getchar();}
 15     while(ch>='0' && ch<='9'){x=x*10-'0'+ch;ch=getchar();}
 16     return x*f;
 17 }
 18 struct edge{
 19     int v,nxt;
 20 }e[mxn<<1];
 21 int hd[mxn],mct=0;
 22 void add_edge(int u,int v){
 23     e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
 24 }
 25 //
 26 LL t[mxn];
 27 int n,m,K;
 28 inline void add(int x,int v){while(x<=n){t[x]+=v;x+=x&-x;}}
 29 inline LL ask(int x){
 30     LL res=0;while(x){res+=t[x];x-=x&-x;}return res;
 31 }
 32 void update(int L,int R,int v){
 33     if(L<=R){
 34         add(L,v);add(R+1,-v);
 35     }
 36     else{
 37         add(1,v);add(R+1,-v);add(L,v);//add(n+1,-v);
 38     }
 39     return;
 40 }
 41 LL query(int x,LL lim){
 42     LL res=0;
 43     for(int i=hd[x];i;i=e[i].nxt){
 44         res+=ask(e[i].v);
 45         if(res>=lim)return res;
 46     }
 47     return res;
 48 }
 49 //
 50 struct con{int id,need;}C[mxn];
 51 int que[mxn];//id
 52 int qL[mxn],qR[mxn];
 53 int ans[mxn];
 54 //
 55 struct change{
 56     int L,R,a;
 57 }a[mxn];
 58 //
 59 void solve(int L,int R,int l,int r){//答案 询问 
 60     if(L>R || l>r){
 61         return;
 62     }
 63     int i,j,mid=(L+R)>>1;
 64     for(i=L;i<=mid;i++){
 65         update(a[i].L,a[i].R,a[i].a);
 66     }
 67     int ql=l-1,qr=r+1;
 68     for(i=l;i<=r;i++){
 69         int tmp=que[i];
 70         LL res=query(tmp,C[tmp].need);
 71         if(res>=C[tmp].need){
 72             ans[C[tmp].id]=min(ans[C[tmp].id],mid);
 73             qL[++ql]=que[i];
 74         }
 75         else{
 76             qR[--qr]=que[i];
 77         }
 78     }
 79     for(i=l;i<=ql;i++)    que[i]=qL[i];
 80     for(i=qr;i<=r;i++)    que[i]=qR[i];
 81 //    printf("fin\n");
 82     //
 83     solve(mid+1,R,qr,r);
 84     for(i=L;i<=mid;i++){
 85         update(a[i].L,a[i].R,-a[i].a);
 86     }
 87     solve(L,mid-1,l,ql);
 88     
 89     return;
 90 }
 91 int main(){
 92     int i,j,x;
 93     m=read();n=read();//swap n,m
 94     //m个国家,区间为n 
 95     for(i=1;i<=n;i++){
 96         x=read();
 97         add_edge(x,i);
 98     }
 99     for(i=1;i<=m;i++){
100         C[i].need=read();C[i].id=i;
101     }
102     K=read();
103     for(i=1;i<=K;i++){
104         a[i].L=read();a[i].R=read();a[i].a=read();
105     }
106     memset(ans,0x3f,sizeof ans);
107     for(i=1;i<=m;i++)que[i]=i;
108     solve(1,K,1,m);
109     for(i=1;i<=m;i++){
110         if(ans[i]==0x3f3f3f3f)printf("NIE\n");
111         else printf("%d\n",ans[i]);
112     }
113     return 0;
114 }

 

posted @ 2017-06-16 17:06  SilverNebula  阅读(142)  评论(0编辑  收藏  举报
AmazingCounters.com