标签:des style blog http io ar color os sp
题目大意:给你n个数字,然后m次查询,每次给你一个x,让你求出来1到x,2到x+1。。。不同数的和。
需要各种预处理,处理出来所有的间隔之间有多少相同的数字,处理出来最后一个被去掉的间隔有多少个不重复的数字。
dp[i] = dp[i-1]-S+T.S代表最后被略去的那个区间的不同的数,T代表新区间扩张之后每个区间增加的不同的数的和。
7 1 1 2 3 4 4 5 3 1 2 3 0
7 10 12
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <time.h> #include <stack> #include <map> #include <set> #define eps 1e-8 ///#define LL long long #define LL __int64 #define INF 0x3f3f3f #define PI 3.1415926535898 #define mod 1000000007 using namespace std; const int maxn = 1001000; int num[maxn]; int vis[maxn]; int f[maxn]; int p[maxn]; LL dp[maxn]; int main() { int n; while(~scanf("%d", &n) && n) { memset(vis, 0, sizeof(vis)); memset(p, 0, sizeof(p)); for(int i = 1; i <= n; i++) { scanf("%d", &num[i]); p[i-vis[num[i]]]++; vis[num[i]] = i; } memset(vis, 0, sizeof(vis)); f[1] = 1; vis[num[n]] = 1; for(int i = 2; i <= n; i++) { if(vis[num[n-i+1]]) { f[i] = f[i-1]; continue; } vis[num[n-i+1]] = 1; f[i] = f[i-1]+1; } dp[1] = n; int sum = n; for(int i = 2; i <= n; i++) { dp[i] = dp[i-1]-f[i-1]; sum -= p[i-1]; dp[i] += sum; } int m; scanf("%d",&m); for(int i = 1; i <= m; i++) { int x; scanf("%d",&x); printf("%I64d\n",dp[x]); } } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/xu12110501127/article/details/41592855