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}$表示 ...
随机推荐
- SDOI2017相关分析 线段树
题目 https://loj.ac/problem/2005 思路 \[ \sum_{L}^{R}{(x_i-x)^{2}} \] \[ \sum_{L}^{R}{(x_i^2-2*x_i*x+x^{ ...
- js 二叉树删除最大值和最小值
//删除最小值function delMinNode (root){ if(!root) { return false; } var current = root; if (current.left ...
- Match function in R
Examples: print(match(5, c(1,2,9,5,3,6,7,4,5)))[1] 4 5 %in% c(1,2,9,5,3,6,7,4,5)[1] TRUE ...
- Nand flash code
(1)流水灯 1>我们来看原理图 2>datasheet 3> ...
- SAP应用创新-维护控制表、视图统一路径
SAP应用创新-维护控制表.视图统一路径 背景: Sap 里面通过技术支持人员维护表的值控制业务操作的表不少,一般通过事物代码或记录在系统外的文档或在某个程序上放置一个按钮.缺点:分散,不易记,不好找 ...
- python测试
[链接]JointhePythonDeveloperscommunityonSlack! http://pythondevelopers.herokuapp.com/
- 用python读写excel的强大工具:openpyxl
最近看到好几次群里有人问xlwt.wlrd的问题,怎么说呢,如果是office2007刚出来,大家用xlsx文件用不习惯,还可以理解,这都10年过去了喂,就算没有进化到office2016,还在用of ...
- springboot 启动报错 java.lang.IllegalStateException: Failed to introspect annotated methods on class org
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ...
- [jshint] 'import' is only available in ES6 (use 'esversion: 6'). (W119) 提示import等ES6语法的jshint错误的,在代码前加一行 /* jshint esversion: 6 */
官方下载了vue的简单项目,用vscode打开main.js,代码前出现黄点,js报错了 把鼠标移至import的波浪线上,出现提示:W119 - ‘import’ is only availabl ...
- eclipse导入项目文件以及 import项目文件后有个红色感叹号
eclipse导入项目文件 步骤:File —>Import—>General—>Existing Projects into Workspace 然后进去选择项目文件的具体路径即可 ...