2020 cattle off winter algorithm base Camp 4 H train

https://ac.nowcoder.com/acm/contest/3005/H

 

When we want to calculate cabs i + 1-th from the i-th time

Only affect the number corresponding to the i-th compartment and the number of colors i + 1 of the corresponding color carriages

Current maintenance around the cabin compartment all the same color prefix number with Fenwick tree and

Have been calculated assuming that the answer before i finished cabins, and now want to calculate the answer to the first compartment i + 1 th

When the color carriage i is a, the compartment i + 1 is b color

In addition to a, b, about the same compartment of the other color is the same as the number of

A color, the i-th cabin left that can do the compartment

The i-th compartment behind the number of colors like his cabin (removing the i + 1-cabins), this color will contribute to increase the number of answers

Color b, the first i + 1 cabins can not be done on the right that the carriage

The i + 1 front compartment how many colors like his cabin (removing the i-th compartment), the contribution of this color will reduce the number of answers

The two arrays can then record

 

There is a point like a long time:

I find the back of the carriages how many colors like his cabin when the (i + 1-carriages remove), you can not control, "removing the first i + 1 cabins" This condition

It is behind

Because if the i-th and i + 1 the same color carriages, one plus one minus offset exactly

 

#include<cstdio>
#include<algorithm>
using namespace std;

#define N 500001

#define lowbit(x) x&-x

int col[N],L[N],R[N];
int suml[N],sumr[N];

long long c[N];

void add(int x,int y)
{
    while(x<N)
    {
        c[x]+=y;
        x+=lowbit(x);
    }
}

long long query(int x)
{
    long long s=0;
    while(x)
    {
        s+=c[x];
        x-=lowbit(x);
    }
    return s;
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%d%d%d",&col[i],&L[i],&R[i]);
        sumr[col[i]]++;
    }
    for(int i=1;i<=n;++i)
    {
        sumr[col[i]]--;
        add(col[i],-suml[col[i]]);
        printf("%lld ",query(R[i])-query(L[i]-1));
        suml[col[i]]++;
        add(col[i],sumr[col[i]]);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/TheRoadToTheGold/p/12302576.html