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}$表示 ...
随机推荐
- Nacos整合Spring Cloud Gateway实践
Spring Cloud Gateway官网:http://spring.io/projects/spring-cloud-gateway Eureka1.0的问题和Nacos对比:https://w ...
- 论文笔记之:Optical Flow Estimation using a Spatial Pyramid Network
Optical Flow Estimation using a Spatial Pyramid Network spynet 本文将经典的 spatial-pyramid formulation ...
- DownAlbum:Chrome的pinterest批量下载插件
一.DownAlbum安装 二.DownAlbum使用 点击DownAlbum图标. 选择Normal. 会出现一个加载的弹窗,等待片刻会打开一个新的窗口. 按Ctrl+S,即可保存相册所有图片. 图 ...
- HDU 5069 Harry And Biological Teacher(AC自动机+线段树)
题意 给定 \(n\) 个字符串,\(m\) 个询问,每次询问 \(a\) 字符串的后缀和 \(b\) 字符串的前缀最多能匹配多长. \(1\leq n,m \leq 10^5\) 思路 多串匹配,考 ...
- Dispose in c#
在标准的Dispose模式中,真正的IDisposable接口的Dispose方法并没有做实际的清理工作,它其实是调用了下面的这个带bool参数且受保护的的虚方法: protected virtual ...
- codeforce 886C Petya and Catacombs (map,思路)
突然发现百度不到这题的单独题解(果然是因为这是水题么),那我就来写一个了~ 先把题给贴了. C. Petya and Catacombs time limit per test 1 second me ...
- RabbitMq的整理 exchange、route、queue关系
https://blog.csdn.net/samxx8/article/details/47417133
- Robot Framework+AutoItLibrary+AutoIt使用
使用记录: 1. 打开被测桌面程序: 2. 打开AutoIt,用finder tool拖拽到控件上,可以看到控件的信息: 3. 如果空间的Title.Control Info抓不到,可以看Mouse下 ...
- js url参数和对象互转
function param(a) { var s = [], rbracket = /\[\]$/, isArray = function(obj) { return Object.prototyp ...
- 【译】第38节---EF6-基于代码的配置
原文:http://www.entityframeworktutorial.net/entityframework6/code-based-configuration.aspx EF6引入了基于代码的 ...