1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cctype> #include <vector> #include <cmath> #include <queue> using namespace std; #define ll long long #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define pii pair<int,int> #define mp make_pair
char buf[1 << 20], *p1, *p2; #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)?EOF: *p1++) template <typename T> inline void read(T &t) { int v = getchar();T f = 1;t = 0; while (!isdigit(v)) {if (v == '-')f = -1;v = getchar();} while (isdigit(v)) {t = t * 10 + v - 48;v = getchar();} t *= f; } template <typename T,typename... Args> inline void read(T &t,Args&... args) { read(t);read(args...); }
const ll mod = 998244353; const double eps = 1e-10; const int N = 5e5 + 10; int n,a[N],pre[N],m;
struct node { int l,r; ll reve,tag0,tag1; ll cnt0,cnt1,sum; }tree[N<<2];
void pushup(int p) { tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum; tree[p].cnt1 = tree[p << 1].cnt1 + tree[p << 1 | 1].cnt1; tree[p].cnt0 = tree[p << 1].cnt0 + tree[p << 1 | 1].cnt0; }
void pushdown(int p) { if (tree[p].reve) { swap(tree[p << 1].cnt0,tree[p << 1].cnt1); swap(tree[p << 1 | 1].cnt0,tree[p << 1 | 1].cnt1);
swap(tree[p << 1].tag0,tree[p << 1].tag1); swap(tree[p << 1 | 1].tag0,tree[p << 1 | 1].tag1); tree[p << 1].reve ^= 1;tree[p << 1 | 1].reve ^= 1; tree[p].reve = 0; } if (tree[p].tag0) { tree[p << 1].sum += tree[p].tag0 * tree[p << 1].cnt0; tree[p << 1 | 1].sum += tree[p].tag0 * tree[p << 1 | 1].cnt0; tree[p << 1].tag0 += tree[p].tag0; tree[p << 1 | 1].tag0 += tree[p].tag0; tree[p].tag0 = 0; } if (tree[p].tag1) { tree[p << 1].sum += tree[p].tag1 * (tree[p << 1].cnt1); tree[p << 1 | 1].sum += tree[p].tag1 * (tree[p << 1 | 1].cnt1); tree[p << 1].tag1 += tree[p].tag1; tree[p << 1 | 1].tag1 += tree[p].tag1; tree[p].tag1 = 0; } }
void build(int p,int l,int r) { tree[p].l = l,tree[p].r = r; if (l == r) { tree[p].cnt0 = 1; return ; } int mid = (l + r) >> 1; build(p << 1,l,mid); build(p << 1 | 1,mid + 1,r); pushup(p); }
void change(int p,int x,int y) { if (tree[p].l >= x && tree[p].r <= y) { swap(tree[p].cnt0,tree[p].cnt1); swap(tree[p].tag0,tree[p].tag1); tree[p].reve ^= 1; return ; } pushdown(p); int mid = (tree[p].l + tree[p].r) >> 1; if (x <= mid) change(p << 1,x,y); if (y > mid) change(p << 1 | 1,x,y); pushup(p); }
ll query(int p,int x,int y) { if (tree[p].l >= x && tree[p].r <= y) { return tree[p].sum; } pushdown(p); int mid = (tree[p].l + tree[p].r) >> 1; ll ans = 0; if (x <= mid) ans += query(p << 1,x,y); if (y > mid) ans += query(p << 1 | 1,x,y); return ans; }
struct que { int l,r,num; friend inline bool operator < (const que &a,const que &b) { return a.r == b.r ? a.l < b.l : a.r < b.r; } }q[N];
int lst[N]; ll ans[N];
signed main() { read(n); for (int i = 1;i <= n;++i) read(a[i]); build(1,1,n); read(m); for (int i = 1;i <= m;++i) { read(q[i].l,q[i].r);q[i].num = i; } sort(q + 1,q + 1 + m); int now = 1; for (int i = 1;i <= m;++i) { while (now <= q[i].r) { change(1,lst[a[now]] + 1,now); tree[1].sum += (tree[1].cnt1); tree[1].tag1 ++; lst[a[now]] = now; ++now; } ans[q[i].num] = query(1,q[i].l,q[i].r); } for (int i = 1;i <= m;++i) printf("%lld\n",ans[i]); return 0; }
|