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);
}
}
New task CodeForces - 788E (线段树优化dp)的更多相关文章
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- Linear Kingdom Races CodeForces - 115E (线段树优化dp)
大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】
BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...
- [AGC011F] Train Service Planning [线段树优化dp+思维]
思路 模意义 这题真tm有意思 我上下楼梯了半天做出来的qwq 首先,考虑到每K分钟有一辆车,那么可以把所有的操作都放到模$K$意义下进行 这时,我们只需要考虑两边的两辆车就好了. 定义一些称呼: 上 ...
- 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp
题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a varian ...
- POJ 2376 Cleaning Shifts (线段树优化DP)
题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...
- 洛谷$P2605\ [ZJOI2010]$基站选址 线段树优化$dp$
正解:线段树优化$dp$ 解题报告: 传送门$QwQ$ 难受阿,,,本来想做考试题的,我还造了个精妙无比的题面,然后今天讲$dp$的时候被讲到了$kk$ 先考虑暴力$dp$?就设$f_{i,j}$表示 ...
随机推荐
- P1494 [国家集训队]小Z的袜子(莫队算法)
莫队板子 代码 #include <cstdio> #include <algorithm> #include <cstring> #include <cma ...
- 题解——loj6281 数列分块入门5 (分块)
分块 若块内最大值为0或1,则不用再开方 然后暴力修改 可以证明,如果开方后向下取整,则最多开方4次一个数就会变成0或1 #include <cstdio> #include <cm ...
- Ubuntu14.04下 安装p4c
参考: Github p4c README Ubuntu14.04下 安装p4c 这里提供一个直接安装p4c的脚本:install_p4c.sh. 1.git clone下来p4c: $ git cl ...
- matplotlib python
#导入包 import matplotlib.pyplot as plt import numpy as np # 从[-1,1]中等距去50个数作为x的取值 x = np.linspace(-1, ...
- HDU 4313 Matrix(并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=4313 题意: 给出一棵树,每条边都有权值,其中有几个点是特殊点,现在破坏边还使得这几个特殊点互相不可达,需要使得 ...
- 【Python】【面向对象】
"""# [[面向对象]]#[访问限制]#如果要让内部属性不被外部访问,可加双下划线,编程私有变量.只有内部可以访问,外部不能访问.class Student(objec ...
- 【Java】【THINK】
1. 新建类,应优先考虑“组织”对象,而不是继承.这样可以保持清爽. 2. Java对象&对象句柄: 声明了一个类型的变量也就是声明了一个该类型的对象.但是这个对象只是个抽象的概念,并不会在内 ...
- BZOJ 4591 【SHOI2015】 超能粒子炮·改
题目链接:超能粒子炮·改 这道题的大体思路就是用\(lucas\)定理,然后合并同类项,就可以得到一个可以递归算的式子了. 我们用\(S(n,k)\)表示答案,\(p\)表示模数(\(2333\)是一 ...
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp
E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Could not find com.android.tools.build:aapt2:3.2.1-4818971.
Could not find com.android.tools.build:aapt2:-. Searched in the following locations: file:/H:/Androi ...