1. 程式人生 > >BZOJ2527[Poi2011]Meteors——整體二分+樹狀數組

BZOJ2527[Poi2011]Meteors——整體二分+樹狀數組

algo fin com art can 輸入 second not exc

題目描述

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

輸入

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 .

輸出

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.

樣例輸入

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

樣例輸出

3
NIE
1
對於每個國家我們可以二分答案然後模擬隕石雨驗證,但時間復雜度爆炸。 既然每場隕石雨會影響多個國家,那麽我們可以整體二分,將所有國家一起二分答案。 整體二分時每次只加入第$[l,mid]$場的隕石雨,並對每個國家驗證,如果滿足$k$就將這個國家置於左區間,否則將$k$減掉這次查詢答案放到右區間。 因為每次隕石雨是對一段區間修改,我們可以差分之後用樹狀數組維護,這樣單次查詢和修改的時間復雜度就都是$O(log_{m})$。總時間復雜度為$O(nlog_{k}log_{m})$
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
vector<int>q[300010];
int n,m,k,x;
ll v[300010];
int ans[300010];
int s[300010];
struct lty
{
    int l,r,val;
}a[300010];
int c[300010];
int b[300010];
int now;
void add(int x,int val)
{
    for(int i=x;i<=m;i+=i&-i)
    {
        v[i]+=1ll*val;
    }
}
ll ask(int x)
{
    ll res=0;
    for(int i=x;i;i-=i&-i)
    {
        res+=v[i];
    }
    return res;
}
void change(int id,int opt)
{
    if(a[id].l<=a[id].r)
    {
        add(a[id].l,opt*a[id].val);
        add(a[id].r+1,-1*opt*a[id].val);
    }
    else
    {
        add(1,opt*a[id].val);
        add(a[id].r+1,-1*opt*a[id].val);
        add(a[id].l,opt*a[id].val);
    }
}
void solve(int l,int r,int L,int R)
{
    if(L>R)
    {
        return ;
    }
    if(l==r)
    {
        for(int i=L;i<=R;i++)
        {
            ans[c[i]]=l;
        }
        return ;
    }
    int mid=(l+r)>>1;
    for(int i=l;i<=mid;i++)
    {
        change(i,1);
    }
    int ql=L,qr=R;
    for(int i=L;i<=R;i++)
    {
        ll res=0;
        int len=q[c[i]].size();
        for(int j=0;j<len;j++)
        {
            res+=ask(q[c[i]][j]);
            if(res>=s[c[i]])
            {
                break;
            }
        }
        if(res>=s[c[i]])
        {
            b[ql++]=c[i];
        }
        else
        {
            b[qr--]=c[i];
            s[c[i]]-=res;
        }
    }
    for(int i=L;i<=R;i++)
    {
        c[i]=b[i];
    }
    for(int i=l;i<=mid;i++)
    {
        change(i,-1);
    }
    solve(l,mid,L,ql-1),solve(mid+1,r,ql,R);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&x);
        q[x].push_back(i);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&s[i]);
        c[i]=i;
    }
    scanf("%d",&k);
    for(int i=1;i<=k;i++)
    {
        scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].val);
    }
    solve(1,k+1,1,n);
    for(int i=1;i<=n;i++)
    {
        printf(ans[i]>k?"NIE\n":"%d\n",ans[i]);
    }
}

還有一種寫法就是維護一個指針$now$表示當前加入了第$[1,now]$場的隕石雨,每次二分時移動$now$指針即可,但這樣做的話被放在右區間的國家的$k$就不能減掉當次詢問的答案了。

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
vector<int>q[300010];
int n,m,k,x;
ll v[300010];
int ans[300010];
int s[300010];
struct lty
{
    int l,r,val;
}a[300010];
int c[300010];
int b[300010];
int now;
void add(int x,int val)
{
    for(int i=x;i<=m;i+=i&-i)
    {
        v[i]+=1ll*val;
    }
}
ll ask(int x)
{
    ll res=0;
    for(int i=x;i;i-=i&-i)
    {
        res+=v[i];
    }
    return res;
}
void change(int id,int opt)
{
    if(a[id].l<=a[id].r)
    {
        add(a[id].l,opt*a[id].val);
        add(a[id].r+1,-1*opt*a[id].val);
    }
    else
    {
        add(1,opt*a[id].val);
        add(a[id].r+1,-1*opt*a[id].val);
        add(a[id].l,opt*a[id].val);
    }
}
void solve(int l,int r,int L,int R)
{
    if(L>R)
    {
        return ;
    }
    if(l==r)
    {
        for(int i=L;i<=R;i++)
        {
            ans[c[i]]=l;
        }
        return ;
    }
    int mid=(l+r)>>1;
    while(now<mid)
    {
        now++;
        change(now,1);
    }
    while(now>mid)
    {
        change(now,-1);
        now--;
    }
    int ql=L,qr=R;
    for(int i=L;i<=R;i++)
    {
        ll res=0;
        int len=q[c[i]].size();
        for(int j=0;j<len;j++)
        {
            res+=ask(q[c[i]][j]);
            if(res>=s[c[i]])
            {
                break;
            }
        }
        if(res>=s[c[i]])
        {
            b[ql++]=c[i];
        }
        else
        {
            b[qr--]=c[i];
        }
    }
    for(int i=L;i<=R;i++)
    {
        c[i]=b[i];
    }
    solve(l,mid,L,ql-1),solve(mid+1,r,ql,R);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&x);
        q[x].push_back(i);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&s[i]);
        c[i]=i;
    }
    scanf("%d",&k);
    for(int i=1;i<=k;i++)
    {
        scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].val);
    }
    solve(1,k+1,1,n);
    for(int i=1;i<=n;i++)
    {
        printf(ans[i]>k?"NIE\n":"%d\n",ans[i]);
    }
}

BZOJ2527[Poi2011]Meteors——整體二分+樹狀數組