比赛 20151019 评测结果 AAAAAAAAAA
题目名称 学数数 最终得分 100
用户昵称 dashgua 运行时间 0.423 s
代码语言 C++ 内存使用 4.51 MiB
提交时间 2015-10-19 21:26:34
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x = (x<<3) + (x<<1) + (c-'0');
    x *= flag;
}
template<class Num>void write(Num x)
{
    if(!x) {putchar('0');return;}
    if(x < 0) putchar('-'), x = -x;
    static char s[20];int sl = 0;
    while(x) s[sl++] = x%10 + '0',x /= 10;
    while(sl) putchar(s[--sl]);
}

const int maxn = 1e5 + 20;

int n, q, a[maxn], b[maxn], len;
int stack[maxn], top;
int l[maxn], r[maxn];
long long cnt[maxn], pre[maxn], sur[maxn];

int main()
{
	freopen("jxthree.in","r",stdin);
	freopen("jxthree.out","w",stdout);
	
	read(n), read(q);
	
	for(int i = 1; i <= n; i++)
		read(a[i]), b[i] = a[i];
	
	std::sort(b + 1, b + n + 1);
	len = std::unique(b + 1, b + n + 1) - (b + 1);
	
	stack[top = 0] = 0;
	for(int i = 1; i <= n; i++)
	{
		while(top && a[i] > a[stack[top]]) top--;
		l[i] = stack[top], stack[++top] = i;
	}
	stack[top = 0] = n + 1;
	for(int i = n; i >= 1; i--)
	{
		while(top && a[i] >= a[stack[top]]) top--;
		r[i] = stack[top], stack[++top] = i;
	}
	for(int i = 1; i <= n; i++)
	{
		int x = std::lower_bound(b + 1, b + len + 1, a[i]) - b;
		cnt[x] += (long long)(i - l[i])*(r[i] - i);
 	}
 	
 	for(int i = 1; i <= len; i++)
 		pre[i] = pre[i - 1] + cnt[i];
 	for(int i = len; i >= 1; i--)
 		sur[i] = sur[i + 1] + cnt[i];
	 	
	for(int i = 1, k; i <= q; i++)
	{
 		static char s[3];
 		
		scanf("%s", s), read(k);
 		
 		if(s[0] == '>')
 		{
 			int x = std::upper_bound(b + 1, b + len + 1, k) - b;
 			write(sur[x]), puts("");
 		}
 		else if(s[0] == '<')
 		{
 			int x = std::lower_bound(b + 1, b + len + 1, k) - b - 1;
 			write(pre[x]), puts("");
 		}
 		else
 		{
 			int x = std::lower_bound(b + 1, b + len + 1, k) - b;
 			write((b[x] == k) ? cnt[x] : 0), puts("");
 		}
 	}
 	
	fclose(stdin);
	fclose(stdout);
	return 0;
}