签到题 J Different Integers(树状数组)

  题目大意:给一个长为n的数组,每一个询问给两个数字i, j ,询问1~i, j~n这两个区间中有多少不同的数字,真的像是莫队裸题,但是两个区间是分隔的,所以可以考虑将数组延长一倍,即num[i] = num[i+n],这样就可以变成一段区间中查询了,不过这样会T.....参考了题解的做法还是比较好用的,写法非常巧妙。记录数字i第一次和最后一次出现的位置分别为first[i], last[i],按每个询问的rhs从小到大排序后,遍历num数组。遍历序号为i,每次遍历处理询问和维护一个树状数组,①只有当某个询问的 rhs == i 时处理该询问②维护树状数组,首先我们可以想到假如某个数x,当last[x] == i 时,下个遍历时下个询问的rhs必然会大于这个last[x](因为是从1~n遍历),所以当这次遍历过后,剩下的询问rhs全部大于last[x],所有右边区间rhs~n已经没有x了,这个时候只要看前面lhs是不是小于first[x]就知道这个数在不在询问区间了,这个时候就可以维护一个前缀和,sum[i]表示表示有多少个数的first值是在1~i内的。

 #include <iostream>
#include <string.h>
#include <cstdio>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <time.h> #define SIGMA_SIZE 26
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a,LL b) { return a>b?a:b; }
inline LL LMin(LL a,LL b) { return a>b?b:a; }
inline int Max(int a,int b) { return a>b?a:b; }
inline int Min(int a,int b) { return a>b?b:a; }
inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 1e4+;
const int maxn = 1e5+; int n, q, tot;
int first[maxn], last[maxn];
int num[maxn], ans[maxn], _count[maxn]; struct node {
int lhs, rhs, id;
}p[maxn]; void add( int x, int d )
{
while ( x > )
{
_count[x] += d;
x -= lowbit(x);
}
} int sum(int x)
{
int s = ;
while ( x <= n )
{
s += _count[x];
x += lowbit(x);
}
return s;
} bool cmp(const node& a, const node &b)
{ return a.rhs < b.rhs; } void init()
{
tot = ;
memset(first, -, sizeof(first));
memset(last, -, sizeof(last));
memset(_count, , sizeof(_count)); for (int i = ; i <= n; i++)
{
scanf("%d", &num[i]);
if (first[num[i]] == -)
{ tot++; first[num[i]] = i;}
last[num[i]] = i;
} for (int i = ; i <= q; i++)
{
scanf("%d %d", &p[i].lhs, &p[i].rhs);
p[i].id = i;
}
sort(p+, p++q, cmp);
} int main()
{
while (~scanf("%d %d", &n, &q))
{
init(); for (int i = , k = ; i <= n; i++)
{ //如果有边界到达了某一点i,则可以开始处理左边界
while (k <= q && p[k].rhs == i)
{
ans[p[k].id] = tot - sum(p[k].lhs);
k++;
} if (last[num[i]] == i)
add(first[num[i]]-, );
} for ( int i = ; i <= q; i++ )
printf("%d\n", ans[i]);
} return ;
}

  

  签到题? D Two Graphs

  题目大意:给两个简单图G1(V, E1), G2(V,E2) ,问有多少个G2的子图与G1是同构的,想了好多骚操作,,结果hash去重就可以了

 #include <iostream>
#include <string.h>
#include <cstdio>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <time.h> #define SIGMA_SIZE 26
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a,LL b) { return a>b?a:b; }
inline LL LMin(LL a,LL b) { return a>b?b:a; }
inline int Max(int a,int b) { return a>b?a:b; }
inline int Min(int a,int b) { return a>b?b:a; }
inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 1e4+;
const int maxn = 1e5+; struct edge2 {
int x, y;
}edge[];
int v, e1, e2;
int g1[][], g2[][], num[]; void init()
{
memset(g1, , sizeof(g1));
memset(g2, , sizeof(g2));
for ( int i = ; i <= v; i++ )
num[i] = i; int x, y;
for ( int i = ; i <= e1; i++ )
{ scanf("%d %d", &x, &y); g1[x][y] = ; g1[y][x] = ; }
for ( int i = ; i <= e2; i++ )
{
scanf("%d %d", &x, &y); g2[x][y] = ; g2[y][x] = ;
edge[i].x = x;
edge[i].y = y;
} } int main()
{
while (~scanf("%d %d %d", &v, &e1, &e2))
{
init(); set<long long> ss;
do {
int cnt = ;
long long has = ;
for (int i = ; i <= e2; i++)
{
if (g1[num[edge[i].x]][num[edge[i].y]])
{
has = has* + i;
has %= mod;
cnt++;
}
} if (cnt == e1)
ss.insert(has); }while(next_permutation(num+, num++v)); printf("%zd\n", ss.size());
} return ;
}

牛客网暑期ACM多校训练营(第一场)菜鸟补题QAQ的更多相关文章

  1. 牛客网暑期ACM多校训练营 第九场

    HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...

  2. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  3. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  4. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  5. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  6. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

  7. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  8. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  9. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  10. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

随机推荐

  1. delphi 第3课

    (1)主程序:汇总或者记载 Delphi应用程序是以窗体为中心的 (1) 1 (2) 控制语句 if 条件 then 语句1: else 语句2: 2018-04-22   21:47:17

  2. 【Servlet】Servlet监听器

    一.Servlet监听器的概念 Servlet监听器是Servlet规范中定义的一种特殊类,用于监听ServletContext.HttpSession和ServletRequest等域对象的创建与销 ...

  3. day26 re正则表达式

     Python之路,Day14 = Python基础14 compile() match() search() findall() m.group() # 括号里面剋跟参数,表示打印里面(分组)的第几 ...

  4. 关于web前端网站优化

    不知道是哪位大牛的文章,转过来嘻嘻. 作者:斯迪链接:https://www.zhihu.com/question/21658448/answer/18903129来源:知乎著作权归作者所有.商业转载 ...

  5. HttpURLConnection与HttpClient浅析AAAA

    . GET请求与POST请求 HTTP协议是现在Internet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源. 在介绍HttpURLConnectio ...

  6. JavaScript编码指南

    出其不意 1920年,William Strunk Jr的<英文写作指南>出版了,这本书给英语的风格定下了一个规范,而且已经沿用至今.代码其实也可以使用相似的方法加以改进. 本文接下来的部 ...

  7. PAT甲级——A1131 Subway Map【30】

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  8. import socket模块二

    ---恢复内容开始--- 优化两个小脚本实现不间断聊天: server.py: import socket sk = socket.socket() # 创建socket addess = ('127 ...

  9. 4_5.springboot2.x之Web开发RestfulCRUD操作

    1).RestfulCRUD:CRUD满足Rest风格 URI: /资源名称/资源标识 HTTP请求方式区分对资源CRUD操作 普通CRUD(uri来区分操作) RestfulCRUD 查询 getE ...

  10. iOS开发系列-NSOutputStream

    NSOutputStream 创建一个NSOutputStream实例 - (nullable instancetype)initToFileAtPath:(NSString *)path appen ...