New task CodeForces - 788E (线段树优化dp)

比较套路的一个题, 对每个数维护一颗线段树来转移就好了.

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc tr[o].l
#define rc tr[o].r
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head




const int N = 1e5+10;
int n, m, ans, tot, T[N];
int a[N], s[N], b[N], c[N], L[N], R[N];
int sub(int x, int y) {int r=x-y;if(r<0)r+=P;return r;}
int add(int x, int y) {int r=x+y;if(r>=P)r-=P;return r;}
int mul(int x, int y) {return (ll)x*y%P;}
struct _ {
	int A,C,S,AB,BC,ABC;
	_ () {}
	_ (int A, int C, int S) : A(A),C(C),S(S),AB(0),BC(0),ABC(0) {}
	_ operator + (const _ &rhs) const {
		_ r;
		r.A = add(A,rhs.A);
		r.C = add(C,rhs.C);
		r.S = add(S,rhs.S);
		r.AB = add(add(AB,rhs.AB),mul(A,rhs.S));
		r.BC = add(add(BC,rhs.BC),mul(S,rhs.C));
		r.ABC = add(add(ABC,rhs.ABC),add(mul(AB,rhs.C),mul(A,rhs.BC)));
		return r;
	}
};
struct {int l,r;_ v;} tr[N<<5];

void ins(int &o, int l, int r, int x, int v1, int v2, int v3) {
	if (!o) o=++tot;
	if (l==r) {
		tr[o].v=_(v1,v2,v3),void();
		return ;
	}
	if (mid>=x) ins(ls,x,v1,v2,v3);
	else ins(rs,x,v1,v2,v3);
	tr[o].v=tr[lc].v+tr[rc].v;
}
int main() {
	scanf("%d", &n);
	REP(i,1,n) scanf("%d", a+i),b[i]=a[i];
	sort(b+1,b+1+n),*b=unique(b+1,b+1+n)-b-1;
	REP(i,1,n) {
		a[i] = lower_bound(b+1,b+1+*b,a[i])-b;
		for (int j=a[i]; j; j^=j&-j) L[i]+=c[j];
		for (int j=a[i]; j<=*b; j+=j&-j) ++c[j];
	}
	memset(c,0,sizeof c);
	PER(i,1,n) {
		for (int j=a[i]; j; j^=j&-j) R[i]+=c[j];
		for (int j=a[i]; j<=*b; j+=j&-j) ++c[j];
	}
	REP(i,1,n) {
		ans = sub(ans,tr[T[a[i]]].v.ABC);
		ins(T[a[i]],1,n,i,L[i],R[i],1);
		ans = add(ans,tr[T[a[i]]].v.ABC);
	}
	scanf("%d", &m);
	REP(i,1,m) {
		int op, x;
		scanf("%d%d", &op, &x);
		ans = sub(ans,tr[T[a[x]]].v.ABC);
		ins(T[a[x]],1,n,x,(op==2)*L[x],(op==2)*R[x],op==2);
		ans = add(ans,tr[T[a[x]]].v.ABC);
		printf("%d\n", ans);
	}
}

 

上一篇:[洛谷P2742]【模板】二维凸包([USACO5.1]圈奶牛Fencing the Cows)


下一篇:Alyona and towers CodeForces - 739C (线段树)