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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include<cstdio> #include<iostream> using namespace std;
int sort_cmp (const void * a, const void * b) { return ( *(int*)a - *(int*)b );}
const int N = 200003;
bool isQuery[N]; int interval[N][2];
int a[N*2], unique_num = 0; struct treeNode{ bool isNode = false; int l, r; int v; long long total; }st[(N*2)<<1];
void build(int li, int ri, int x); void update(int li, int ri, int x); long long query(int li, int ri, int x); int bisearch(int li, int ri, int g); int original_to_discrete(int x); int discrete_to_original(int y);
int temp[N*4];
int main() { int n, m; scanf("%d%d", &n, &m); cin.ignore(100,'\n'); char o; int l, r; for(int i=0; i<m; i++){ scanf("%c%d%d", &o, &l, &r);
isQuery[i] = (o=='Q'); interval[i][0] = l; interval[i][1] = r; temp[i*4 + 0] = l; temp[i*4 + 1] = l+1; temp[i*4 + 2] = r; temp[i*4 + 3] = r+1;
cin.ignore(100,'\n'); }
qsort(temp, 4*m, sizeof(int), sort_cmp); a[unique_num++] = temp[0]; for(int i=1; i<4*m; i++) { if(temp[i]!=temp[i-1]) a[unique_num++] = temp[i]; }
build(0, unique_num, 0);
for(int i=0; i<m; i++){ if(isQuery[i]){ cout<<query(original_to_discrete(interval[i][0]), original_to_discrete(interval[i][1]), 0)<<endl; } else { update(original_to_discrete(interval[i][0]), original_to_discrete(interval[i][1]), 0); } }
return 0; }
void build(int li, int ri, int x){ st[x].isNode = true; st[x].l = li; st[x].r = ri; st[x].v = 0;
if(li+1 != ri) { int mid = (li+ri)/2; build(li,mid,(x<<1) + 1); build(mid,ri,(x<<1) + 2); }
return; }
void update(int li, int ri, int x){
int dis = discrete_to_original(st[x].r-1) - discrete_to_original(st[x].l) + 1;
if(li<=st[x].l && ri>=st[x].r-1){
st[x].v += 1; st[x].total += dis; return; }
if(!st[(x<<1)+1].isNode) return;
if(st[(x<<1)+1].isNode && li <= st[(x<<1)+1].r-1){ update(li, ri, (x<<1)+1); }
if(st[(x<<1)+2].isNode && ri >= st[(x<<1)+2].l){ update(li, ri, (x<<1)+2); }
st[x].total = st[x].v * dis + st[(x<<1)+1].total + st[(x<<1)+2].total; return;
}
long long query(int li, int ri, int x){
long long res = 0;
if(li<=st[x].l && ri>=st[x].r-1){ res += st[x].total; return res; }
int dis = discrete_to_original(min(st[x].r-1, ri)) - discrete_to_original(max(st[x].l, li)) + 1; res += st[x].v * dis;
if(st[(x<<1)+1].isNode && li <= st[(x<<1)+1].r-1){ res += query(li, ri, (x<<1)+1); }
if(st[(x<<1)+2].isNode && ri >= st[(x<<1)+2].l){ res += query(li, ri, (x<<1)+2); }
return res;
}
int bisearch(int li, int ri, int g){ int mid = 0; while(ri > li+1){ mid = (li+ri)>>1; if(a[mid]<=g) li = mid; else ri = mid; } return li; }
int discrete_to_original(int y){ return a[y]; }
int original_to_discrete(int x){ return bisearch(0, unique_num, x); }
|